diff --git a/src/main/java/com/application/munera/data/Category.java b/src/main/java/com/application/munera/data/Category.java index 6cbb74e..51bb6ba 100644 --- a/src/main/java/com/application/munera/data/Category.java +++ b/src/main/java/com/application/munera/data/Category.java @@ -22,6 +22,6 @@ public class Category { @Column(name = "Description") private String description; - @Column(name = "userId", nullable = false, unique = true) + @Column(name = "userId", nullable = false) private Long userId; } diff --git a/src/main/java/com/application/munera/data/Expense.java b/src/main/java/com/application/munera/data/Expense.java index a424a9f..c2269d0 100644 --- a/src/main/java/com/application/munera/data/Expense.java +++ b/src/main/java/com/application/munera/data/Expense.java @@ -65,6 +65,6 @@ public class Expense { @Column(name = "expenseType", nullable = false) private ExpenseType expenseType; - @Column(name = "userId", nullable = false, unique = true) + @Column(name = "userId", nullable = false) private Long userId; } \ No newline at end of file diff --git a/src/main/java/com/application/munera/initializers/CategoryInitializer.java b/src/main/java/com/application/munera/initializers/CategoryInitializer.java index 24cd660..dd753db 100644 --- a/src/main/java/com/application/munera/initializers/CategoryInitializer.java +++ b/src/main/java/com/application/munera/initializers/CategoryInitializer.java @@ -18,42 +18,49 @@ public class CategoryInitializer { // Create and save the Food category Category foodCategory = new Category(); foodCategory.setName("Food"); + foodCategory.setUserId(1L); foodCategory.setDescription("All expenses related to food"); categoryService.save(foodCategory); // Create and save the Travel category Category travelCategory = new Category(); travelCategory.setName("Travel"); + travelCategory.setUserId(1L); travelCategory.setDescription("Expenses related to traveling, including transport and accommodation"); categoryService.save(travelCategory); // Create and save the Electronics category Category electronicsCategory = new Category(); electronicsCategory.setName("Electronics"); + electronicsCategory.setUserId(1L); electronicsCategory.setDescription("All expenses related to electronic devices and gadgets"); categoryService.save(electronicsCategory); // Create and save the Events category Category eventsCategory = new Category(); eventsCategory.setName("Events"); + eventsCategory.setUserId(1L); eventsCategory.setDescription("Expenses related to attending or organizing events"); categoryService.save(eventsCategory); // Create and save the Clothing category Category clothingCategory = new Category(); clothingCategory.setName("Clothing"); + clothingCategory.setUserId(1L); clothingCategory.setDescription("Expenses related to clothes and accessories"); categoryService.save(clothingCategory); // Create and save the Bills category Category billsCategory = new Category(); billsCategory.setName("Bills"); + billsCategory.setUserId(1L); billsCategory.setDescription("Recurring expenses like utilities, internet, and other bills"); categoryService.save(billsCategory); // Create and save the Rent category Category rentCategory = new Category(); rentCategory.setName("Rent"); + rentCategory.setUserId(1L); rentCategory.setDescription("Expenses related to rental payments for housing or office space"); categoryService.save(rentCategory); } diff --git a/src/main/java/com/application/munera/repositories/CategoryRepository.java b/src/main/java/com/application/munera/repositories/CategoryRepository.java index e458d2c..cad566b 100644 --- a/src/main/java/com/application/munera/repositories/CategoryRepository.java +++ b/src/main/java/com/application/munera/repositories/CategoryRepository.java @@ -4,5 +4,9 @@ import com.application.munera.data.Category; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import java.util.List; + public interface CategoryRepository extends JpaRepository, JpaSpecificationExecutor { + + List findByUserId(Long userId); } diff --git a/src/main/java/com/application/munera/services/CategoryService.java b/src/main/java/com/application/munera/services/CategoryService.java index 8599e6f..5f91c1c 100644 --- a/src/main/java/com/application/munera/services/CategoryService.java +++ b/src/main/java/com/application/munera/services/CategoryService.java @@ -22,8 +22,8 @@ public class CategoryService { return categoryRepository.findById(id); } - public List findAll() { - return categoryRepository.findAll(); + public List findAllByUserId(Long userId) { + return categoryRepository.findByUserId(userId); } public void update(Category category) { diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index e15b2dc..8235f36 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -89,7 +89,7 @@ public class PersonService { * @return Person entity of the logged-in user, or null if not found. */ public Person getLoggedInPerson() { - final var user = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); + final var user = userService.getLoggedInUser(); return Objects.requireNonNull(personRepository.findByUserId(user.getId()).orElse(null)); } diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index 459c4cc..26819ef 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -41,13 +41,12 @@ public class UserService { * * @return User entity of the logged-in user, or null if not found. */ - public Optional getLoggedInUser() { - UserDetails userDetails = getLoggedInUserDetails(); - if (userDetails != null) { - String username = userDetails.getUsername(); - return userRepository.findByUsername(username); - } - return null; + public User getLoggedInUser() { + final var userDetails = getLoggedInUserDetails(); + if (userDetails == null) throw new IllegalStateException("User is not logged in."); + final var username = userDetails.getUsername(); + return userRepository.findByUsername(username) + .orElseThrow(() -> new IllegalStateException("User not found: " + username)); } @Transactional diff --git a/src/main/java/com/application/munera/views/MainLayout.java b/src/main/java/com/application/munera/views/MainLayout.java index 194de97..8e125c8 100644 --- a/src/main/java/com/application/munera/views/MainLayout.java +++ b/src/main/java/com/application/munera/views/MainLayout.java @@ -130,7 +130,7 @@ public class MainLayout extends AppLayout { } private boolean isUserAdmin() { - final var user = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); + final var user = userService.getLoggedInUser(); return user.getRoles().contains("ROLE_ADMIN"); } diff --git a/src/main/java/com/application/munera/views/categories/CategoriesView.java b/src/main/java/com/application/munera/views/categories/CategoriesView.java index 36e72e9..da97bea 100644 --- a/src/main/java/com/application/munera/views/categories/CategoriesView.java +++ b/src/main/java/com/application/munera/views/categories/CategoriesView.java @@ -2,6 +2,7 @@ package com.application.munera.views.categories; import com.application.munera.data.Category; import com.application.munera.services.CategoryService; +import com.application.munera.services.UserService; import com.application.munera.views.MainLayout; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; @@ -50,20 +51,18 @@ public class CategoriesView extends Div implements BeforeEnterObserver { private Category category; private final CategoryService categoryService; + private final UserService userService; private TextField name; - private TextArea description; - - public CategoriesView(CategoryService categoryService) { + public CategoriesView(CategoryService categoryService, UserService userService) { this.categoryService = categoryService; + this.userService = userService; addClassNames("expenses-view"); // Create UI SplitLayout splitLayout = new SplitLayout(); - createGridLayout(splitLayout); createEditorLayout(splitLayout); - add(splitLayout); // Configure Grid @@ -71,9 +70,7 @@ public class CategoriesView extends Div implements BeforeEnterObserver { grid.addColumn(Category::getDescription).setHeader("Description").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); - grid.setItems(query -> categoryService.list( - PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query))) - .stream()); + grid.setItems(this.categoryService.findAllByUserId(this.userService.getLoggedInUser().getId())); grid.addThemeVariants(GridVariant.LUMO_NO_BORDER); // when a row is selected or deselected, populate form @@ -158,6 +155,7 @@ public class CategoriesView extends Div implements BeforeEnterObserver { } private void createEditorLayout(SplitLayout splitLayout) { + TextArea description; Div editorLayoutDiv = new Div(); editorLayoutDiv.setClassName("editor-layout"); diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 59b406f..dc5219a 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -235,6 +235,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { } private void createEditorLayout(SplitLayout splitLayout) { + final var userId = this.userService.getLoggedInUser().getId(); Div editorLayoutDiv = new Div(); editorLayoutDiv.setClassName("editor-layout"); Div editorDiv = new Div(); @@ -246,7 +247,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { name = new TextField("Name"); cost = new TextField("Cost"); category = new ComboBox<>("Category"); - category.setItems(categoryService.findAll()); + category.setItems(categoryService.findAllByUserId(userId)); category.setItemLabelGenerator(Category::getName); description = new TextArea("Description"); periodUnit = new ComboBox<>("Period Unit"); diff --git a/src/main/java/com/application/munera/views/settings/SettingsView.java b/src/main/java/com/application/munera/views/settings/SettingsView.java index 9beae0e..54921ff 100644 --- a/src/main/java/com/application/munera/views/settings/SettingsView.java +++ b/src/main/java/com/application/munera/views/settings/SettingsView.java @@ -47,7 +47,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver createForm(); - loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); + loggedInUser = userService.getLoggedInUser(); binder = new BeanValidationBinder<>(User.class); // Bind fields. This is where you'd define e.g. validation rules @@ -109,11 +109,11 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver @Override public void beforeEnter(BeforeEnterEvent event) { - final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); - firstName.setValue(loggedInUser.getFirstName()); - lastName.setValue(loggedInUser.getLastName()); - password.setValue(loggedInUser.getPassword()); - email.setValue(loggedInUser.getEmail()); + final var getLoggedInUser = userService.getLoggedInUser(); + firstName.setValue(getLoggedInUser.getFirstName()); + lastName.setValue(getLoggedInUser.getLastName()); + password.setValue(getLoggedInUser.getPassword()); + email.setValue(getLoggedInUser.getEmail()); monthlyIncome.setValue(""); } }