/// <summary>
        /// Validate LGA
        /// </summary>
        /// <param name="model"></param>
        /// <param name="errormsg"></param>
        /// <returns></returns>
        public static bool ValidateModel(LGAMiniModel model, out string errormsg)
        {
            errormsg = string.Empty;

            if (model == null)
            {
                errormsg = "LGA Name and Code Required";
                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);
        }
        /// <summary>
        /// Create LGA entity
        /// </summary>
        /// <param name="model"></param>
        /// <param name="theState"></param>
        /// <returns></returns>
        public static LGA Create(LGAMiniModel model, State theState)
        {
            return(new LGA()
            {
                Code = model.Code,
                Name = model.Name,
                TheState = theState,

                Id = Guid.NewGuid().ToString(),
                CreatedAt = DateTime.Now,
                IsApproved = true,
                IsRestricted = false,
                ModifiedAt = DateTime.MinValue,
                RecordStatus = RecordStatus.ACTIVE
            });
        }
Exemple #3
0
        public async Task <ActionResult <DataResponse <LGAExtModel> > > Post([FromBody] LGAMiniModel model)
        {
            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, 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))
                    {
                        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.InsertAsync(_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(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));
            }
        }