diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index 0c0e89c..ab882b3 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using BrightGlimmer.Data.Repositories; /* REMOVE LATER */ using MediatR; using Microsoft.AspNetCore.Mvc; @@ -12,18 +11,17 @@ namespace BrightGlimmer.Api.Controllers [ApiController] public class StudentController : ControllerBase { - // private readonly IMediator mediator; - private readonly StudentRepository studentRepository; + private readonly IMediator mediator; - public StudentController(StudentRepository studentRepository) + public StudentController(IMediator mediator) { - this.studentRepository = studentRepository; + this.mediator = mediator; } [HttpGet] public IActionResult GetAll() { - var customers = studentRepository.GetAll(); + var customers = mediator.GetAll(); if (customers == null) { return NotFound(); @@ -32,28 +30,28 @@ namespace BrightGlimmer.Api.Controllers return new ObjectResult(customers); } - [HttpGet("createstudent")] - public IActionResult CreateStudent() - { - var created = studentRepository.Create(new Data.Domain.Student - { - Id = Guid.NewGuid(), - FirstName = "Giovani", - LastName = "Rodriguez", - Email = "giovaniluisrodriguez@gmail.com", - Phones = new List - { - new Data.Domain.Phone - { - Id = Guid.NewGuid(), - AreaCode = 305, - Number = 8888888, - Type = Data.Domain.PhoneType.HOMEPHONE - } - } - }); + //[HttpGet("createstudent")] + //public IActionResult CreateStudent() + //{ + // var created = studentRepository.Create(new Data.Domain.Student + // { + // Id = Guid.NewGuid(), + // FirstName = "Giovani", + // LastName = "Rodriguez", + // Email = "giovaniluisrodriguez@gmail.com", + // Phones = new List + // { + // new Data.Domain.Phone + // { + // Id = Guid.NewGuid(), + // AreaCode = 305, + // Number = 8888888, + // Type = Data.Domain.PhoneType.HOMEPHONE + // } + // } + // }); - return new ObjectResult(created); - } + // return new ObjectResult(created); + //} } } \ No newline at end of file diff --git a/BrightGlimmer.Api/Startup.cs b/BrightGlimmer.Api/Startup.cs index 9674410..e051c98 100644 --- a/BrightGlimmer.Api/Startup.cs +++ b/BrightGlimmer.Api/Startup.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using BrightGlimmer.Data; +using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Data.Repositories; /* REMOVE LATER */ using MediatR; using Microsoft.AspNetCore.Builder; @@ -31,10 +32,10 @@ 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.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); - services.AddTransient(); + services.AddMediatR(typeof(Cqrs.Cqrs).Assembly); // Registers handlers in services project + services.AddScoped(typeof(IRepository<>)); /* FIGURE OUT LATER */ + services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); + services.AddTransient(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -55,7 +56,7 @@ namespace BrightGlimmer.Api // Makes sure that the database is in fact created using (var serviceScope = app.ApplicationServices.GetService().CreateScope()) { - var context = serviceScope.ServiceProvider.GetRequiredService(); + var context = serviceScope.ServiceProvider.GetRequiredService(); context.Database.EnsureCreated(); } } diff --git a/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs b/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs new file mode 100644 index 0000000..0f50fc2 --- /dev/null +++ b/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs @@ -0,0 +1,29 @@ +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; +using BrightGlimmer.Services.Queries; +using MediatR; +using Microsoft.EntityFrameworkCore; /* TODO */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BrightGlimmer.Services.Handlers.QueryHandlers +{ + public class GetAllStudentsQueryHandler : IRequestHandler> + { + private readonly IRepository repository; + + public GetAllStudentsQueryHandler(IRepository repository) + { + this.repository = repository; + } + + public async Task> Handle(GetAllStudentsQuery request, CancellationToken cancellationToken) + { + return await repository.Get().ToListAsync(); + } + } +} diff --git a/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs b/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs new file mode 100644 index 0000000..fe3c1e5 --- /dev/null +++ b/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs @@ -0,0 +1,12 @@ +using BrightGlimmer.Domain; +using MediatR; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BrightGlimmer.Services.Queries +{ + public class GetAllStudentsQuery : IRequest> + { + } +} diff --git a/BrightGlimmer.Data/SqliteContext.cs b/BrightGlimmer.Data/BgContext.cs similarity index 72% rename from BrightGlimmer.Data/SqliteContext.cs rename to BrightGlimmer.Data/BgContext.cs index 7bdea95..1a02aac 100644 --- a/BrightGlimmer.Data/SqliteContext.cs +++ b/BrightGlimmer.Data/BgContext.cs @@ -1,4 +1,6 @@ using BrightGlimmer.Data.Domain; +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -6,9 +8,9 @@ using System.Text; namespace BrightGlimmer.Data { - public class SqliteContext : DbContext + public class BgContext : DbContext, IUnitOfWork { - public SqliteContext(DbContextOptions options) + public BgContext(DbContextOptions options) : base(options) { } diff --git a/BrightGlimmer.Data/BrightGlimmer.Data.csproj b/BrightGlimmer.Data/BrightGlimmer.Data.csproj index 2e8c223..d3f0c41 100644 --- a/BrightGlimmer.Data/BrightGlimmer.Data.csproj +++ b/BrightGlimmer.Data/BrightGlimmer.Data.csproj @@ -11,7 +11,7 @@ - + diff --git a/BrightGlimmer.Data/Interfaces/IRepository.cs b/BrightGlimmer.Data/Interfaces/IRepository.cs new file mode 100644 index 0000000..446319a --- /dev/null +++ b/BrightGlimmer.Data/Interfaces/IRepository.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BrightGlimmer.Data.Interfaces +{ + public interface IRepository + { + IUnitOfWork UnitOfWork { get; } + + T Create(T entity); + + T Update(T entity); + + void Remove(Guid id); + + IQueryable Get(); + + T Get(Guid id); + } +} diff --git a/BrightGlimmer.Data/Interfaces/IUnitOfWork.cs b/BrightGlimmer.Data/Interfaces/IUnitOfWork.cs new file mode 100644 index 0000000..894a303 --- /dev/null +++ b/BrightGlimmer.Data/Interfaces/IUnitOfWork.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BrightGlimmer.Data.Interfaces +{ + public interface IUnitOfWork + { + Task SaveChangesAsync(CancellationToken cancellationToken = default); + } +} diff --git a/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs b/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs index 41fe516..d44294a 100644 --- a/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs +++ b/BrightGlimmer.Data/Migrations/SqliteDatabaseContextModelSnapshot.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace BrightGlimmer.Data.Migrations { - [DbContext(typeof(SqliteContext))] + [DbContext(typeof(BgContext))] partial class SqliteDatabaseContextModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) diff --git a/BrightGlimmer.Data/Repositories/Repository.cs b/BrightGlimmer.Data/Repositories/Repository.cs new file mode 100644 index 0000000..9674115 --- /dev/null +++ b/BrightGlimmer.Data/Repositories/Repository.cs @@ -0,0 +1,47 @@ +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BrightGlimmer.Data.Repositories +{ + public abstract class Repository : IRepository where T : Entity + { + public IUnitOfWork UnitOfWork => context; + + protected BgContext context; + + public Repository(BgContext context) + { + this.context = context; + } + + public virtual T Create(T entity) + { + return context.Set().Add(entity).Entity; + } + + public virtual T Update(T entity) + { + return context.Set().Update(entity).Entity; + } + + public virtual void Remove(Guid id) + { + context.Set().Remove(Get(id)); + } + + public virtual IQueryable Get() + { + return context.Set(); + } + + public virtual T Get(Guid id) + { + return context.Set().Find(id); + } + } +} diff --git a/BrightGlimmer.Data/Repositories/StudentRepository.cs b/BrightGlimmer.Data/Repositories/StudentRepository.cs index 59a6704..0e25f81 100644 --- a/BrightGlimmer.Data/Repositories/StudentRepository.cs +++ b/BrightGlimmer.Data/Repositories/StudentRepository.cs @@ -1,4 +1,5 @@ -using BrightGlimmer.Data.Domain; +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; @@ -7,42 +8,8 @@ using System.Text; namespace BrightGlimmer.Data.Repositories { - public class StudentRepository + public class StudentRepository : Repository { - private readonly SqliteContext context; - - - public StudentRepository(SqliteContext context) - { - this.context = context; - } - - public Student Create(Student student) - { - EntityEntry entry = context.Students.Add(student); - context.SaveChanges(); - return entry.Entity; - } - - public void Update(Student student) - { - context.SaveChanges(); - } - - public void Remove(Guid id) - { - context.Students.Remove(GetById(id)); - context.SaveChanges(); - } - - public IQueryable GetAll() - { - return context.Students; - } - - public Student GetById(Guid id) - { - return context.Students.Find(id); - } + public StudentRepository(BgContext context) : base(context) { } } } diff --git a/BrightGlimmer.Domain/BrightGlimmer.Domain.csproj b/BrightGlimmer.Domain/BrightGlimmer.Domain.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/BrightGlimmer.Domain/BrightGlimmer.Domain.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/BrightGlimmer.Data/Domain/Course.cs b/BrightGlimmer.Domain/Course.cs similarity index 68% rename from BrightGlimmer.Data/Domain/Course.cs rename to BrightGlimmer.Domain/Course.cs index 01f670b..f111c71 100644 --- a/BrightGlimmer.Data/Domain/Course.cs +++ b/BrightGlimmer.Domain/Course.cs @@ -2,14 +2,12 @@ using System.Collections.Generic; using System.Text; -namespace BrightGlimmer.Data.Domain +namespace BrightGlimmer.Domain { - public class Course + public class Course : Entity { - public Guid Id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Code { get; set; } - } } diff --git a/BrightGlimmer.Domain/Entity.cs b/BrightGlimmer.Domain/Entity.cs new file mode 100644 index 0000000..86d619f --- /dev/null +++ b/BrightGlimmer.Domain/Entity.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BrightGlimmer.Domain +{ + public class Entity + { + public Guid Id { get; set; } + } +} diff --git a/BrightGlimmer.Data/Domain/Phone.cs b/BrightGlimmer.Domain/Phone.cs similarity index 68% rename from BrightGlimmer.Data/Domain/Phone.cs rename to BrightGlimmer.Domain/Phone.cs index e3858a7..99bb05e 100644 --- a/BrightGlimmer.Data/Domain/Phone.cs +++ b/BrightGlimmer.Domain/Phone.cs @@ -2,11 +2,10 @@ using System.Collections.Generic; using System.Text; -namespace BrightGlimmer.Data.Domain +namespace BrightGlimmer.Domain { - public class Phone + public class Phone : Entity { - public Guid Id { get; set; } public PhoneType Type { get; set; } public int AreaCode { get; set; } public int Number { get; set; } diff --git a/BrightGlimmer.Data/Domain/PhoneType.cs b/BrightGlimmer.Domain/PhoneType.cs similarity index 80% rename from BrightGlimmer.Data/Domain/PhoneType.cs rename to BrightGlimmer.Domain/PhoneType.cs index 7b14a40..405a3b1 100644 --- a/BrightGlimmer.Data/Domain/PhoneType.cs +++ b/BrightGlimmer.Domain/PhoneType.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace BrightGlimmer.Data.Domain +namespace BrightGlimmer.Domain { public enum PhoneType { diff --git a/BrightGlimmer.Data/Domain/Student.cs b/BrightGlimmer.Domain/Student.cs similarity index 76% rename from BrightGlimmer.Data/Domain/Student.cs rename to BrightGlimmer.Domain/Student.cs index 7e3a2b1..3188119 100644 --- a/BrightGlimmer.Data/Domain/Student.cs +++ b/BrightGlimmer.Domain/Student.cs @@ -2,11 +2,10 @@ using System.Collections.Generic; using System.Text; -namespace BrightGlimmer.Data.Domain +namespace BrightGlimmer.Domain { - public class Student + public class Student : Entity { - public Guid Id { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } diff --git a/BrightGlimmer.sln b/BrightGlimmer.sln index 65b0812..6198bfc 100644 --- a/BrightGlimmer.sln +++ b/BrightGlimmer.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BrightGlimmer.Data", "Brigh EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BrightGlimmer.Services", "BrightGlimmer.CQRS\BrightGlimmer.Services.csproj", "{AA6A646D-C493-43DC-BBE4-FF253852D9BF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrightGlimmer.Domain", "BrightGlimmer.Domain\BrightGlimmer.Domain.csproj", "{4E234F19-13EF-49A3-B8EB-FF7C9D2BF75F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {AA6A646D-C493-43DC-BBE4-FF253852D9BF}.Debug|Any CPU.Build.0 = Debug|Any CPU {AA6A646D-C493-43DC-BBE4-FF253852D9BF}.Release|Any CPU.ActiveCfg = Release|Any CPU {AA6A646D-C493-43DC-BBE4-FF253852D9BF}.Release|Any CPU.Build.0 = Release|Any CPU + {4E234F19-13EF-49A3-B8EB-FF7C9D2BF75F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4E234F19-13EF-49A3-B8EB-FF7C9D2BF75F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4E234F19-13EF-49A3-B8EB-FF7C9D2BF75F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4E234F19-13EF-49A3-B8EB-FF7C9D2BF75F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE