feat: too much to write here tbh

This commit is contained in:
effe 2024-09-14 11:07:31 -04:00
parent 73d1811d04
commit 8d0823a206
11 changed files with 37 additions and 28 deletions

View file

@ -22,6 +22,6 @@ public class Category {
@Column(name = "Description") @Column(name = "Description")
private String description; private String description;
@Column(name = "userId", nullable = false, unique = true) @Column(name = "userId", nullable = false)
private Long userId; private Long userId;
} }

View file

@ -65,6 +65,6 @@ public class Expense {
@Column(name = "expenseType", nullable = false) @Column(name = "expenseType", nullable = false)
private ExpenseType expenseType; private ExpenseType expenseType;
@Column(name = "userId", nullable = false, unique = true) @Column(name = "userId", nullable = false)
private Long userId; private Long userId;
} }

View file

@ -18,42 +18,49 @@ public class CategoryInitializer {
// Create and save the Food category // Create and save the Food category
Category foodCategory = new Category(); Category foodCategory = new Category();
foodCategory.setName("Food"); foodCategory.setName("Food");
foodCategory.setUserId(1L);
foodCategory.setDescription("All expenses related to food"); foodCategory.setDescription("All expenses related to food");
categoryService.save(foodCategory); categoryService.save(foodCategory);
// Create and save the Travel category // Create and save the Travel category
Category travelCategory = new Category(); Category travelCategory = new Category();
travelCategory.setName("Travel"); travelCategory.setName("Travel");
travelCategory.setUserId(1L);
travelCategory.setDescription("Expenses related to traveling, including transport and accommodation"); travelCategory.setDescription("Expenses related to traveling, including transport and accommodation");
categoryService.save(travelCategory); categoryService.save(travelCategory);
// Create and save the Electronics category // Create and save the Electronics category
Category electronicsCategory = new Category(); Category electronicsCategory = new Category();
electronicsCategory.setName("Electronics"); electronicsCategory.setName("Electronics");
electronicsCategory.setUserId(1L);
electronicsCategory.setDescription("All expenses related to electronic devices and gadgets"); electronicsCategory.setDescription("All expenses related to electronic devices and gadgets");
categoryService.save(electronicsCategory); categoryService.save(electronicsCategory);
// Create and save the Events category // Create and save the Events category
Category eventsCategory = new Category(); Category eventsCategory = new Category();
eventsCategory.setName("Events"); eventsCategory.setName("Events");
eventsCategory.setUserId(1L);
eventsCategory.setDescription("Expenses related to attending or organizing events"); eventsCategory.setDescription("Expenses related to attending or organizing events");
categoryService.save(eventsCategory); categoryService.save(eventsCategory);
// Create and save the Clothing category // Create and save the Clothing category
Category clothingCategory = new Category(); Category clothingCategory = new Category();
clothingCategory.setName("Clothing"); clothingCategory.setName("Clothing");
clothingCategory.setUserId(1L);
clothingCategory.setDescription("Expenses related to clothes and accessories"); clothingCategory.setDescription("Expenses related to clothes and accessories");
categoryService.save(clothingCategory); categoryService.save(clothingCategory);
// Create and save the Bills category // Create and save the Bills category
Category billsCategory = new Category(); Category billsCategory = new Category();
billsCategory.setName("Bills"); billsCategory.setName("Bills");
billsCategory.setUserId(1L);
billsCategory.setDescription("Recurring expenses like utilities, internet, and other bills"); billsCategory.setDescription("Recurring expenses like utilities, internet, and other bills");
categoryService.save(billsCategory); categoryService.save(billsCategory);
// Create and save the Rent category // Create and save the Rent category
Category rentCategory = new Category(); Category rentCategory = new Category();
rentCategory.setName("Rent"); rentCategory.setName("Rent");
rentCategory.setUserId(1L);
rentCategory.setDescription("Expenses related to rental payments for housing or office space"); rentCategory.setDescription("Expenses related to rental payments for housing or office space");
categoryService.save(rentCategory); categoryService.save(rentCategory);
} }

View file

@ -4,5 +4,9 @@ import com.application.munera.data.Category;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
public interface CategoryRepository extends JpaRepository<Category, Long>, JpaSpecificationExecutor<Category> { public interface CategoryRepository extends JpaRepository<Category, Long>, JpaSpecificationExecutor<Category> {
List<Category> findByUserId(Long userId);
} }

View file

@ -22,8 +22,8 @@ public class CategoryService {
return categoryRepository.findById(id); return categoryRepository.findById(id);
} }
public List<Category> findAll() { public List<Category> findAllByUserId(Long userId) {
return categoryRepository.findAll(); return categoryRepository.findByUserId(userId);
} }
public void update(Category category) { public void update(Category category) {

View file

@ -89,7 +89,7 @@ public class PersonService {
* @return Person entity of the logged-in user, or null if not found. * @return Person entity of the logged-in user, or null if not found.
*/ */
public Person getLoggedInPerson() { 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)); return Objects.requireNonNull(personRepository.findByUserId(user.getId()).orElse(null));
} }

View file

