예제 #1
0
        public string EditExpense(Expense expense)
        {
            try
            {
                var expenseToUpdate = _dbContext.Expenses.FirstOrDefault(e => e.Id == expense.Id);
                if (expenseToUpdate == null)
                {
                    _logger.LogInformation($"[EditExpense]: Expense '{expense.Id}' could not be found");

                    return("Could not find an expense with the specified id");
                }
                _dbContext.Entry(expenseToUpdate).State = EntityState.Detached;
                _dbContext.Entry(expense).State         = EntityState.Modified;
                _dbContext.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.LogError($"[EditExpense]: Could not edit the specified entry: {e.Message} - {e.InnerException}");

                return("Could not edit the expense");
            }
            _logger.LogInformation($"[EditExpense]: Expense '{expense.Id}' edited successfully");

            return(null);
        }
        public async Task <IActionResult> PutExpenseType(int id, ExpenseType expenseType)
        {
            if (id != expenseType.ExpenseTypeId)
            {
                return(BadRequest());
            }

            _context.Entry(expenseType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExpenseTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetExpenseType", new { id = expenseType.ExpenseTypeId }, expenseType));
        }
예제 #3
0
        public async Task <Expense> Update(int id, Expense expense)
        {
            _context.Entry(expense).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExpenseExists(id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }
            var result = await _context.Expenses.FindAsync(id);

            return(result);
        }
예제 #4
0
        public async Task <ExpenseType> Update(int id, ExpenseType expenseType)
        {
            _context.Entry(expenseType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExpenseTypeExists(id))
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }
            //check the return value
            Console.WriteLine(expenseType);

            return(expenseType);
        }