Exemplo n.º 1
0
        public async Task <IActionResult> AddCar([FromForm] NewCarDto dto)
        {
            try
            {
                _logger.LogInformation($"Create new car ad by userID: {_currentUserService.UserId}");
                var newCar = _mapper.Map <Car>(dto);

                if (dto.Image != null)
                {
                    var imageResult = await _storageService.UploadImageToStorage(_currentUserService.UserId, dto.Image);

                    if (imageResult != null)
                    {
                        newCar.Image = imageResult.Name;
                    }
                }

                var result = await _carService.AddCar(newCar, _currentUserService.UserId);

                if (result != null)
                {
                    return(CreatedAtAction(nameof(Get), new { carId = result.Id },
                                           _mapper.Map <CarDto>(result)));
                }

                _logger.LogError($"Something went wrong to create new car");
                return(BadRequest());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside AddCar action: {e.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Exemplo n.º 2
0
        public IActionResult NewCar([FromBody] NewCarDto model)
        {
            var car = _mapper.Map <CarModel>(model);

            try
            {
                _carService.NewCar(car);
                return(Ok());
            }
            catch (AppException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }