Beispiel #1
0
        public async Task <DTOs.Notification> Invite(DTOs.RequestJoinBand request)
        {
            var guest = await banditRepository.Get(request.Guest.Id);

            if (guest == null)
            {
                throw new NotFoundException("Bandit", request.Guest.Id);
            }

            var band = await bandRepository.Get(request.Band.Id);

            if (band == null)
            {
                throw new NotFoundException("Band", request.Band.Id);
            }

            if (!band.CanRequestToJoin(guest))
            {
                throw new InvalidOperationException(Strings.CantRequestToJoin.Format(guest.Name, band.Name));
            }

            var invitation = Invitation.Create(guest, band, band.Boss.Bandit);
            await invitationRepository.Add(invitation);

            var message = Notifications.RequestJoinBand(guest.Name, band.Name);
            await notifications.Send(message, to : band.Boss.Bandit);

            return(new DTOs.Notification
            {
                To = band.Boss.Bandit.Name,
                Title = message.Title,
                Content = message.Body
            });
        }
        public async Task <Invitation> Add(Invitation invitation)
        {
            var dbInvitation = new Entities.Invitation
            {
                GuestId   = invitation.Guest.Id,
                BandId    = invitation.Band.Id,
                HandlerId = invitation.Handler.Id
            };

            await dbContext
            .Set <Entities.Invitation>()
            .AddAsync(dbInvitation);

            await dbContext.SaveChangesAsync();

            return(Invitation.Create(dbInvitation.Id, invitation.Guest, invitation.Band, invitation.Handler));
        }
        public async Task HandleAsync(SendFriendshipInvitationCommand command)
        {
            var receiverDto = await _moduleClient.GetAsync <UserDto>("/identity/users", new GetUserQuery(command.ReceiverNickname));

            if (receiverDto is null)
            {
                throw new PersonNotFoundException();
            }

            if (await _friendshipRepository.ExistsBetweenAsync(new PersonId(command.SenderId), new PersonId(receiverDto.Id)))
            {
                throw new InvalidInvitationException();
            }

            var invitation = await Invitation.Create(new PersonId(command.SenderId), new PersonId(receiverDto.Id), _rule, _clock);

            await _invitationRepository.AddAsync(invitation);
        }
        public async Task <Invitation> Remove(int id)
        {
            var invitation = await dbContext
                             .Set <Entities.Invitation>()
                             .Include(i => i.Guest)
                             .Include(i => i.Band)
                             .ThenInclude(b => b.Boss)
                             .ThenInclude(b => b.Bandit)
                             .Include(i => i.Handler)
                             .FirstOrDefaultAsync(i => i.Id == id);

            if (invitation == null)
            {
                return(null);
            }

            var guest = Bandit.Create(invitation.Guest.Id,
                                      invitation.Guest.Name,
                                      invitation.Guest.Email);

            var handler = Bandit.Create(invitation.Handler.Id,
                                        invitation.Handler.Name,
                                        invitation.Handler.Email);

            var boss = Bandit.Create(invitation.Band.Boss.Bandit.Id,
                                     invitation.Band.Boss.Bandit.Name,
                                     invitation.Band.Boss.Bandit.Email);

            var band = Band.Create(invitation.Band.Id,
                                   invitation.Band.Name,
                                   boss);

            var invite = Invitation.Create(invitation.Id, guest, band, handler);

            dbContext
            .Set <Entities.Invitation>()
            .Remove(invitation);

            await dbContext.SaveChangesAsync();

            return(invite);
        }
        public async Task <IQueryable <Invitation> > GetFor(int banditId)
        {
            var dbInvitations = dbContext
                                .Set <Entities.Invitation>()
                                .Include(i => i.Guest)
                                .Include(i => i.Band)
                                .ThenInclude(b => b.Boss)
                                .ThenInclude(b => b.Bandit)
                                .Include(i => i.Handler)
                                .Where(i => i.HandlerId == banditId);

            await dbInvitations.LoadAsync();

            var invitations = dbInvitations
                              .AsEnumerable()
                              .Select(i => {
                var guest = Bandit.Create(i.Guest.Id,
                                          i.Guest.Name,
                                          i.Guest.Email);

                var handler = Bandit.Create(i.Handler.Id,
                                            i.Handler.Name,
                                            i.Handler.Email);

                var boss = Bandit.Create(i.Band.Boss.Bandit.Id,
                                         i.Band.Boss.Bandit.Name,
                                         i.Band.Boss.Bandit.Email);

                var band = Band.Create(i.Band.Id,
                                       i.Band.Name,
                                       boss);

                return(Invitation.Create(i.Id, guest, band, handler));
            })
                              .AsQueryable();

            return(invitations);
        }