feat: CategoryService, CategoryRepository

This commit is contained in:
Filippo Ferrari 2024-05-21 00:00:41 +02:00
parent d3808cce7d
commit 1b4a6d8419
4 changed files with 57 additions and 6 deletions

View file

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

View file

@ -0,0 +1,40 @@
package com.application.munera.services;
import com.application.munera.data.Category;
import com.application.munera.repositories.CategoryRepository;
import jakarta.persistence.Cache;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;
@Service
public class CategoryService {
private final CategoryRepository categoryRepository;
public CategoryService(final CategoryRepository categoryRepository){
this.categoryRepository = categoryRepository;
}
public Optional<Category> get(Long id) {
return categoryRepository.findById(id);
}
public List<Category> findAll() {
return categoryRepository.findAll();
}
public Category update(Category category) {
return categoryRepository.save(category);
}
public void delete(Category category) {
categoryRepository.delete(category);
}
public Page<Category> list(Pageable pageable){
return categoryRepository.findAll(pageable);
}
}

View file

@ -7,6 +7,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Service @Service
@ -22,6 +23,8 @@ public class ExpenseService {
return repository.findById(id); return repository.findById(id);
} }
public List<Expense> findAll() {return repository.findAll();}
public Expense update(Expense entity) { public Expense update(Expense entity) {
return repository.save(entity); return repository.save(entity);
} }

View file

@ -1,8 +1,7 @@
package com.application.munera.views.expenses; package com.application.munera.views.expenses;
import com.application.munera.data.Category;
import com.application.munera.data.Expense; import com.application.munera.data.Expense;
import com.application.munera.repositories.ExpenseRepository; import com.application.munera.services.ExpenseService;
import com.application.munera.views.MainLayout; import com.application.munera.views.MainLayout;
import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout;
@ -17,12 +16,12 @@ import java.util.List;
@PermitAll @PermitAll
public class ExpensesView extends VerticalLayout { public class ExpensesView extends VerticalLayout {
private final ExpenseRepository expenseRepository; private final ExpenseService expenseService;
private final Grid<Expense> grid; private final Grid<Expense> grid;
@Autowired @Autowired
public ExpensesView(ExpenseRepository expenseRepository) { public ExpensesView(ExpenseService expenseService) {
this.expenseRepository = expenseRepository; this.expenseService = expenseService;
this.grid = new Grid<>(Expense.class); this.grid = new Grid<>(Expense.class);
addClassName("expenses-view"); addClassName("expenses-view");
setSizeFull(); setSizeFull();
@ -40,7 +39,7 @@ public class ExpensesView extends VerticalLayout {
} }
private void updateList() { private void updateList() {
List<Expense> expenses = expenseRepository.findAll(); List<Expense> expenses = expenseService.findAll();
grid.setItems(expenses); grid.setItems(expenses);
} }
} }