Api/Brizco.Repository/Handlers/Tasks/UpdateTaskCommandHandler.cs

113 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Brizco.Domain.Entities.Tasks;
namespace Brizco.Repository.Handlers.Tasks;
public class UpdateActivityCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<UpdateTaskCommand, bool>
{
public async Task<bool> Handle(UpdateTaskCommand request, CancellationToken cancellationToken)
{
var task = await repositoryWrapper.SetRepository<Domain.Entities.Tasks.Task>()
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id,cancellationToken);
if (task == null)
throw new AppException("Task not found", ApiResultStatusCode.NotFound);
if (currentUserService.ComplexId == null)
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
var newTask = Domain.Entities.Tasks.Task.Create(request.Title,
request.Description,
request.Type,
request.IsDisposable,
DateTimeExtensions.UnixTimeStampToDateTime(request.SetFor),
request.HasDisposed,
request.Amount,
request.AmountType,
complexId,
request.ScheduleType);
newTask.Id = request.Id;
if (request.Routines.Count == 0)
throw new AppException("لطفا روتین های وظیفه را انتخاب نمایید");
var routines = await repositoryWrapper.SetRepository<TaskRoutine>()
.TableNoTracking
.Where(tr => tr.TaskId == newTask.Id)
.ToListAsync(cancellationToken);
foreach (var taskRoutine in routines.Where(taskR => !request.Routines.Exists(r=>r== taskR.RoutineId)))
{
repositoryWrapper.SetRepository<TaskRoutine>()
.Delete(taskRoutine);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
foreach (var routine in request.Routines.Where(requestRoutine => !routines.Exists(r=>r.RoutineId==requestRoutine)))
{
newTask.AddRoutine(routine);
}
if (request.Shifts.Count == 0)
throw new AppException("اگر فعالیت برای یک گروه نقش انتخاب شده باشد باید لیست نقش ها را ارسال نمایید");
var shifts = await repositoryWrapper.SetRepository<TaskShift>()
.TableNoTracking
.Where(tr => tr.TaskId == newTask.Id)
.ToListAsync(cancellationToken);
foreach (var taskShift in shifts.Where(taskS => !request.Shifts.Exists(r => r == taskS.ShiftId)))
{
repositoryWrapper.SetRepository<TaskShift>()
.Delete(taskShift);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
foreach (var shift in request.Shifts.Where(requestShift => !shifts.Exists(r => r.ShiftId == requestShift)))
{
newTask.AddShift(shift);
}
if (request.Positions.Count == 0)
throw new AppException("اگر فعالیت برای یک گروه نقش انتخاب شده باشد باید لیست نقش ها را ارسال نمایید");
var positions = await repositoryWrapper.SetRepository<TaskPosition>()
.TableNoTracking
.Where(tr => tr.TaskId == newTask.Id)
.ToListAsync(cancellationToken);
foreach (var taskPosition in positions.Where(taskP => !request.Positions.Exists(r => r == taskP.PositionId)))
{
repositoryWrapper.SetRepository<TaskPosition>()
.Delete(taskPosition);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
foreach (var position in request.Positions.Where(requestP => !positions.Exists(r => r.PositionId == requestP)))
{
newTask.AddPosition(position);
}
if (task.ScheduleType == TaskScheduleType.Weekly && request.Days.Count == 0)
throw new AppException("اگر تکرار فعالیت به صورت هفتگی باشد باید روزهای ان هفته را انتخاب کنید");
request.Days.ForEach(d => task.SetDay(d));
var days = await repositoryWrapper.SetRepository<TaskDay>()
.TableNoTracking
.Where(tr => tr.TaskId == newTask.Id)
.ToListAsync(cancellationToken);
foreach (var taskDay in days.Where(taskD => !request.Days.Exists(r => r == taskD.DayOfWeek)))
{
repositoryWrapper.SetRepository<TaskDay>()
.Delete(taskDay);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
foreach (var day in request.Days.Where(requestP => !days.Exists(r => r.DayOfWeek == requestP)))
{
newTask.SetDay(day);
}
repositoryWrapper.SetRepository<Domain.Entities.Tasks.Task>()
.Update(newTask);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}