public async Task <CreateRequestResponseDto> CreateOneAsync(CreateRequestRequestDto requestDto)
        {
            var foundUser = await userRepository.FindOneByIdAsync(requestDto.UserId);

            if (foundUser == null)
            {
                throw new NotFoundException("No se encontró el User para la creación del Request");
            }

            var sharedSpaceEntity = await sharedSpaceRepository.FindOneByIdAsync(requestDto.SharedSpaceId);

            if (sharedSpaceEntity == null)
            {
                throw new NotFoundException("No se encontró el SharedSpace para la creación del Request");
            }

            var requestsAlreadySent = requestRepository.GetAllByUserIdAndPublicationId(requestDto.UserId, sharedSpaceEntity.PublicationId);

            if (requestsAlreadySent.Count() > 0)
            {
                throw new BadRequestException("El usuario ya ha mandado una solicitud para esa publicacion");
            }

            if (sharedSpaceEntity.IsOccupied)
            {
                throw new BadRequestException("El SharedSpace ya se encuentra ocupado");
            }

            var requestEntity = requestDto.ToRequestEntity();
            await requestRepository.CreateOneAsync(requestEntity);

            return(CreateRequestResponseDto.FromRequestEntity(requestEntity));
        }
Exemple #2
0
        public async Task <IActionResult> Create([FromBody] CreateRequestRequestDto requestDto)
        {
            try
            {
                var response = await requestService.CreateOneAsync(requestDto);

                return(Ok(response));
            }
            catch (Exception e)
            {
                return(HttpExceptionMapper.ToHttpActionResult(e));
            }
        }