Skip to content

Commit 8d53454

Browse files
committed
chore: add the endpoint getAll
1 parent 7f631a6 commit 8d53454

File tree

7 files changed

+146
-1
lines changed

7 files changed

+146
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fun.trackmoney.pots.controller;
2+
3+
import fun.trackmoney.pots.service.PotsService;
4+
import fun.trackmoney.utils.response.ApiResponse;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("pots")
13+
public class PotsController {
14+
15+
private final PotsService potsService;
16+
17+
public PotsController(PotsService potsService) {
18+
this.potsService = potsService;
19+
}
20+
21+
@GetMapping("/{id}")
22+
public ResponseEntity<ApiResponse<?>> getPots(@PathVariable Integer id) {
23+
return ResponseEntity.ok(new ApiResponse<>(true, "Pots retrieved successfully", potsService.findAllPots(id), null));
24+
}
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package fun.trackmoney.pots.dtos;
2+
3+
public record PotsResponseDTO(String name,
4+
String description,
5+
Long targetAmount,
6+
Long currentAmount) {
7+
}

src/main/java/fun/trackmoney/pots/entity/PotsEntity.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package fun.trackmoney.pots.entity;
22

3+
import fun.trackmoney.account.entity.AccountEntity;
34
import jakarta.persistence.Entity;
45
import jakarta.persistence.GeneratedValue;
56
import jakarta.persistence.GenerationType;
67
import jakarta.persistence.Id;
8+
import jakarta.persistence.JoinColumn;
9+
import jakarta.persistence.ManyToOne;
710
import jakarta.persistence.Table;
811

912
@Entity
@@ -18,7 +21,72 @@ public class PotsEntity {
1821
private String description;
1922
private Long targetAmount;
2023
private Long currentAmount;
24+
@ManyToOne
25+
@JoinColumn(name = "account_id")
26+
private AccountEntity account;
2127

28+
public PotsEntity() {
29+
}
2230

31+
public PotsEntity(Long potId,
32+
String name,
33+
String description,
34+
Long targetAmount,
35+
Long currentAmount,
36+
AccountEntity accountId) {
37+
this.potId = potId;
38+
this.name = name;
39+
this.description = description;
40+
this.targetAmount = targetAmount;
41+
this.currentAmount = currentAmount;
42+
this.account = accountId;
43+
}
2344

45+
public Long getPotId() {
46+
return potId;
47+
}
48+
49+
public void setPotId(Long potId) {
50+
this.potId = potId;
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public void setName(String name) {
58+
this.name = name;
59+
}
60+
61+
public String getDescription() {
62+
return description;
63+
}
64+
65+
public void setDescription(String description) {
66+
this.description = description;
67+
}
68+
69+
public Long getTargetAmount() {
70+
return targetAmount;
71+
}
72+
73+
public void setTargetAmount(Long targetAmount) {
74+
this.targetAmount = targetAmount;
75+
}
76+
77+
public Long getCurrentAmount() {
78+
return currentAmount;
79+
}
80+
81+
public void setCurrentAmount(Long currentAmount) {
82+
this.currentAmount = currentAmount;
83+
}
84+
85+
public AccountEntity getAccount() {
86+
return account;
87+
}
88+
89+
public void setAccount(AccountEntity account) {
90+
this.account = account;
91+
}
2492
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fun.trackmoney.pots.mapper;
2+
3+
import fun.trackmoney.pots.dtos.PotsResponseDTO;
4+
import fun.trackmoney.pots.entity.PotsEntity;
5+
import org.mapstruct.Mapper;
6+
7+
import java.util.List;
8+
9+
@Mapper(componentModel = "spring")
10+
public interface PotsMapper {
11+
12+
List<PotsResponseDTO> listToResponse(List<PotsEntity> entityList);
13+
}

src/main/java/fun/trackmoney/pots/repository/PotsRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import fun.trackmoney.pots.entity.PotsEntity;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
8+
import java.util.List;
59

610
public interface PotsRepository extends JpaRepository<PotsEntity, Long> {
11+
@Query("SELECT p FROM PotsEntity p WHERE p.account.accountId = :accountId")
12+
List<PotsEntity> findAllPotsByAccountId(@Param("accountId") Integer accountId);
713
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fun.trackmoney.pots.service;
2+
3+
import fun.trackmoney.pots.dtos.PotsResponseDTO;
4+
import fun.trackmoney.pots.mapper.PotsMapper;
5+
import fun.trackmoney.pots.repository.PotsRepository;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
@Service
11+
public class PotsService {
12+
13+
private final PotsRepository potsRepository;
14+
private final PotsMapper potsMapper;
15+
16+
public PotsService(PotsRepository potsRepository, PotsMapper potsMapper) {
17+
this.potsRepository = potsRepository;
18+
this.potsMapper = potsMapper;
19+
}
20+
21+
public List<PotsResponseDTO> findAllPots(Integer accountId) {
22+
return potsMapper.listToResponse(potsRepository.findAllPotsByAccountId(accountId));
23+
}
24+
}

src/main/resources/db/migration/V4__create_pots_schema.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ CREATE TABLE tb_pots (
33
name VARCHAR(255),
44
description VARCHAR(255),
55
target_amount BIGINT,
6-
current_amount BIGINT
6+
current_amount BIGINT,
7+
account_id BIGINT,
8+
CONSTRAINT fk_pots_account FOREIGN KEY (account_id) REFERENCES tb_account(account_id)
79
);

0 commit comments

Comments
 (0)