Skip to content

Commit b5e24e7

Browse files
authored
Merge pull request #16 from ArielMAJ/release/0.3.0
Release/0.3.0
2 parents 5a5b72f + 5607e37 commit b5e24e7

9 files changed

Lines changed: 374 additions & 21 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212
<groupId>tech.artadevs</groupId>
1313
<artifactId>Finances</artifactId>
14-
<version>0.2.0-SNAPSHOT</version>
14+
<version>0.3.0-SNAPSHOT</version>
1515
<name>Finances</name>
1616
<description>A finance manager backend in Java - Springboot.</description>
1717
<url />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tech.artadevs.finances.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.validation.annotation.Validated;
8+
import org.springframework.web.bind.annotation.DeleteMapping;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.ResponseStatus;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
18+
import tech.artadevs.finances.dtos.FinancialTransactionRequestDto;
19+
import tech.artadevs.finances.dtos.FinancialTransactionResponseDto;
20+
import tech.artadevs.finances.services.FinancialTransactionService;
21+
22+
@Validated
23+
@RestController
24+
@RequestMapping("/user/me/transactions")
25+
public class FinancialTransactionController {
26+
27+
@Autowired
28+
private FinancialTransactionService financialTransactionService;
29+
30+
@PostMapping
31+
@SecurityRequirement(name = "bearerAuth")
32+
public FinancialTransactionResponseDto createTransaction(
33+
@RequestBody FinancialTransactionRequestDto financialTransactionRequest) {
34+
return financialTransactionService.create(financialTransactionRequest);
35+
}
36+
37+
@GetMapping
38+
@SecurityRequirement(name = "bearerAuth")
39+
public List<FinancialTransactionResponseDto> listAllTransactions() {
40+
return financialTransactionService.listAllForCurrentUser();
41+
}
42+
43+
@GetMapping("/total")
44+
@SecurityRequirement(name = "bearerAuth")
45+
public Double calculateTotalTransactionsValue() {
46+
return financialTransactionService.calculateTotalTransactionValueForCurrentUser();
47+
}
48+
49+
@ResponseStatus(HttpStatus.NO_CONTENT)
50+
@DeleteMapping("/{id}")
51+
@SecurityRequirement(name = "bearerAuth")
52+
public void deleteOwnItemById(@PathVariable Long id) {
53+
financialTransactionService.deleteOwnItemById(id);
54+
}
55+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tech.artadevs.finances.dtos;
2+
3+
import org.hibernate.validator.constraints.NotEmpty;
4+
5+
import jakarta.validation.constraints.Size;
6+
import lombok.Data;
7+
8+
@Data
9+
public class FinancialTransactionRequestDto {
10+
11+
private Double value;
12+
13+
@SuppressWarnings("deprecation")
14+
@NotEmpty(message = "The description is required.")
15+
@Size(min = 2, max = 100, message = "The length of description must be between 2 and 100 characters.")
16+
private String description;
17+
18+
public FinancialTransactionRequestDto() {
19+
}
20+
21+
public Double getValue() {
22+
return value;
23+
}
24+
25+
public String getDescription() {
26+
return description;
27+
}
28+
29+
public FinancialTransactionRequestDto setValue(Double value) {
30+
this.value = value;
31+
return this;
32+
}
33+
34+
public FinancialTransactionRequestDto setDescription(String description) {
35+
this.description = description;
36+
return this;
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tech.artadevs.finances.dtos;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class FinancialTransactionResponseDto {
7+
8+
private Long id;
9+
private Double value;
10+
private String description;
11+
12+
public FinancialTransactionResponseDto() {
13+
}
14+
15+
public Long getId() {
16+
return id;
17+
}
18+
19+
public Double getValue() {
20+
return value;
21+
}
22+
23+
public String getDescription() {
24+
return description;
25+
}
26+
27+
public FinancialTransactionResponseDto setId(Long id) {
28+
this.id = id;
29+
return this;
30+
}
31+
32+
public FinancialTransactionResponseDto setValue(Double value) {
33+
this.value = value;
34+
return this;
35+
}
36+
37+
public FinancialTransactionResponseDto setDescription(String description) {
38+
this.description = description;
39+
return this;
40+
}
41+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package tech.artadevs.finances.models;
2+
3+
import java.util.Date;
4+
5+
import org.hibernate.annotations.CreationTimestamp;
6+
import org.hibernate.annotations.UpdateTimestamp;
7+
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.GeneratedValue;
11+
import jakarta.persistence.GenerationType;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.JoinColumn;
14+
import jakarta.persistence.ManyToOne;
15+
import jakarta.persistence.Table;
16+
import tech.artadevs.finances.dtos.FinancialTransactionResponseDto;
17+
18+
@Entity
19+
@Table(name = "financial_transactions")
20+
public class FinancialTransaction {
21+
22+
@Id
23+
@GeneratedValue(strategy = GenerationType.AUTO)
24+
@Column(nullable = false)
25+
private Long id;
26+
27+
@Column(nullable = false)
28+
private Double value;
29+
30+
@Column(nullable = false)
31+
private String description;
32+
33+
@Column(name = "deleted_at", nullable = true)
34+
private Date deletedAt = null;
35+
36+
@CreationTimestamp
37+
@Column(updatable = false, name = "created_at", nullable = false)
38+
private Date createdAt;
39+
40+
@UpdateTimestamp
41+
@Column(name = "updated_at", nullable = false)
42+
private Date updatedAt;
43+
44+
@ManyToOne(optional = false)
45+
@JoinColumn(name = "user_id", nullable = false)
46+
private User user;
47+
48+
public FinancialTransaction() {
49+
}
50+
51+
public FinancialTransaction(Double value, String description, User user) {
52+
this.value = value;
53+
this.description = description;
54+
this.user = user;
55+
}
56+
57+
public Long getId() {
58+
return id;
59+
}
60+
61+
public FinancialTransaction setId(Long id) {
62+
this.id = id;
63+
return this;
64+
}
65+
66+
public Double getValue() {
67+
return value;
68+
}
69+
70+
public FinancialTransaction setValue(Double value) {
71+
this.value = value;
72+
return this;
73+
}
74+
75+
public String getDescription() {
76+
return description;
77+
}
78+
79+
public FinancialTransaction setDescription(String description) {
80+
this.description = description;
81+
return this;
82+
}
83+
84+
public User getUser() {
85+
return user;
86+
}
87+
88+
public FinancialTransaction setUser(User user) {
89+
this.user = user;
90+
return this;
91+
}
92+
93+
public Date getDeletedAt() {
94+
return deletedAt;
95+
}
96+
97+
public FinancialTransaction setDeletedAt(Date deletedAt) {
98+
this.deletedAt = deletedAt;
99+
return this;
100+
}
101+
102+
public Date getCreatedAt() {
103+
return createdAt;
104+
}
105+
106+
public FinancialTransaction setCreatedAt(Date createdAt) {
107+
this.createdAt = createdAt;
108+
return this;
109+
}
110+
111+
public Date getUpdatedAt() {
112+
return updatedAt;
113+
}
114+
115+
public FinancialTransaction setUpdatedAt(Date updatedAt) {
116+
this.updatedAt = updatedAt;
117+
return this;
118+
}
119+
120+
public FinancialTransactionResponseDto toFinancialTransactionResponseDto() {
121+
return new FinancialTransactionResponseDto()
122+
.setId(id)
123+
.setDescription(description)
124+
.setValue(value);
125+
}
126+
}

src/main/java/tech/artadevs/finances/models/User.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ public class User implements UserDetails {
5050
@Column(nullable = false)
5151
private boolean enabled = true;
5252

53-
54-
5553
public User setEnabled(boolean enabled) {
5654
this.enabled = enabled;
5755
return this;
5856
}
5957

60-
public boolean getEnabled(){
58+
public boolean getEnabled() {
6159
return this.enabled;
6260
}
6361

@@ -119,25 +117,25 @@ public String getUsername() {
119117
return email;
120118
}
121119

122-
@Override
123-
public boolean isAccountNonExpired() {
124-
return deletedAt == null;
125-
}
120+
@Override
121+
public boolean isAccountNonExpired() {
122+
return deletedAt == null;
123+
}
126124

127-
@Override
128-
public boolean isAccountNonLocked() {
129-
return deletedAt == null;
130-
}
125+
@Override
126+
public boolean isAccountNonLocked() {
127+
return deletedAt == null;
128+
}
131129

132-
@Override
133-
public boolean isCredentialsNonExpired() {
134-
return deletedAt == null;
135-
}
130+
@Override
131+
public boolean isCredentialsNonExpired() {
132+
return deletedAt == null;
133+
}
136134

137-
@Override
138-
public boolean isEnabled() {
139-
return enabled && deletedAt == null;
140-
}
135+
@Override
136+
public boolean isEnabled() {
137+
return enabled && deletedAt == null;
138+
}
141139

142140
public User setPassword(String password) {
143141
this.password = password;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
package tech.artadevs.finances.repositories;
3+
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
9+
10+
import tech.artadevs.finances.models.FinancialTransaction;
11+
import tech.artadevs.finances.models.User;
12+
13+
public interface FinancialTransactionRepository extends JpaRepository<FinancialTransaction, Long> {
14+
@Query("SELECT ft FROM FinancialTransaction ft WHERE ft.user = :user AND ft.deletedAt IS NULL")
15+
List<FinancialTransaction> findByUser(User user);
16+
17+
Optional<FinancialTransaction> findByIdAndUser(Long id, User user);
18+
19+
}

0 commit comments

Comments
 (0)