Exemplo n.º 1
0
        public async Task <TripDto> CreateAsync(CreateTripDto createTripDto)
        {
            var tripEntity    = _mapper.Map <TripEntity>(createTripDto);
            var currentUserId = _currentUserService.UserId;

            await ValidateUserExistsByIdAsync(currentUserId);
            await ValidateTripRoleExistsByIdAsync((int)TripRoles.Admin);

            var tripSubscriberEntity = new TripSubscriberEntity()
            {
                UserId = currentUserId,
                Trip   = tripEntity
            };

            var tripSubscriberRoleEntity = new TripSubscriberRoleEntity()
            {
                TripSubscriber = tripSubscriberEntity,
                TripRoleId     = (int)TripRoles.Admin
            };

            await _tripFlipDbContext.TripSubscribersRoles.AddAsync(tripSubscriberRoleEntity);

            await _tripFlipDbContext.SaveChangesAsync();

            var tripDto = _mapper.Map <TripDto>(tripEntity);

            return(tripDto);
        }
Exemplo n.º 2
0
        public async Task SubscribeToTripAsync(int tripId)
        {
            var currentUserId = _currentUserService.UserId;

            var userExists = await _tripFlipDbContext.Users
                             .AnyAsync(user => user.Id == currentUserId);

            if (!userExists)
            {
                throw new NotFoundException(ErrorConstants.NotAuthorized);
            }

            var tripEntity = await _tripFlipDbContext.Trips
                             .Include(t => t.TripSubscribers)
                             .FirstOrDefaultAsync(t => t.Id == tripId);

            EntityValidationHelper.ValidateEntityNotNull(tripEntity, ErrorConstants.TripNotFound);

            var isAlreadySubscriber = tripEntity.TripSubscribers
                                      .Any(subscriber => subscriber.UserId == currentUserId);

            if (isAlreadySubscriber)
            {
                throw new ArgumentException(ErrorConstants.IsAlreadyTripSubscriber);
            }

            var subscriberRole = new TripSubscriberRoleEntity()
            {
                TripSubscriber = new TripSubscriberEntity()
                {
                    UserId = currentUserId,
                    TripId = tripId
                },

                TripRoleId = (int)TripRoles.Guest
            };

            await _tripFlipDbContext.TripSubscribersRoles.AddAsync(subscriberRole);

            await _tripFlipDbContext.SaveChangesAsync();
        }