Exemple #1
0
        public async Task <bool> AcceptAsync(string id)
        {
            if (id == null)
            {
                return(false);
            }

            var request = await this.joinRequestsRepository.All()
                          .SingleOrDefaultAsync(r => r.Id == id);

            if (request == null)
            {
                return(false);
            }

            var userRide = new UserRide
            {
                UserId = request.UserId,
                RideId = request.RideId,
            };

            await this.userRidesRepository.AddAsync(userRide);

            this.joinRequestsRepository.Remove(request);

            await this.joinRequestsRepository.SaveChangesAsync();

            return(true);
        }
        public async Task AcceptAsync_WithUserAlreadyJoined_ReturnsFalse()
        {
            // Arrange
            const string testKey = "key111";

            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var ride = new Ride();

            await context.Rides.AddAsync(ride);

            var user = new PoolItUser
            {
                UserName = "******"
            };

            await context.Users.AddAsync(user);

            var userRide = new UserRide
            {
                User = user,
                Ride = ride
            };

            await context.UserRides.AddAsync(userRide);

            await context.Invitations.AddAsync(new Invitation
            {
                Key  = testKey,
                Ride = ride
            });

            context.SaveChanges();

            var invitationsService = new InvitationsService(
                new EfRepository <Invitation>(context),
                new EfRepository <Ride>(context),
                new EfRepository <JoinRequest>(context),
                new EfRepository <PoolItUser>(context),
                new EfRepository <UserRide>(context),
                null);

            // Act
            var result = await invitationsService.AcceptAsync(user.UserName, testKey);

            // Assert
            Assert.False(result);

            var invitationExists = await context.Invitations.AnyAsync();

            Assert.True(invitationExists);
        }
Exemple #3
0
        public async Task <bool> AcceptAsync(string userName, string invitationKey)
        {
            if (userName == null || invitationKey == null)
            {
                return(false);
            }

            var invitation = await this.invitationsRepository.All()
                             .Include(i => i.Ride.JoinRequests)
                             .Include(i => i.Ride.Participants)
                             .ThenInclude(p => p.User)
                             .SingleOrDefaultAsync(i => i.Key == invitationKey);

            var user = await this.usersRepository.All().SingleOrDefaultAsync(u => u.UserName == userName);

            if (invitation == null || user == null ||
                invitation.Ride.Participants.Any(p => p.User.UserName == userName))
            {
                return(false);
            }

            var requests = invitation.Ride.JoinRequests
                           .Where(r => r.UserId == user.Id)
                           .ToArray();

            //Remove user's join requests for this ride
            foreach (var joinRequest in requests)
            {
                this.joinRequestsRepository.Remove(joinRequest);
            }

            var userRide = new UserRide
            {
                UserId = user.Id,
                RideId = invitation.RideId,
            };

            await this.userRidesRepository.AddAsync(userRide);

            //Delete invitation
            this.invitationsRepository.Remove(invitation);

            await this.invitationsRepository.SaveChangesAsync();

            return(true);
        }
 public bool RideReservation(Ride ride, User user)
 {
     try
     {
         var itm = new UserRide()
         {
             Ride   = ride,
             RideId = ride.RideId,
             User   = user,
             UserId = user.Id
         };
         ctx.UserRides.Add(itm);
         ctx.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }