예제 #1
0
        private async Task AddParticipantsToGroup(Command request, CancellationToken cancellationToken,
                                                  List <GroupUser> users, List <User> usersToGroup)
        {
            Model.Domain.GroupClass groupClass = await _context
                                                 .GroupClass
                                                 .Include(x => x.Participants)
                                                 .Include(x => x.Schedule)
                                                 .ThenInclude(x => x.PresenceParticipants)
                                                 .Where(x => x.Id == request.GroupId)
                                                 .FirstOrDefaultAsync(cancellationToken);

            GroupClassAggregate groupClassAggregate = GroupClassAggregate.FromState(groupClass);

            foreach (User user in usersToGroup)
            {
                if (groupClassAggregate.IsParticipantExists(user.Id))
                {
                    continue;
                }
                PassAggregate passAggregate = PassAggregate.New()
                                              .WithStartDate(groupClass.StartClasses)
                                              .WithPrice(groupClass.PassPrice)
                                              .WithNumberOfEntry(groupClass.NumberOfClasses)
                                              .WithParticipant(user);
                GroupUser groupUser = users.First(x => x.Email == user.Email);
                groupClassAggregate.AddParticipant(user, groupUser.Role, passAggregate);
            }

            _context.GroupClass.Update(groupClass);
        }
예제 #2
0
        public async Task <DataResult> Handle(Command request, CancellationToken cancellationToken)
        {
            Model.Domain.Pass pass = await _context.Passes
                                     .Where(x => x.Id == request.Id)
                                     .Include(x => x.ParticipantClassTimes)
                                     .FirstOrDefaultAsync(cancellationToken);

            if (pass is null)
            {
                return(DataResult.Error(PolishReadableMessage.Pass.NotFound));
            }

            if (pass.Used == 0 && !pass.ParticipantClassTimes.Any())
            {
                _context.Remove(pass);
                await _context.SaveChangesAsync(cancellationToken);

                return(DataResult.Success());
            }

            PassAggregate.FromState(pass)
            .AsRemoved();
            _context.Update(pass);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }
예제 #3
0
        public async Task ExecuteShouldReturnPassAndRemoveWhenItWasGenerated()
        {
            //Arrange
            SchoolManagementContext context = new ContextBuilder().BuildClean();

            new RoleBuilder(context).WithName(Roles.Participant).BuildAndSave();

            GroupClass groupClass = new GroupClassBuilder(context)
                                    .WithStartClasses(DateTime.Now)
                                    .WithTimeDurationInMinutes(90)
                                    .AddClassDayOfWeek(x => x.WithDate(DayOfWeek.Monday, new TimeSpan(18, 0, 0)))
                                    .AddParticipant(userBuilder => userBuilder.WithName("Piotr", "Kowalski"), ParticipantRole.Follower,
                                                    passBuilder => passBuilder.WithNumberOfEntry(2).AsActive())
                                    .WithName("Boogie woogie")
                                    .WithNumberOfClasses(5)
                                    .CreateSchedule().BuildAndSave();

            ParticipantGroupClass participantGroupClass = groupClass.Participants.First();
            ParticipantClassTime  participantClassTime  = context.ParticipantPresences.First();
            Pass pass = new PassBuilder(context)
                        .WithParticipant(participantGroupClass.User)
                        .WithParticipantGroupClass(participantGroupClass)
                        .WithNumberOfEntry(2)
                        .With(x => x.ParticipantClassTimes.Add(participantClassTime))
                        .With(x => x.Used = 0).BuildAndSave();
            Pass generatedPass = new PassBuilder(context).AsActive()
                                 .WithParticipant(participantGroupClass.User)
                                 .WithParticipantGroupClass(participantGroupClass)
                                 .With(x => x.WasGenerateAutomatically = true)
                                 .WithNumberOfEntry(2)
                                 .With(x => x.ParticipantClassTimes.Add(participantClassTime))
                                 .With(x => x.Used = 0).BuildAndSave();

            PassAggregate.FromState(generatedPass).UsePass(participantClassTime);
            context.Update(participantClassTime);
            await context.SaveChangesAsync();

            Command command = new Command
            {
                ParticipantId = participantGroupClass.UserId,
                ClassTimeId   = participantClassTime.ClassTimeId
            };
            //Act
            DataResult dataResult = await new Handler(context).Handle(command, CancellationToken.None);

            //Assert
            context.Passes.Should().NotContain(generatedPass);
            context.Passes.Should().Contain(pass);
            pass.Used.Should().Be(0);
        }
예제 #4
0
        public async Task <DataResult> Handle(Command request, CancellationToken cancellationToken)
        {
            Model.Domain.Pass pass = await _context.Passes.Where(x => x.Id == request.Id).FirstOrDefaultAsync(cancellationToken);

            if (pass is null)
            {
                return(DataResult.Error(PolishReadableMessage.Pass.NotFound));
            }

            PassAggregate.FromState(pass).UpdateByCommand(request);
            _context.Passes.Update(pass);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }
예제 #5
0
        private async Task AddNewMembers(Command request, GroupClassAggregate groupClassAggregate, List <string> currentIds, List <string> updateMembers,
                                         CancellationToken cancellationToken)
        {
            List <string> newParticipantIds = GetNewIds(currentIds, updateMembers);
            List <User>   newMembers        =
                await _context.Users.Where(x => newParticipantIds.Contains(x.Id)).ToListAsync(cancellationToken);

            newMembers.ForEach(member =>
            {
                PassAggregate passAggregate = PassAggregate.New()
                                              .UpdateByCommand(request)
                                              .WithParticipant(member);
                ParticipantDto participantDto = request.Participants.First(p => p.Id == member.Id);
                groupClassAggregate.AddParticipant(member, participantDto.Role, passAggregate);
            });
        }
