diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index 0c0e89c..a965780 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -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; } diff --git a/BrightGlimmer.Api/Startup.cs b/BrightGlimmer.Api/Startup.cs index c846e07..772cc3d 100644 --- a/BrightGlimmer.Api/Startup.cs +++ b/BrightGlimmer.Api/Startup.cs @@ -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(); /* REMOVE LATER */ + services.AddScoped(); /* REMOVE LATER */ + services.AddScoped(); /* REMOVE LATER */ services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); services.AddTransient(); } diff --git a/BrightGlimmer.Data/BrightGlimmer.Data.csproj b/BrightGlimmer.Data/BrightGlimmer.Data.csproj index 5892ee8..b770ae0 100644 --- a/BrightGlimmer.Data/BrightGlimmer.Data.csproj +++ b/BrightGlimmer.Data/BrightGlimmer.Data.csproj @@ -6,7 +6,6 @@ - diff --git a/BrightGlimmer.Data/Domain/Phone.cs b/BrightGlimmer.Data/Domain/Phone.cs index e3858a7..40d2dd6 100644 --- a/BrightGlimmer.Data/Domain/Phone.cs +++ b/BrightGlimmer.Data/Domain/Phone.cs @@ -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; } } } diff --git a/BrightGlimmer.Data/Domain/Student.cs b/BrightGlimmer.Data/Domain/Student.cs index 7e3a2b1..638b2bb 100644 --- a/BrightGlimmer.Data/Domain/Student.cs +++ b/BrightGlimmer.Data/Domain/Student.cs @@ -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 Phones { get; set; } } } diff --git a/BrightGlimmer.Data/Repositories/MongoStudentRepository.cs b/BrightGlimmer.Data/Repositories/MongoStudentRepository.cs new file mode 100644 index 0000000..05511e2 --- /dev/null +++ b/BrightGlimmer.Data/Repositories/MongoStudentRepository.cs @@ -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 GetStudents() + { + return db.GetCollection(collectionName).Find(x => true).ToList(); + } + + public Student GetStudent(Guid id) + { + return db.GetCollection(collectionName).Find(student => student.Id == id).SingleOrDefault(); + } + + public Student GetStudentByEmail(string email) + { + return db.GetCollection(collectionName).Find(student => student.Email == email).Single(); + } + + public void Create(Student student) + { + db.GetCollection(collectionName).InsertOne(student); + } + + public void Update(Student student) + { + var filter = Builders.Filter.Where(x => x.Id == student.Id); + db.GetCollection(collectionName).ReplaceOne(filter, student); + } + + public void Remove(Guid id) + { + var filter = Builders.Filter.Where(x => x.Id == id); + var operation = db.GetCollection(collectionName).DeleteOne(filter); + } + } +} diff --git a/BrightGlimmer.Data/Repositories/StudentRepository.cs b/BrightGlimmer.Data/Repositories/SqliteStudentRepository.cs similarity index 89% rename from BrightGlimmer.Data/Repositories/StudentRepository.cs rename to BrightGlimmer.Data/Repositories/SqliteStudentRepository.cs index 738595a..139cb5b 100644 --- a/BrightGlimmer.Data/Repositories/StudentRepository.cs +++ b/BrightGlimmer.Data/Repositories/SqliteStudentRepository.cs @@ -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; }