/// <summary>
        /// Update LGA entity
        /// </summary>
        /// <param name="model"></param>
        /// <param name="theState"></param>
        /// <returns></returns>
        public static LGA Create(LGAModel model, State theState)
        {
            return(new LGA()
            {
                Id = model.Id,
                Code = model.Code,
                Name = model.Name,
                TheState = theState,

                IsApproved = model.IsApproved,
                IsRestricted = model.IsRestricted,
                ModifiedAt = DateTime.Now,
                RecordStatus = model.RecordStatus
            });
        }
        /// <summary>
        /// Validate LGA
        /// </summary>
        /// <param name="model"></param>
        /// <param name="id"></param>
        /// <param name="errormsg"></param>
        /// <returns></returns>
        public static bool ValidateModel(LGAModel model, string id, out string errormsg)
        {
            errormsg = string.Empty;
            if (model == null)
            {
                errormsg = "LGA Name and Code Required";
                return(false);
            }

            if (String.IsNullOrEmpty(id))
            {
                errormsg = "LGA ID Required";
                return(false);
            }

            if (model.Id != id)
            {
                errormsg = "ID mismatch";
                return(false);
            }

            if (string.IsNullOrEmpty(model.Name.Trim()))
            {
                errormsg = "LGA Name Required";
                return(false);
            }

            if (string.IsNullOrEmpty(model.Code.Trim()))
            {
                errormsg = "LGA Code Required";
                return(false);
            }

            if (string.IsNullOrEmpty(model.StateId.Trim()))
            {
                errormsg = "State Required";
                return(false);
            }
            return(true);
        }
Exemplo n.º 3
0
        public async Task <ActionResult <DataResponse <LGAExtModel> > > Put([FromBody] LGAModel model, [FromRoute] string id)
        {
            DataResponse <LGAExtModel> response = new DataResponse <LGAExtModel>();
            LGAExtModel _DTO     = null;
            string      errorMsg = String.Empty;

            try
            {
                if (ModelState.IsValid && model != null)
                {
                    // Custom Validations
                    if (!LGAFactory.ValidateModel(model, id, out errorMsg))
                    {
                        response.Code        = ResponseCode.FAILED;
                        response.Description = ResponseDescription.FAILED;
                        response.Message     = errorMsg;
                        response.Data        = _DTO;
                        return(BadRequest(response));
                    }

                    State _theState = await _unitOfWork.States.GetAsync(model.StateId).ConfigureAwait(false);

                    // Check Entity Exist
                    if (_unitOfWork.LGAs.CheckExist(model.Name, model.Code, _theState, out errorMsg, id))
                    {
                        response.Code        = ResponseCode.FAILED;
                        response.Description = ResponseDescription.FAILED;
                        response.Message     = errorMsg;
                        response.Data        = _DTO;
                        return(BadRequest(response));
                    }

                    // Generate entity
                    var _entity = LGAFactory.Create(model, _theState);

                    // Create user
                    var _LGA = await _unitOfWork.LGAs.UpdateAsync(_entity).ConfigureAwait(false);

                    int done = await _unitOfWork.CompleteAsync().ConfigureAwait(false);

                    if (done > 0)
                    {
                        _DTO = _mapper.Map <LGAExtModel>(_LGA);

                        response.Code        = ResponseCode.SUCCESS;
                        response.Description = ResponseDescription.SUCCESS;
                        response.Message     = null;
                        response.Data        = _DTO;
                        return(Ok(_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));
            }
        }