Exemplo n.º 1
0
        public async Task <IHttpActionResult> Post([FromBody] ServiceForm req)
        {
            try
            {
                string errorMessage = "UnknowError";
                string errorCode    = ErrorCodeEnum.UnknownError.ToString();

                #region Validate
                if (!Validate(req, out errorCode, out errorMessage))
                {
                    return(Ok(new RequestErrorCode(false, errorCode, errorMessage)));
                }
                #endregion
                // lay connectionTypeName
                string cnTypeName = "S";
                var    svPack     =
                    MemoryInfo.GetListServicePackByField(req.IdServicePack.ToString(),
                                                         ServicePack.ServicePackFields.Id);
                if (svPack != null && svPack.Count > 0 && svPack[0] != null)
                {
                    var cnType = MemoryInfo.GetListConnectionTypeByField(svPack[0].IdConnectionType.ToString(),
                                                                         ConnectionType.ConnectionTypeFields.Id);
                    if (cnType != null && cnType.Count > 0 && cnType[0] != null)
                    {
                        cnTypeName = cnType[0].Name;
                    }
                }

                var    startStringId        = cnTypeName[0];
                var    lstSvFormWithStartId = MemoryInfo.GetListServicesFormByStartId(startStringId.ToString());
                int    idSvFormWithStartId  = lstSvFormWithStartId.Count + 1;
                string serviceFormId        = startStringId.ToString();
                int    countZeroNumber      = 10 - idSvFormWithStartId.ToString().Length;
                for (int i = 0; i < countZeroNumber; i++)
                {
                    serviceFormId += "0";
                }
                serviceFormId    += idSvFormWithStartId.ToString();
                req.ServiceFormId = serviceFormId;
                #region Tạo key
                var oldKey = Memory.Memory.GetMaxKey(req.GetName());
                int newKey = oldKey + 1;
                // set key
                req.Id = newKey;
                #endregion

                #region Process
                req.CreatedAt = DateTime.Now;
                req.CreatedBy = 0;
                req.IsDeleted = 0;
                UpdateEntitySql updateEntitySql = new UpdateEntitySql();
                var             lstCommand      = new List <EntityCommand>();
                lstCommand.Add(new EntityCommand {
                    BaseEntity = new Entity.Entity(req), EntityAction = EntityAction.Insert
                });
                bool isOkDone = updateEntitySql.UpdateDefault(lstCommand);
                if (!isOkDone)
                {
                    return(Ok(new RequestErrorCode(false, errorCode, errorMessage)));
                }
                #endregion
                // update memory
                MemorySet.UpdateAndInsertEntity(req);
                var result = new RequestErrorCode(true);
                result.DataResult = req;
                return(Ok(result));
            }
            catch (Exception ex)
            {
                Logger.Write(ex.ToString());
            }
            return(BadRequest("Unknow"));
        }