Merge branch 'master' of https://github.com/lumijiez/ExpenseTrackerFAF
This commit is contained in:
@@ -61,14 +61,18 @@ public class ExpenseController {
|
||||
}
|
||||
|
||||
|
||||
// TODO: has to be checked on auto extracting Uuid
|
||||
@PatchMapping()
|
||||
public ResponseEntity<ExpenseDTO> updateExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
||||
// TODO: check if the expense belongs to the user
|
||||
@PatchMapping("/update/{id}")
|
||||
public ResponseEntity<Void> updateExpense(@PathVariable long id, @RequestBody ExpenseCreationDTO expenseDTO,
|
||||
BindingResult bindingResult) {
|
||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
||||
Expense expense = expenseService.getTransactionById(id);
|
||||
ExpenseCategory category = expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory());
|
||||
expense.setCategory(category);
|
||||
expense.setAmount(expenseDTO.getAmount());
|
||||
|
||||
if (!bindingResult.hasErrors()) {
|
||||
expenseService.createOrUpdate(expense);
|
||||
return ResponseEntity.ok(expenseMapper.toDto(expense));
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
@@ -98,4 +102,9 @@ public class ExpenseController {
|
||||
if (!categories.isEmpty()) return ResponseEntity.ok(categories);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public void deleteCategory(@PathVariable long id) {
|
||||
expenseService.deleteExpenseById(id);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,7 @@ package com.faf223.expensetrackerfaf.controller;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Income;
|
||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.model.*;
|
||||
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||
import com.faf223.expensetrackerfaf.service.UserService;
|
||||
@@ -60,13 +58,18 @@ public class IncomeController {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<IncomeDTO> updateIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeMapper.toIncome(incomeDTO);
|
||||
// TODO: check if the income belongs to the user, extract logic into service
|
||||
@PatchMapping("/update/{id}")
|
||||
public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeService.getTransactionById(id);
|
||||
IncomeCategory category = incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory());
|
||||
income.setCategory(category);
|
||||
income.setAmount(incomeDTO.getAmount());
|
||||
|
||||
if (!bindingResult.hasErrors()) {
|
||||
incomeService.createOrUpdate(income);
|
||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
@@ -96,4 +99,9 @@ public class IncomeController {
|
||||
if (!categories.isEmpty()) return ResponseEntity.ok(categories);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public void deleteIncome(@PathVariable long id) {
|
||||
incomeService.deleteIncomeById(id);
|
||||
}
|
||||
}
|
||||
@@ -40,4 +40,8 @@ public class ExpenseService implements ITransactionService {
|
||||
public Expense getTransactionById(long id) {
|
||||
return expenseRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public void deleteExpenseById(long id) {
|
||||
expenseRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -40,4 +40,8 @@ public class IncomeService implements ITransactionService {
|
||||
public Income getTransactionById(long id) {
|
||||
return incomeRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public void deleteIncomeById(long id) {
|
||||
incomeRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
"name": "expensetracker",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"g": "vite dev -- --open",
|
||||
"dev": "vite dev",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const expenseCategory = $expenseTypes.find(incomeType => incomeType.id === id);
|
||||
|
||||
console.log(amount);
|
||||
|
||||
if (expenseCategory) {
|
||||
const newIncome = {
|
||||
incomeId: 0,
|
||||
@@ -27,7 +25,7 @@
|
||||
},
|
||||
expenseCategory: expenseCategory,
|
||||
date: today,
|
||||
amount: amount
|
||||
amount: parseInt(amount)
|
||||
};
|
||||
|
||||
newData = $expenseData;
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
const selectedIncome = $incomeTypes.find(income => income.id === $selectedIncomeId);
|
||||
const data = {
|
||||
incomeCategory: selectedIncome.id,
|
||||
amount: amount,
|
||||
amount: parseInt(amount),
|
||||
};
|
||||
|
||||
addNewIncome(selectedIncome.id, amount);
|
||||
|
||||
Reference in New Issue
Block a user