diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index c9f8ea5..2ab5703 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -46,5 +46,12 @@ namespace BrightGlimmer.Api.Controllers var student = await mediator.Send(command); return new JsonResult(student); } + + [HttpDelete("{id}")] + public async Task Delete(Guid id) + { + var success = await mediator.Send(new DeleteStudentCommand(id)); + return new JsonResult(success); + } } } \ No newline at end of file diff --git a/BrightGlimmer.Data/BgContext.cs b/BrightGlimmer.Data/BgContext.cs index 30f2910..077d7d7 100644 --- a/BrightGlimmer.Data/BgContext.cs +++ b/BrightGlimmer.Data/BgContext.cs @@ -3,6 +3,7 @@ using BrightGlimmer.Domain; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Linq.Expressions; using System.Text; namespace BrightGlimmer.Data @@ -28,6 +29,21 @@ namespace BrightGlimmer.Data }); modelBuilder.Entity() .HasOne(x => x.Course); + + // Make all entities soft deletables and filter by soft deleted on queries + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + entityType.GetOrAddProperty("IsDeleted", typeof(bool)); + + var parameter = Expression.Parameter(entityType.ClrType); + var propertyMethodInfo = typeof(EF).GetMethod("Property").MakeGenericMethod(typeof(bool)); + var isDeletedProperty = Expression.Call(propertyMethodInfo, parameter, Expression.Constant("IsDeleted")); + + BinaryExpression compareExpression = Expression.MakeBinary(ExpressionType.Equal, isDeletedProperty, Expression.Constant(false)); + + var lambda = Expression.Lambda(compareExpression, parameter); + modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda); + } } public DbSet Students { get; set; } diff --git a/BrightGlimmer.Data/bright_glimmer.db b/BrightGlimmer.Data/bright_glimmer.db index 86b149f..a007be3 100644 Binary files a/BrightGlimmer.Data/bright_glimmer.db and b/BrightGlimmer.Data/bright_glimmer.db differ diff --git a/BrightGlimmer.Domain/Entity.cs b/BrightGlimmer.Domain/Entity.cs index d38898e..1caf729 100644 --- a/BrightGlimmer.Domain/Entity.cs +++ b/BrightGlimmer.Domain/Entity.cs @@ -24,7 +24,7 @@ namespace BrightGlimmer.Domain ModifiedDate = DateTime.UtcNow; } - protected void Delete() + public void Delete() { IsDeleted = true; } diff --git a/BrightGlimmer.Service/Commands/DeleteStudentCommand.cs b/BrightGlimmer.Service/Commands/DeleteStudentCommand.cs new file mode 100644 index 0000000..d875f96 --- /dev/null +++ b/BrightGlimmer.Service/Commands/DeleteStudentCommand.cs @@ -0,0 +1,17 @@ +using MediatR; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BrightGlimmer.Service.Commands +{ + public class DeleteStudentCommand : IRequest + { + public Guid Id { get; set; } + + public DeleteStudentCommand(Guid id) + { + Id = id; + } + } +} diff --git a/BrightGlimmer.Service/Handlers/CommandHandlers/DeleteStudentCommandHandler.cs b/BrightGlimmer.Service/Handlers/CommandHandlers/DeleteStudentCommandHandler.cs new file mode 100644 index 0000000..f795627 --- /dev/null +++ b/BrightGlimmer.Service/Handlers/CommandHandlers/DeleteStudentCommandHandler.cs @@ -0,0 +1,36 @@ +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; +using BrightGlimmer.Service.Commands; +using MediatR; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BrightGlimmer.Service.Handlers.CommandHandlers +{ + public class DeleteStudentCommandHandler : IRequestHandler + { + private readonly ICommandRepository repository; + + public DeleteStudentCommandHandler(ICommandRepository repository) + { + this.repository = repository; + } + + public async Task Handle(DeleteStudentCommand request, CancellationToken cancellationToken) + { + /* TODO: CHANGE TO SOFT DELETE */ + var student = await repository.GetAsync(request.Id); + + student.Address.Delete(); + student.AssignedCourses.ToList().ForEach(x => x.Delete()); + student.Phones.ToList().ForEach(x => x.Delete()); + + await repository.UnitOfWork.SaveChangesAsync(); + return true; + } + } +}