public async Task <IActionResult> PutExpense(long id, ExpenseForUpdateDto expenseForUpdateDto) { try { var expense = await _repository.DetailAsync(id); if (expense == null) { _logger.LogInfo($"{nameof(PutExpense)}({id}) : return NotFound from database"); return(NotFound()); } expense = _mapper.Map(expenseForUpdateDto, expense); await _repository.UpdateAsync(expense); _logger.LogInfo($"{nameof(PutExpense)}({id}) : return NoContent from database"); return(NoContent()); } catch (Exception ex) { _logger.LogError($"{nameof(PutExpense)}({id}) : something went wrong with {JsonSerializer.Serialize(expenseForUpdateDto)}" + $"{Environment.NewLine}\t{ex.Message}"); throw; } }
public async Task UpdateTest() { var expenseForUpdate = new ExpenseForUpdateDto { TransactionDate = DateTime.Today.AddDays(1), Type = ExpenseType.Food, Amount = 42.3, Currency = "BRL", Recipient = "Eve" }; var content = new StringContent(JsonConvert.SerializeObject(expenseForUpdate), Encoding.UTF8, "application/json"); var response = await _client.PutAsync("/api/v1/expenses/2", content); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); var updatedResponse = await _client.GetAsync("/api/v1/expenses/2"); Assert.Equal(HttpStatusCode.OK, updatedResponse.StatusCode); var updatedContent = await updatedResponse.Content.ReadAsStringAsync(); var expense = JsonConvert.DeserializeObject <ExpenseDto>(updatedContent); Assert.Equal(DateTime.Today.AddDays(1), expense.TransactionDate); Assert.Equal(ExpenseType.Food, expense.Type); Assert.Equal(42.3, expense.Amount); Assert.Equal("BRL", expense.Currency); Assert.Equal("Eve", expense.Recipient); }
public async Task UpdateExpenseRecord(ExpenseForUpdateDto expenseForUpdateDto, IMapper mapper) { var expenseFromDb = await Get(expenseForUpdateDto.Id); var bankAccountFromDb = expenseFromDb.BankAccount; bankAccountFromDb.Ballance = bankAccountFromDb.Ballance + expenseFromDb.Ammount - expenseForUpdateDto.Ammount; expenseFromDb.NewBallance = expenseFromDb.NewBallance + expenseFromDb.Ammount - expenseForUpdateDto.Ammount; mapper.Map(expenseForUpdateDto, expenseFromDb); }
public async Task <IActionResult> UpdateExpense(int expenseId, ExpenseForUpdateDto expenseForUpdateDto) { var command = new UpdateExpenseCommand(expenseId, expenseForUpdateDto); var result = await Mediator.Send(command); if (result) { return(NoContent()); } throw new Exception($"Update Expense {expenseId} failed on save."); }
public async Task <IHttpActionResult> UpdateExpenseRecord(long userId, ExpenseForUpdateDto expenseForUpdateDto) { if (!IsUserAuthorized(userId)) { return(Unauthorized()); } await _unitOfWork.Expenses.UpdateExpenseRecord(expenseForUpdateDto, _mapper); try { await _unitOfWork.Complete(); return(Ok("Updated expense record with id: " + expenseForUpdateDto.Id)); } catch (Exception) { return(BadRequest("An error happened while updateing expense record: " + expenseForUpdateDto.Id)); } }
public async Task <IActionResult> UpdateExpense(ExpenseForUpdateDto expenseForUpdateDto) { var expense = await _repository.Get(expenseForUpdateDto.Id); if (expense == null) { return(NotFound()); } var AR = await _authorizationService.AuthorizeAsync(HttpContext.User, expense, "Permission"); if (!AR.Succeeded) { return(Forbid()); } _mapper.Map(expenseForUpdateDto, expense); _repository.Update(expense); await _repository.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult> UpdateExpense([FromBody] ExpenseForUpdateDto expenseForUpdateDto) { var expenseFromRepo = await _repo .GetExpense(expenseForUpdateDto.TallyId, expenseForUpdateDto.ExpenseId); // var earningToReturn = _mapper.Map(earningForUpdateDto, earningFromRepo); if (expenseForUpdateDto.Amount != expenseFromRepo.Amount || expenseForUpdateDto.Description != expenseFromRepo.Description) { var expenseToReturn = _mapper.Map(expenseForUpdateDto, expenseFromRepo); if (await _repo.SaveAll()) { return(Ok()); } } else { ModelState.AddModelError("sameValue", "amount or description have the same value"); return(BadRequest(ModelState)); } throw new Exception("failed on updating the expense"); }
public UpdateExpenseCommand(int expenseId, ExpenseForUpdateDto expenseForUpdateDto) { ExpenseForUpdateDto = expenseForUpdateDto; ExpenseId = expenseId; }