public IActionResult Create(Employee employee) { db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(Employee employee) { db.Employees.Attach(employee); db.Employees.Update(employee); db.SaveChanges(); return(RedirectToAction("Index")); }
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 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 Edit([FromBody] Models.Account account) { try { if (account == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } using (var context = new EMContext()) { context.Configuration.LazyLoadingEnabled = false; Models.Account updatedAccount = new Models.Account(); DB.Models.Account existingAccount = context.Accounts.Where(x => x.AccountID == account.AccountID).FirstOrDefault(); if (existingAccount != null) { existingAccount.AccountTypeID = account.AccountTypeID; existingAccount.AccountName = account.AccountName; existingAccount.CreatedDate = DateTime.Now; context.SaveChanges(); updatedAccount = context.Accounts.Where(x => x.AccountID == account.AccountID).Select(x => new Models.Account() { AccountID = x.AccountID, AccountTypeID = x.AccountTypeID, AccountName = x.AccountName, CreatedDate = x.CreatedDate }).FirstOrDefault(); } APIResponse <Models.Account> apiResponse = new APIResponse <Models.Account>() { StatusCode = (int)(updatedAccount == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), StatusRemark = "", Content = (updatedAccount ?? null), }; return(Request.CreateResponse((updatedAccount == 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, "")); } } }
public void InitAccountTypeTable() { using (var context = new EMContext()) { foreach (KeyValuePair <Guid, string> entry in AccountTypes) { AccountType foundAccountType = context.AccountTypes.Where(x => x.AccountTypeName == entry.Value).FirstOrDefault(); if (foundAccountType == null) { context.AccountTypes.Add(new AccountType() { AccountTypeID = entry.Key, AccountTypeName = entry.Value }); context.SaveChanges(); } } } }
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 accountID = Guid.Parse(id); DB.Models.Transaction foundTransaction = context.Transactions.Where(x => x.FromAccountID == accountID || x.ToAccountID == accountID).FirstOrDefault(); if (foundTransaction == null) { DB.Models.Account foundAccount = context.Accounts.Where(x => x.AccountID == accountID).FirstOrDefault(); if (foundAccount != null) { context.Accounts.Remove(foundAccount); context.SaveChanges(); result = true; } else { result = false; } } else { result = false; error = "Error, found that this account 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, "")); } } }
public HttpResponseMessage Update([FromBody] Models.Transaction transaction) { try { if (transaction == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } using (var context = new EMContext()) { context.Configuration.LazyLoadingEnabled = false; DB.Models.Transaction existingTransaction = context.Transactions.Include(x => x.FromTransaction).Include(x => x.TransactionType).Where(x => x.TransactionID == transaction.TransactionID).FirstOrDefault(); DB.Models.TransactionType transactionType = context.TransactionTypes.Where(x => x.TransactionTypeID == transaction.TransactionTypeID).FirstOrDefault(); if (existingTransaction != null && transactionType != null) { DB.Models.Transaction foundTransaction = new DB.Models.Transaction(); Models.Transaction createdTransaction = new Models.Transaction(); switch (transactionType.TransactionTypeName) { case "Income": existingTransaction.TransactionTypeID = transaction.TransactionTypeID; existingTransaction.FromAccountID = transaction.FromAccountID; existingTransaction.CategoryID = transaction.CategoryID; existingTransaction.Operator = true; existingTransaction.TransactionDate = transaction.TransactionDate; existingTransaction.TransactionTime = transaction.TransactionTime; existingTransaction.Amount = transaction.Amount; existingTransaction.Note = transaction.Note; existingTransaction.CreatedDate = DateTime.Now; if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty) { existingTransaction.SubCategoryID = transaction.SubCategoryID; } else { existingTransaction.SubCategoryID = null; } context.SaveChanges(); foundTransaction = context.Transactions.Where(x => x.TransactionID == existingTransaction.TransactionID).FirstOrDefault(); context.Entry(foundTransaction).Reference(x => x.FromAccount).Load(); context.Entry(foundTransaction).Reference(x => x.Category).Load(); if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty) { context.Entry(foundTransaction).Reference(x => x.SubCategory).Load(); } createdTransaction = new Models.Transaction { TransactionID = foundTransaction.TransactionID, TransactionTypeID = foundTransaction.TransactionTypeID, FromAccountID = foundTransaction.FromAccountID, CategoryID = foundTransaction.CategoryID, SubCategoryID = foundTransaction.SubCategoryID, Operator = foundTransaction.Operator, TransactionDate = foundTransaction.TransactionDate, TransactionTime = foundTransaction.TransactionTime, Amount = foundTransaction.Amount, Note = foundTransaction.Note, CreatedDate = foundTransaction.CreatedDate }; break; case "Expense": existingTransaction.TransactionTypeID = transaction.TransactionTypeID; existingTransaction.FromAccountID = transaction.FromAccountID; existingTransaction.CategoryID = transaction.CategoryID; existingTransaction.Operator = false; existingTransaction.TransactionDate = transaction.TransactionDate; existingTransaction.TransactionTime = transaction.TransactionTime; existingTransaction.Amount = transaction.Amount; existingTransaction.Note = transaction.Note; existingTransaction.CreatedDate = DateTime.Now; if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty) { existingTransaction.SubCategoryID = transaction.SubCategoryID; } else { existingTransaction.SubCategoryID = null; } context.SaveChanges(); foundTransaction = context.Transactions.Where(x => x.TransactionID == existingTransaction.TransactionID).FirstOrDefault(); context.Entry(foundTransaction).Reference(x => x.FromAccount).Load(); context.Entry(foundTransaction).Reference(x => x.Category).Load(); if (transaction.SubCategoryID != null && transaction.SubCategoryID != Guid.Empty) { context.Entry(foundTransaction).Reference(x => x.SubCategory).Load(); } createdTransaction = new Models.Transaction { TransactionID = foundTransaction.TransactionID, TransactionTypeID = foundTransaction.TransactionTypeID, FromAccountID = foundTransaction.FromAccountID, CategoryID = foundTransaction.CategoryID, SubCategoryID = foundTransaction.SubCategoryID, Operator = foundTransaction.Operator, TransactionDate = foundTransaction.TransactionDate, TransactionTime = foundTransaction.TransactionTime, Amount = foundTransaction.Amount, Note = foundTransaction.Note, CreatedDate = foundTransaction.CreatedDate }; break; case "Transfer": existingTransaction.TransactionTypeID = transaction.TransactionTypeID; existingTransaction.FromAccountID = transaction.FromAccountID; existingTransaction.ToAccountID = transaction.ToAccountID; existingTransaction.Operator = false; existingTransaction.TransactionDate = transaction.TransactionDate; existingTransaction.TransactionTime = transaction.TransactionTime; existingTransaction.Amount = transaction.Amount; existingTransaction.Note = transaction.Note; existingTransaction.CreatedDate = DateTime.Now; context.SaveChanges(); DB.Models.Transaction existingToTransaction = context.Transactions.Where(x => x.FromTransactionID == existingTransaction.TransactionID).FirstOrDefault(); if (existingToTransaction != null) { existingToTransaction.TransactionTypeID = transaction.TransactionTypeID; existingToTransaction.FromAccountID = transaction.ToAccountID ?? Guid.Empty; existingToTransaction.ToAccountID = transaction.FromAccountID; existingToTransaction.Operator = true; existingToTransaction.TransactionDate = transaction.TransactionDate; existingToTransaction.TransactionTime = transaction.TransactionTime; existingToTransaction.Amount = transaction.Amount; existingToTransaction.Note = transaction.Note; existingToTransaction.CreatedDate = DateTime.Now; context.SaveChanges(); } foundTransaction = context.Transactions.Where(x => x.TransactionID == existingTransaction.TransactionID).FirstOrDefault(); context.Entry(foundTransaction).Reference(x => x.FromAccount).Load(); context.Entry(foundTransaction).Reference(x => x.ToAccount).Load(); createdTransaction = new Models.Transaction { TransactionID = foundTransaction.TransactionID, TransactionTypeID = foundTransaction.TransactionTypeID, FromAccountID = foundTransaction.FromAccountID, ToAccountID = foundTransaction.ToAccountID, Operator = foundTransaction.Operator, TransactionDate = foundTransaction.TransactionDate, TransactionTime = foundTransaction.TransactionTime, Amount = foundTransaction.Amount, Note = foundTransaction.Note, CreatedDate = foundTransaction.CreatedDate }; break; default: return(Request.CreateResponse(HttpStatusCode.BadRequest, "")); } APIResponse <Models.Transaction> apiResponse = new APIResponse <Models.Transaction>() { StatusCode = (int)(createdTransaction == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), StatusRemark = "", Content = (createdTransaction ?? null), }; return(Request.CreateResponse((createdTransaction == null ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK), apiResponse)); } return(Request.CreateResponse(HttpStatusCode.ExpectationFailed, "")); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); if (HttpContext.Current.IsDebuggingEnabled) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex)); } else { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "")); } } }