Skip to content

Commit e55cb79

Browse files
committed
Merge branch 'development'
2 parents dc724ff + fa4c30c commit e55cb79

File tree

10 files changed

+77
-83
lines changed

10 files changed

+77
-83
lines changed

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
container_name: postgres-container
88
command: ["postgres", "-c", "log_statement=all", "-c", "log_destination=stderr"]
99
ports:
10-
- 5432:5432
10+
- "5432:5432"
1111
environment:
1212
- POSTGRES_USER=root
1313
- POSTGRES_PASSWORD=root
@@ -26,8 +26,8 @@ services:
2626
depends_on:
2727
- postgresql
2828
ports:
29-
- 8080:8080
30-
- 8000:8000
29+
- "8080:8080"
30+
- "8000:8000"
3131
environment:
3232
DB_URL: "postgresql:5432/common_app"
3333
DB_USERNAME: "root"

src/main/java/com/github/throyer/common/springboot/controllers/api/UsersController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ public ResponseEntity<UserDetails> show(@PathVariable Long id) {
8181
@PostMapping
8282
@ResponseStatus(CREATED)
8383
public ResponseEntity<UserDetails> save(
84-
@Validated @RequestBody CreateUserProps body
84+
@Validated @RequestBody CreateUserProps props
8585
) {
86-
var user = createService.create(body);
86+
props.validate();
87+
var user = createService.create(props);
8788
return created(user, "api/users");
8889
}
8990

src/main/java/com/github/throyer/common/springboot/controllers/app/RecoveryController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

3-
import static com.github.throyer.common.springboot.utils.Responses.validate;
3+
import static com.github.throyer.common.springboot.utils.Responses.validateAndUpdateModel;
44

55
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryConfirmService;
66
import com.github.throyer.common.springboot.domain.recovery.service.RecoveryService;
@@ -49,7 +49,7 @@ public String index(
4949
Model model
5050
) {
5151

52-
if (validate(model, recovery, "recovery", result)) {
52+
if (validateAndUpdateModel(model, recovery, "recovery", result)) {
5353
return "app/recovery/index";
5454
}
5555

@@ -70,7 +70,7 @@ public String confirm(
7070
Model model
7171
) {
7272

73-
if (validate(model, codes, "recovery", result)) {
73+
if (validateAndUpdateModel(model, codes, "recovery", result)) {
7474
return "app/recovery/confirm";
7575
}
7676

@@ -97,7 +97,7 @@ public String update(
9797
) {
9898
update.validate(result);
9999

100-
if (validate(model, update, "update", result)) {
100+
if (validateAndUpdateModel(model, update, "update", result)) {
101101
return "app/recovery/update";
102102
}
103103

src/main/java/com/github/throyer/common/springboot/controllers/app/RegisterController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

33
import static com.github.throyer.common.springboot.domain.shared.Type.SUCCESS;
4-
import static com.github.throyer.common.springboot.utils.Responses.validate;
4+
import static com.github.throyer.common.springboot.utils.Responses.validateAndUpdateModel;
55

66
import com.github.throyer.common.springboot.domain.user.model.CreateUserProps;
77
import com.github.throyer.common.springboot.domain.user.service.CreateUserService;
@@ -38,8 +38,10 @@ public String create(
3838
RedirectAttributes redirect,
3939
Model model
4040
) {
41-
42-
if (validate(model, props, "user", result)) {
41+
42+
props.validate(result);
43+
44+
if (validateAndUpdateModel(model, props, "user", result)) {
4345
return "app/register/index";
4446
}
4547

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
11
package com.github.throyer.common.springboot.domain.user.entity;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.github.throyer.common.springboot.domain.management.entity.Auditable;
6+
import com.github.throyer.common.springboot.domain.management.model.Addressable;
7+
import com.github.throyer.common.springboot.domain.role.entity.Role;
8+
import com.github.throyer.common.springboot.domain.session.model.Authorized;
9+
import com.github.throyer.common.springboot.domain.user.model.CreateUserProps;
10+
import com.github.throyer.common.springboot.domain.user.model.UpdateUserProps;
11+
import lombok.Getter;
12+
import lombok.Setter;
13+
import org.hibernate.Hibernate;
14+
import org.hibernate.annotations.Where;
15+
16+
import javax.persistence.*;
17+
import java.io.Serial;
318
import java.io.Serializable;
419
import java.security.Principal;
520
import java.util.List;
21+
import java.util.Map;
622
import java.util.Objects;
723

8-
import javax.persistence.CascadeType;
9-
import javax.persistence.Column;
10-
import javax.persistence.Entity;
11-
import javax.persistence.FetchType;
12-
import javax.persistence.GeneratedValue;
13-
import javax.persistence.GenerationType;
14-
import javax.persistence.Id;
15-
import javax.persistence.JoinColumn;
16-
import javax.persistence.JoinTable;
17-
import javax.persistence.ManyToMany;
18-
import javax.persistence.PrePersist;
19-
import javax.persistence.Table;
20-
21-
import com.fasterxml.jackson.annotation.JsonIgnore;
22-
import com.fasterxml.jackson.annotation.JsonProperty;
23-
import com.fasterxml.jackson.annotation.JsonProperty.Access;
24-
import com.github.throyer.common.springboot.domain.user.model.CreateUserProps;
25-
26-
import com.github.throyer.common.springboot.domain.session.model.Authorized;
27-
import com.github.throyer.common.springboot.domain.user.model.UpdateUserProps;
28-
import com.github.throyer.common.springboot.domain.management.entity.Auditable;
29-
import com.github.throyer.common.springboot.domain.role.entity.Role;
30-
import com.github.throyer.common.springboot.domain.management.model.Addressable;
24+
import static com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY;
3125
import static com.github.throyer.common.springboot.domain.management.repository.Queries.NON_DELETED_CLAUSE;
3226
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.PASSWORD_ENCODER;
27+
import static com.github.throyer.common.springboot.utils.JsonUtils.toJson;
28+
import static java.util.Optional.ofNullable;
29+
import static javax.persistence.CascadeType.DETACH;
30+
import static javax.persistence.FetchType.LAZY;
31+
import static javax.persistence.GenerationType.IDENTITY;
3332

34-
import lombok.Data;
35-
36-
import org.hibernate.annotations.Where;
37-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
38-
39-
@Data
4033
@Entity
34+
@Getter
35+
@Setter
4136
@Table(name = "user")
4237
@Where(clause = NON_DELETED_CLAUSE)
4338
public class User extends Auditable implements Serializable, Addressable {
44-
45-
public static final Integer PASSWORD_STRENGTH = 10;
46-
39+
@Serial
4740
private static final long serialVersionUID = -8080540494839892473L;
4841

4942
@Id
50-
@GeneratedValue(strategy = GenerationType.IDENTITY)
43+
@GeneratedValue(strategy = IDENTITY)
5144
private Long id;
5245

5346
@Column(name = "name", nullable = false)
@@ -60,11 +53,11 @@ public class User extends Auditable implements Serializable, Addressable {
6053
@Column(name = "deleted_email")
6154
private String deletedEmail;
6255

63-
@JsonProperty(access = Access.WRITE_ONLY)
56+
@JsonProperty(access = WRITE_ONLY)
6457
@Column(name = "password", nullable = false)
6558
private String password;
6659

67-
@ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
60+
@ManyToMany(cascade = DETACH, fetch = LAZY)
6861
@JoinTable(name = "user_role",
6962
joinColumns = {
7063
@JoinColumn(name = "user_id")},
@@ -110,38 +103,16 @@ public String getEmail() {
110103
return email;
111104
}
112105

113-
public void merge(UpdateUserProps dto) {
114-
setName(dto.getName());
115-
setEmail(dto.getEmail());
106+
public void merge(UpdateUserProps props) {
107+
this.name = props.getName();
108+
this.email = props.getEmail();
116109
}
117110

118111
public void updatePassword(String newPassword) {
119-
this.password = new BCryptPasswordEncoder(PASSWORD_STRENGTH)
112+
this.password = PASSWORD_ENCODER
120113
.encode(newPassword);
121114
}
122115

123-
@Override
124-
public int hashCode() {
125-
int hash = 7;
126-
hash = 97 * hash + Objects.hashCode(this.id);
127-
return hash;
128-
}
129-
130-
@Override
131-
public boolean equals(Object obj) {
132-
if (this == obj) {
133-
return true;
134-
}
135-
if (obj == null) {
136-
return false;
137-
}
138-
if (getClass() != obj.getClass()) {
139-
return false;
140-
}
141-
final User other = (User) obj;
142-
return Objects.equals(this.id, other.id);
143-
}
144-
145116
public boolean isPrincipal(Principal principal) {
146117
if (Objects.nonNull(principal) && principal instanceof Authorized authorized) {
147118
return getId().equals(authorized.getId());
@@ -167,6 +138,22 @@ private void created() {
167138

168139
@Override
169140
public String toString() {
170-
return Objects.nonNull(getName()) ? name : "null";
141+
return toJson(Map.of(
142+
"name", ofNullable(this.name).orElse(""),
143+
"email", ofNullable(this.email).orElse("")
144+
));
145+
}
146+
147+
@Override
148+
public boolean equals(Object o) {
149+
if (this == o) return true;
150+
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
151+
User user = (User) o;
152+
return id != null && Objects.equals(id, user.id);
153+
}
154+
155+
@Override
156+
public int hashCode() {
157+
return getClass().hashCode();
171158
}
172159
}

src/main/java/com/github/throyer/common/springboot/domain/user/model/CreateUserProps.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import lombok.Data;
1414
import lombok.NoArgsConstructor;
15+
import org.springframework.validation.BindingResult;
1516

1617
@Data
1718
@NoArgsConstructor
@@ -34,6 +35,10 @@ public CreateUserProps(String name, String email, String password) {
3435
setPassword(password);
3536
}
3637

38+
public void validate(BindingResult result) {
39+
validateEmailUniqueness(this, result);
40+
}
41+
3742
public void validate() {
3843
validateEmailUniqueness(this);
3944
}

src/main/java/com/github/throyer/common/springboot/domain/user/model/UserDetails.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import java.util.List;
55

6+
import com.github.throyer.common.springboot.domain.role.entity.Role;
67
import com.github.throyer.common.springboot.domain.user.entity.User;
78
import com.github.throyer.common.springboot.domain.management.model.Entity;
89
import static java.util.Optional.ofNullable;
@@ -22,7 +23,7 @@ public UserDetails(User user) {
2223

2324
this.roles = user.getRoles()
2425
.stream()
25-
.map(role -> role.getAuthority())
26+
.map(Role::getAuthority)
2627
.toList();
2728
}
2829

src/main/java/com/github/throyer/common/springboot/domain/user/service/CreateUserService.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ public class CreateUserService {
2020
@Autowired
2121
RoleRepository roleRepository;
2222

23-
public UserDetails create(CreateUserProps data) {
24-
25-
data.validate();
23+
public UserDetails create(CreateUserProps props) {
2624

2725
var roles = roleRepository.findOptionalByInitials("USER")
28-
.map(role -> List.of(role))
26+
.map(List::of)
2927
.orElseGet(() -> List.of());
3028

31-
var user = userRepository.save(new User(data, roles));
29+
var user = userRepository.save(new User(props, roles));
3230

3331
return new UserDetails(user);
3432
}

src/main/java/com/github/throyer/common/springboot/utils/Responses.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static final ResponseStatusException InternalServerError(String reason) {
104104
return new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, reason);
105105
}
106106

107-
public static final <P> Boolean validate(Model model, P props, String propertyName, BindingResult result) {
107+
public static final <P> Boolean validateAndUpdateModel(Model model, P props, String propertyName, BindingResult result) {
108108
if (result.hasErrors()) {
109109
model.addAttribute(propertyName, props);
110110
Toasts.add(model, result);

src/test/java/com/github/throyer/common/springboot/controllers/SessionsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void should_sigh_in_with_correct_password() throws Exception {
4949

5050
var body = """
5151
{
52-
\"email\": \"%s\",
53-
\"password\": \"%s\"
52+
"email": "%s",
53+
"password": "%s"
5454
}
5555
""";
5656

0 commit comments

Comments
 (0)