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")
private String description;
@Column(name = "userId", nullable = false, unique = true)
@Column(name = "userId", nullable = false)
private Long userId;
}

View file

@ -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;
}

View file

@ -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);
}

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.JpaSpecificationExecutor;
import java.util.List;
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);
}
public List<Category> findAll() {
return categoryRepository.findAll();
public List<Category> findAllByUserId(Long userId) {
return categoryRepository.findByUserId(userId);
}
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.
*/
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));
}

View file

@ -41,13 +41,12 @@ public class UserService {
*
* @return User entity of the logged-in user, or null if not found.
*/
public Optional<User> 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

View file

@ -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");
}

View file

@ -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");

View file

@ -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");

View file

@ -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("");
}
}