Fleshed out entity classes and created command to create student
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using BrightGlimmer.Service.Commands;
|
||||||
using BrightGlimmer.Services.Queries;
|
using BrightGlimmer.Services.Queries;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@@ -20,12 +21,14 @@ namespace BrightGlimmer.Api.Controllers
|
|||||||
public async Task<ActionResult> GetAll()
|
public async Task<ActionResult> GetAll()
|
||||||
{
|
{
|
||||||
var students = await mediator.Send(new GetAllStudentsQuery());
|
var students = await mediator.Send(new GetAllStudentsQuery());
|
||||||
if (students == null)
|
return new JsonResult(students);
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonResult(students);
|
[HttpPost]
|
||||||
|
public async Task<ActionResult> Create([FromBody]CreateStudentCommand command)
|
||||||
|
{
|
||||||
|
var student = await mediator.Send(command);
|
||||||
|
return new JsonResult(student);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,12 +19,13 @@ namespace BrightGlimmer.Data
|
|||||||
// dotnet ef migrations update --project ./BrightGlimmer.Data --startup-project ./BrightGlimmer.Api
|
// dotnet ef migrations update --project ./BrightGlimmer.Data --startup-project ./BrightGlimmer.Api
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
modelBuilder.Entity<Student>()
|
modelBuilder.Entity<Student>(entity =>
|
||||||
.HasMany(x => x.Phones);
|
{
|
||||||
modelBuilder.Entity<Student>()
|
entity.HasIndex(e => e.StudentNumber).IsUnique();
|
||||||
.HasOne(x => x.Address);
|
entity.HasOne(e => e.Address);
|
||||||
modelBuilder.Entity<Student>()
|
entity.HasMany(e => e.Phones);
|
||||||
.HasMany(x => x.AssignedCourses);
|
entity.HasMany(e => e.AssignedCourses);
|
||||||
|
});
|
||||||
modelBuilder.Entity<AssignedCourse>()
|
modelBuilder.Entity<AssignedCourse>()
|
||||||
.HasOne(x => x.Course);
|
.HasOne(x => x.Course);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|||||||
namespace BrightGlimmer.Data.Migrations
|
namespace BrightGlimmer.Data.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(BgContext))]
|
[DbContext(typeof(BgContext))]
|
||||||
[Migration("20190424174147_CreateDatabase")]
|
[Migration("20190424233105_CreateDatabase")]
|
||||||
partial class CreateDatabase
|
partial class CreateDatabase
|
||||||
{
|
{
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
@@ -157,6 +157,9 @@ namespace BrightGlimmer.Data.Migrations
|
|||||||
|
|
||||||
b.HasIndex("AddressId");
|
b.HasIndex("AddressId");
|
||||||
|
|
||||||
|
b.HasIndex("StudentNumber")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
b.ToTable("Students");
|
b.ToTable("Students");
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -147,6 +147,12 @@ namespace BrightGlimmer.Data.Migrations
|
|||||||
name: "IX_Students_AddressId",
|
name: "IX_Students_AddressId",
|
||||||
table: "Students",
|
table: "Students",
|
||||||
column: "AddressId");
|
column: "AddressId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Students_StudentNumber",
|
||||||
|
table: "Students",
|
||||||
|
column: "StudentNumber",
|
||||||
|
unique: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
@@ -155,6 +155,9 @@ namespace BrightGlimmer.Data.Migrations
|
|||||||
|
|
||||||
b.HasIndex("AddressId");
|
b.HasIndex("AddressId");
|
||||||
|
|
||||||
|
b.HasIndex("StudentNumber")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
b.ToTable("Students");
|
b.ToTable("Students");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -14,7 +15,9 @@ namespace BrightGlimmer.Domain
|
|||||||
public string StateCode { get; set; }
|
public string StateCode { get; set; }
|
||||||
public string County { get; set; }
|
public string County { get; set; }
|
||||||
public string ZipCode { get; set; }
|
public string ZipCode { get; set; }
|
||||||
|
[JsonIgnore]
|
||||||
public decimal Latitude { get; set; }
|
public decimal Latitude { get; set; }
|
||||||
|
[JsonIgnore]
|
||||||
public decimal Longitude { get; set; }
|
public decimal Longitude { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Newtonsoft.Json">
|
||||||
|
<HintPath>..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.ComponentModel.Annotations">
|
<Reference Include="System.ComponentModel.Annotations">
|
||||||
<HintPath>..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.ComponentModel.Annotations.dll</HintPath>
|
<HintPath>..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.ComponentModel.Annotations.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace BrightGlimmer.Domain
|
namespace BrightGlimmer.Domain
|
||||||
{
|
{
|
||||||
public class Entity
|
public class Entity
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
[Key]
|
||||||
public bool IsDeleted { get; set; }
|
public Guid Id { get; private set; }
|
||||||
public DateTime CreatedDate { get; set; }
|
[JsonIgnore]
|
||||||
public DateTime ModifiedDate { get; set; }
|
public bool IsDeleted { get; protected set; }
|
||||||
|
[JsonIgnore]
|
||||||
|
public DateTime CreatedDate { get; protected set; }
|
||||||
|
[JsonIgnore]
|
||||||
|
public DateTime ModifiedDate { get; protected set; }
|
||||||
|
|
||||||
public Entity()
|
public Entity()
|
||||||
{
|
{
|
||||||
@@ -17,6 +23,15 @@ namespace BrightGlimmer.Domain
|
|||||||
CreatedDate = DateTime.UtcNow;
|
CreatedDate = DateTime.UtcNow;
|
||||||
ModifiedDate = DateTime.UtcNow;
|
ModifiedDate = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void Delete()
|
||||||
|
{
|
||||||
|
IsDeleted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void MarkModified()
|
||||||
|
{
|
||||||
|
ModifiedDate = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,19 +11,44 @@ namespace BrightGlimmer.Domain
|
|||||||
[NotMapped]
|
[NotMapped]
|
||||||
private readonly int MAX_STUDENT_NUMBER = 100000000;
|
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 FirstName { get; set; }
|
||||||
public string MiddleName { get; set; }
|
public string MiddleName { get; set; }
|
||||||
public string LastName { get; set; }
|
public string LastName { get; set; }
|
||||||
public string Email { get; set; }
|
public string Email { get; private set; }
|
||||||
public List<Phone> Phones { get; set; }
|
|
||||||
public Address Address { get; set; }
|
public Address Address { get; set; }
|
||||||
public string ProfilePictureUrl { get; set; }
|
public string ProfilePictureUrl { get; set; }
|
||||||
public List<AssignedCourse> AssignedCourses { get; set; }
|
|
||||||
|
|
||||||
public Student() : base()
|
private readonly List<Phone> phones = new List<Phone>();
|
||||||
|
public IReadOnlyList<Phone> Phones => phones;
|
||||||
|
|
||||||
|
private readonly List<AssignedCourse> assignedCourses = new List<AssignedCourse>();
|
||||||
|
public IReadOnlyList<AssignedCourse> AssignedCourses => assignedCourses;
|
||||||
|
|
||||||
|
private Student() { }
|
||||||
|
|
||||||
|
public Student(string firstName,
|
||||||
|
string lastName,
|
||||||
|
string email,
|
||||||
|
List<Phone> phones,
|
||||||
|
Address address) : base()
|
||||||
{
|
{
|
||||||
StudentNumber = new Random().Next(MAX_STUDENT_NUMBER); /* TODO: Optimize student number creation */
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Interfaces\" />
|
<Folder Include="Interfaces\" />
|
||||||
<Folder Include="Handlers\CommandHandlers\" />
|
|
||||||
<Folder Include="Handlers\QueryHandlers\" />
|
<Folder Include="Handlers\QueryHandlers\" />
|
||||||
<Folder Include="Queries\" />
|
<Folder Include="Queries\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
using MediatR;
|
using BrightGlimmer.Domain;
|
||||||
|
using MediatR;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace BrightGlimmer.Service.Commands
|
namespace BrightGlimmer.Service.Commands
|
||||||
{
|
{
|
||||||
public class CreateStudentCommand : IRequest<bool>
|
public class CreateStudentCommand : IRequest<Student>
|
||||||
{
|
{
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string MiddleName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public string Email { get; set; }
|
||||||
|
public List<Phone> Phones { get; set; }
|
||||||
|
public Address Address { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<CreateStudentCommand, Student>
|
||||||
|
{
|
||||||
|
private readonly ICommandRepository<Student> repository;
|
||||||
|
|
||||||
|
public CreateStudentCommandHandler(ICommandRepository<Student> repository)
|
||||||
|
{
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Student> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user