/// <summary> /// Validate State /// </summary> /// <param name="model"></param> /// <param name="errormsg"></param> /// <returns></returns> public static bool ValidateModel(StateMiniModel model, out string errormsg) { errormsg = string.Empty; if (model == null) { errormsg = "State Name and Code Required"; return(false); } if (string.IsNullOrEmpty(model.Name.Trim())) { errormsg = "State Name Required"; return(false); } if (string.IsNullOrEmpty(model.Code.Trim())) { errormsg = "State Code Required"; return(false); } if (string.IsNullOrEmpty(model.CountryId.Trim())) { errormsg = "Country Required"; return(false); } return(true); }
/// <summary> /// Create State entity /// </summary> /// <param name="model"></param> /// <param name="theCountry"></param> /// <returns></returns> public static State Create(StateMiniModel model, Country theCountry) { return(new State() { Code = model.Code, Name = model.Name, TheCountry = theCountry, Id = Guid.NewGuid().ToString(), CreatedAt = DateTime.Now, IsApproved = true, IsRestricted = false, ModifiedAt = DateTime.MinValue, RecordStatus = RecordStatus.ACTIVE }); }
public async Task <ActionResult <DataResponse <StateExtModel> > > Post([FromBody] StateMiniModel model) { DataResponse <StateExtModel> response = new DataResponse <StateExtModel>(); StateExtModel _DTO = null; string errorMsg = String.Empty; try { if (ModelState.IsValid && model != null) { // Custom Validations if (!StateFactory.ValidateModel(model, out errorMsg)) { response.Code = ResponseCode.FAILED; response.Description = ResponseDescription.FAILED; response.Message = errorMsg; response.Data = _DTO; return(BadRequest(response)); } Country _theCountry = await _unitOfWork.Countries.GetAsync(model.CountryId).ConfigureAwait(false); // Check Entity Exist if (_unitOfWork.States.CheckExist(model.Name, model.Code, _theCountry, out errorMsg)) { response.Code = ResponseCode.FAILED; response.Description = ResponseDescription.FAILED; response.Message = errorMsg; response.Data = _DTO; return(BadRequest(response)); } // Generate entity var _entity = StateFactory.Create(model, _theCountry); // Create user var _state = await _unitOfWork.States.InsertAsync(_entity).ConfigureAwait(false); int done = await _unitOfWork.CompleteAsync().ConfigureAwait(false); if (done > 0) { _DTO = _mapper.Map <StateExtModel>(_state); response.Code = ResponseCode.SUCCESS; response.Description = ResponseDescription.SUCCESS; response.Message = null; response.Data = _DTO; return(Created(new Uri($"{Request.Path}/{_DTO.Id}", UriKind.Relative), _DTO)); } } response.Code = ResponseCode.FAILED; response.Description = ResponseDescription.FAILED; response.Message = "Invalid user input"; response.Data = _DTO; return(BadRequest(response)); } catch (Exception ex) { Guid _ErrorCode = Guid.NewGuid(); Log.Error("Fatal Error [{ErrorCode}]: {Message}", _ErrorCode, ex.Message); response.Code = ResponseCode.SYSTEM_ERROR; response.Description = ResponseDescription.SYSTEM_ERROR; response.Message = $"System Error: Something went wrong here! Kindly contact the support with this error code [{_ErrorCode}]"; response.Data = _DTO; return(BadRequest(response)); } }