Add CategoryService and integrate category handling in ExpenseListController
All checks were successful
Build and Deploy Spring Boot Server / build (push) Successful in 10m8s
All checks were successful
Build and Deploy Spring Boot Server / build (push) Successful in 10m8s
- Introduced CategoryService to manage standard categories. - Updated ExpenseListController to set default categories when creating an expense list. - Modified ExpenseChangeRequest and ExpenseInput to include category field. - Enhanced DataInitializer to ensure standard categories are initialized.
This commit is contained in:
@@ -24,6 +24,8 @@ import de.zendric.app.xpensely_server.model.ExpenseChangeRequest;
|
||||
import de.zendric.app.xpensely_server.model.ExpenseInput;
|
||||
import de.zendric.app.xpensely_server.model.ExpenseList;
|
||||
import de.zendric.app.xpensely_server.model.InviteRequest;
|
||||
import de.zendric.app.xpensely_server.model.XpenselyStandardCategories;
|
||||
import de.zendric.app.xpensely_server.services.CategoryService;
|
||||
import de.zendric.app.xpensely_server.services.ExpenseListService;
|
||||
import de.zendric.app.xpensely_server.services.UserService;
|
||||
|
||||
@@ -33,11 +35,14 @@ class ExpenseListController {
|
||||
|
||||
private ExpenseListService expenseListService;
|
||||
private UserService userService;
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
public ExpenseListController(ExpenseListService expenseListService, UserService userService) {
|
||||
public ExpenseListController(ExpenseListService expenseListService, UserService userService,
|
||||
CategoryService categoryService) {
|
||||
this.expenseListService = expenseListService;
|
||||
this.userService = userService;
|
||||
this.categoryService = categoryService;
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@@ -105,6 +110,8 @@ class ExpenseListController {
|
||||
throw new IllegalArgumentException("Owner does not exist.");
|
||||
}
|
||||
expenseList.setOwner(existingOwner);
|
||||
XpenselyStandardCategories standardCategories = categoryService.getDefaultCategories();
|
||||
expenseList.setXpenselyStandardCategories(standardCategories);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Owner is required.");
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package de.zendric.app.xpensely_server.model.DTO;
|
||||
|
||||
public class ExpenseListDTO {
|
||||
|
||||
// TODO should combine the two categories two one;
|
||||
// TODO should combine the two categories to one;
|
||||
|
||||
// private List<CategoryDTO> availableCategories;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ public class ExpenseChangeRequest {
|
||||
private Double personalUseAmount;
|
||||
private Double otherPersonAmount;
|
||||
private LocalDate date;
|
||||
private String category;
|
||||
|
||||
public Expense convertToExpense(Long userId, ExpenseList expenseList) {
|
||||
AppUser appUser = new AppUser();
|
||||
@@ -33,6 +34,7 @@ public class ExpenseChangeRequest {
|
||||
expense.setId(id);
|
||||
expense.setOwner(appUser);
|
||||
expense.setTitle(title);
|
||||
expense.setCategory(category);
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public class ExpenseInput {
|
||||
private Double otherPersonAmount;
|
||||
|
||||
private LocalDate date;
|
||||
private String category;
|
||||
|
||||
private ExpenseList expenseList;
|
||||
|
||||
@@ -46,6 +47,7 @@ public class ExpenseInput {
|
||||
expense.setId(id);
|
||||
expense.setOwner(appUser);
|
||||
expense.setTitle(title);
|
||||
expense.setCategory(category);
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class XpenselyStandardCategories {
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
|
||||
@JoinColumn(name = "global_categories_id")
|
||||
private List<XpenselyStandardCategory> categories;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.zendric.app.xpensely_server.preparation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
@@ -18,18 +19,22 @@ public class DataInitializer implements CommandLineRunner {
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
if (!globalRepo.findById(1L).isPresent()) {
|
||||
XpenselyStandardCategories global = new XpenselyStandardCategories();
|
||||
global.setId(1L);
|
||||
Optional<XpenselyStandardCategories> optional = globalRepo.findById(1L);
|
||||
|
||||
List<XpenselyStandardCategory> categories = List.of(
|
||||
new XpenselyStandardCategory(null, "Food", "#FF5733"),
|
||||
new XpenselyStandardCategory(null, "Transportation", "#33C3FF"),
|
||||
new XpenselyStandardCategory(null, "Entertainment", "#33FF57"),
|
||||
new XpenselyStandardCategory(null, "Shopping", "#FF33A8"),
|
||||
new XpenselyStandardCategory(null, "Bills", "#33FFEA"));
|
||||
global.setCategories(categories);
|
||||
globalRepo.save(global);
|
||||
}
|
||||
XpenselyStandardCategories global = optional.orElseGet(() -> {
|
||||
XpenselyStandardCategories g = new XpenselyStandardCategories();
|
||||
g.setId(1L);
|
||||
return g;
|
||||
});
|
||||
|
||||
List<XpenselyStandardCategory> categories = List.of(
|
||||
new XpenselyStandardCategory(null, "Food", "#FF5733"),
|
||||
new XpenselyStandardCategory(null, "Transportation", "#33C3FF"),
|
||||
new XpenselyStandardCategory(null, "Entertainment", "#33FF57"),
|
||||
new XpenselyStandardCategory(null, "Shopping", "#FF33A8"),
|
||||
new XpenselyStandardCategory(null, "Household", "#FFC733"),
|
||||
new XpenselyStandardCategory(null, "Other", "#9D33FF"));
|
||||
global.setCategories(categories);
|
||||
globalRepo.save(global);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package de.zendric.app.xpensely_server.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import de.zendric.app.xpensely_server.model.XpenselyStandardCategories;
|
||||
import de.zendric.app.xpensely_server.repo.XpenselyStandardCategoriesRepository;
|
||||
|
||||
@Service
|
||||
public class CategoryService {
|
||||
|
||||
private final XpenselyStandardCategoriesRepository standardCategoriesRepo;
|
||||
|
||||
@Autowired
|
||||
public CategoryService(XpenselyStandardCategoriesRepository standardCategoriesRepo) {
|
||||
this.standardCategoriesRepo = standardCategoriesRepo;
|
||||
}
|
||||
|
||||
public XpenselyStandardCategories getDefaultCategories() {
|
||||
return standardCategoriesRepo.findById(1L)
|
||||
.orElseThrow(() -> new IllegalStateException("Standard categories not found"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -183,6 +183,7 @@ public class ExpenseListService {
|
||||
existingExpense.setOtherPersonAmount(updatedExpense.getOtherPersonAmount());
|
||||
existingExpense.setDate(updatedExpense.getDate());
|
||||
existingExpense.setOwner(updatedExpense.getOwner());
|
||||
existingExpense.setCategory(updatedExpense.getCategory());
|
||||
|
||||
return expenseRepository.save(existingExpense);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user