public async Task <IActionResult> PutExpenseItem(int id, ExpenseItem expenseItem) { if (id != expenseItem.Id) { _logger.LogWarning($"The id of the item to update ({expenseItem.Id}) does not match the id in the request ({id})."); return(BadRequest()); } _context.Entry(expenseItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ExpenseItemExists(id)) { _logger.LogWarning($"Item with the id={id} does not exist."); return(NotFound()); } else { _logger.LogError($"An error has occured."); throw; } } return(NoContent()); }
public bool Delete<TEntity>(int Id) where TEntity : class { if (Id == 0 || Id < 0) { return false; } var entity = _context.Set<TEntity>().Find(Id); if (entity == null) return false; var dbSet = _context.Set<TEntity>(); if (_context.Entry(entity).State == EntityState.Modified) { dbSet.Attach(entity); return true; } else { return false; } }
public ActionResult Edit([Bind(Include = "IdAccount,Name")] Account account) { if (ModelState.IsValid) { db.Entry(account).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(account)); }
public RepositoryActionResult <ExpenseGroup> UpdateExpenseGroup(ExpenseGroup eg) { try { // you can only update when an expensegroup already exists for this id var existingEG = _ctx.ExpenseGroups.FirstOrDefault(exg => exg.Id == eg.Id); if (existingEG == null) { return(new RepositoryActionResult <ExpenseGroup>(eg, RepositoryActionStatus.NotFound)); } // change the original entity status to detached; otherwise, we get an error on attach // as the entity is already in the dbSet // set original entity state to detached _ctx.Entry(existingEG).State = EntityState.Detached; // attach & save _ctx.ExpenseGroups.Attach(eg); // set the updated entity state to modified, so it gets updated. _ctx.Entry(eg).State = EntityState.Modified; var result = _ctx.SaveChanges(); if (result > 0) { return(new RepositoryActionResult <ExpenseGroup>(eg, RepositoryActionStatus.Updated)); } else { return(new RepositoryActionResult <ExpenseGroup>(eg, RepositoryActionStatus.NothingModified, null)); } } catch (Exception ex) { return(new RepositoryActionResult <ExpenseGroup>(null, RepositoryActionStatus.Error, ex)); } }
public ActionResult Edit([Bind(Include = "IdTransaction,FKAccount,FkCategory,Name,date,notes")] Transaction transaction) { if (ModelState.IsValid) { db.Entry(transaction).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.FKAccount = new SelectList(db.Accounts, "IdAccount", "Name", transaction.FKAccount); ViewBag.FkCategory = new SelectList(db.Categories, "IdCategory", "Name", transaction.FkCategory); return(View(transaction)); }
public async Task <IActionResult> Update(Expense expense) { var expenseToUpdate = await ctx.FindAsync <Expense>(expense.ExpenseID); if (expenseToUpdate == null) { return(NotFound()); } ctx.Entry(expenseToUpdate).CurrentValues.SetValues(expense); ctx.SaveChanges(); return(Ok(expense)); }
public async Task Update(long id, Expense expense) { expense.Id = id; _context.Entry(expense).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception e) { if (!ExpenseExists(id)) { _logger.LogError($"Failed to update Expense with id {id} because it does not exist"); throw new ItemNotFoundException(ErrorMessages.ItemNotFoundError); } else { _logger.LogError($"Failed to update Expense with id {id}: {e.Message}"); throw; } } _logger.LogInformation($"Updated expense with id {expense.Id}"); }