Esempio n. 1
0
        public async Task <EstablishmentDto> Create(CreateEstablishmentDto createEstablishmentDto, Guid userId)
        {
            if (createEstablishmentDto is null)
            {
                throw new ArgumentNullException(nameof(createEstablishmentDto));
            }

            if (createEstablishmentDto.ShortCode == null)
            {
                try
                {
                    createEstablishmentDto.ShortCode = await _shortCodeService.NewShortCode();
                }
                catch (ShortCodeServiceException ex)
                {
                    throw new EstablishmentServiceException("Could not generate a short code.", ex);
                }
            }

            var establishmentEntity = _mapper.Map <Establishment>(createEstablishmentDto);

            establishmentEntity.UserId = userId;
            establishmentEntity.Status = EstablishmentStatus.Pending;

            _unitOfWork.EstablishmentRepo.Add(establishmentEntity);
            await _unitOfWork.SaveAsync();

            return(_mapper.Map <EstablishmentDto>(establishmentEntity));
        }
Esempio n. 2
0
        public async Task <ActionResult <EstablishmentDto> > CreateEstablishment([CustomizeValidator(Skip = true)] CreateEstablishmentDto createEstablishmentDto,
                                                                                 [FromServices] CreateEstablishmentValidator validator)
        {
            if (createEstablishmentDto is null)
            {
                throw new ArgumentNullException(nameof(createEstablishmentDto));
            }

            if (validator is null)
            {
                throw new ArgumentNullException(nameof(validator));
            }

            // Manual validation because ASP.NET’s validation pipeline is not asynchronous
            var validationResult = await validator.ValidateAsync(createEstablishmentDto);

            validationResult.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            var currentUserId = Guid.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            try
            {
                var establishmentToReturn = await _establishmentService.Create(createEstablishmentDto, currentUserId);

                return(CreatedAtAction(nameof(GetById), new { establishmentId = establishmentToReturn.Id }, establishmentToReturn));
            }
            catch (EstablishmentServiceException ex)
            {
                return(new JsonResult(new { ex.Message })
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }