public HttpResponseMessage Edit([FromBody] Models.Category category) { try { if (category == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } using (var context = new EMContext()) { context.Configuration.LazyLoadingEnabled = false; Models.Category updatedCategory = new Models.Category(); DB.Models.Category existingCategory = context.Categories.Where(x => x.CategoryID == category.CategoryID).FirstOrDefault(); if (existingCategory != null) { existingCategory.TransactionTypeID = category.TransactionTypeID; existingCategory.CategoryName = category.CategoryName; existingCategory.Sequence = category.Sequence; existingCategory.CreatedDate = DateTime.Now; context.SaveChanges(); updatedCategory = context.Categories.Where(x => x.CategoryID == category.CategoryID).Select(x => new Models.Category() { CategoryID = x.CategoryID, TransactionTypeID = x.TransactionTypeID, CategoryName = x.CategoryName, Sequence = x.Sequence, CreatedDate = x.CreatedDate }).FirstOrDefault(); } APIResponse <Models.Category> apiResponse = new APIResponse <Models.Category>() { StatusCode = (int)(updatedCategory == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), StatusRemark = "", Content = (updatedCategory ?? null), }; return(Request.CreateResponse((updatedCategory == 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(string id) { try { if (id == null || (id != null && id.Trim().Length == 0)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } bool result = false; string error = string.Empty; using (var context = new EMContext()) { context.Configuration.LazyLoadingEnabled = false; Guid categoryID = Guid.Parse(id); DB.Models.Transaction foundTransaction = context.Transactions.Where(x => x.CategoryID == categoryID).FirstOrDefault(); if (foundTransaction == null) { DB.Models.Category foundCategory = context.Categories.Where(x => x.CategoryID == categoryID).FirstOrDefault(); if (foundCategory != null) { context.Categories.Remove(foundCategory); context.SaveChanges(); result = true; } else { result = false; } } else { result = false; error = "Error, found that this category in used in transaction."; } APIResponse <bool> apiResponse = new APIResponse <bool>() { StatusCode = (int)(!result ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), StatusRemark = error, 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, "")); } } }