public async Task AddUserToDestination_ShouldWorkCorrectlyAnd_ReturnViewModel() { var dest = new Destination { Id = "1", EndDateToJoin = DateTime.Now.AddDays(3) }; var destination = new List <Destination> { dest }.AsQueryable(); var destRepoMock = new Mock <IRepository <Destination> >(); destRepoMock.Setup(d => d.All()) .Returns(destination).Verifiable(); var user = new GoUser() { Id = "2" }; var du = new DestinationsUsers { DestinationId = "7", ParticipantId = "5" }; var destUsers = new List <DestinationsUsers> { du }.AsQueryable(); var destUserRepoMock = new Mock <IRepository <DestinationsUsers> >(); destUserRepoMock.Setup(d => d.All()) .Returns(destUsers).Verifiable(); var sut = new DestinationService(destRepoMock.Object, destUserRepoMock.Object, null, null, null, null, null, null); var actual = await sut.AddUserToDestination(user, "1"); DestUserViewModel expected = new DestUserViewModel() { DestinationId = dest.Id, ParticipantId = user.Id }; destRepoMock.Verify(); Assert.Equal(expected, actual, new DestUserViewModelComparer()); }
public async Task <DestUserViewModel> AddUserToDestination(GoUser user, string id) //destinationId { var destination = this.destRepository.All().FirstOrDefault(x => x.Id == id); if (destination == null) { throw new ArgumentException(DestinationNotExist); } var destUserModel = new DestUserViewModel { Destination = destination, DestinationId = destination.Id, Participant = user, ParticipantId = user.Id }; var destinationUser = new DestinationsUsers { Destination = destination, DestinationId = destination.Id, Participant = user, ParticipantId = user.Id }; if (this.destUsersRepository.All().FirstOrDefault(x => x.DestinationId == destinationUser.DestinationId && x.ParticipantId == destinationUser.ParticipantId) != null) { throw new ArgumentException(YouAreAureadyInThisGroup); } if (destination.EndDateToJoin < DateTime.Now) { throw new ArgumentException(TheEndDateToJoinIsPassed); } await this.destUsersRepository.AddAsync(destinationUser); await this.destUsersRepository.SaveChangesAsync(); return(destUserModel); }
public static async Task Seed(IServiceProvider provider, GoDbContext context) { if (!context.DestinationsUsers.Any()) { var allDests = context.Destinations.ToList(); foreach (var dest in allDests) { var countParticipans = new Random().Next(1, 20); for (int k = 0; k < countParticipans; k++) { var socialization = new Random().Next(1, 3); var randomUser = context.Users.OrderBy(x => Guid.NewGuid()).First(); var destUser = new DestinationsUsers { ParticipantId = randomUser.Id, Participant = randomUser, DestinationId = dest.Id, Destination = dest, Socialization = (Socialization)socialization }; var userExist = context.DestinationsUsers .FirstOrDefault(x => x.DestinationId == dest.Id && x.ParticipantId == randomUser.Id); if (userExist == null) { await context.DestinationsUsers.AddAsync(destUser); await context.SaveChangesAsync(); } } } } }