Added mongo db repository

This commit is contained in:
2019-04-22 17:57:23 +00:00
parent 0f9a598fae
commit 83385a66c7
7 changed files with 74 additions and 8 deletions

View File

@@ -13,9 +13,9 @@ namespace BrightGlimmer.Api.Controllers
public class StudentController : ControllerBase public class StudentController : ControllerBase
{ {
// private readonly IMediator mediator; // private readonly IMediator mediator;
private readonly StudentRepository studentRepository; private readonly SqliteStudentRepository studentRepository;
public StudentController(StudentRepository studentRepository) public StudentController(SqliteStudentRepository studentRepository)
{ {
this.studentRepository = studentRepository; this.studentRepository = studentRepository;
} }

View File

@@ -32,7 +32,8 @@ namespace BrightGlimmer.Api
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMediatR(); services.AddMediatR();
services.AddMediatR(typeof(Cqrs.Cqrs).Assembly); // Registers handlers in Cqrs project 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.AddDbContext<SqliteDatabaseContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<SqliteDatabaseContext>(); services.AddTransient<SqliteDatabaseContext>();
} }

View File

@@ -6,7 +6,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Domain\Interfaces\" /> <Folder Include="Domain\Interfaces\" />
<Folder Include="Repositories\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,4 +1,5 @@
using System; using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -6,9 +7,13 @@ namespace BrightGlimmer.Data.Domain
{ {
public class Phone public class Phone
{ {
[BsonElement("Id")]
public Guid Id { get; set; } public Guid Id { get; set; }
[BsonElement("Type")]
public PhoneType Type { get; set; } public PhoneType Type { get; set; }
[BsonElement("AreaCode")]
public int AreaCode { get; set; } public int AreaCode { get; set; }
[BsonElement("Number")]
public int Number { get; set; } public int Number { get; set; }
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -6,11 +7,17 @@ namespace BrightGlimmer.Data.Domain
{ {
public class Student public class Student
{ {
[BsonElement("Id")]
public Guid Id { get; set; } public Guid Id { get; set; }
[BsonElement("FirstName")]
public string FirstName { get; set; } public string FirstName { get; set; }
[BsonElement("MiddleName")]
public string MiddleName { get; set; } public string MiddleName { get; set; }
[BsonElement("LastName")]
public string LastName { get; set; } public string LastName { get; set; }
[BsonElement("Email")]
public string Email { get; set; } public string Email { get; set; }
[BsonElement("Phones")]
public List<Phone> Phones { get; set; } public List<Phone> Phones { get; set; }
} }
} }

View 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);
}
}
}

View File

@@ -7,11 +7,12 @@ using System.Text;
namespace BrightGlimmer.Data.Repositories namespace BrightGlimmer.Data.Repositories
{ {
public class StudentRepository public class SqliteStudentRepository
{ {
private readonly SqliteDatabaseContext context; private readonly SqliteDatabaseContext context;
public StudentRepository(SqliteDatabaseContext context)
public SqliteStudentRepository(SqliteDatabaseContext context)
{ {
this.context = context; this.context = context;
} }