Skip to content

Commit 409d949

Browse files
committed
feat: add goals crud
1 parent 703a7df commit 409d949

File tree

7 files changed

+183
-10
lines changed

7 files changed

+183
-10
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fun.trackmoney.goal.controller;
2+
3+
import fun.trackmoney.goal.dtos.CreateGoalsDTO;
4+
import fun.trackmoney.goal.dtos.GoalsResponseDTO;
5+
import fun.trackmoney.goal.service.GoalsService;
6+
import fun.trackmoney.utils.response.ApiResponse;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.HttpStatusCode;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.DeleteMapping;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.PutMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import java.util.List;
20+
21+
@RestController
22+
@RequestMapping("goals")
23+
public class GoalsController {
24+
25+
private final GoalsService goalsService;
26+
27+
public GoalsController(GoalsService goalsService) {
28+
this.goalsService = goalsService;
29+
}
30+
31+
@PostMapping
32+
public ResponseEntity<ApiResponse<GoalsResponseDTO>> create(@RequestBody CreateGoalsDTO dto){
33+
return ResponseEntity.status(HttpStatus.CREATED).body(
34+
new ApiResponse<>(true, "Goals Created!", goalsService.createGoals(dto), null));
35+
}
36+
37+
@GetMapping
38+
public ResponseEntity<ApiResponse<List<GoalsResponseDTO>>> findAll(){
39+
return ResponseEntity.status(HttpStatus.OK).body(
40+
new ApiResponse<>(true, "All goals", goalsService.findAllGoals(), null));
41+
}
42+
43+
@GetMapping("/{id}")
44+
public ResponseEntity<ApiResponse<GoalsResponseDTO>> findById(@PathVariable Integer id){
45+
return ResponseEntity.status(HttpStatus.OK).body(
46+
new ApiResponse<>(true, "All goals", goalsService.findById(id), null));
47+
}
48+
49+
@PutMapping("/{id}")
50+
public ResponseEntity<ApiResponse<GoalsResponseDTO>> update(@PathVariable Integer id,
51+
@RequestBody CreateGoalsDTO dto){
52+
return ResponseEntity.status(HttpStatus.OK).body(
53+
new ApiResponse<>(true, "All goals", goalsService.update(id, dto), null));
54+
}
55+
56+
57+
@DeleteMapping("/{id}")
58+
public ResponseEntity<ApiResponse<String>> delete(@PathVariable Integer id){
59+
goalsService.deleteById(id);
60+
return ResponseEntity.status(HttpStatus.OK).body(
61+
new ApiResponse<>(true, "All goals","Goals deleted", null));
62+
}
63+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fun.trackmoney.goal.dtos;
2+
3+
import java.math.BigDecimal;
4+
5+
public record CreateGoalsDTO(String goal,
6+
Integer accountId,
7+
BigDecimal targetAmount,
8+
BigDecimal currentAmount) {
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fun.trackmoney.goal.dtos;
2+
3+
import fun.trackmoney.account.dtos.AccountResponseDTO;
4+
5+
import java.math.BigDecimal;
6+
7+
public record GoalsResponseDTO(Integer goalsId,
8+
String goal,
9+
AccountResponseDTO account,
10+
BigDecimal targetAmount,
11+
BigDecimal currentAmount,
12+
Integer progress) {
13+
}

src/main/java/fun/trackmoney/goal/entity/GoalsEntity.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ public GoalsEntity() {
6868
* @param account The account associated with the goal.
6969
* @param targetAmount The target amount for the goal.
7070
* @param currentAmount The current amount towards the goal.
71-
* @param progress The progress percentage towards the goal.
7271
*/
7372
public GoalsEntity(Integer goalsId, String goal, AccountEntity account, BigDecimal targetAmount,
74-
BigDecimal currentAmount, Integer progress) {
73+
BigDecimal currentAmount) {
7574
this.goalsId = goalsId;
7675
this.goal = goal;
7776
this.account = account;
7877
this.targetAmount = targetAmount;
7978
this.currentAmount = currentAmount;
80-
this.progress = progress;
79+
updateProgress();
8180
}
8281

8382
/**
@@ -150,6 +149,7 @@ public BigDecimal getTargetAmount() {
150149
*/
151150
public void setTargetAmount(BigDecimal targetAmount) {
152151
this.targetAmount = targetAmount;
152+
updateProgress();
153153
}
154154

155155
/**
@@ -168,6 +168,7 @@ public BigDecimal getCurrentAmount() {
168168
*/
169169
public void setCurrentAmount(BigDecimal currentAmount) {
170170
this.currentAmount = currentAmount;
171+
updateProgress();
171172
}
172173

173174
/**
@@ -179,12 +180,14 @@ public Integer getProgress() {
179180
return progress;
180181
}
181182

182-
/**
183-
* Sets the progress percentage toward the goal.
184-
*
185-
* @param progress The progress percentage to set.
186-
*/
187-
public void setProgress(Integer progress) {
188-
this.progress = progress;
183+
public void updateProgress() {
184+
if (targetAmount != null && currentAmount != null && targetAmount.compareTo(BigDecimal.ZERO) > 0) {
185+
BigDecimal progressDecimal = currentAmount
186+
.divide(targetAmount, 2, BigDecimal.ROUND_HALF_UP)
187+
.multiply(BigDecimal.valueOf(100));
188+
this.progress = progressDecimal.intValue();
189+
} else {
190+
this.progress = 0;
191+
}
189192
}
190193
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fun.trackmoney.goal.mapper;
2+
3+
import fun.trackmoney.goal.dtos.CreateGoalsDTO;
4+
import fun.trackmoney.goal.dtos.GoalsResponseDTO;
5+
import fun.trackmoney.goal.entity.GoalsEntity;
6+
import org.mapstruct.Mapper;
7+
8+
import java.util.List;
9+
10+
@Mapper(componentModel = "spring")
11+
public interface GoalsMapper {
12+
GoalsEntity toEntity(CreateGoalsDTO dto);
13+
14+
GoalsResponseDTO toResponseDTO(GoalsEntity entity);
15+
16+
List<GoalsResponseDTO> toListResponseDTO(List<GoalsEntity> entity);
17+
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package fun.trackmoney.goal.repository;
2+
3+
import fun.trackmoney.goal.entity.GoalsEntity;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface GoalsRepository extends JpaRepository<GoalsEntity, Integer> {
7+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package fun.trackmoney.goal.service;
2+
3+
import fun.trackmoney.account.entity.AccountEntity;
4+
import fun.trackmoney.account.mapper.AccountMapper;
5+
import fun.trackmoney.account.service.AccountService;
6+
import fun.trackmoney.goal.dtos.CreateGoalsDTO;
7+
import fun.trackmoney.goal.dtos.GoalsResponseDTO;
8+
import fun.trackmoney.goal.entity.GoalsEntity;
9+
import fun.trackmoney.goal.mapper.GoalsMapper;
10+
import fun.trackmoney.goal.repository.GoalsRepository;
11+
import org.springframework.stereotype.Service;
12+
13+
import java.util.List;
14+
15+
@Service
16+
public class GoalsService {
17+
18+
private final GoalsRepository goalsRepository;
19+
private final AccountService accountService;
20+
private final AccountMapper accountMapper;
21+
private final GoalsMapper goalsMapper;
22+
23+
public GoalsService(GoalsRepository goalsRepository, AccountService accountService, AccountMapper accountMapper, GoalsMapper goalsMapper) {
24+
this.goalsRepository = goalsRepository;
25+
this.accountService = accountService;
26+
this.accountMapper = accountMapper;
27+
this.goalsMapper = goalsMapper;
28+
}
29+
30+
public GoalsResponseDTO createGoals(CreateGoalsDTO dto){
31+
GoalsEntity goals = goalsMapper.toEntity(dto);
32+
AccountEntity account = accountMapper.accountResponseToEntity(accountService.findAccountById(dto.accountId()));
33+
goals.setAccount(account);
34+
return goalsMapper.toResponseDTO(goalsRepository.save(goals));
35+
}
36+
37+
public List<GoalsResponseDTO> findAllGoals(){
38+
List<GoalsEntity> list = goalsRepository.findAll();
39+
return goalsMapper.toListResponseDTO(list);
40+
}
41+
42+
public GoalsResponseDTO findById(Integer id) {
43+
return goalsMapper.toResponseDTO(goalsRepository.findById(id)
44+
.orElseThrow(() -> new RuntimeException("Goals not found!")));
45+
}
46+
47+
public GoalsResponseDTO update(Integer id, CreateGoalsDTO dto) {
48+
GoalsEntity goals = goalsRepository.findById(id)
49+
.orElseThrow(() -> new RuntimeException("Goals not found!"));
50+
goals.setGoal(dto.goal());
51+
goals.setCurrentAmount(dto.currentAmount());
52+
goals.setTargetAmount(dto.targetAmount());
53+
return goalsMapper.toResponseDTO(goalsRepository.save(goals));
54+
}
55+
56+
public void deleteById(Integer id) {
57+
goalsRepository.deleteById(id);
58+
}
59+
}

0 commit comments

Comments
 (0)