public HttpResponseMessage Create([FromBody] Models.SubCategory subCategory) { try { if (subCategory == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } using (var context = new EMContext()) { context.Configuration.LazyLoadingEnabled = false; DB.Models.SubCategory newSubCategory = new DB.Models.SubCategory { SubCategoryID = subCategory.SubCategoryID, CategoryID = subCategory.CategoryID, SubCategoryName = subCategory.SubCategoryName, Sequence = subCategory.Sequence, CreatedDate = DateTime.Now }; context.SubCategories.Add(newSubCategory); context.SaveChanges(); Models.SubCategory createdSubCategory = context.SubCategories.Where(x => x.SubCategoryID == newSubCategory.SubCategoryID).Select(x => new Models.SubCategory() { SubCategoryID = x.SubCategoryID, CategoryID = x.CategoryID, SubCategoryName = x.SubCategoryName, Sequence = x.Sequence, CreatedDate = x.CreatedDate }).FirstOrDefault(); APIResponse <Models.SubCategory> apiResponse = new APIResponse <Models.SubCategory>() { StatusCode = (int)(createdSubCategory == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), StatusRemark = "", Content = (createdSubCategory ?? null), }; return(Request.CreateResponse((createdSubCategory == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), apiResponse)); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); if (HttpContext.Current.IsDebuggingEnabled) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex)); } else { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "")); } } }
public HttpResponseMessage Delete([FromBody] Models.SubCategory subCategory) { try { if (subCategory == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } bool result = false; using (var context = new EMContext()) { context.Configuration.LazyLoadingEnabled = false; DB.Models.SubCategory foundSubCategory = context.SubCategories.Where(x => x.SubCategoryID == subCategory.SubCategoryID).FirstOrDefault(); if (foundSubCategory != null) { context.SubCategories.Remove(foundSubCategory); context.SaveChanges(); result = true; } else { result = false; } APIResponse <bool> apiResponse = new APIResponse <bool>() { StatusCode = (int)(!result ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), StatusRemark = "", Content = result, }; return(Request.CreateResponse((!result ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), apiResponse)); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); if (HttpContext.Current.IsDebuggingEnabled) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex)); } else { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "")); } } }