Exemple #1
0
        public IActionResult CreateAccount([FromBody] BankDTO bankDTO)
        {
            try
            {
                if (bankDTO == null)
                {
                    return(BadRequest(ModelState));
                }

                if (_IbankRepo.AccountExists(bankDTO.AccountNumber))
                {
                    ModelState.AddModelError("", "Bank Account Exixts");
                    return(StatusCode(404, ModelState));
                }

                //We have [required] field in the model
                //so don't need ModelState.IsValid to check

                var bankObj = _mapper.Map <Bank>(bankDTO); //Converting the received DTO to db obj

                if (!_IbankRepo.CreateAccount(bankObj))
                {
                    ModelState.AddModelError("", $"Something happened while saving the record {bankObj.AccountNumber}");
                    return(StatusCode(500, ModelState));
                }

                //return Ok();

                //CreatedAtrouted gives 201 statuscode and displays the new created obj
                //It uses the HHTPGET byt Id method from previous and passes the new ID
                return(CreatedAtRoute("GetAccountById", new { Id = bankObj.Id }, bankObj));
            }
            catch (Exception e)
            {
                throw new Exception("Created Account Error :", e);
            }
        }