Skip to content

Commit 137d9b8

Browse files
committed
fix: review improvements #34
1 parent 919fe3d commit 137d9b8

7 files changed

Lines changed: 41 additions & 19 deletions

File tree

backend/src/main/java/ch/puzzle/pcts/controller/RoleController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.web.bind.annotation.*;
1919

2020
@RestController
21-
@RequestMapping("/api/v1/role")
21+
@RequestMapping("/api/v1/roles")
2222
public class RoleController {
2323
private final RoleMapper mapper;
2424
private final RoleBusinessService service;

backend/src/main/java/ch/puzzle/pcts/model/error/ErrorKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public enum ErrorKey {
88
NOT_FOUND,
99
ROLE_NAME_IS_NULL,
1010
ROLE_NAME_IS_EMPTY,
11+
ROLE_NAME_HAS_SPACE_AT_BEGINNING_OR_END,
1112
}

backend/src/main/java/ch/puzzle/pcts/model/role/Role.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import jakarta.persistence.*;
44
import java.time.LocalDateTime;
5+
import org.hibernate.annotations.SQLDelete;
6+
import org.hibernate.annotations.Where;
57

68
@Entity
9+
@SQLDelete(sql = "UPDATE role SET deleted_at = CURRENT_TIMESTAMP WHERE id = ?")
10+
@Where(clause = "deleted_at IS NULL")
711
public class Role {
812
@Id
913
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_role")
@@ -12,7 +16,7 @@ public class Role {
1216

1317
private String name;
1418

15-
private LocalDateTime deletedAt;
19+
private LocalDateTime deletedAt = null;
1620

1721
private boolean isManagement;
1822

backend/src/main/java/ch/puzzle/pcts/repository/RoleRepository.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,4 @@ public interface RoleRepository extends JpaRepository<Role, Long> {
2222
@Modifying
2323
@Query("UPDATE Role r SET r.name = :name, r.isManagement = :isManagement WHERE r.id = :id")
2424
void update(@Param("id") Long id, @Param("name") String name, @Param("isManagement") boolean isManagement);
25-
26-
@Transactional
27-
@Modifying
28-
@Query("UPDATE Role r SET r.deletedAt = CURRENT_TIMESTAMP WHERE r.id = :id")
29-
void softDeleteById(@Param("id") Long id);
3025
}

backend/src/main/java/ch/puzzle/pcts/service/business/RoleBusinessService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,22 @@ public List<Role> getAll() {
2121
}
2222

2323
public Role getById(long id) {
24-
validationService.validateIfExists(id);
24+
validationService.validateOnGetById(id);
2525
return persistenceService.getById(id);
2626
}
2727

2828
public Role create(Role role) {
29-
validationService.validateOnSetData(role);
29+
validationService.validateOnCreate(role);
3030
return persistenceService.create(role);
3131
}
3232

3333
public Role update(Long id, Role role) {
34-
validationService.validateIfExists(id);
35-
validationService.validateName(role.getName());
34+
validationService.validateOnUpdate(id, role);
3635
return persistenceService.update(id, role);
3736
}
3837

3938
public Role delete(Long id) {
40-
validationService.validateIfExists(id);
39+
validationService.validateOnDelete(id);
4140
Role deletedRole = persistenceService.getById(id);
4241
persistenceService.delete(id);
4342
return deletedRole;

backend/src/main/java/ch/puzzle/pcts/service/persistence/RolePersistenceService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public Role update(Long id, Role role) {
3333
}
3434

3535
public void delete(Long id) {
36-
repository.softDeleteById(id);
36+
repository.deleteById(id);
3737
}
3838
}

backend/src/main/java/ch/puzzle/pcts/service/validation/RoleValidationService.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,48 @@ public RoleValidationService(RolePersistenceService persistenceService) {
1818
this.persistenceService = persistenceService;
1919
}
2020

21-
public void validateOnSetData(Role role) {
22-
if (role.getId() != null) {
23-
throw new PCTSException(HttpStatus.BAD_REQUEST, "Id needs to be undefined", ErrorKey.ID_IS_NOT_NULL);
24-
}
21+
public void validateOnGetById(Long id) {
22+
validateIfExists(id);
23+
}
24+
25+
public void validateOnCreate(Role role) {
26+
validateIfIdIsNull(role.getId());
27+
validateName(role.getName());
28+
}
29+
30+
public void validateOnDelete(Long id) {
31+
validateIfExists(id);
32+
}
2533

34+
public void validateOnUpdate(Long id, Role role) {
35+
validateIfExists(id);
36+
validateIfIdIsNull(role.getId());
2637
validateName(role.getName());
2738
}
2839

29-
public void validateName(String name) {
40+
private void validateIfIdIsNull(Long id) {
41+
if (id != null) {
42+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Id needs to be undefined", ErrorKey.ID_IS_NOT_NULL);
43+
}
44+
}
45+
46+
private void validateName(String name) {
3047
if (name == null) {
3148
throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be null", ErrorKey.ROLE_NAME_IS_NULL);
3249
}
3350

3451
if (Objects.equals(name, "")) {
3552
throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be empty", ErrorKey.ROLE_NAME_IS_EMPTY);
3653
}
54+
55+
if (!name.matches("^\\S(.*\\S)?$")) {
56+
throw new PCTSException(HttpStatus.BAD_REQUEST,
57+
"Name must not have any space at the beginning and end",
58+
ErrorKey.ROLE_NAME_HAS_SPACE_AT_BEGINNING_OR_END);
59+
}
3760
}
3861

39-
public void validateIfExists(long id) {
62+
private void validateIfExists(long id) {
4063
if (persistenceService.getById(id) == null) {
4164
throw new PCTSException(HttpStatus.NOT_FOUND, "Role does not exists", ErrorKey.NOT_FOUND);
4265
}

0 commit comments

Comments
 (0)