예제 #6
0
        private async Task AddParticipants(Command request, GroupClassAggregate groupClass,
                                           CancellationToken cancellationToken)
        {
            if (request.Participants.Any())
            {
                List <string> membersIds = request.Participants.Select(x => x.Id).ToList();
                List <User>   members    = await _context.Users.Where(x => membersIds.Contains(x.Id)).ToListAsync(cancellationToken);

                members.ForEach(x =>
                {
                    PassAggregate passAggregate = PassAggregate.New()
                                                  .UpdateByCommand(request)
                                                  .WithParticipant(x);

                    ParticipantDto participantDto = request.Participants.First(p => p.Id == x.Id);
                    groupClass.AddParticipant(x, participantDto.Role, passAggregate);
                });
            }
        }
예제 #7
0
        private async Task GenerateNextPass(CancellationToken cancellationToken, List <Model.Domain.Pass> passes, DateTime startDate,
                                            ParticipantClassTime participantPresence)
        {
            Model.Domain.Pass lastPass = passes.Where(x => x.Status == Model.Domain.Pass.PassStatus.NotActive)
                                         .OrderBy(x => x.PassNumber).First();
            Model.Domain.Pass newPass          = lastPass.Copy();
            PassAggregate     newPassAggregate = PassAggregate.FromState(newPass);

            newPassAggregate.AsActive()
            .WithStartDate(startDate)
            .WithPassNumber(passes.Count)
            .AsGenerateAutomatically();
            newPassAggregate.UsePass(participantPresence);

            await _context.AddAsync(newPassAggregate.State, cancellationToken);

            _context.Update(participantPresence);

            await _context.SaveChangesAsync(cancellationToken);
        }
예제 #8
0
        public async Task <DataResult <PassMessage> > Handle(Command request, CancellationToken cancellationToken)
        {
            ParticipantClassTime participantPresence = await _context.ParticipantPresences
                                                       .Where(x => x.Id == request.ParticipantClassTimeId)
                                                       .FirstOrDefaultAsync(cancellationToken);

            var classTimes = await _context.ClassTimes.Where(x => x.Id == participantPresence.ClassTimeId).Select(x => new { x.GroupClassId, x.StartDate }).FirstOrDefaultAsync(cancellationToken);


            List <Model.Domain.Pass> passes = await _context.Passes
                                              .Include(x => x.ParticipantGroupClass)
                                              .Where(x => x.ParticipantId == participantPresence.ParticipantId)
                                              .Where(x => x.ParticipantGroupClass.GroupClassId == classTimes.GroupClassId)
                                              .ToListAsync(cancellationToken);

            Model.Domain.Pass activePass = passes
                                           .Where(x => x.Status == Model.Domain.Pass.PassStatus.Active)
                                           .OrderBy(x => x.PassNumber)
                                           .FirstOrDefault();

            if (activePass is null)
            {
                await GenerateNextPass(cancellationToken, passes, classTimes.StartDate, participantPresence);

                return(DataResult <PassMessage> .Success(PassMessage.Error("Brak ważnego karnetu")));
            }

            PassAggregate passAggregate = PassAggregate.FromState(activePass);

            passAggregate.UsePass(participantPresence);
            _context.Update(passAggregate.State);
            _context.Update(participantPresence);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult <PassMessage> .Success(PassMessage.Success("Udanej nauki!")));
        }
예제 #9
0
        public async Task <DataResult> Handle(Command request, CancellationToken cancellationToken)
        {
            PassAggregate passAggregate = PassAggregate.New();

            passAggregate.UpdateByCommand(request);

            ParticipantGroupClass participantGroupClass = await _context.GroupClassMembers.Where(x => x.Id == request.MemberId)
                                                          .Include(x => x.Passes)
                                                          .Include(x => x.User)
                                                          .Include(x => x.GroupClass)
                                                          .FirstOrDefaultAsync(cancellationToken);

            passAggregate.WithParticipant(participantGroupClass.User)
            .WithPassNumber(participantGroupClass.Passes.Count)
            .WithStartDate(participantGroupClass.GroupClass.StartClasses);

            participantGroupClass.Passes.Add(passAggregate.State);
            _context.Update(participantGroupClass);
            await _context.Passes.AddAsync(passAggregate.State, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }
예제 #10
0
        public async Task <DataResult> Handle(Command request, CancellationToken cancellationToken)
        {
            ParticipantClassTime participantPresence = await _context.ParticipantPresences
                                                       .Include(x => x.Pass)
                                                       .Where(x => x.ParticipantId == request.ParticipantId)
                                                       .Where(x => x.ClassTimeId == request.ClassTimeId)
                                                       .FirstOrDefaultAsync(cancellationToken);

            Model.Domain.Pass pass = participantPresence.Pass;
            if (pass is null)
            {
                if (participantPresence.PresenceType != PresenceType.Help)
                {
                    return(DataResult.Error("Karnet nie został zarejestrowany "));
                }

                return(DataResult.Success());
            }
            PassAggregate passAggregate = PassAggregate.FromState(pass);

            passAggregate.ReturnPass(participantPresence);

            if (pass.WasGenerateAutomatically && pass.Used == 0)
            {
                _context.Remove(pass);
            }
            else
            {
                _context.Update(pass);
            }

            _context.Update(participantPresence);
            await _context.SaveChangesAsync(cancellationToken);

            return(DataResult.Success());
        }