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 GrantTripRoleAsync(GrantTripRolesDto grantTripRolesDto)
        {
            // Validate user-to-grant-role-to exists.
            var userToGrantRole = await _tripFlipDbContext.Users
                                  .SingleOrDefaultAsync(user => user.Id == grantTripRolesDto.UserId);

            EntityValidationHelper.ValidateEntityNotNull(
                userToGrantRole, ErrorConstants.UserNotFound);

            // Validate trip exists.
            var trip = await _tripFlipDbContext.Trips
                       .Include(t => t.TripSubscribers)
                       .ThenInclude(subscribers => subscribers.TripRoles)
                       .FirstOrDefaultAsync(t => t.Id == grantTripRolesDto.TripId);

            EntityValidationHelper.
            ValidateEntityNotNull(trip, ErrorConstants.TripNotFound);

            // Validate current user is trip admin.
            await EntityValidationHelper.ValidateCurrentUserTripRoleAsync(
                _currentUserService,
                _tripFlipDbContext,
                grantTripRolesDto.TripId,
                TripRoles.Admin,
                ErrorConstants.NotTripAdmin);

            // Remove invalid values from requested role id collection.
            var realTripRolesIds = (IEnumerable <int>)Enum.GetValues(typeof(TripRoles));

            grantTripRolesDto.TripRoleIds = grantTripRolesDto
                                            .TripRoleIds
                                            .Distinct()
                                            .Where(requestedId => realTripRolesIds.Contains(requestedId));

            var tripSubscriber = trip.TripSubscribers
                                 .FirstOrDefault(subscribers => subscribers.UserId == grantTripRolesDto.UserId);

            // Subscribe given user to trip if he's not subscribed.
            if (tripSubscriber is null)
            {
                tripSubscriber = new TripSubscriberEntity()
                {
                    TripId = grantTripRolesDto.TripId,
                    UserId = grantTripRolesDto.UserId
                };
            }

            // Remove subscriber's current set of roles.
            if (!(tripSubscriber.TripRoles is null))
            {
                _tripFlipDbContext.TripSubscribersRoles.RemoveRange(
                    tripSubscriber.TripRoles);
            }

            // Add requested set of roles to subscriber.
            tripSubscriber.TripRoles = grantTripRolesDto.TripRoleIds
                                       .Select(tripRoleId => new TripSubscriberRoleEntity()
            {
                TripRoleId     = tripRoleId,
                TripSubscriber = tripSubscriber
            }).ToList();

            await _tripFlipDbContext.SaveChangesAsync();
        }