Exemplo n.º 1
0
        public async Task<IHttpActionResult> RegisterCommonMasters([FromUri]string name, CommonMasters model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var modelErrors = new List<string>();
                    foreach (var modelState in ModelState.Values)
                    {
                        foreach (var modelError in modelState.Errors)
                        {
                            modelErrors.Add(modelError.ErrorMessage);
                        }
                    }
                    return Content(HttpStatusCode.BadRequest, MessageResponse.Message(HttpStatusCode.BadRequest.ToString(), modelErrors[0].ToString()), new JsonMediaTypeFormatter());
                }

                CommonMasters addNewCommonMaster = new CommonMasters();
                addNewCommonMaster.Code = model.Code;
                addNewCommonMaster.Value = model.Value;
                addNewCommonMaster.Rank = model.Rank;

                // add scoreCard code
                string query = @"UPDATE " + _bucket.Name + " SET "+ name + " = ARRAY_APPEND("+ name + ", " + Newtonsoft.Json.JsonConvert.SerializeObject(addNewCommonMaster).ToString() + ") where meta().id='REF_EN_US'";
                var result = _bucket.Query<object>(query);
                return Content(HttpStatusCode.OK, MessageResponse.Message(HttpStatusCode.OK.ToString(), "Master data has been added sucessfully"), new JsonMediaTypeFormatter());
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.InternalServerError, MessageResponse.Message(HttpStatusCode.InternalServerError.ToString(), ex.StackTrace), new JsonMediaTypeFormatter());
            }
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> UpdateCommonMasters([FromUri]string name, CommonMasters model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var modelErrors = new List<string>();
                    foreach (var modelState in ModelState.Values)
                    {
                        foreach (var modelError in modelState.Errors)
                        {
                            modelErrors.Add(modelError.ErrorMessage);
                        }
                    }
                    return Content(HttpStatusCode.BadRequest, MessageResponse.Message(HttpStatusCode.BadRequest.ToString(), modelErrors[0].ToString()), new JsonMediaTypeFormatter());
                }

                string query = @"SELECT Contents." + name + " From " + _bucket.Name + " as CommonMasters where meta().id='REF_EN_US'";
                var CommonMasterDocument = _bucket.Query<object>(query).ToList();
                foreach (var item1 in CommonMasterDocument)
                {
                    var cMData = ((Newtonsoft.Json.Linq.JToken)item1).Root[""+ name +""];

                    foreach (var item in cMData)
                    {
                        if (item["Code"].ToString()==model.Code)
                        {
                            // edit Common Master
                            string updateQuery = @"UPDATE " + _bucket.Name + " SET value = '" + model.Value + "',rank = '" + model.Rank + "' where meta().id='REF_EN_US'";
                            var result = _bucket.Query<object>(updateQuery);
                            return Content(HttpStatusCode.OK, MessageResponse.Message(HttpStatusCode.OK.ToString(), name + " has been updated sucessfully"), new JsonMediaTypeFormatter());
                        }
                    }
                }
                return Content(HttpStatusCode.BadRequest, MessageResponse.Message(HttpStatusCode.BadRequest.ToString(), "code not found."), new JsonMediaTypeFormatter());
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.InternalServerError, MessageResponse.Message(HttpStatusCode.InternalServerError.ToString(), ex.StackTrace), new JsonMediaTypeFormatter());
            }
        }