Added mongo db repository
This commit is contained in:
@@ -13,9 +13,9 @@ namespace BrightGlimmer.Api.Controllers
|
||||
public class StudentController : ControllerBase
|
||||
{
|
||||
// private readonly IMediator mediator;
|
||||
private readonly StudentRepository studentRepository;
|
||||
private readonly SqliteStudentRepository studentRepository;
|
||||
|
||||
public StudentController(StudentRepository studentRepository)
|
||||
public StudentController(SqliteStudentRepository studentRepository)
|
||||
{
|
||||
this.studentRepository = studentRepository;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,8 @@ namespace BrightGlimmer.Api
|
||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
||||
services.AddMediatR();
|
||||
services.AddMediatR(typeof(Cqrs.Cqrs).Assembly); // Registers handlers in Cqrs project
|
||||
services.AddScoped<StudentRepository, StudentRepository>(); /* REMOVE LATER */
|
||||
services.AddScoped<SqliteStudentRepository, SqliteStudentRepository>(); /* REMOVE LATER */
|
||||
services.AddScoped<MongoStudentRepository, MongoStudentRepository>(); /* REMOVE LATER */
|
||||
services.AddDbContext<SqliteDatabaseContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
|
||||
services.AddTransient<SqliteDatabaseContext>();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Domain\Interfaces\" />
|
||||
<Folder Include="Repositories\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
@@ -6,9 +7,13 @@ namespace BrightGlimmer.Data.Domain
|
||||
{
|
||||
public class Phone
|
||||
{
|
||||
[BsonElement("Id")]
|
||||
public Guid Id { get; set; }
|
||||
[BsonElement("Type")]
|
||||
public PhoneType Type { get; set; }
|
||||
[BsonElement("AreaCode")]
|
||||
public int AreaCode { get; set; }
|
||||
[BsonElement("Number")]
|
||||
public int Number { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
@@ -6,11 +7,17 @@ namespace BrightGlimmer.Data.Domain
|
||||
{
|
||||
public class Student
|
||||
{
|
||||
[BsonElement("Id")]
|
||||
public Guid Id { get; set; }
|
||||
[BsonElement("FirstName")]
|
||||
public string FirstName { get; set; }
|
||||
[BsonElement("MiddleName")]
|
||||
public string MiddleName { get; set; }
|
||||
[BsonElement("LastName")]
|
||||
public string LastName { get; set; }
|
||||
[BsonElement("Email")]
|
||||
public string Email { get; set; }
|
||||
[BsonElement("Phones")]
|
||||
public List<Phone> Phones { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
53
BrightGlimmer.Data/Repositories/MongoStudentRepository.cs
Normal file
53
BrightGlimmer.Data/Repositories/MongoStudentRepository.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using BrightGlimmer.Data.Domain;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Repositories
|
||||
{
|
||||
public class MongoStudentRepository
|
||||
{
|
||||
private const string dbName = "bright_glimmer";
|
||||
private const string collectionName = "Students";
|
||||
private IMongoDatabase db;
|
||||
|
||||
public MongoStudentRepository()
|
||||
{
|
||||
var client = new MongoClient("mongodb://localhost:27017"); /* REMOVE LATER */
|
||||
db = client.GetDatabase(dbName);
|
||||
}
|
||||
|
||||
public List<Student> GetStudents()
|
||||
{
|
||||
return db.GetCollection<Student>(collectionName).Find(x => true).ToList();
|
||||
}
|
||||
|
||||
public Student GetStudent(Guid id)
|
||||
{
|
||||
return db.GetCollection<Student>(collectionName).Find(student => student.Id == id).SingleOrDefault();
|
||||
}
|
||||
|
||||
public Student GetStudentByEmail(string email)
|
||||
{
|
||||
return db.GetCollection<Student>(collectionName).Find(student => student.Email == email).Single();
|
||||
}
|
||||
|
||||
public void Create(Student student)
|
||||
{
|
||||
db.GetCollection<Student>(collectionName).InsertOne(student);
|
||||
}
|
||||
|
||||
public void Update(Student student)
|
||||
{
|
||||
var filter = Builders<Student>.Filter.Where(x => x.Id == student.Id);
|
||||
db.GetCollection<Student>(collectionName).ReplaceOne(filter, student);
|
||||
}
|
||||
|
||||
public void Remove(Guid id)
|
||||
{
|
||||
var filter = Builders<Student>.Filter.Where(x => x.Id == id);
|
||||
var operation = db.GetCollection<Student>(collectionName).DeleteOne(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,12 @@ using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Repositories
|
||||
{
|
||||
public class StudentRepository
|
||||
public class SqliteStudentRepository
|
||||
{
|
||||
private readonly SqliteDatabaseContext context;
|
||||
|
||||
public StudentRepository(SqliteDatabaseContext context)
|
||||
|
||||
public SqliteStudentRepository(SqliteDatabaseContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
Reference in New Issue
Block a user