From acba25cc0bee5d573288c265a8c18a7f76a84138 Mon Sep 17 00:00:00 2001 From: Giovani Date: Wed, 24 Apr 2019 23:57:55 +0000 Subject: [PATCH] Fleshed out entity classes and created command to create student --- .../Controllers/StudentController.cs | 13 ++++--- BrightGlimmer.Data/BgContext.cs | 13 ++++--- ...20190424233105_CreateDatabase.Designer.cs} | 5 ++- ...se.cs => 20190424233105_CreateDatabase.cs} | 6 +++ .../Migrations/BgContextModelSnapshot.cs | 3 ++ BrightGlimmer.Data/bright_glimmer.db | Bin 69632 -> 73728 bytes BrightGlimmer.Domain/Address.cs | 5 ++- .../BrightGlimmer.Domain.csproj | 3 ++ BrightGlimmer.Domain/Entity.cs | 27 +++++++++++--- BrightGlimmer.Domain/Student.cs | 35 +++++++++++++++--- .../BrightGlimmer.Service.csproj | 1 - .../Commands/CreateStudentCommand.cs | 11 +++++- .../CreateStudentCommandHandler.cs | 34 +++++++++++++++++ 13 files changed, 129 insertions(+), 27 deletions(-) rename BrightGlimmer.Data/Migrations/{20190424174147_CreateDatabase.Designer.cs => 20190424233105_CreateDatabase.Designer.cs} (97%) rename BrightGlimmer.Data/Migrations/{20190424174147_CreateDatabase.cs => 20190424233105_CreateDatabase.cs} (97%) create mode 100644 BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index 0a9cc9d..1e633e3 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using BrightGlimmer.Service.Commands; using BrightGlimmer.Services.Queries; using MediatR; using Microsoft.AspNetCore.Mvc; @@ -20,12 +21,14 @@ namespace BrightGlimmer.Api.Controllers public async Task GetAll() { var students = await mediator.Send(new GetAllStudentsQuery()); - if (students == null) - { - return NotFound(); - } - return new JsonResult(students); } + + [HttpPost] + public async Task Create([FromBody]CreateStudentCommand command) + { + var student = await mediator.Send(command); + return new JsonResult(student); + } } } \ No newline at end of file diff --git a/BrightGlimmer.Data/BgContext.cs b/BrightGlimmer.Data/BgContext.cs index 04f494e..30f2910 100644 --- a/BrightGlimmer.Data/BgContext.cs +++ b/BrightGlimmer.Data/BgContext.cs @@ -19,12 +19,13 @@ namespace BrightGlimmer.Data // dotnet ef migrations update --project ./BrightGlimmer.Data --startup-project ./BrightGlimmer.Api protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity() - .HasMany(x => x.Phones); - modelBuilder.Entity() - .HasOne(x => x.Address); - modelBuilder.Entity() - .HasMany(x => x.AssignedCourses); + modelBuilder.Entity(entity => + { + entity.HasIndex(e => e.StudentNumber).IsUnique(); + entity.HasOne(e => e.Address); + entity.HasMany(e => e.Phones); + entity.HasMany(e => e.AssignedCourses); + }); modelBuilder.Entity() .HasOne(x => x.Course); } diff --git a/BrightGlimmer.Data/Migrations/20190424174147_CreateDatabase.Designer.cs b/BrightGlimmer.Data/Migrations/20190424233105_CreateDatabase.Designer.cs similarity index 97% rename from BrightGlimmer.Data/Migrations/20190424174147_CreateDatabase.Designer.cs rename to BrightGlimmer.Data/Migrations/20190424233105_CreateDatabase.Designer.cs index 3a364a0..2ab00e8 100644 --- a/BrightGlimmer.Data/Migrations/20190424174147_CreateDatabase.Designer.cs +++ b/BrightGlimmer.Data/Migrations/20190424233105_CreateDatabase.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace BrightGlimmer.Data.Migrations { [DbContext(typeof(BgContext))] - [Migration("20190424174147_CreateDatabase")] + [Migration("20190424233105_CreateDatabase")] partial class CreateDatabase { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -157,6 +157,9 @@ namespace BrightGlimmer.Data.Migrations b.HasIndex("AddressId"); + b.HasIndex("StudentNumber") + .IsUnique(); + b.ToTable("Students"); }); diff --git a/BrightGlimmer.Data/Migrations/20190424174147_CreateDatabase.cs b/BrightGlimmer.Data/Migrations/20190424233105_CreateDatabase.cs similarity index 97% rename from BrightGlimmer.Data/Migrations/20190424174147_CreateDatabase.cs rename to BrightGlimmer.Data/Migrations/20190424233105_CreateDatabase.cs index e93a11e..c366193 100644 --- a/BrightGlimmer.Data/Migrations/20190424174147_CreateDatabase.cs +++ b/BrightGlimmer.Data/Migrations/20190424233105_CreateDatabase.cs @@ -147,6 +147,12 @@ namespace BrightGlimmer.Data.Migrations name: "IX_Students_AddressId", table: "Students", column: "AddressId"); + + migrationBuilder.CreateIndex( + name: "IX_Students_StudentNumber", + table: "Students", + column: "StudentNumber", + unique: true); } protected override void Down(MigrationBuilder migrationBuilder) diff --git a/BrightGlimmer.Data/Migrations/BgContextModelSnapshot.cs b/BrightGlimmer.Data/Migrations/BgContextModelSnapshot.cs index c039b2b..aa5ed36 100644 --- a/BrightGlimmer.Data/Migrations/BgContextModelSnapshot.cs +++ b/BrightGlimmer.Data/Migrations/BgContextModelSnapshot.cs @@ -155,6 +155,9 @@ namespace BrightGlimmer.Data.Migrations b.HasIndex("AddressId"); + b.HasIndex("StudentNumber") + .IsUnique(); + b.ToTable("Students"); }); diff --git a/BrightGlimmer.Data/bright_glimmer.db b/BrightGlimmer.Data/bright_glimmer.db index 50d21859df77d6b9c3e05692816924b916447e60..26a327e722ae0e0557f5e5d564075bd1d57884dd 100644 GIT binary patch delta 863 zcmZozz|wGlWrDOIGXn#I5D>!v_e33I9%cqTod90`9}I$wUnXWNGBJMHI5CZ}zE+T3 z++LQkQ9Cm)CAGpcB0jjJG$l2!q!`BVE6q(xErN*(IS08qhPWz(`gsP1x+-}3xwu9s zDB;wmq~PzTpaj#bq@V#~LrhiD++56*EX!tOY;0&?y7`VgzW|gy`J=ut8zX-y1Apme zL4j=k`bG{$UL6i+XKh(UK~)A};YlS`>H^w4PQ8s%&DC#js2Ld;TIw2@=o*6sdt7@1iZ;g&KsG%_|(@XF7~E6&eT2rh992vIOJHZb%`EJ(~#aLz2L^a_Bm z#hmj?^GYfW^bCNA3mCAB{BIce-)t5%c*HLv%&fzS?w(BsEDAsmlrZp@06mZe^Z+L# z56A;7(hTy(It=*{a$+^}?zql>+p~`USgkIaBaN+$&GjrS3``BpO>s+^F`g*c?Vg!m zmYA0rl%GS&m0kp6Ih3Rpm*^$u=K>ukjuKkfe1PsEs1If`@XrMLpr5}!m5~$V z0S+b>od@Pxz3Fq)y=UE<5K;FzBpS_$(9kz9H8wTIC1qh@$XvnDzQ_XIUWD6V=@&w8 zmN&SxJ^7=)@M4w#1!TOLCE$ + + ..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll + ..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.ComponentModel.Annotations.dll diff --git a/BrightGlimmer.Domain/Entity.cs b/BrightGlimmer.Domain/Entity.cs index dce4fd1..858ac97 100644 --- a/BrightGlimmer.Domain/Entity.cs +++ b/BrightGlimmer.Domain/Entity.cs @@ -1,15 +1,21 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; namespace BrightGlimmer.Domain { public class Entity { - public Guid Id { get; set; } - public bool IsDeleted { get; set; } - public DateTime CreatedDate { get; set; } - public DateTime ModifiedDate { get; set; } + [Key] + public Guid Id { get; private set; } + [JsonIgnore] + public bool IsDeleted { get; protected set; } + [JsonIgnore] + public DateTime CreatedDate { get; protected set; } + [JsonIgnore] + public DateTime ModifiedDate { get; protected set; } public Entity() { @@ -17,6 +23,15 @@ namespace BrightGlimmer.Domain CreatedDate = DateTime.UtcNow; ModifiedDate = DateTime.UtcNow; } - } + protected void Delete() + { + IsDeleted = true; + } + + protected void MarkModified() + { + ModifiedDate = DateTime.UtcNow; + } + } } diff --git a/BrightGlimmer.Domain/Student.cs b/BrightGlimmer.Domain/Student.cs index 8c8acd1..cde5a05 100644 --- a/BrightGlimmer.Domain/Student.cs +++ b/BrightGlimmer.Domain/Student.cs @@ -11,19 +11,44 @@ namespace BrightGlimmer.Domain [NotMapped] private readonly int MAX_STUDENT_NUMBER = 100000000; - public int StudentNumber { get; set; } + public int StudentNumber { get; private set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } - public string Email { get; set; } - public List Phones { get; set; } + public string Email { get; private set; } public Address Address { get; set; } public string ProfilePictureUrl { get; set; } - public List AssignedCourses { get; set; } - public Student() : base() + private readonly List phones = new List(); + public IReadOnlyList Phones => phones; + + private readonly List assignedCourses = new List(); + public IReadOnlyList AssignedCourses => assignedCourses; + + private Student() { } + + public Student(string firstName, + string lastName, + string email, + List phones, + Address address) : base() { StudentNumber = new Random().Next(MAX_STUDENT_NUMBER); /* TODO: Optimize student number creation */ + FirstName = firstName; + LastName = lastName; + Email = email; + Address = address; + this.phones = phones; + } + + public void AddPhone(Phone phone) + { + phones.Add(phone); + } + + public void AddAssignedCourse(AssignedCourse assignedCourse) + { + assignedCourses.Add(assignedCourse); } } } diff --git a/BrightGlimmer.Service/BrightGlimmer.Service.csproj b/BrightGlimmer.Service/BrightGlimmer.Service.csproj index a764589..b40d112 100644 --- a/BrightGlimmer.Service/BrightGlimmer.Service.csproj +++ b/BrightGlimmer.Service/BrightGlimmer.Service.csproj @@ -6,7 +6,6 @@ - diff --git a/BrightGlimmer.Service/Commands/CreateStudentCommand.cs b/BrightGlimmer.Service/Commands/CreateStudentCommand.cs index 8787ce5..a620853 100644 --- a/BrightGlimmer.Service/Commands/CreateStudentCommand.cs +++ b/BrightGlimmer.Service/Commands/CreateStudentCommand.cs @@ -1,11 +1,18 @@ -using MediatR; +using BrightGlimmer.Domain; +using MediatR; using System; using System.Collections.Generic; using System.Text; namespace BrightGlimmer.Service.Commands { - public class CreateStudentCommand : IRequest + public class CreateStudentCommand : IRequest { + public string FirstName { get; set; } + public string MiddleName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public List Phones { get; set; } + public Address Address { get; set; } } } diff --git a/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs b/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs new file mode 100644 index 0000000..4a94b66 --- /dev/null +++ b/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs @@ -0,0 +1,34 @@ +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; +using BrightGlimmer.Service.Commands; +using MediatR; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BrightGlimmer.Service.Handlers.CommandHandlers +{ + public class CreateStudentCommandHandler : IRequestHandler + { + private readonly ICommandRepository repository; + + public CreateStudentCommandHandler(ICommandRepository repository) + { + this.repository = repository; + } + + public async Task Handle(CreateStudentCommand request, CancellationToken cancellationToken) + { + var student = new Student(request.FirstName, + request.LastName, + request.Email, + request.Phones, + request.Address); + repository.Create(student); + await repository.UnitOfWork.SaveChangesAsync(); + return student; + } + } +}