Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import com.finboostplus.projection.UserExpenseDivisionProjection;

public record UserExpenseDivisionDTO(Long expenseId, String title, String description, Long groupId, String groupName,
Status status, BigDecimal total, Instant createdAt, List<UserExpenseDivisionProjection> memberList) {
Status status, BigDecimal total, Long categoryId, String categoryName, Instant createdAt, List<UserExpenseDivisionProjection> memberList) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.finboostplus.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -20,7 +18,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.finboostplus.DTO.CategoryDTO;
import com.finboostplus.DTO.ExpenseCreateDTO;
import com.finboostplus.DTO.ExpenseUpdateDTO;
import com.finboostplus.DTO.UserExpenseDivisionDTO;
Expand Down Expand Up @@ -50,11 +47,6 @@ public ResponseEntity<String> createNewExpense(@Valid @RequestBody ExpenseCreate
: new ResponseEntity<String>("", HttpStatus.BAD_REQUEST);
}

@GetMapping("/categories")
public ResponseEntity<List<CategoryDTO>> getCategory(){
return ResponseEntity.ok(categoryService.findAllCategories());
}

@GetMapping("{expenseId}")
public ResponseEntity<Object> getExpenseDetails(@PathVariable Long groupId,
@PathVariable Long expenseId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.finboostplus.DTO.CategoryDTO;
import com.finboostplus.DTO.GroupCreateDTO;
import com.finboostplus.DTO.GroupDetailsDTO;
import com.finboostplus.DTO.GroupMemberDTO;
Expand All @@ -30,6 +31,7 @@
import com.finboostplus.model.Group;
import com.finboostplus.projection.GroupProjection;
import com.finboostplus.repository.UserRepository;
import com.finboostplus.service.CategoryService;
import com.finboostplus.service.ExpenseService;
import com.finboostplus.service.GroupMemberService;
import com.finboostplus.service.GroupService;
Expand All @@ -50,12 +52,20 @@ public class GroupController {
@Autowired
GroupMemberService groupMemberService;

@Autowired
CategoryService categoryService;

@Autowired
ExpenseService expenseService;

@Autowired
UserRepository userRepository;

@GetMapping("/expenses/categories")
public ResponseEntity<List<CategoryDTO>> getCategory() {
return ResponseEntity.ok(categoryService.findAllCategories());
}

@GetMapping
public ResponseEntity<Page<GroupProjection>> listUserGroupsPaged(
@RequestParam(name = "page", defaultValue = "0") int page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public interface GroupExpenseProjection {

BigDecimal getPartialValue();

Long getCategoryId();

String getCategoryName();

Long getGroupId();

String getGroupName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,46 +78,52 @@ public interface ExpenseRepository extends JpaRepository<Expense, Long> {

@Query(nativeQuery = true, countQuery = "Select count(*) from user_expense_divisions", value = """
SELECT
E.ID AS EXPENSE_ID,
E.TITLE AS TITLE,
G.ID AS GROUP_ID,
G.NAME AS GROUP_NAME,
UED.PARTIAL_VALUE AS PARTIAL_VALUE,
UED.STATUS AS STATUS,
E.DEADLINE_DATE AS DEADLINE_DATE,
G.ICON
E.ID AS EXPENSE_ID,
E.TITLE AS TITLE,
G.ID AS GROUP_ID,
G.NAME AS GROUP_NAME,
UED.PARTIAL_VALUE AS PARTIAL_VALUE,
C.ID AS CATEGORY_ID,
C.Name AS CATEGORY_NAME,
UED.STATUS AS STATUS,
E.DEADLINE_DATE AS DEADLINE_DATE,
G.ICON
FROM
EXPENSES E
INNER JOIN USER_EXPENSE_DIVISIONS UED ON UED.EXPENSE_ID = E.ID
INNER JOIN GROUPS G ON G.ID = E.GROUP_ID
EXPENSES E
INNER JOIN USER_EXPENSE_DIVISIONS UED ON UED.EXPENSE_ID = E.ID
INNER JOIN GROUPS G ON G.ID = E.GROUP_ID
INNER JOIN CATEGORIES C ON C.ID = E.CATEGORY_ID
WHERE
G.ID = :groupId
AND UED.USER_ID = :memberId
G.ID = :groupId
AND UED.USER_ID = :memberId
ORDER BY
CASE
WHEN E.DEADLINE_DATE < NOW() THEN 1
WHEN DATE(E.DEADLINE_DATE) = DATE(NOW()) THEN 2
WHEN E.DEADLINE_DATE > NOW() THEN 3
ELSE 4
WHEN E.DEADLINE_DATE < NOW() THEN 1
WHEN DATE(E.DEADLINE_DATE) = DATE(NOW()) THEN 2
WHEN E.DEADLINE_DATE > NOW() THEN 3
ELSE 4
END,
E.DEADLINE_DATE ASC
""")
Page<GroupMemberExpenseProjection> getAllGroupExpenses(Long memberId, Long groupId, Pageable pageable);

@Query(nativeQuery = true, countQuery = "Select count(*) from user_expense_divisions", value = """
SELECT
E.ID AS EXPENSE_ID,
E.TITLE AS TITLE,
G.ID AS GROUP_ID,
G.NAME AS GROUP_NAME,
UED.PARTIAL_VALUE AS PARTIAL_VALUE,
UED.STATUS AS STATUS,
E.DEADLINE_DATE AS DEADLINE_DATE,
G.ICON
E.ID AS EXPENSE_ID,
E.TITLE AS TITLE,
G.ID AS GROUP_ID,
G.NAME AS GROUP_NAME,
UED.PARTIAL_VALUE AS PARTIAL_VALUE,
C.ID AS CATEGORY_ID,
C.Name AS CATEGORY_NAME,
UED.STATUS AS STATUS,
E.DEADLINE_DATE AS DEADLINE_DATE,
G.ICON
FROM
EXPENSES E
INNER JOIN USER_EXPENSE_DIVISIONS UED ON UED.EXPENSE_ID = E.ID
INNER JOIN GROUPS G ON G.ID = E.GROUP_ID
EXPENSES E
INNER JOIN USER_EXPENSE_DIVISIONS UED ON UED.EXPENSE_ID = E.ID
INNER JOIN GROUPS G ON G.ID = E.GROUP_ID
INNER JOIN CATEGORIES C ON C.ID = E.CATEGORY_ID
WHERE
G.ID = :groupId
AND UED.USER_ID = :memberId
Expand All @@ -141,13 +147,16 @@ Page<GroupMemberExpenseProjection> getAllGroupExpensesFiltered(Long memberId, Lo
G.ID AS GROUP_ID,
G.NAME AS GROUP_NAME,
COALESCE(UED_USER.PARTIAL_VALUE, 0) AS PARTIAL_VALUE,
C.ID AS CATEGORY_ID,
C.Name AS CATEGORY_NAME,
COALESCE(E.VALUE, 0) AS TOTAL,
COALESCE(E.VALUE, 0) - COALESCE(UED_PAID.TOTAL_PAID, 0) AS REMAINING_VALUE,
E.DEADLINE_DATE AS DEADLINE_DATE,
G.ICON,
UED_USER.STATUS AS STATUS
FROM EXPENSES E
INNER JOIN GROUPS G ON G.ID = E.GROUP_ID
INNER JOIN CATEGORIES C ON C.ID = E.CATEGORY_ID

LEFT JOIN (
SELECT EXPENSE_ID, PARTIAL_VALUE, STATUS, USER_ID
Expand Down Expand Up @@ -183,13 +192,16 @@ Page<GroupAuthorityExpenseProjection> getAllGroupExpensesOfAllMembers(Long userI
G.ID AS GROUP_ID,
G.NAME AS GROUP_NAME,
COALESCE(UED_USER.PARTIAL_VALUE, 0) AS PARTIAL_VALUE,
C.ID AS CATEGORY_ID,
C.Name AS CATEGORY_NAME,
COALESCE(E.VALUE, 0) AS TOTAL,
COALESCE(E.VALUE, 0) - COALESCE(UED_PAID.TOTAL_PAID, 0) AS REMAINING_VALUE,
E.DEADLINE_DATE AS DEADLINE_DATE,
G.ICON,
UED_USER.STATUS AS STATUS
FROM EXPENSES E
INNER JOIN GROUPS G ON G.ID = E.GROUP_ID
INNER JOIN CATEGORIES C ON C.ID = E.CATEGORY_ID

LEFT JOIN (
SELECT EXPENSE_ID, PARTIAL_VALUE, STATUS, USER_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,17 @@ public UserExpenseDivisionDTO getExpenseInfoDetails(Long groupId, Long expenseId
.doesUserHasAnyAuthority(user.getId(), groupId, AUTHLEVELS);
if (!hasAuthority) {
throw new ForbiddenResourceException(
"Usuário não tem permissão para visualizar detelhes da despesa");
"Acesso negado");
}
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new GroupNotFoundException("Grupo não encontrado"));
Expense expense = expenseRepository.findById(expenseId).orElseThrow(
() -> new ForbiddenResourceException("Despesa nao encontrada"));
Category category = expense.getCategory();
List<UserExpenseDivisionProjection> memberList = getExpenseDivisionDetails(group.getId(), expenseId);
return new UserExpenseDivisionDTO(expense.getId(), expense.getTitle(),
expense.getDescription(), groupId, group.getName(), expense.getStatus(),
expense.getValue(), expense.getCreatedAt(), memberList);
expense.getValue(), category.getId(), category.getName(), expense.getCreatedAt(), memberList);
}

private List<UserExpenseDivisionProjection> getExpenseDivisionDetails(Long groupId, Long expenseId) {
Expand Down
Loading