public async Task <IActionResult> CreateAccountType([FromBody] AccountTypeForCreationDto accountType)
        {
            try
            {
                if (accountType == null)
                {
                    _logger.LogError("AccountType object sent from client is null.");
                    return(BadRequest("AccountType object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid AccountType object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var accountTypeEntity = _mapper.Map <AccountType>(accountType);

                _repository.AccountType.CreateAccountType(accountTypeEntity);
                await _repository.SaveAsync();

                var createdAccountType = _mapper.Map <AccountTypesDTO>(accountTypeEntity);

                return(CreatedAtRoute("AccountTypeById", new { id = createdAccountType.AccountTypeID }, createdAccountType));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateAccountType action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult <AccountTypeDto> > PostAccountType([FromBody] AccountTypeForCreationDto accountTypeForCreationDto)
        {
            var accountType = mapper.Map <AccountType>(accountTypeForCreationDto);

            await _context.AccountTypes.AddAsync(accountType);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetAccountType", new { id = accountType.Id }, accountType));
        }