Ejemplo n.º 1
0
        private IHttpActionResult doUpdate(Customers model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Entry(model).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                ReturnFail respFail = new ReturnFail
                {
                    success    = false,
                    error_code = "500",
                    message    = ex.Message
                };

                return(this.Json(respFail));
            }

            ReturnSuccess respSucc = new ReturnSuccess
            {
                success = true
            };

            return(this.Json(respSucc));
        }
            public void Returns_success_on_child_failure()
            {
                var returnSuccess = new ReturnSuccess();

                returnSuccess.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build());

                Assert.AreEqual(TaskStatus.Success, returnSuccess.Update());
            }
            public void Returns_continue_on_child_continue()
            {
                var returnSuccess = new ReturnSuccess();

                returnSuccess.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build());

                Assert.AreEqual(TaskStatus.Continue, returnSuccess.Update());
            }
Ejemplo n.º 4
0
        private IHttpActionResult doDelete(string idList)
        {
            ReturnFail respFail = new ReturnFail
            {
                success    = false,
                error_code = "500",
                message    = "驗證失敗"
            };

            var dataSource = new List <Customers>();

            try
            {
                if (idList.Contains(","))
                {
                    List <string> foo = idList.Split(',').ToList();
                    dataSource = db.Customers.Where(x => foo.Contains(x.CustomerID)).ToList();
                }
                else
                {
                    dataSource = db.Customers.Where(x => x.CustomerID == idList).ToList();
                }

                if (dataSource.Count < 1)
                {
                    return(this.Json(respFail));
                }

                //與order 連動.有可能刪除失敗
                db.Customers.RemoveRange(dataSource);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                respFail.message = ex.Message;
                return(this.Json(respFail));
            }
            ReturnSuccess respSucc = new ReturnSuccess
            {
                success = true
            };

            return(this.Json(respSucc));
        }
Ejemplo n.º 5
0
        private IHttpActionResult doCreate(Customers model)
        {
            ReturnFail respFail = new ReturnFail
            {
                success    = false,
                error_code = "500",
                message    = "驗證失敗"
            };

            if (!ModelState.IsValid)
            {
                return(this.Json(respFail));
            }

            db.Customers.Add(model);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                if (CustomersExists(model.CustomerID))
                {
                    return(this.Json(respFail));
                }
                else
                {
                    return(this.Json(respFail));
                }
            }

            //  CreatedAtRoute("DefaultApi", new { id = model.CustomerID }, model);

            ReturnSuccess respSucc = new ReturnSuccess
            {
                success = true
            };

            return(this.Json(respSucc));
        }
Ejemplo n.º 6
0
        private IHttpActionResult queryData(SearchModel model, int rtnType = 0)
        {
            if (model == null)
            {
                model = new SearchModel()
                {
                    limit   = 10,
                    offset  = 0,
                    orderby = "asc",
                    sortby  = "CustomerID"
                };
            }
            //  query case
            if (!model.offset.HasValue)
            {
                model.offset = 0;
            }
            if (!model.limit.HasValue)
            {
                model.limit = 10;
            }
            if (string.IsNullOrEmpty(model.orderby))
            {
                model.orderby = "asc";
            }
            if (string.IsNullOrEmpty(model.sortby))
            {
                model.sortby = "CustomerID";
            }

            var data = db.Customers.AsQueryable().OrderBy(o => model.orderby).SortBy(model.sortby);

            //通用寫法
            if (!string.IsNullOrEmpty(model.name))
            {
                data = data.Where(x => x.CompanyName == model.name);
            }

            //查詢條件包在 JSON filters  中
            if (!string.IsNullOrEmpty(model.filters))
            {
                Customers qryCase = JsonConvert.DeserializeObject <Customers>(model.filters);

                // 多個欄位
                if (!string.IsNullOrEmpty(qryCase.CustomerID))
                {
                    data = data.Where(x => x.CustomerID == qryCase.CustomerID);
                }

                if (!string.IsNullOrEmpty(qryCase.CompanyName))
                {
                    data = data.Where(x => x.CompanyName == qryCase.CompanyName);
                }

                if (!string.IsNullOrEmpty(qryCase.City))
                {
                    data = data.Where(x => x.City == qryCase.City);
                }

                if (!string.IsNullOrEmpty(qryCase.Phone))
                {
                    data = data.Where(x => x.Phone == qryCase.Phone);
                }
            }

            var dataSource = data.Skip(model.offset.Value).Take(model.limit.Value).ToList();

            if (dataSource.Count > 0)
            {
                if (rtnType == 1)
                {
                    return(this.Json(dataSource));
                }
                else
                {
                    ReturnSuccess respSucc = new ReturnSuccess
                    {
                        success       = true,
                        totalRowCount = data.Count(),
                        totalPage     = (int)Math.Ceiling((double)data.Count() / model.limit.Value),
                        result        = dataSource
                    };
                    return(this.Json(respSucc));
                }
            }
            else
            {
                if (rtnType == 1)
                {
                    return(null);
                }

                ReturnFail respFail = new ReturnFail
                {
                    success    = false,
                    error_code = "404",
                    message    = "查無資料"
                };

                return(this.Json(respFail));
            }
        }