wip being able to soft delete a student
This commit is contained in:
@@ -46,5 +46,12 @@ namespace BrightGlimmer.Api.Controllers
|
||||
var student = await mediator.Send(command);
|
||||
return new JsonResult(student);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult> Delete(Guid id)
|
||||
{
|
||||
var success = await mediator.Send(new DeleteStudentCommand(id));
|
||||
return new JsonResult(success);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<AssignedCourse>()
|
||||
.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<Student> Students { get; set; }
|
||||
|
||||
Binary file not shown.
@@ -24,7 +24,7 @@ namespace BrightGlimmer.Domain
|
||||
ModifiedDate = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
protected void Delete()
|
||||
public void Delete()
|
||||
{
|
||||
IsDeleted = true;
|
||||
}
|
||||
|
||||
17
BrightGlimmer.Service/Commands/DeleteStudentCommand.cs
Normal file
17
BrightGlimmer.Service/Commands/DeleteStudentCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using MediatR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Service.Commands
|
||||
{
|
||||
public class DeleteStudentCommand : IRequest<bool>
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public DeleteStudentCommand(Guid id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<DeleteStudentCommand, bool>
|
||||
{
|
||||
private readonly ICommandRepository<Student> repository;
|
||||
|
||||
public DeleteStudentCommandHandler(ICommandRepository<Student> repository)
|
||||
{
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task<bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user