-
Notifications
You must be signed in to change notification settings - Fork 1
feature/34-implement-/roles-endpoint #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+927
−12
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5d61fd6
feat: add role entity class #34
lcanobbio d959760
feat: add role dto #34
lcanobbio f54e220
feat: add role mapper and rename dto class #34
lcanobbio 0d8a1d8
feat: add role repository #34
lcanobbio 4934493
feat: add validation business and persitience service #34
lcanobbio 30ca498
feat: add role migration #34
lcanobbio aa030ea
refactor: add attribute isManagement to role #34
lcanobbio bee7f04
feat: add role controller and add isManagement to migration #34
lcanobbio 4a6673a
feat: add all enpoints and modified migration #34
lcanobbio 0c175b7
fix: review improvements #34
lcanobbio 06886ec
fix: change datatype from varchar to text #34
lcanobbio c42de2f
fix: change from @where to @sqlrestriction because where is obsoltetd…
lcanobbio 376f690
fix: rename file like okr and change id assigment #34
lcanobbio 4c0de31
fix: internal server error when searching for a not existing role #34
nevio18324 5fddbb0
fix: internal server error when searching for a not existing role #34
nevio18324 55f43e1
fix: change namo of test-data-migration to avoid version problems #34
nevio18324 77ee84c
test: add all tests about role #34
lcanobbio 2501a82
chore: change sonar warnings #34
lcanobbio 47ecc33
fix: naming and delete service returns void #34
schiltpuzzle 53c4bc8
fix: review improvements #34
lcanobbio 5b2af87
chore: format one test and set default value to isManagement
lcanobbio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
backend/src/main/java/ch/puzzle/pcts/controller/RoleController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ch.puzzle.pcts.controller; | ||
|
|
||
| import ch.puzzle.pcts.dto.role.RoleDto; | ||
| import ch.puzzle.pcts.mapper.RoleMapper; | ||
| import ch.puzzle.pcts.model.role.Role; | ||
| import ch.puzzle.pcts.service.business.RoleBusinessService; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.media.ArraySchema; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import jakarta.validation.Valid; | ||
| import java.util.List; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/roles") | ||
| public class RoleController { | ||
| private final RoleMapper mapper; | ||
| private final RoleBusinessService service; | ||
|
|
||
| @Autowired | ||
| public RoleController(RoleMapper mapper, RoleBusinessService service) { | ||
| this.mapper = mapper; | ||
| this.service = service; | ||
| } | ||
|
|
||
| @Operation(summary = "List all roles") | ||
| @ApiResponse(responseCode = "200", description = "A list of roles", content = { | ||
| @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = RoleDto.class))) }) | ||
| @GetMapping | ||
| public ResponseEntity<List<RoleDto>> getRole() { | ||
| return ResponseEntity.ok(mapper.toDto(service.getAll())); | ||
| } | ||
|
|
||
| @Operation(summary = "Get a role by ID") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "200", description = "A single role", content = { | ||
| @Content(mediaType = "application/json", schema = @Schema(implementation = RoleDto.class)) }), | ||
| @ApiResponse(responseCode = "404", description = "Role not found", content = @Content) }) | ||
| @GetMapping("{id}") | ||
| public ResponseEntity<RoleDto> getRoleById(@PathVariable long id) { | ||
| Role role = service.getById(id); | ||
| return ResponseEntity.ok(mapper.toDto(role)); | ||
| } | ||
|
|
||
| @Operation(summary = "Create a new role") | ||
| @ApiResponse(responseCode = "201", description = "Role created successfully", content = { | ||
| @Content(mediaType = "application/json", schema = @Schema(implementation = RoleDto.class)) }) | ||
| @PostMapping | ||
| public ResponseEntity<RoleDto> createNew(@Valid @RequestBody RoleDto dto) { | ||
| Role newRole = service.create(mapper.fromDto(dto)); | ||
| return ResponseEntity.status(201).body(mapper.toDto(newRole)); | ||
| } | ||
|
|
||
| @Operation(summary = "Update a role") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "200", description = "Role updated successfully", content = { | ||
| @Content(mediaType = "application/json", schema = @Schema(implementation = RoleDto.class)) }), | ||
| @ApiResponse(responseCode = "404", description = "Role not found", content = @Content) }) | ||
| @PutMapping("{id}") | ||
| public ResponseEntity<RoleDto> updateRole(@PathVariable Long id, @RequestBody RoleDto dto) { | ||
| Role updatedRole = service.update(id, mapper.fromDto(dto)); | ||
| return ResponseEntity.ok(mapper.toDto(updatedRole)); | ||
| } | ||
|
|
||
| @Operation(summary = "Delete a role") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "204", description = "Role deleted successfully", content = @Content), | ||
| @ApiResponse(responseCode = "404", description = "Role not found", content = @Content) }) | ||
| @DeleteMapping("{id}") | ||
| public ResponseEntity<Void> deleteRole(@PathVariable Long id) { | ||
| service.delete(id); | ||
| return ResponseEntity.status(204).build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package ch.puzzle.pcts.dto.role; | ||
|
|
||
| public record RoleDto(Long id, String name, boolean isManagement) { | ||
| } |
26 changes: 26 additions & 0 deletions
26
backend/src/main/java/ch/puzzle/pcts/mapper/RoleMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ch.puzzle.pcts.mapper; | ||
|
|
||
| import ch.puzzle.pcts.dto.role.RoleDto; | ||
| import ch.puzzle.pcts.model.role.Role; | ||
| import java.util.List; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class RoleMapper { | ||
|
|
||
| public List<RoleDto> toDto(List<Role> models) { | ||
| return models.stream().map(this::toDto).toList(); | ||
| } | ||
|
|
||
| public List<Role> fromDto(List<RoleDto> dtos) { | ||
| return dtos.stream().map(this::fromDto).toList(); | ||
| } | ||
|
|
||
| public RoleDto toDto(Role model) { | ||
| return new RoleDto(model.getId(), model.getName(), model.getIsManagement()); | ||
| } | ||
|
|
||
| public Role fromDto(RoleDto dto) { | ||
| return new Role(dto.id(), dto.name(), dto.isManagement()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package ch.puzzle.pcts.model.role; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| @Entity | ||
| @SQLDelete(sql = "UPDATE role SET deleted_at = CURRENT_TIMESTAMP WHERE id = ?") | ||
| @SQLRestriction("deleted_at IS NULL") | ||
| public class Role { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| private boolean isManagement; | ||
|
|
||
| public Role(Long id, String name, boolean isManagement) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.isManagement = isManagement; | ||
| } | ||
|
|
||
| public Role() { | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public boolean getIsManagement() { | ||
| return this.isManagement; | ||
| } | ||
|
|
||
| public void setIsManagement(boolean isManagement) { | ||
| this.isManagement = isManagement; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/ch/puzzle/pcts/repository/RoleRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ch.puzzle.pcts.repository; | ||
|
|
||
| import ch.puzzle.pcts.model.role.Role; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface RoleRepository extends JpaRepository<Role, Long> { | ||
| } |
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/ch/puzzle/pcts/service/business/RoleBusinessService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package ch.puzzle.pcts.service.business; | ||
|
|
||
| import ch.puzzle.pcts.exception.PCTSException; | ||
| import ch.puzzle.pcts.model.error.ErrorKey; | ||
| import ch.puzzle.pcts.model.role.Role; | ||
| import ch.puzzle.pcts.service.persistence.RolePersistenceService; | ||
| import ch.puzzle.pcts.service.validation.RoleValidationService; | ||
| import java.util.List; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class RoleBusinessService { | ||
| private final RoleValidationService validationService; | ||
| private final RolePersistenceService persistenceService; | ||
|
|
||
| public RoleBusinessService(RoleValidationService validationService, RolePersistenceService persistenceService) { | ||
| this.validationService = validationService; | ||
| this.persistenceService = persistenceService; | ||
| } | ||
|
|
||
| public List<Role> getAll() { | ||
| return persistenceService.getAll(); | ||
| } | ||
|
|
||
| public Role getById(long id) { | ||
| validationService.validateOnGetById(id); | ||
| return persistenceService | ||
| .getById(id) | ||
| .orElseThrow(() -> new PCTSException(HttpStatus.NOT_FOUND, | ||
| "Role with id: " + id + " does not exist.", | ||
| ErrorKey.NOT_FOUND)); | ||
| } | ||
|
|
||
| public Role create(Role role) { | ||
| validationService.validateOnCreate(role); | ||
| return persistenceService.create(role); | ||
| } | ||
|
|
||
| public Role update(Long id, Role role) { | ||
| validationService.validateOnUpdate(id, role); | ||
| return persistenceService.update(id, role); | ||
| } | ||
|
|
||
| public void delete(Long id) { | ||
| validationService.validateOnDelete(id); | ||
| persistenceService.delete(id); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/ch/puzzle/pcts/service/persistence/RolePersistenceService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ch.puzzle.pcts.service.persistence; | ||
|
|
||
| import ch.puzzle.pcts.model.role.Role; | ||
| import ch.puzzle.pcts.repository.RoleRepository; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class RolePersistenceService { | ||
| private final RoleRepository repository; | ||
|
|
||
| @Autowired | ||
| public RolePersistenceService(RoleRepository repository) { | ||
| this.repository = repository; | ||
| } | ||
|
|
||
| public Role create(Role role) { | ||
| return repository.save(role); | ||
| } | ||
|
|
||
| public Optional<Role> getById(long id) { | ||
| return repository.findById(id); | ||
| } | ||
|
|
||
| public List<Role> getAll() { | ||
| return repository.findAll(); | ||
| } | ||
|
|
||
| public Role update(Long id, Role role) { | ||
| role.setId(id); | ||
| return repository.save(role); | ||
| } | ||
|
|
||
| public void delete(Long id) { | ||
| repository.deleteById(id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
backend/src/main/java/ch/puzzle/pcts/service/validation/RoleValidationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package ch.puzzle.pcts.service.validation; | ||
|
|
||
| import ch.puzzle.pcts.exception.PCTSException; | ||
| import ch.puzzle.pcts.model.error.ErrorKey; | ||
| import ch.puzzle.pcts.model.role.Role; | ||
| import ch.puzzle.pcts.service.persistence.RolePersistenceService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class RoleValidationService { | ||
| private final RolePersistenceService persistenceService; | ||
|
|
||
| @Autowired | ||
| public RoleValidationService(RolePersistenceService persistenceService) { | ||
| this.persistenceService = persistenceService; | ||
| } | ||
|
|
||
| public void validateOnGetById(Long id) { | ||
| validateIfExists(id); | ||
| } | ||
|
|
||
| public void validateOnCreate(Role role) { | ||
| validateIfIdIsNull(role.getId()); | ||
| validateName(role.getName()); | ||
| } | ||
|
|
||
| public void validateOnDelete(Long id) { | ||
| validateIfExists(id); | ||
| } | ||
|
|
||
| public void validateOnUpdate(Long id, Role role) { | ||
| validateIfExists(id); | ||
| validateIfIdIsNull(role.getId()); | ||
| validateName(role.getName()); | ||
| } | ||
|
|
||
| private void validateIfIdIsNull(Long id) { | ||
| if (id != null) { | ||
| throw new PCTSException(HttpStatus.BAD_REQUEST, "Id needs to be undefined", ErrorKey.ID_IS_NOT_NULL); | ||
| } | ||
| } | ||
|
|
||
| private void validateName(String name) { | ||
| if (name == null) { | ||
| throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be null", ErrorKey.ROLE_NAME_IS_NULL); | ||
| } | ||
|
|
||
| if (name.isBlank()) { | ||
| throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be empty", ErrorKey.ROLE_NAME_IS_EMPTY); | ||
| } | ||
| } | ||
|
|
||
| private void validateIfExists(long id) { | ||
| persistenceService | ||
| .getById(id) | ||
| .orElseThrow(() -> new PCTSException(HttpStatus.NOT_FOUND, | ||
| "Role with id: " + id + " does not exist.", | ||
| ErrorKey.NOT_FOUND)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
MasterEvarior marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CREATE TABLE IF NOT EXISTS role | ||
| ( | ||
| id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
| name TEXT NOT NULL, | ||
| deleted_at TIMESTAMP DEFAULT NULL, | ||
| is_management BOOLEAN NOT NULL DEFAULT FALSE | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.