@ -41,13 +41,12 @@ public class UserService {
* *
* @return User entity of the logged-in user, or null if not found. * @return User entity of the logged-in user, or null if not found.
*/ */
public Optional<User> getLoggedInUser() { public User getLoggedInUser() {
UserDetails userDetails = getLoggedInUserDetails(); final var userDetails = getLoggedInUserDetails();
if (userDetails != null) { if (userDetails == null) throw new IllegalStateException("User is not logged in.");
String username = userDetails.getUsername(); final var username = userDetails.getUsername();
return userRepository.findByUsername(username); return userRepository.findByUsername(username)
} .orElseThrow(() -> new IllegalStateException("User not found: " + username));
return null;
} }
@Transactional @Transactional

View file

@ -130,7 +130,7 @@ public class MainLayout extends AppLayout {
} }
private boolean isUserAdmin() { 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"); return user.getRoles().contains("ROLE_ADMIN");
} }

View file

@ -2,6 +2,7 @@ package com.application.munera.views.categories;
import com.application.munera.data.Category; import com.application.munera.data.Category;
import com.application.munera.services.CategoryService; import com.application.munera.services.CategoryService;
import com.application.munera.services.UserService;
import com.application.munera.views.MainLayout; import com.application.munera.views.MainLayout;
import com.vaadin.flow.component.UI; import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
@ -50,20 +51,18 @@ public class CategoriesView extends Div implements BeforeEnterObserver {
private Category category; private Category category;
private final CategoryService categoryService; private final CategoryService categoryService;
private final UserService userService;
private TextField name; private TextField name;
private TextArea description; public CategoriesView(CategoryService categoryService, UserService userService) {
public CategoriesView(CategoryService categoryService) {
this.categoryService = categoryService; this.categoryService = categoryService;
this.userService = userService;
addClassNames("expenses-view"); addClassNames("expenses-view");
// Create UI // Create UI
SplitLayout splitLayout = new SplitLayout(); SplitLayout splitLayout = new SplitLayout();
createGridLayout(splitLayout); createGridLayout(splitLayout);
createEditorLayout(splitLayout); createEditorLayout(splitLayout);
add(splitLayout); add(splitLayout);
// Configure Grid // Configure Grid
@ -71,9 +70,7 @@ public class CategoriesView extends Div implements BeforeEnterObserver {
grid.addColumn(Category::getDescription).setHeader("Description").setSortable(true); grid.addColumn(Category::getDescription).setHeader("Description").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(query -> categoryService.list( grid.setItems(this.categoryService.findAllByUserId(this.userService.getLoggedInUser().getId()));
PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))
.stream());
grid.addThemeVariants(GridVariant.LUMO_NO_BORDER); grid.addThemeVariants(GridVariant.LUMO_NO_BORDER);
// when a row is selected or deselected, populate form // 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) { private void createEditorLayout(SplitLayout splitLayout) {
TextArea description;
Div editorLayoutDiv = new Div(); Div editorLayoutDiv = new Div();
editorLayoutDiv.setClassName("editor-layout"); editorLayoutDiv.setClassName("editor-layout");

View file

@ -235,6 +235,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
} }
private void createEditorLayout(SplitLayout splitLayout) { private void createEditorLayout(SplitLayout splitLayout) {
final var userId = this.userService.getLoggedInUser().getId();
Div editorLayoutDiv = new Div(); Div editorLayoutDiv = new Div();
editorLayoutDiv.setClassName("editor-layout"); editorLayoutDiv.setClassName("editor-layout");
Div editorDiv = new Div(); Div editorDiv = new Div();
@ -246,7 +247,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
name = new TextField("Name"); name = new TextField("Name");
cost = new TextField("Cost"); cost = new TextField("Cost");
category = new ComboBox<>("Category"); category = new ComboBox<>("Category");
category.setItems(categoryService.findAll()); category.setItems(categoryService.findAllByUserId(userId));
category.setItemLabelGenerator(Category::getName); category.setItemLabelGenerator(Category::getName);
description = new TextArea("Description"); description = new TextArea("Description");
periodUnit = new ComboBox<>("Period Unit"); periodUnit = new ComboBox<>("Period Unit");

View file

@ -47,7 +47,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
createForm(); createForm();
loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); loggedInUser = userService.getLoggedInUser();
binder = new BeanValidationBinder<>(User.class); binder = new BeanValidationBinder<>(User.class);
// Bind fields. This is where you'd define e.g. validation rules // Bind fields. This is where you'd define e.g. validation rules
@ -109,11 +109,11 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
@Override @Override
public void beforeEnter(BeforeEnterEvent event) { public void beforeEnter(BeforeEnterEvent event) {
final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); final var getLoggedInUser = userService.getLoggedInUser();
firstName.setValue(loggedInUser.getFirstName()); firstName.setValue(getLoggedInUser.getFirstName());
lastName.setValue(loggedInUser.getLastName()); lastName.setValue(getLoggedInUser.getLastName());
password.setValue(loggedInUser.getPassword()); password.setValue(getLoggedInUser.getPassword());
email.setValue(loggedInUser.getEmail()); email.setValue(getLoggedInUser.getEmail());
monthlyIncome.setValue(""); monthlyIncome.setValue("");
} }
} }