Skip to content

Commit 74909cb

Browse files
committed
small refactoring and code style changes
1 parent 2b870b5 commit 74909cb

File tree

12 files changed

+64
-85
lines changed

12 files changed

+64
-85
lines changed

src/main/java/io/github/patternatlas/api/entities/candidate/comment/CandidateComment.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ public class CandidateComment extends Comment implements Serializable {
3232
@ManyToOne
3333
private Candidate candidate;
3434

35-
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
36-
@ToString.Exclude
37-
@ManyToOne
38-
private UserEntity user;
39-
4035
@JsonIgnore
4136
@OneToMany(mappedBy = "candidateComment", cascade = CascadeType.ALL, orphanRemoval = true)
4237
private Set<CandidateCommentRating> userRating = new HashSet<>();
4338

4439
private int rating = 0;
4540

4641
public CandidateComment(String text, Candidate candidate, UserEntity user) {
47-
super(text);
42+
super(text, user);
4843
this.candidate = candidate;
49-
this.user = user;
5044
}
5145

5246
public void updateComment(String text) {

src/main/java/io/github/patternatlas/api/entities/issue/comment/IssueComment.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,15 @@ public class IssueComment extends Comment implements Serializable {
3030
@ManyToOne
3131
private Issue issue;
3232

33-
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
34-
@ToString.Exclude
35-
@ManyToOne
36-
private UserEntity user;
37-
3833
@JsonIgnore
3934
@OneToMany(mappedBy = "issueComment", cascade = CascadeType.ALL, orphanRemoval = true)
4035
private List<IssueCommentRating> userRating = new ArrayList<>();
4136

4237
private int rating = 0;
4338

4439
public IssueComment(String text, Issue issue, UserEntity user) {
45-
super(text);
40+
super(text, user);
4641
this.issue = issue;
47-
this.user = user;
4842
}
4943

5044
public void updateComment(String text) {

src/main/java/io/github/patternatlas/api/entities/shared/Comment.java

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

33
import java.util.Objects;
44
import java.util.UUID;
5+
6+
import io.github.patternatlas.api.entities.user.UserEntity;
7+
8+
import com.fasterxml.jackson.annotation.JsonProperty;
59
import javax.persistence.GeneratedValue;
610
import javax.persistence.Id;
11+
import javax.persistence.ManyToOne;
712
import javax.persistence.MappedSuperclass;
813

914
import lombok.Data;
1015
import lombok.NoArgsConstructor;
16+
import lombok.ToString;
1117

1218
@MappedSuperclass
1319
@Data
@@ -20,8 +26,14 @@ public abstract class Comment {
2026

2127
private String text;
2228

23-
public Comment(String text) {
29+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
30+
@ToString.Exclude
31+
@ManyToOne
32+
private UserEntity user;
33+
34+
public Comment(String text, UserEntity user) {
2435
this.text = text;
36+
this.user = user;
2537
}
2638

2739
@Override
@@ -30,7 +42,8 @@ public boolean equals(Object o) {
3042
if (!(o instanceof Comment)) return false;
3143
Comment that = (Comment) o;
3244
return id.equals(that.id) &&
33-
text.equals(that.text);
45+
text.equals(that.text) &&
46+
user.equals(that.user);
3447
}
3548

3649
@Override

src/main/java/io/github/patternatlas/api/entities/user/UserEntity.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,8 @@ public class UserEntity implements Serializable{
5252
@JsonIgnore
5353
@ToString.Exclude
5454
@ManyToMany()
55-
/*@JoinTable(
56-
name = "user_entity_roles",
57-
joinColumns = { @JoinColumn(name = "users_id") },
58-
inverseJoinColumns = { @JoinColumn(name = "roles_id") }
59-
)*/
6055
private Set<Role> roles;
6156

62-
//@NaturalId(mutable = true)
6357
@Column(nullable = false, unique = false)
6458
private String email;
6559

@@ -107,13 +101,6 @@ public class UserEntity implements Serializable{
107101
private List<CandidateEvidence> candidateEvidence = new ArrayList<>();
108102

109103
/** Pattern fields*/
110-
// @JsonIgnore
111-
// @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
112-
// private Set<CandidateRating> candidateRatings = new HashSet<>();
113-
//
114-
// @JsonIgnore
115-
// @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
116-
// private List<CandidateComment> candidateComments = new ArrayList<>();
117104

118105
public UserEntity(UserModel userModel, String password) {
119106
this(userModel.getName(), userModel.getEmail(), password, null);

src/main/java/io/github/patternatlas/api/entities/user/role/Role.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public class Role {
3131
private Set<UserEntity> users;
3232

3333
@ManyToMany(cascade = CascadeType.ALL)
34-
/*@JoinTable(
35-
name = "role_privileges",
36-
joinColumns = { @JoinColumn(name = "roles_id") },
37-
inverseJoinColumns = { @JoinColumn(name = "privileges_id") }
38-
)*/
3934
private Collection<Privilege> privileges;
4035

4136
public Role(String name) {

src/main/java/io/github/patternatlas/api/rest/controller/CandidateController.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.swagger.v3.oas.annotations.media.Content;
1717
import io.swagger.v3.oas.annotations.responses.ApiResponse;
1818

19-
2019
import org.slf4j.Logger;
2120
import org.slf4j.LoggerFactory;
2221

@@ -65,19 +64,19 @@ public CandidateController(
6564
*/
6665
@Operation(operationId = "getAllCandidates", responses = {@ApiResponse(responseCode = "200")}, description = "Retrieve all candidates")
6766
@GetMapping(value = "")
68-
CollectionModel<EntityModel<CandidateModel>> all(@RequestParam(value = "lid", required = false) UUID languageId) {
69-
67+
CollectionModel<EntityModel<CandidateModel>> getAllCandidates(@RequestParam(value = "lid", required = false) UUID languageId) {
68+
7069
List<EntityModel<CandidateModel>> candidates;
7170
if (languageId == null) {
7271
candidates = this.candidateService.getAllCandidates()
73-
.stream()
74-
.map(candidate -> new EntityModel<>(new CandidateModel(candidate)))
75-
.collect(Collectors.toList());
72+
.stream()
73+
.map(candidate -> new EntityModel<>(new CandidateModel(candidate)))
74+
.collect(Collectors.toList());
7675
} else {
7776
candidates = this.candidateService.getAllCandidatesByLanguageId(languageId)
78-
.stream()
79-
.map(candidate -> new EntityModel<>(new CandidateModel(candidate)))
80-
.collect(Collectors.toList());
77+
.stream()
78+
.map(candidate -> new EntityModel<>(new CandidateModel(candidate)))
79+
.collect(Collectors.toList());
8180
}
8281

8382
return new CollectionModel<>(candidates);

src/main/java/io/github/patternatlas/api/rest/controller/IssueController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ CollectionModel<EntityModel<IssueModel>> getAll() {
7171
List<EntityModel<IssueModel>> issues = this.issueService.getAllIssues()
7272
.stream()
7373
.map(issue -> new EntityModel<>(new IssueModel(issue)))
74-
// .sorted(Comparator.comparingInt(i -> i.getContent().getRating()))
7574
.sorted((i1, i2) -> Integer.compare(i2.getContent().getRating(), i1.getContent().getRating()))
7675
.collect(Collectors.toList());
7776
return CollectionModel.of(issues);

src/main/java/io/github/patternatlas/api/rest/controller/UserController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.stream.Collectors;
99

1010
import io.github.patternatlas.api.entities.user.UserEntity;
11+
import io.github.patternatlas.api.entities.user.role.Privilege;
12+
import io.github.patternatlas.api.entities.user.role.Role;
1113
import io.github.patternatlas.api.rest.model.user.PrivilegeModel;
1214
import io.github.patternatlas.api.rest.model.user.RoleModel;
1315
import io.github.patternatlas.api.rest.model.user.RoleModelRequest;
@@ -98,8 +100,13 @@ public Map<String, Object> user(Principal principal) {
98100
Map<String, Object> model = new HashMap<String, Object>();
99101
model.put("name", user.getName());
100102
model.put("id", user.getId());
101-
model.put("role", user.getRoles().stream().map(role -> role.getName()).collect(Collectors.toList()));
102-
model.put("privileges", user.getRoles().stream().flatMap(role -> role.getPrivileges().stream()).map(privilege -> privilege.getName()).collect(Collectors.toList()));
103+
model.put("role", user.getRoles().stream()
104+
.map(Role::getName)
105+
.collect(Collectors.toList()));
106+
model.put("privileges", user.getRoles().stream()
107+
.flatMap(role -> role.getPrivileges().stream())
108+
.map(Privilege::getName)
109+
.collect(Collectors.toList()));
103110
return model;
104111
}
105112
return null;

src/main/java/io/github/patternatlas/api/rest/model/shared/CommentModel.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.github.patternatlas.api.entities.issue.IssueRating;
66
import io.github.patternatlas.api.entities.issue.comment.IssueComment;
77
import io.github.patternatlas.api.entities.issue.comment.IssueCommentRating;
8+
import io.github.patternatlas.api.entities.shared.Comment;
9+
import io.github.patternatlas.api.entities.user.UserEntity;
810

911
import lombok.Data;
1012
import lombok.EqualsAndHashCode;
@@ -27,24 +29,32 @@ public class CommentModel {
2729
private Collection<UUID> upVotes = new ArrayList<>();
2830
private Collection<UUID> downVotes = new ArrayList<>();
2931

32+
private void initialize(Comment comment) {
33+
this.id = comment.getId();
34+
this.userId = comment.getUser().getId();
35+
this.userName = comment.getUser().getName();
36+
this.text = comment.getText();
37+
this.rating = 0;
38+
}
39+
40+
private void updateRatingInformation(UserEntity user, int rating) {
41+
if (rating == 1) {
42+
this.upVotes.add(user.getId());
43+
this.rating = this.rating + 1;
44+
}
45+
if (rating == -1) {
46+
this.downVotes.add(user.getId());
47+
this.rating = this.rating - 1;
48+
}
49+
}
50+
3051
/**
3152
* For Issue Comments
3253
*/
3354
public CommentModel(IssueComment issueComment) {
34-
this.id = issueComment.getId();
35-
this.userId = issueComment.getUser().getId();
36-
this.userName = issueComment.getUser().getName();
37-
this.text = issueComment.getText();
38-
this.rating = 0;
55+
initialize(issueComment);
3956
for (IssueCommentRating issueRating : issueComment.getUserRating()) {
40-
if (issueRating.getRating() == 1) {
41-
this.upVotes.add(issueRating.getUser().getId());
42-
this.rating = this.rating + 1;
43-
}
44-
if (issueRating.getRating() == -1) {
45-
this.downVotes.add(issueRating.getUser().getId());
46-
this.rating = this.rating - 1;
47-
}
57+
updateRatingInformation(issueRating.getUser(), issueRating.getRating());
4858
}
4959
}
5060

@@ -56,20 +66,9 @@ public static CommentModel from(IssueComment issueComment) {
5666
* For Candidate Comments
5767
*/
5868
public CommentModel(CandidateComment candidateComment) {
59-
this.id = candidateComment.getId();
60-
this.userId = candidateComment.getUser().getId();
61-
this.userName = candidateComment.getUser().getName();
62-
this.text = candidateComment.getText();
63-
this.rating = 0;
64-
for (CandidateCommentRating candidateCommentRating : candidateComment.getUserRating()) {
65-
if (candidateCommentRating.getRating() == 1) {
66-
this.upVotes.add(candidateCommentRating.getUser().getId());
67-
this.rating = this.rating + 1;
68-
}
69-
if (candidateCommentRating.getRating() == -1) {
70-
this.downVotes.add(candidateCommentRating.getUser().getId());
71-
this.rating = this.rating - 1;
72-
}
69+
initialize(candidateComment);
70+
for (CandidateCommentRating issueRating : candidateComment.getUserRating()) {
71+
updateRatingInformation(issueRating.getUser(), issueRating.getRating());
7372
}
7473
}
7574

src/main/java/io/github/patternatlas/api/service/CandidateServiceImpl.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,6 @@ public Candidate updateCandidateRating(UUID candidateId, UUID userId, RatingMode
260260
}
261261
CandidateRating updatedCandidateRating = this.candidateRatingRepository.save(candidateRating);
262262
return updatedCandidateRating.getCandidate();
263-
264-
// if (ratingModelMultiRequest.getRatingType().equals(RatingType.READABILITY)) {
265-
// return new RatingModel(updatedCandidateRating, updatedCandidateRating.getReadability());
266-
// } else if (ratingModelMultiRequest.getRatingType().equals(RatingType.UNDERSTANDABILITY)) {
267-
// return new RatingModel(updatedCandidateRating, updatedCandidateRating.getUnderstandability());
268-
// } else{
269-
// return new RatingModel(updatedCandidateRating, updatedCandidateRating.getAppropriateness());
270-
// }
271-
272263
}
273264

274265
@Override

0 commit comments

Comments
 (0)