Example #1
0
        public IActionResult Post([FromBody] DealerDto dealerDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var dealer = Mapper.Map <Dealer>(dealerDto);
                if (_dealerRepository.DealerExists(dealer))
                {
                    return(StatusCode(500, "Dealer already exists."));
                }

                var userId        = User.FindFirstValue(ClaimTypes.NameIdentifier);
                var profile       = _accountRepository.GetUserProfile(userId);
                var createdDealer = _dealerRepository.CreateDealer(dealer, profile.UserProfileId);

                if (createdDealer == null)
                {
                    return(StatusCode(500, "A problem happened while handling your request."));
                }

                var createdDealerToReturn = Mapper.Map <DealerDto>(createdDealer);
                return(Created(createdDealerToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed in Post /Dealers: {ex}");
                return(StatusCode(500, "A problem happened while handling your request."));
            }
        }