Skip to content

Commit 01390d2

Browse files
committed
fix: refactor directories
1 parent d6690d7 commit 01390d2

34 files changed

+259
-246
lines changed

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/domain/entities/ErrorLogEntity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public class ErrorLogEntity {
2121
private UUID userId;
2222

2323
@ManyToOne(cascade = CascadeType.ALL)
24-
@JoinColumn(name = "userId", referencedColumnName = "id", insertable = false, updatable = false)
24+
@JoinColumn(name = "userId", insertable = false, updatable = false)
2525
private UserEntity user;
2626

2727
@Column(nullable = false)
2828
@CreationTimestamp
2929
private LocalDateTime timestamp;
3030

3131
@Column(nullable = false)
32-
private Number code;
32+
private Integer code;
3333

3434
@Column(nullable = false)
3535
private String description;
@@ -40,7 +40,7 @@ public ErrorLogEntity(
4040
UUID id,
4141
@Nullable UUID userId,
4242
LocalDateTime timestamp,
43-
Number code,
43+
Integer code,
4444
String description
4545
) {
4646
this.id = id;

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/domain/entities/EventLogEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class EventLogEntity {
1919
private UUID userId;
2020

2121
@ManyToOne(cascade = CascadeType.ALL)
22-
@JoinColumn(name = "userId", referencedColumnName = "id", insertable = false, updatable = false)
22+
@JoinColumn(name = "userId", insertable = false, updatable = false)
2323
private UserEntity user;
2424

2525
@Column(nullable = false)

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/domain/services/BaseService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public abstract class BaseService {
1717

1818
protected void generateErrorLog(
1919
@Nullable UUID userId,
20-
Number code,
20+
Integer code,
2121
String description
2222
) {
2323
this.errorLogsRepository.save(

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/posts/controllers/CreatePostController.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ public class CreatePostController extends BaseController {
3939
})
4040
public ResponseEntity<Object> handle(
4141
HttpServletRequest req,
42-
@Valid CreatePostDTO post
42+
@RequestBody @Valid CreatePostDTO post
4343
) throws Exception {
44-
Object userIdAtt = req.getAttribute("userId");
45-
UUID userId = UUID.fromString(userIdAtt.toString());
44+
Object userIdAtt = req.getAttribute("userId");
45+
UUID userId = UUID.fromString(userIdAtt.toString());
4646

47-
PostDTO createdPost = this.createPostService.execute(post, userId);
47+
PostDTO createdPost = this.createPostService.execute(post, userId);
4848

49-
ResponseDTO response = this.responseMapper.toDTO(
50-
201,
51-
"Post created with success!",
52-
post
53-
);
49+
ResponseDTO response = this.responseMapper.toDTO(
50+
201,
51+
"Post created with success!",
52+
createdPost
53+
);
5454

55-
this.generateEventLog(
56-
userId,
57-
201,
58-
"Usuário com id: " + userId + " criou um post com sucesso!"
59-
);
55+
this.generateEventLog(
56+
userId,
57+
201,
58+
"Usuário com id: " + userId + " criou um post com sucesso!"
59+
);
6060

61-
return ResponseEntity.status(201).body(response);
61+
return ResponseEntity.status(201).body(response);
6262
}
6363
}

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/posts/dtos/PostDTO.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.joaov1ct0r.restful_api_users_java.modules.posts.dtos;
22

3-
import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity;
4-
53
import java.time.LocalDateTime;
64
import java.util.UUID;
75

@@ -11,7 +9,6 @@ public class PostDTO {
119
public LocalDateTime createdAt;
1210
public LocalDateTime updatedAt;
1311
public UUID userWhoCreatedId;
14-
public UUID userWhoUpdatedId;
1512

1613
public UUID getId() {
1714
return this.id;
@@ -53,11 +50,5 @@ public void setUserWhoCreatedId(UUID userWhoCreatedId) {
5350
this.userWhoCreatedId = userWhoCreatedId;
5451
}
5552

56-
public UUID getUserWhoUpdatedId() {
57-
return this.userWhoUpdatedId;
58-
}
5953

60-
public void setUserWhoUpdatedId(UUID userWhoUpdatedId) {
61-
this.userWhoUpdatedId = userWhoUpdatedId;
62-
}
6354
}
Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.joaov1ct0r.restful_api_users_java.modules.posts.entities;
22

3-
import com.joaov1ct0r.restful_api_users_java.modules.domain.entities.ErrorLogEntity;
4-
import com.joaov1ct0r.restful_api_users_java.modules.domain.entities.EventLogEntity;
53
import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity;
64
import jakarta.annotation.Nullable;
75
import jakarta.persistence.*;
86
import jakarta.validation.constraints.NotBlank;
97
import org.hibernate.annotations.CreationTimestamp;
108
import org.hibernate.annotations.UpdateTimestamp;
119
import java.time.LocalDateTime;
12-
import java.util.List;
1310
import java.util.UUID;
1411

1512
@Entity(name = "posts")
@@ -27,47 +24,31 @@ public class PostEntity {
2724
@CreationTimestamp
2825
private LocalDateTime createdAt;
2926

30-
@Column(nullable = false)
27+
@Column()
3128
@UpdateTimestamp
3229
private LocalDateTime updatedAt;
3330

34-
@Column(nullable = false)
35-
@NotBlank(message = "User who created id must not be blank")
31+
@Column()
3632
private UUID userWhoCreatedId;
3733

38-
@Column(nullable = true)
39-
private UUID userWhoUpdatedId;
40-
41-
@ManyToOne(cascade = CascadeType.ALL)
42-
@JoinColumn(name = "userWhoCreatedId", insertable = false, updatable = false, referencedColumnName = "id")
34+
@ManyToOne
35+
@JoinColumn(name = "UserWhoCreatedId", insertable = false, updatable = false)
4336
private UserEntity userWhoCreated;
4437

45-
@ManyToOne(cascade = CascadeType.ALL)
46-
@JoinColumn(name = "userWhoUpdatedId", insertable = false, updatable = false)
47-
private UserEntity userWhoUpdated;
48-
49-
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "id")
50-
private List<ErrorLogEntity> errorLogs;
51-
52-
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "id")
53-
private List<EventLogEntity> eventLogs;
54-
5538
public PostEntity() {}
5639

5740
public PostEntity(
5841
UUID id,
5942
String content,
6043
LocalDateTime createdAt,
6144
@Nullable LocalDateTime updatedAt,
62-
UUID userWhoCreatedId,
63-
@Nullable UUID userWhoUpdatedId
45+
UUID userWhoCreatedId
6446
) {
6547
this.id = id;
6648
this.content = content;
6749
this.createdAt = createdAt;
6850
this.updatedAt = updatedAt;
6951
this.userWhoCreatedId = userWhoCreatedId;
70-
this.userWhoUpdatedId = userWhoUpdatedId;
7152
}
7253

7354
public UUID getId() {
@@ -90,10 +71,6 @@ public LocalDateTime getCreatedAt() {
9071
return this.createdAt;
9172
}
9273

93-
public void setCreatedAt(LocalDateTime createdAt) {
94-
this.createdAt = createdAt;
95-
}
96-
9774
public LocalDateTime getUpdatedAt() {
9875
return this.updatedAt;
9976
}
@@ -105,24 +82,4 @@ public void setUpdatedAt(LocalDateTime updatedAt) {
10582
public UUID getUserWhoCreatedId() {
10683
return this.userWhoCreatedId;
10784
}
108-
109-
public void setUserWhoCreatedId(UUID userWhoCreatedId) {
110-
this.userWhoCreatedId = userWhoCreatedId;
111-
}
112-
113-
public UUID getUserWhoUpdatedId() {
114-
return this.userWhoUpdatedId;
115-
}
116-
117-
public void setUserWhoUpdatedId(UUID userWhoUpdatedId) {
118-
this.userWhoUpdatedId = userWhoUpdatedId;
119-
}
120-
121-
public UserEntity getUserWhoCreated() {
122-
return this.userWhoCreated;
123-
}
124-
125-
public UserEntity getUserWhoUpdated() {
126-
return this.userWhoUpdated;
127-
}
12885
}

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/posts/mappers/PostMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.CreatePostDTO;
44
import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.PostDTO;
55
import com.joaov1ct0r.restful_api_users_java.modules.posts.entities.PostEntity;
6+
67
import java.time.LocalDateTime;
78
import java.util.UUID;
89

@@ -14,7 +15,6 @@ public static PostDTO toDTO(PostEntity post) {
1415
postDTO.setCreatedAt(post.getCreatedAt());
1516
postDTO.setUpdatedAt(post.getUpdatedAt());
1617
postDTO.setUserWhoCreatedId(post.getUserWhoCreatedId());
17-
postDTO.setUserWhoUpdatedId(post.getUserWhoUpdatedId());
1818
return postDTO;
1919
}
2020

@@ -24,8 +24,7 @@ public static PostEntity toPersistence(CreatePostDTO postDTO, UUID userWhoCreate
2424
postDTO.getContent(),
2525
LocalDateTime.now(),
2626
null,
27-
userWhoCreatedId,
28-
null
27+
userWhoCreatedId
2928
);
3029
}
3130
}

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/users/entities/UserEntity.java

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,13 @@ public class UserEntity implements UserDetails {
5656
@UpdateTimestamp
5757
private LocalDateTime updatedAt;
5858

59-
@Column(nullable = true)
60-
@Nullable
61-
private UUID userWhoUpdatedId;
62-
63-
@ManyToOne(cascade = CascadeType.ALL)
64-
@JoinColumn(name = "userWhoUpdatedId", insertable = true, updatable = true, referencedColumnName = "id")
65-
private UserEntity userWhoUpdated;
66-
6759
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
6860
private List<ErrorLogEntity> errorLogs;
6961

7062
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
7163
private List<EventLogEntity> eventLogs;
7264

73-
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
65+
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "userWhoCreated")
7466
private List<PostEntity> posts;
7567

7668
public UserEntity() {}
@@ -81,10 +73,9 @@ public UserEntity(
8173
String email,
8274
String username,
8375
String password,
84-
String photoUrl,
76+
@Nullable String photoUrl,
8577
LocalDateTime createdAt,
86-
@Nullable LocalDateTime updatedAt,
87-
@Nullable UUID userWhoUpdatedId
78+
@Nullable LocalDateTime updatedAt
8879
) {
8980
this.id = id;
9081
this.name = name;
@@ -94,26 +85,17 @@ public UserEntity(
9485
this.photoUrl = photoUrl;
9586
this.createdAt = createdAt;
9687
this.updatedAt = updatedAt;
97-
this.userWhoUpdatedId = userWhoUpdatedId;
9888
}
9989

90+
@Nullable
10091
public String getPhotoUrl() {
10192
return this.photoUrl;
10293
}
10394

104-
public void setPhotoUrl(String photoUrl) {
95+
public void setPhotoUrl(@Nullable String photoUrl) {
10596
this.photoUrl = photoUrl;
10697
}
10798

108-
@Nullable
109-
public UUID getUserWhoUpdatedId() {
110-
return userWhoUpdatedId;
111-
}
112-
113-
public void setUserWhoUpdatedId(@Nullable UUID userWhoUpdatedId) {
114-
this.userWhoUpdatedId = userWhoUpdatedId;
115-
}
116-
11799
@Nullable
118100
public LocalDateTime getUpdatedAt() {
119101
return updatedAt;
@@ -196,7 +178,7 @@ public void setId(UUID id) {
196178
this.id = id;
197179
}
198180

199-
public List<PostEntity> getPosts() {
200-
return this.posts;
201-
}
181+
// public List<PostEntity> getPosts() {
182+
// return this.posts;
183+
// }
202184
}

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/users/mappers/UserMapper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static UserDTO toDTO(UserEntity user) {
1616
userDTO.setName(user.getName());
1717
userDTO.setCreatedAt(user.getCreatedAt());
1818
userDTO.setUpdatedAt(user.getUpdatedAt());
19-
userDTO.setUserWhoUpdatedId(user.getUserWhoUpdatedId());
2019
userDTO.setPhotoUrl(user.getPhotoUrl());
2120
return userDTO;
2221
}
@@ -30,7 +29,6 @@ public static UserEntity toPersistence(CreateUserDTO user) {
3029
user.getPassword(),
3130
user.getPhotoUrl(),
3231
LocalDateTime.now(),
33-
null,
3432
null
3533
);
3634
}

src/main/java/com/joaov1ct0r/restful_api_users_java/modules/users/services/CountAllUsersService.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,25 @@ public class CountAllUsersService extends BaseService {
1313
@Autowired
1414
private UserRepository userRepository;
1515

16-
public long execute(CountAllUsersDTO query)
17-
{
16+
public long execute(CountAllUsersDTO query) {
17+
boolean isQuery = query.getName() != null || query.getEmail() != null || query.getUsername() != null;
18+
1819
Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");
1920
var page = PageRequest.of(query.getPage() - 1, query.getPerPage(), sort);
20-
var users = this.userRepository.countByNameContainingAndUsernameContainingAndEmailContaining(
21-
query.getName(),
22-
query.getUsername(),
23-
query.getEmail(),
24-
page
25-
);
2621

27-
return users.stream().count();
22+
long users;
23+
24+
if (isQuery) {
25+
users = this.userRepository.findAllByNameContainingAndUsernameContainingAndEmailContaining(
26+
query.getName(),
27+
query.getUsername(),
28+
query.getEmail(),
29+
page
30+
).stream().count();
31+
} else {
32+
users = this.userRepository.findAll(page).stream().count();
33+
}
34+
35+
return users;
2836
}
2937
}

0 commit comments

Comments
 (0)