fix: removed old events entity

This commit is contained in:
effe 2024-09-14 10:28:45 -04:00
parent 8187115de2
commit 52ec376e4d
8 changed files with 3 additions and 314 deletions

View file

@ -1,34 +0,0 @@
package com.application.munera.data;
import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import java.util.Set;
@Entity
@Getter
@Setter
@Table(name = "events")
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(max = 100)
@Column(name = "Name", nullable = false)
private String name;
@Size(max = 100)
@Column(name = "Description")
private String description;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "Event_participants",
joinColumns = @JoinColumn(name = "event_id"),
inverseJoinColumns = @JoinColumn(name = "participant_id"))
private Set<Person> participants;
}

View file

@ -52,10 +52,6 @@ public class Expense {
@JoinColumn(name = "DebtorId") @JoinColumn(name = "DebtorId")
private Person beneficiary; private Person beneficiary;
@ManyToOne
@JoinColumn(name = "EventId")
private Event event;
@Column(name = "Date", nullable = false, columnDefinition = "DATE DEFAULT CURRENT_DATE") @Column(name = "Date", nullable = false, columnDefinition = "DATE DEFAULT CURRENT_DATE")
private LocalDate date; private LocalDate date;

View file

@ -14,7 +14,7 @@ import java.util.Set;
@Getter @Getter
@Setter @Setter
@Table(name = "people") @Table(name = "people")
public class Person extends AbstractEntity { public class Person {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ -46,9 +46,6 @@ public class Person extends AbstractEntity {
@OneToMany(mappedBy = "beneficiary") @OneToMany(mappedBy = "beneficiary")
private Set<Expense> expensesAsBeneficiary; private Set<Expense> expensesAsBeneficiary;
@ManyToMany(mappedBy = "participants")
private Set<Event> events;
@Column(name = "Username", unique = true) @Column(name = "Username", unique = true)
private String username; // This field will link to the User entity private String username; // This field will link to the User entity

View file

@ -1,8 +0,0 @@
package com.application.munera.repositories;
import com.application.munera.data.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface EventRepository extends JpaRepository<Event, Long>, JpaSpecificationExecutor<Event> {
}

View file

@ -1,40 +0,0 @@
package com.application.munera.services;
import com.application.munera.data.Event;
import com.application.munera.repositories.EventRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class EventService {
private final EventRepository eventRepository;
public EventService(final EventRepository eventRepository){
this.eventRepository = eventRepository;
}
public Optional<Event> findById(Long id) {
return eventRepository.findById(id);
}
public List<Event> findAll() {
return eventRepository.findAll();
}
public void update(Event event) {
eventRepository.save(event);
}
public void delete(Event event) {
eventRepository.delete(event);
}
public Page<Event> list(Pageable pageable){
return eventRepository.findAll(pageable);
}
}

View file

@ -5,7 +5,6 @@ import com.application.munera.services.ExpenseService;
import com.application.munera.services.UserService; import com.application.munera.services.UserService;
import com.application.munera.views.categories.CategoriesView; import com.application.munera.views.categories.CategoriesView;
import com.application.munera.views.dashboard.DashboardView; import com.application.munera.views.dashboard.DashboardView;
import com.application.munera.views.events.EventsView;
import com.application.munera.views.expenses.ExpensesView; import com.application.munera.views.expenses.ExpensesView;
import com.application.munera.views.people.PeopleView; import com.application.munera.views.people.PeopleView;
import com.application.munera.views.settings.SettingsView; import com.application.munera.views.settings.SettingsView;
@ -120,7 +119,6 @@ public class MainLayout extends AppLayout {
nav.addItem(new SideNavItem("Expenses", ExpensesView.class, LineAwesomeIcon.MONEY_BILL_SOLID.create())); nav.addItem(new SideNavItem("Expenses", ExpensesView.class, LineAwesomeIcon.MONEY_BILL_SOLID.create()));
nav.addItem(new SideNavItem("Categories", CategoriesView.class, LineAwesomeIcon.FOLDER.create())); nav.addItem(new SideNavItem("Categories", CategoriesView.class, LineAwesomeIcon.FOLDER.create()));
nav.addItem(new SideNavItem("People", PeopleView.class, LineAwesomeIcon.USER.create())); nav.addItem(new SideNavItem("People", PeopleView.class, LineAwesomeIcon.USER.create()));
nav.addItem(new SideNavItem("Events", EventsView.class, LineAwesomeIcon.BANDCAMP.create()));
nav.addItem(new SideNavItem("Dashboard", DashboardView.class, LineAwesomeIcon.CHART_LINE_SOLID.create())); nav.addItem(new SideNavItem("Dashboard", DashboardView.class, LineAwesomeIcon.CHART_LINE_SOLID.create()));
// Check user roles before adding sensitive menu items // Check user roles before adding sensitive menu items

View file

@ -1,213 +0,0 @@
package com.application.munera.views.events;
import com.application.munera.data.Event;
import com.application.munera.data.Person;
import com.application.munera.services.EventService;
import com.application.munera.services.PersonService;
import com.application.munera.views.MainLayout;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.MultiSelectComboBox;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.splitlayout.SplitLayout;
import com.vaadin.flow.component.textfield.TextArea;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.ValidationException;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.data.VaadinSpringDataHelpers;
import jakarta.annotation.security.PermitAll;
import org.springframework.data.domain.PageRequest;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import java.util.Optional;
@PageTitle("Events")
@PermitAll
@Route(value = "events/:eventID?/:action?(edit)", layout = MainLayout.class)
@Uses(Icon.class)
public class EventsView extends Div implements BeforeEnterObserver {
private static final String EVENT_ID = "eventID";
private static final String EVENT_EDIT_ROUTE_TEMPLATE = "events/%s/edit";
private final Grid<Event> grid = new Grid<>(Event.class, false);
private final Button cancel = new Button("Cancel");
private final Button save = new Button("Save");
private final Button delete = new Button("Delete");
private final BeanValidationBinder<Event> binder;
private Event event;
private final EventService eventService;
private final PersonService personService;
private TextField name;
private TextArea description;
private MultiSelectComboBox<Person> participants;
public EventsView(EventService eventService, PersonService personService) {
this.eventService = eventService;
this.personService = personService;
addClassNames("expenses-view");
// Create UI
SplitLayout splitLayout = new SplitLayout();
createGridLayout(splitLayout);
createEditorLayout(splitLayout);
add(splitLayout);
// Configure Grid
grid.addColumn(Event::getName).setHeader("Name").setSortable(true);
grid.addColumn(Event::getDescription).setHeader("Description").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(query -> eventService.list(
PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))
.stream());
grid.addThemeVariants(GridVariant.LUMO_NO_BORDER);
// when a row is selected or deselected, populate form
grid.asSingleSelect().addValueChangeListener(event -> {
if (event.getValue() != null) UI.getCurrent().navigate(String.format(EVENT_EDIT_ROUTE_TEMPLATE, event.getValue().getId()));
else {
clearForm();
UI.getCurrent().navigate(EventsView.class);
}
});
// Configure Form
binder = new BeanValidationBinder<>(Event.class);
// Bind fields. This is where you'd define e.g. validation rules
binder.bindInstanceFields(this);
cancel.addClickListener(e -> {
clearForm();
refreshGrid();
});
save.addClickListener(e -> {
try {
if (this.event == null) {
this.event = new Event();
}
binder.writeBean(this.event);
eventService.update(this.event);
clearForm();
refreshGrid();
Notification.show("Data updated");
UI.getCurrent().navigate(EventsView.class);
} catch (ObjectOptimisticLockingFailureException exception) {
Notification n = Notification.show(
"Error updating the data. Somebody else has updated the record while you were making changes.");
n.setPosition(Notification.Position.MIDDLE);
n.addThemeVariants(NotificationVariant.LUMO_ERROR);
} catch (ValidationException validationException) {
Notification.show("Failed to update the event. Check again that all values are valid");
}
});
delete.addClickListener(e -> {
try {
if (this.event == null) throw new RuntimeException("Event is null!"); //TODO: create proper exception
eventService.delete(this.event);
clearForm();
refreshGrid();
Notification.show("Data deleted");
UI.getCurrent().navigate(EventsView.class);
} catch (ObjectOptimisticLockingFailureException exception) {
Notification n = Notification.show(
"Error updating the data. Somebody else has updated the record while you were making changes.");
n.setPosition(Notification.Position.MIDDLE);
n.addThemeVariants(NotificationVariant.LUMO_ERROR);
}
});
}
@Override
public void beforeEnter(BeforeEnterEvent event) {
Optional<Long> eventId = event.getRouteParameters().get(EVENT_ID).map(Long::parseLong);
if (eventId.isPresent()) {
Optional<Event> eventFromBackend = eventService.findById(eventId.get());
if (eventFromBackend.isPresent()) {
populateForm(eventFromBackend.get());
} else {
Notification.show(
String.format("The requested event was not found, ID = %s", eventId.get()), 3000,
Notification.Position.BOTTOM_START);
// when a row is selected but the data is no longer available,
// refresh grid
refreshGrid();
event.forwardTo(EventsView.class);
}
}
}
private void createEditorLayout(SplitLayout splitLayout) {
Div editorLayoutDiv = new Div();
editorLayoutDiv.setClassName("editor-layout");
Div editorDiv = new Div();
editorDiv.setClassName("editor");
editorLayoutDiv.add(editorDiv);
FormLayout formLayout = new FormLayout();
name = new TextField("Name");
description = new TextArea("Description");
participants = new MultiSelectComboBox<>("Participants");
participants.setItems(personService.findAll());
participants.setItemLabelGenerator(Person::getFirstName);
formLayout.add(name, description, participants);
editorDiv.add(formLayout);
createButtonLayout(editorLayoutDiv);
splitLayout.addToSecondary(editorLayoutDiv);
}
private void createButtonLayout(Div editorLayoutDiv) {
HorizontalLayout buttonLayout = new HorizontalLayout();
buttonLayout.setClassName("button-layout");
cancel.addThemeVariants(ButtonVariant.LUMO_TERTIARY);
save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
delete.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
buttonLayout.add(save, delete, cancel);
editorLayoutDiv.add(buttonLayout);
}
private void createGridLayout(SplitLayout splitLayout) {
Div wrapper = new Div();
wrapper.setClassName("grid-wrapper");
splitLayout.addToPrimary(wrapper);
wrapper.add(grid);
}
private void refreshGrid() {
grid.select(null);
grid.getDataProvider().refreshAll();
}
private void clearForm() {
populateForm(null);
}
private void populateForm(Event value) {
this.event = value;
binder.readBean(this.event);
}
}

View file

@ -60,7 +60,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
private final ExpenseService expenseService; private final ExpenseService expenseService;
private final CategoryService categoryService; private final CategoryService categoryService;
private final PersonService personService; private final PersonService personService;
private final EventService eventService;
private final ViewsService viewsService; private final ViewsService viewsService;
private final UserService userService; private final UserService userService;
private TextField name; private TextField name;
@ -74,14 +73,12 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
private DatePicker date; private DatePicker date;
private ComboBox<Person> payer; private ComboBox<Person> payer;
private ComboBox<Person> beneficiary; private ComboBox<Person> beneficiary;
private ComboBox<Event> event;
@Autowired @Autowired
public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService, ViewsService viewsService, UserService userService) { public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, ViewsService viewsService, UserService userService) {
this.expenseService = expenseService; this.expenseService = expenseService;
this.categoryService = categoryService; this.categoryService = categoryService;
this.personService = personService; this.personService = personService;
this.eventService = eventService;
this.viewsService = viewsService; this.viewsService = viewsService;
this.userService = userService; this.userService = userService;
addClassNames("expenses-view"); addClassNames("expenses-view");
@ -101,7 +98,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
grid.addColumn(Expense::getPeriodInterval).setHeader("Period Interval").setSortable(true); grid.addColumn(Expense::getPeriodInterval).setHeader("Period Interval").setSortable(true);
grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true); grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true);
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date"); grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
// grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true);
grid.addColumn(new ComponentRenderer<>(this.viewsService::createExpenseBadge)).setHeader("Status").setSortable(true); grid.addColumn(new ComponentRenderer<>(this.viewsService::createExpenseBadge)).setHeader("Status").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.getColumns().forEach(col -> col.setAutoWidth(true));
@ -259,9 +255,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
payer = new ComboBox<>("Payer"); payer = new ComboBox<>("Payer");
payer.setItems(people); payer.setItems(people);
payer.setItemLabelGenerator(person -> person.getFirstName() + " " + person.getLastName()); payer.setItemLabelGenerator(person -> person.getFirstName() + " " + person.getLastName());
event = new ComboBox<>("Event");
event.setItems(eventService.findAll());
event.setItemLabelGenerator(Event::getName);
beneficiary = new ComboBox<>("Beneficiary"); beneficiary = new ComboBox<>("Beneficiary");
beneficiary.setItems(people); beneficiary.setItems(people);
beneficiary.setItemLabelGenerator(person -> person.getFirstName() + " " + person.getLastName()); beneficiary.setItemLabelGenerator(person -> person.getFirstName() + " " + person.getLastName());
@ -273,7 +266,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
isPaid = new Checkbox("Paid"); isPaid = new Checkbox("Paid");
checkboxLayout.add(isPeriodic, isPaid); checkboxLayout.add(isPeriodic, isPaid);
formLayout.add(name, cost, category, description, checkboxLayout, periodUnit, periodInterval, date, payer, beneficiary, event); formLayout.add(name, cost, category, description, checkboxLayout, periodUnit, periodInterval, date, payer, beneficiary);
editorDiv.add(formLayout); editorDiv.add(formLayout);
createButtonLayout(editorLayoutDiv); createButtonLayout(editorLayoutDiv);