using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Domain.Service; 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) { var student = await repository.GetAsync(request.Id); student.Address?.Delete(); student.AssignedCourses.ToList().ForEach(x => x.Delete()); student.Phones.ToList().ForEach(x => x.Delete()); student.Delete(); await repository.UnitOfWork.SaveChangesAsync(); return true; } } }