-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from szymonpoltorak/collaborators
Collaborators
- Loading branch information
Showing
18 changed files
with
323 additions
and
3 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...backend/src/main/java/razepl/dev/todoapp/api/collaborator/CollaboratorControllerImpl.java
This file contains 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,57 @@ | ||
package razepl.dev.todoapp.api.collaborator; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorRequest; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorResponse; | ||
import razepl.dev.todoapp.api.collaborator.interfaces.CollaboratorController; | ||
import razepl.dev.todoapp.api.collaborator.interfaces.CollaboratorService; | ||
import razepl.dev.todoapp.entities.user.User; | ||
|
||
import java.util.List; | ||
|
||
import static razepl.dev.todoapp.api.collaborator.constants.CollaboratorMappings.ADD_USER_AS_COLLABORATOR; | ||
import static razepl.dev.todoapp.api.collaborator.constants.CollaboratorMappings.ASSIGN_COLLABORATOR_TO_TASK; | ||
import static razepl.dev.todoapp.api.collaborator.constants.CollaboratorMappings.COLLABORATOR_ENDPOINT_MAPPING; | ||
import static razepl.dev.todoapp.api.collaborator.constants.CollaboratorMappings.GET_COLLABORATORS_OF_TASK; | ||
import static razepl.dev.todoapp.api.collaborator.constants.CollaboratorMappings.GET_LIST_OF_COLLABORATORS_MAPPING; | ||
|
||
@RestController | ||
@RequestMapping(value = COLLABORATOR_ENDPOINT_MAPPING) | ||
@RequiredArgsConstructor | ||
public class CollaboratorControllerImpl implements CollaboratorController { | ||
private final CollaboratorService collaboratorService; | ||
|
||
@Override | ||
@GetMapping(value = GET_LIST_OF_COLLABORATORS_MAPPING) | ||
public final List<CollaboratorResponse> getListOfCollaborators(@AuthenticationPrincipal User user) { | ||
return collaboratorService.getListOfCollaborators(user); | ||
} | ||
|
||
@Override | ||
@GetMapping(value = GET_COLLABORATORS_OF_TASK) | ||
public final List<CollaboratorResponse> getCollaboratorsAssignedToTask(@RequestParam long taskId, | ||
@AuthenticationPrincipal User user) { | ||
return collaboratorService.getCollaboratorsAssignedToTask(taskId, user); | ||
} | ||
|
||
@Override | ||
@PostMapping(value = ADD_USER_AS_COLLABORATOR) | ||
public final CollaboratorResponse addUserAsCollaborator(@RequestParam String collaboratorUsername, | ||
@AuthenticationPrincipal User user) { | ||
return collaboratorService.addUserAsCollaborator(collaboratorUsername, user); | ||
} | ||
|
||
@Override | ||
@PatchMapping(value = ASSIGN_COLLABORATOR_TO_TASK) | ||
public final CollaboratorResponse assignCollaboratorToTask(@RequestBody CollaboratorRequest collaboratorRequest) { | ||
return collaboratorService.assignCollaboratorToTask(collaboratorRequest); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...pp-backend/src/main/java/razepl/dev/todoapp/api/collaborator/CollaboratorServiceImpl.java
This file contains 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,105 @@ | ||
package razepl.dev.todoapp.api.collaborator; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorRequest; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorResponse; | ||
import razepl.dev.todoapp.api.collaborator.interfaces.CollaboratorMapper; | ||
import razepl.dev.todoapp.api.collaborator.interfaces.CollaboratorService; | ||
import razepl.dev.todoapp.entities.collaborator.Collaborator; | ||
import razepl.dev.todoapp.entities.collaborator.interfaces.CollaboratorRepository; | ||
import razepl.dev.todoapp.entities.task.Task; | ||
import razepl.dev.todoapp.entities.task.interfaces.TaskRepository; | ||
import razepl.dev.todoapp.entities.user.User; | ||
import razepl.dev.todoapp.entities.user.interfaces.UserRepository; | ||
import razepl.dev.todoapp.exceptions.tasks.TaskDoesNotExistException; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class CollaboratorServiceImpl implements CollaboratorService { | ||
private final CollaboratorRepository collaboratorRepository; | ||
private final TaskRepository taskRepository; | ||
private final UserRepository userRepository; | ||
private final CollaboratorMapper collaboratorMapper; | ||
|
||
@Override | ||
public final List<CollaboratorResponse> getListOfCollaborators(User user) { | ||
List<Collaborator> collaborators = collaboratorRepository.findCollaboratorsByUser(user); | ||
|
||
log.info("Finding collaborators for User : {}", user.getUsername()); | ||
log.info("Found '{}' collaborators", collaborators.size()); | ||
|
||
return collaborators | ||
.stream() | ||
.map(collaboratorMapper::toCollaboratorResponse) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public final List<CollaboratorResponse> getCollaboratorsAssignedToTask(long taskId, User user) { | ||
log.info("Getting collaborators associated with task of id '{}' by the user '{}'", taskId, user.getUsername()); | ||
|
||
Task task = taskRepository.findById(taskId) | ||
.orElseThrow(() -> new TaskDoesNotExistException("Task does not exist!")); | ||
|
||
log.info("Task that was found : {}", task); | ||
|
||
return task | ||
.getCollaborator() | ||
.stream() | ||
.map(collaboratorMapper::toCollaboratorResponse) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public final CollaboratorResponse addUserAsCollaborator(String collaboratorUsername, User user) { | ||
if (collaboratorUsername.equals(user.getUsername())) { | ||
log.error("Username : {}", collaboratorUsername); | ||
|
||
throw new UsernameNotFoundException("User cannot add himself as collaborator!"); | ||
} | ||
log.info("Adding user : {} as collaborator for user : {}", collaboratorUsername, user.getUsername()); | ||
|
||
User collaboratorUser = userRepository.findByUsername(collaboratorUsername) | ||
.orElseThrow(() -> new UsernameNotFoundException("User does not exist!")); | ||
|
||
Collaborator collaborator = Collaborator | ||
.builder() | ||
.fullName(collaboratorUser.getFullName()) | ||
.username(collaboratorUsername) | ||
.user(user) | ||
.build(); | ||
collaborator = collaboratorRepository.save(collaborator); | ||
|
||
log.info("Mapping to respone collaborator: {}", collaborator); | ||
|
||
return collaboratorMapper.toCollaboratorResponse(collaborator); | ||
} | ||
|
||
@Override | ||
public final CollaboratorResponse assignCollaboratorToTask(CollaboratorRequest collaboratorRequest) { | ||
log.info("Received request : {}", collaboratorRequest); | ||
|
||
Task task = taskRepository.findById(collaboratorRequest.taskId()) | ||
.orElseThrow(() -> new TaskDoesNotExistException("Task has not been found in repository!")); | ||
log.info("Found task : {}", task); | ||
|
||
Collaborator collaborator = collaboratorRepository.findByUsername(collaboratorRequest.collaboratorUsername()) | ||
.orElseThrow(() -> new UsernameNotFoundException("Collaborator does not exist!")); | ||
log.info("Found collaborator : {}", collaborator); | ||
log.info("Number of collaborators before : {}", task.getCollaborator().size()); | ||
|
||
task.getCollaborator().add(collaborator); | ||
|
||
task = taskRepository.save(task); | ||
|
||
log.info("Number of collaborators after : {}", task.getCollaborator().size()); | ||
|
||
return collaboratorMapper.toCollaboratorResponse(collaborator); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...end/src/main/java/razepl/dev/todoapp/api/collaborator/constants/CollaboratorMappings.java
This file contains 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,16 @@ | ||
package razepl.dev.todoapp.api.collaborator.constants; | ||
|
||
public final class CollaboratorMappings { | ||
public static final String COLLABORATOR_ENDPOINT_MAPPING = "/api/collaborator"; | ||
|
||
public static final String GET_LIST_OF_COLLABORATORS_MAPPING = "listOfCollaborators"; | ||
|
||
public static final String GET_COLLABORATORS_OF_TASK = "collaboratorsOfTask"; | ||
|
||
public static final String ADD_USER_AS_COLLABORATOR = "addCollaborator"; | ||
|
||
public static final String ASSIGN_COLLABORATOR_TO_TASK = "assignToTask"; | ||
|
||
private CollaboratorMappings() { | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...p-backend/src/main/java/razepl/dev/todoapp/api/collaborator/data/CollaboratorRequest.java
This file contains 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 razepl.dev.todoapp.api.collaborator.data; | ||
|
||
public record CollaboratorRequest(long taskId, String collaboratorUsername) { | ||
} |
7 changes: 7 additions & 0 deletions
7
...-backend/src/main/java/razepl/dev/todoapp/api/collaborator/data/CollaboratorResponse.java
This file contains 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 @@ | ||
package razepl.dev.todoapp.api.collaborator.data; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CollaboratorResponse(String fullName, String username, long collaboratorId) { | ||
} |
17 changes: 17 additions & 0 deletions
17
.../src/main/java/razepl/dev/todoapp/api/collaborator/interfaces/CollaboratorController.java
This file contains 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,17 @@ | ||
package razepl.dev.todoapp.api.collaborator.interfaces; | ||
|
||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorRequest; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorResponse; | ||
import razepl.dev.todoapp.entities.user.User; | ||
|
||
import java.util.List; | ||
|
||
public interface CollaboratorController { | ||
List<CollaboratorResponse> getListOfCollaborators(User user); | ||
|
||
List<CollaboratorResponse> getCollaboratorsAssignedToTask(long taskId, User user); | ||
|
||
CollaboratorResponse addUserAsCollaborator(String collaboratorUsername, User user); | ||
|
||
CollaboratorResponse assignCollaboratorToTask(CollaboratorRequest collaboratorRequest); | ||
} |
10 changes: 10 additions & 0 deletions
10
...kend/src/main/java/razepl/dev/todoapp/api/collaborator/interfaces/CollaboratorMapper.java
This file contains 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,10 @@ | ||
package razepl.dev.todoapp.api.collaborator.interfaces; | ||
|
||
import org.mapstruct.Mapper; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorResponse; | ||
import razepl.dev.todoapp.entities.collaborator.Collaborator; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface CollaboratorMapper { | ||
CollaboratorResponse toCollaboratorResponse(Collaborator collaborator); | ||
} |
17 changes: 17 additions & 0 deletions
17
...end/src/main/java/razepl/dev/todoapp/api/collaborator/interfaces/CollaboratorService.java
This file contains 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,17 @@ | ||
package razepl.dev.todoapp.api.collaborator.interfaces; | ||
|
||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorRequest; | ||
import razepl.dev.todoapp.api.collaborator.data.CollaboratorResponse; | ||
import razepl.dev.todoapp.entities.user.User; | ||
|
||
import java.util.List; | ||
|
||
public interface CollaboratorService { | ||
List<CollaboratorResponse> getListOfCollaborators(User user); | ||
|
||
List<CollaboratorResponse> getCollaboratorsAssignedToTask(long taskId, User user); | ||
|
||
CollaboratorResponse addUserAsCollaborator(String collaboratorUsername, User user); | ||
|
||
CollaboratorResponse assignCollaboratorToTask(CollaboratorRequest collaboratorRequest); | ||
} |
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
todo-app-backend/src/main/java/razepl/dev/todoapp/api/tasks/constants/Constants.java
This file contains 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 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
39 changes: 39 additions & 0 deletions
39
todo-app-backend/src/main/java/razepl/dev/todoapp/entities/collaborator/Collaborator.java
This file contains 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 razepl.dev.todoapp.entities.collaborator; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import razepl.dev.todoapp.entities.user.User; | ||
|
||
import static razepl.dev.todoapp.entities.collaborator.constants.CollaboratorConstants.COLLABORATORS_TABLE_NAME; | ||
import static razepl.dev.todoapp.entities.task.constants.TaskConstants.USER_ID_COLUMN_NAME; | ||
|
||
@Data | ||
@Entity | ||
@Builder | ||
@Table(name = COLLABORATORS_TABLE_NAME) | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Collaborator { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private long collaboratorId; | ||
|
||
private String fullName; | ||
|
||
@Column(unique = true) | ||
private String username; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = USER_ID_COLUMN_NAME) | ||
private User user; | ||
} |
10 changes: 10 additions & 0 deletions
10
...c/main/java/razepl/dev/todoapp/entities/collaborator/constants/CollaboratorConstants.java
This file contains 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,10 @@ | ||
package razepl.dev.todoapp.entities.collaborator.constants; | ||
|
||
public final class CollaboratorConstants { | ||
public static final String COLLABORATORS_TABLE_NAME = "Collaborators"; | ||
|
||
public static final String COLLABORATOR_ID_COLUMN = "collaborators_task_id"; | ||
|
||
private CollaboratorConstants() { | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/razepl/dev/todoapp/entities/collaborator/interfaces/CollaboratorRepository.java
This file contains 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,16 @@ | ||
package razepl.dev.todoapp.entities.collaborator.interfaces; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import razepl.dev.todoapp.entities.collaborator.Collaborator; | ||
import razepl.dev.todoapp.entities.user.User; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface CollaboratorRepository extends JpaRepository<Collaborator, Long> { | ||
List<Collaborator> findCollaboratorsByUser(User user); | ||
|
||
Optional<Collaborator> findByUsername(String username); | ||
} |
This file contains 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
Oops, something went wrong.