Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Okane.Application/ExpenseResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class ExpenseResponse
public int Amount { get; set; }
public required string Category { get; set; }
public string? Description { get; set; }
public string CreatedAt { get; set; }
}
33 changes: 30 additions & 3 deletions Okane.Application/ExpenseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public ExpenseService(IExpensesRepository expensesRepository) =>

public ExpenseResponse RegisterExpense(CreateExpenseRequest request)
{

var expense = new Expense
{
{


Amount = request.Amount,
Description = request.Description,
Category = request.Category
Category = request.Category,

};

_expensesRepository.Add(expense);
Expand Down Expand Up @@ -52,6 +56,29 @@ private static ExpenseResponse CreateExpenseResponse(Expense expense) =>
Id = expense.Id,
Category = expense.Category,
Description = expense.Description,
Amount = expense.Amount
Amount = expense.Amount,
CreatedAt = expense.CreatedAt
};


public ExpenseResponse updateExpense(int id, CreateExpenseRequest expenseRequest)
{

var tempExpense = this._expensesRepository.ById(id);

tempExpense.Amount = expenseRequest.Amount;
tempExpense.Category =expenseRequest.Category;
tempExpense.Description = expenseRequest.Description;

return new ExpenseResponse()
{
Id = tempExpense.Id,
Category = tempExpense.Category,
Description = tempExpense.Description,
Amount = tempExpense.Amount,
CreatedAt = tempExpense.CreatedAt

};

}
}
2 changes: 2 additions & 0 deletions Okane.Application/IExpenseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface IExpenseService
ExpenseResponse? ById(int id);
IEnumerable<ExpenseResponse> Search(string? category = null);
bool Delete(int id);
ExpenseResponse updateExpense(int id, CreateExpenseRequest expenseRequest);

}
6 changes: 6 additions & 0 deletions Okane.Application/InMemoryExpensesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ private InMemoryExpensesRepository(IList<Expense> expenses) =>

public void Add(Expense expense)
{
DateTime now = DateTime.Now;

expense.Id = _nextId++;
Console.WriteLine($"Date That will be saved: {now.ToString("yyyy-MM-dd HH:mm:ss")}");

expense.CreatedAt = now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine($"Actual Date that was saved: {expense.CreatedAt}");
_expenses.Add(expense);
}

Expand Down
2 changes: 2 additions & 0 deletions Okane.Domain/Expense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ namespace Okane.Domain;
public class Expense
{
public int Id { get; set; }

public int Amount { get; set; }
public required string Category { get; set; }
public string? Description { get; set; }
public string CreatedAt { get; set; }
}
23 changes: 23 additions & 0 deletions Okane.WebApi/Controllers/ExpensesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,27 @@ public ActionResult<ExpenseResponse> Get(int id)
[HttpDelete("{id}")]
public bool Delete(int id) =>
_expensesService.Delete(id);


//PUT /expenses/:id
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ExpenseResponse))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<ExpenseResponse> Put(int id, CreateExpenseRequest expenseRequest)
{
var response = _expensesService.ById(id);
if (response == null)
{
return NotFound();

}

return Ok(_expensesService.updateExpense(id, expenseRequest));


}




}