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());
        }
Example #2
0
        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);
        }