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

            if (classTime is null)
            {
                return(DataResult.Error(PolishReadableMessage.Presence.ClassNotFound));
            }

            ParticipantClassTime participantClassTime = classTime.PresenceParticipants.FirstOrDefault(x => x.ParticipantId == request.ParticipantId);

            if (participantClassTime is null)
            {
                return(DataResult.Error(PolishReadableMessage.Presence.ParticipantNotFound));
            }
            if (participantClassTime.PresenceType != PresenceType.Help)
            {
                return(DataResult.Error(PolishReadableMessage.Presence.RemoveWrongType));
            }
            ClassTimeAggregate classTimeAggregate = ClassTimeAggregate.FromState(classTime);

            classTimeAggregate.RemoveParticipant(participantClassTime.ParticipantId);
            _context.Remove(participantClassTime);
            _context.Update(classTimeAggregate.State);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
0
        public async Task <DataResult> Handle(Command request, CancellationToken cancellationToken)
        {
            User participant = await _context.Users.Where(x => x.Id == request.ParticipantId).FirstOrDefaultAsync(cancellationToken);

            if (participant is null)
            {
                return(DataResult.Error(PolishReadableMessage.Presence.ParticipantNotFound));
            }

            ClassTime classTime = await _context.ClassTimes
                                  .Where(x => x.Id == request.ClassTimeId)
                                  .Include(x => x.PresenceParticipants)
                                  .FirstOrDefaultAsync(cancellationToken);

            if (classTime is null)
            {
                return(DataResult.Error(PolishReadableMessage.Presence.ClassNotFound));
            }

            ClassTimeAggregate   classTimeAggregate   = ClassTimeAggregate.FromState(classTime);
            ParticipantClassTime participantClassTime = classTimeAggregate.AddParticipant(participant, PresenceType.Help);

            participantClassTime.WasPresence = true;
            await _context.AddAsync(participantClassTime, cancellationToken);

            _context.Update(classTimeAggregate.State);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }
Ejemplo n.º 4
0
        public GroupClassBuilder CreateSchedule()
        {
            if (State.DurationTimeInMinutes == 0)
            {
                throw new BuilderException("Duration time is not set");
            }

            if (!State.ClassDaysOfWeek.Any())
            {
                throw new BuilderException("Class day of week is not set");
            }

            if (State.NumberOfClasses == 0)
            {
                throw new BuilderException("Number of classes is not set");
            }

            DateTime startClasses = State.StartClasses;

            if (startClasses == new DateTime())
            {
                throw new BuilderException("Start date is not set");
            }

            var numberOfWeeks           = State.NumberOfClasses / State.ClassDaysOfWeek.Count;
            List <ClassTime> classTimes = new List <ClassTime>();
            int numberOfClasses         = 1;

            for (int i = 0; i < numberOfWeeks; i++)
            {
                foreach (ClassDayOfWeek requestDayOfWeek in State.ClassDaysOfWeek)
                {
                    DateTime nextDayOfWeeks = FindNearestOfDayOfWeek(startClasses, requestDayOfWeek);
                    nextDayOfWeeks = nextDayOfWeeks.Add(requestDayOfWeek.Hour);
                    ClassTime classTime = ClassTimeAggregate.New()
                                          .AddParticipant(State.Participants.Select(x => x.User).ToList(), PresenceType.None)
                                          .FromGroupClass(State)
                                          .WithNumberOfClass(numberOfClasses++)
                                          .WithDate(nextDayOfWeeks, State.DurationTimeInMinutes).State;
                    classTimes.Add(classTime);
                }

                startClasses = startClasses.AddDays(7);
            }

            State.Schedule.AddRange(classTimes);
            return(this);
        }