Exemplo n.º 1
0
        public async Task <DataResult> Handle(Command request, CancellationToken cancellationToken)
        {
            ClassTime classTime = await _context.ClassTimes.Where(x => x.Id == request.Id)
                                  .Include(x => x.GroupClass)
                                  .FirstOrDefaultAsync(cancellationToken);

            ClassTimeAggregate.FromState(classTime)
            .WithDate(request.Start, request.End);

            //Update order
            List <ClassTime> allClasses = await _context.ClassTimes.Where(x => x.Id != request.Id)
                                          .Where(x => x.GroupClassId == classTime.GroupClassId)
                                          .OrderBy(x => x.StartDate)
                                          .ToListAsync(cancellationToken);

            allClasses.Add(classTime);
            allClasses = allClasses.OrderBy(x => x.StartDate).ToList();
            int nextNumber = 1;

            allClasses.ForEach(x =>
            {
                x.NumberOfClass   = nextNumber++;
                x.NumberOfClasses = classTime.GroupClass.NumberOfClasses;
            });



            _context.UpdateRange(allClasses);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }