diff --git a/api/src/main/java/com/wbrawner/budgetserver/category/Category.java b/api/src/main/java/com/wbrawner/budgetserver/category/Category.java index c41a624..7489545 100644 --- a/api/src/main/java/com/wbrawner/budgetserver/category/Category.java +++ b/api/src/main/java/com/wbrawner/budgetserver/category/Category.java @@ -16,6 +16,8 @@ public class Category implements Comparable { @ManyToOne private Budget budget; private boolean expense; + @Column(nullable = false, columnDefinition = "boolean default false") + private boolean archived; public Category() { this(null, null, 0L, null, true); @@ -83,4 +85,12 @@ public class Category implements Comparable { public void setExpense(boolean expense) { this.expense = expense; } + + public boolean isArchived() { + return archived; + } + + public void setArchived(boolean archived) { + this.archived = archived; + } } diff --git a/api/src/main/java/com/wbrawner/budgetserver/category/CategoryController.java b/api/src/main/java/com/wbrawner/budgetserver/category/CategoryController.java index 9acc73c..ab0fb75 100644 --- a/api/src/main/java/com/wbrawner/budgetserver/category/CategoryController.java +++ b/api/src/main/java/com/wbrawner/budgetserver/category/CategoryController.java @@ -8,6 +8,9 @@ import com.wbrawner.budgetserver.transaction.TransactionRepository; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.Authorization; + +import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; @@ -44,6 +47,7 @@ class CategoryController { ResponseEntity> getCategories( @RequestParam(name = "budgetIds", required = false) List budgetIds, @RequestParam(name = "isExpense", required = false) Boolean isExpense, + @RequestParam(name = "includeArchived", required = false) Boolean includeArchived, @RequestParam(name = "count", required = false) Integer count, @RequestParam(name = "page", required = false) Integer page, @RequestParam(name = "false", required = false) String sortBy, @@ -69,13 +73,8 @@ class CategoryController { sortOrder != null ? sortOrder : Sort.Direction.ASC, sortBy != null ? sortBy : "title" ); - List categories; - if (isExpense == null) { - categories = categoryRepository.findAllByBudgetIn(budgets, pageRequest); - } else { - categories = categoryRepository.findAllByBudgetInAndExpense(budgets, isExpense, pageRequest); - } - + Boolean archived = includeArchived == null || includeArchived == false ? false : null; + List categories = categoryRepository.findAllByBudgetIn(budgets, isExpense, archived, pageRequest); return ResponseEntity.ok( categories.stream() .map(CategoryResponse::new) @@ -153,6 +152,9 @@ class CategoryController { if (request.getExpense() != null) { category.setExpense(request.getExpense()); } + if (request.getArchived() != null) { + category.setArchived(request.getArchived()); + } return ResponseEntity.ok(new CategoryResponse(categoryRepository.save(category))); } diff --git a/api/src/main/java/com/wbrawner/budgetserver/category/CategoryRepository.java b/api/src/main/java/com/wbrawner/budgetserver/category/CategoryRepository.java index bf93f56..fca986b 100644 --- a/api/src/main/java/com/wbrawner/budgetserver/category/CategoryRepository.java +++ b/api/src/main/java/com/wbrawner/budgetserver/category/CategoryRepository.java @@ -1,7 +1,9 @@ package com.wbrawner.budgetserver.category; import com.wbrawner.budgetserver.budget.Budget; + import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; import java.util.List; @@ -10,12 +12,11 @@ import java.util.Optional; public interface CategoryRepository extends PagingAndSortingRepository { List findAllByBudget(Budget budget, Pageable pageable); - List findAllByBudgetIn(List budgets, Pageable pageable); + @Query("SELECT c FROM Category c where c.budget IN (:budgets) AND (:expense IS NULL OR c.expense = :expense) AND (:archived IS NULL OR c.archived = :archived)") + List findAllByBudgetIn(List budgets, Boolean expense, Boolean archived, Pageable pageable); Optional findByBudgetInAndId(List budgets, Long id); - List findAllByBudgetInAndExpense(List budgets, Boolean isExpense, Pageable pageable); - Optional findByBudgetAndId(Budget budget, Long id); List findAllByBudgetInAndIdIn(List budgets, List ids, Pageable pageable); diff --git a/api/src/main/java/com/wbrawner/budgetserver/category/CategoryResponse.java b/api/src/main/java/com/wbrawner/budgetserver/category/CategoryResponse.java index 15839cd..07e1db2 100644 --- a/api/src/main/java/com/wbrawner/budgetserver/category/CategoryResponse.java +++ b/api/src/main/java/com/wbrawner/budgetserver/category/CategoryResponse.java @@ -9,6 +9,7 @@ public class CategoryResponse { private final long amount; private final long budgetId; private final boolean expense; + private final boolean archived; public CategoryResponse(Category category) { this( @@ -17,17 +18,19 @@ public class CategoryResponse { category.getDescription(), category.getAmount(), Objects.requireNonNull(category.getBudget()).getId(), - category.isExpense() + category.isExpense(), + category.isArchived() ); } - public CategoryResponse(long id, String title, String description, long amount, long budgetId, boolean expense) { + public CategoryResponse(long id, String title, String description, long amount, long budgetId, boolean expense, boolean archived) { this.id = id; this.title = title; this.description = description; this.amount = amount; this.budgetId = budgetId; this.expense = expense; + this.archived = archived; } public long getId() { @@ -53,4 +56,8 @@ public class CategoryResponse { public boolean isExpense() { return expense; } + + public boolean isArchived() { + return archived; + } } diff --git a/api/src/main/java/com/wbrawner/budgetserver/category/UpdateCategoryRequest.java b/api/src/main/java/com/wbrawner/budgetserver/category/UpdateCategoryRequest.java index 2fc9530..f69b72f 100644 --- a/api/src/main/java/com/wbrawner/budgetserver/category/UpdateCategoryRequest.java +++ b/api/src/main/java/com/wbrawner/budgetserver/category/UpdateCategoryRequest.java @@ -5,16 +5,18 @@ public class UpdateCategoryRequest { private final String description; private final Long amount; private final Boolean expense; + private final Boolean archived; public UpdateCategoryRequest() { - this(null, null, null, null); + this(null, null, null, null, null); } - public UpdateCategoryRequest(String title, String description, Long amount, Boolean expense) { + public UpdateCategoryRequest(String title, String description, Long amount, Boolean expense, Boolean archived) { this.title = title; this.description = description; this.amount = amount; this.expense = expense; + this.archived = archived; } public String getTitle() { @@ -32,4 +34,8 @@ public class UpdateCategoryRequest { public Boolean getExpense() { return expense; } + + public Boolean getArchived() { + return archived; + } } diff --git a/api/src/main/java/com/wbrawner/budgetserver/transaction/TransactionController.java b/api/src/main/java/com/wbrawner/budgetserver/transaction/TransactionController.java index 40110f3..fb3ec46 100644 --- a/api/src/main/java/com/wbrawner/budgetserver/transaction/TransactionController.java +++ b/api/src/main/java/com/wbrawner/budgetserver/transaction/TransactionController.java @@ -74,7 +74,7 @@ public class TransactionController { if (categoryIds != null && !categoryIds.isEmpty()) { categories = categoryRepository.findAllByBudgetInAndIdIn(budgets, categoryIds, null); } else { - categories = categoryRepository.findAllByBudgetIn(budgets, null); + categories = categoryRepository.findAllByBudgetIn(budgets, null, null, null); } var pageRequest = PageRequest.of( Math.min(0, page != null ? page - 1 : 0),