Skip to content

Commit

Permalink
Fixed map struct unamapping properties from all mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonpoltorak committed Feb 23, 2024
1 parent 3bf7a6f commit 1fc1a0f
Show file tree
Hide file tree
Showing 29 changed files with 179 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public final class BacklogItemServiceConstants {
public static final String GETTING_BY_ID = "Getting {} by id: {}";
public static final String BACKLOG_ITEM_NOT_FOUND_MESSAGE = "Backlog item not found";
public static final String RETURNING_RESPONSE_OF = "Returning response of: {}";
public static final String PROJECT_MEMBER_NOT_FOUND_MESSAGE = "Project member not found";
public static final String PROJECT_MEMBER_NOT_FOUND_MESSAGE = "Project assignee not found";
public static final String SPRINT_NOT_FOUND_MESSAGE = "Sprint not found";
public static final String PROJECT_NOT_FOUND_MESSAGE = "Project not found";
public static final String BACKLOG_ITEM = "BacklogItem";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import dev.corn.cornbackend.api.backlog.comment.data.BacklogItemCommentResponse;
import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberResponse;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;

import dev.corn.cornbackend.entities.user.data.UserResponse;
import lombok.Builder;

import java.util.List;

@Builder
public record BacklogItemDetails(List<BacklogItemCommentResponse> comments,
ProjectMemberResponse member,
UserResponse assignee,
SprintResponse sprint,
ProjectResponse projectResponse) {
ProjectResponse project) {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.corn.cornbackend.api.project.member;

import dev.corn.cornbackend.api.project.member.data.ProjectMemberResponse;
import dev.corn.cornbackend.api.project.member.interfaces.ProjectMemberController;
import dev.corn.cornbackend.api.project.member.interfaces.ProjectMemberService;
import dev.corn.cornbackend.config.jwtprocessing.JwtAuthed;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -28,23 +28,23 @@ public class ProjectMemberControllerImpl implements ProjectMemberController {

@Override
@PostMapping(value = ADD_MEMBER_TO_PROJECT_MAPPING)
public final ProjectMemberResponse addMemberToProject(@RequestParam String username,
@RequestParam long projectId,
@JwtAuthed User user) {
public final UserResponse addMemberToProject(@RequestParam String username,
@RequestParam long projectId,
@JwtAuthed User user) {
return projectMemberService.addMemberToProject(username, projectId, user);
}

@Override
@GetMapping(value = GET_MEMBERS_OF_PROJECT_MAPPING)
public final List<ProjectMemberResponse> getProjectMembers(@RequestParam long projectId,
public final List<UserResponse> getProjectMembers(@RequestParam long projectId,
@RequestParam int page,
@JwtAuthed User user) {
return projectMemberService.getProjectMembers(projectId, page, user);
}

@Override
@DeleteMapping(value = REMOVE_MEMBER_FROM_PROJECT_MAPPING)
public final ProjectMemberResponse removeMemberFromProject(@RequestParam String username,
public final UserResponse removeMemberFromProject(@RequestParam String username,
@RequestParam long projectId,
@JwtAuthed User user) {
return projectMemberService.removeMemberFromProject(username, projectId, user);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.corn.cornbackend.api.project.member;

import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoResponse;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberResponse;
import dev.corn.cornbackend.api.project.member.interfaces.ProjectMemberService;
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.project.interfaces.ProjectRepository;
import dev.corn.cornbackend.entities.project.member.ProjectMember;
import dev.corn.cornbackend.entities.project.member.interfaces.ProjectMemberMapper;
import dev.corn.cornbackend.entities.project.member.interfaces.ProjectMemberRepository;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import dev.corn.cornbackend.entities.user.interfaces.UserRepository;
import dev.corn.cornbackend.utils.exceptions.project.ProjectDoesNotExistException;
import dev.corn.cornbackend.utils.exceptions.project.member.InvalidUsernameException;
Expand Down Expand Up @@ -38,15 +38,15 @@ public class ProjectMemberServiceImpl implements ProjectMemberService {
private static final String PROJECT_NOT_FOUND = "Project with project id: %d does not exist";

@Override
public final ProjectMemberResponse addMemberToProject(String username, long projectId, User user) {
public final UserResponse addMemberToProject(String username, long projectId, User user) {

log.info("Adding member to project with username: {} and projectId: {}", username, projectId);
log.info("Adding assignee to project with username: {} and projectId: {}", username, projectId);

User userToAdd = getUserFromRepository(username);
Project project = getProjectFromRepositoryIfOwner(projectId, user);

if(projectRepository.existsByProjectMemberAndProjectId(userToAdd, projectId)) {
throw new InvalidUsernameException(String.format("User: %s is already owner or member of this project", username));
throw new InvalidUsernameException(String.format("User: %s is already owner or assignee of this project", username));
}

log.info("Found user: {} and project: {}", userToAdd, project);
Expand All @@ -61,11 +61,11 @@ public final ProjectMemberResponse addMemberToProject(String username, long proj

ProjectMember newMember = projectMemberRepository.save(projectMember);

return projectMemberMapper.toProjectMemberResponse(newMember);
return projectMemberMapper.mapProjectMememberToUserResponse(newMember);
}

@Override
public final List<ProjectMemberResponse> getProjectMembers(long projectId, int page, User user) {
public final List<UserResponse> getProjectMembers(long projectId, int page, User user) {
Pageable pageable = PageRequest.of(page, MEMBERS_PAGE_SIZE);

log.info("Getting project members for projectId: {}", projectId);
Expand All @@ -80,29 +80,29 @@ public final List<ProjectMemberResponse> getProjectMembers(long projectId, int p

return projectMembers
.stream()
.map(projectMemberMapper::toProjectMemberResponse)
.map(projectMemberMapper::mapProjectMememberToUserResponse)
.toList();
}

@Override
public final ProjectMemberResponse removeMemberFromProject(String username, long projectId, User user) {
public final UserResponse removeMemberFromProject(String username, long projectId, User user) {

if(username.equals(user.getUsername())) {
throw new InvalidUsernameException("You cannot remove yourself from the project");
}

log.info("Removing member from project with username: {} and projectId: {}", username, projectId);
log.info("Removing assignee from project with username: {} and projectId: {}", username, projectId);

User userToRemove = getUserFromRepository(username);
ProjectMember projectMember = projectMemberRepository
.findByProjectAndUser(getProjectFromRepositoryIfOwner(projectId, user), userToRemove)
.orElseThrow(() -> new ProjectMemberDoesNotExistException(String.format("Project member of id username %s in project %s does not exist", username, projectId))
.orElseThrow(() -> new ProjectMemberDoesNotExistException(String.format("Project assignee of id username %s in project %s does not exist", username, projectId))
);
log.info("Found projectMember: {}", projectMember);

projectMemberRepository.deleteById(projectMember.getProjectMemberId());

return projectMemberMapper.toProjectMemberResponse(projectMember);
return projectMemberMapper.mapProjectMememberToUserResponse(projectMember);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.corn.cornbackend.api.project.member.constants;

public final class ProjectMemberMappings {
public static final String PROJECT_MEMBER_MAPPING = "/api/project/member";
public static final String PROJECT_MEMBER_MAPPING = "/api/project/assignee";

public static final String ADD_MEMBER_TO_PROJECT_MAPPING = "/addMember";

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.corn.cornbackend.api.project.member.interfaces;

import dev.corn.cornbackend.api.project.member.data.ProjectMemberResponse;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;

import java.util.List;

Expand All @@ -10,32 +10,32 @@
*/
public interface ProjectMemberController {
/**
* Adds a member to a project
* Adds a assignee to a project
*
* @param username the username of the member to add
* @param projectId the id of the project to add the member to
* @param username the username of the assignee to add
* @param projectId the id of the project to add the assignee to
* @param user the user making the request
* @return a ProjectMemberResponse object
* @return a UserResponse object
*/
ProjectMemberResponse addMemberToProject(String username, long projectId, User user);
UserResponse addMemberToProject(String username, long projectId, User user);

/**
* Gets a list of project members
*
* @param projectId the id of the project to get the members of
* @param page the page number to get the members from
* @param user the user making the request
* @return a list of ProjectMemberResponse objects
* @return a list of UserResponse objects
*/
List<ProjectMemberResponse> getProjectMembers(long projectId, int page, User user);
List<UserResponse> getProjectMembers(long projectId, int page, User user);

/**
* Removes a member from a project
* Removes a assignee from a project
*
* @param username the username of the member to remove
* @param projectId the id of the project to remove the member from
* @param username the username of the assignee to remove
* @param projectId the id of the project to remove the assignee from
* @param user the user making the request
* @return a ProjectMemberResponse object
* @return a UserResponse object
*/
ProjectMemberResponse removeMemberFromProject(String username, long projectId, User user);
UserResponse removeMemberFromProject(String username, long projectId, User user);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.corn.cornbackend.api.project.member.interfaces;

import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoResponse;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberResponse;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;

import java.util.List;

Expand All @@ -11,40 +11,40 @@
*/
public interface ProjectMemberService {
/**
* Add a member to a project
* Add a assignee to a project
*
* @param username the username of the member to add
* @param projectId the id of the project to add the member to
* @param username the username of the assignee to add
* @param projectId the id of the project to add the assignee to
* @param user the user making the request
* @return project member response data
* @return project assignee response data
*/
ProjectMemberResponse addMemberToProject(String username, long projectId, User user);
UserResponse addMemberToProject(String username, long projectId, User user);

/**
* Get the members of a project
*
* @param projectId the id of the project to get the members of
* @param page the page of the members to get
* @param user the user making the request
* @return list of project member response data
* @return list of project assignee response data
*/
List<ProjectMemberResponse> getProjectMembers(long projectId, int page, User user);
List<UserResponse> getProjectMembers(long projectId, int page, User user);

/**
* Remove a member from a project
* Remove a assignee from a project
*
* @param username the username of the member to remove
* @param projectId the id of the project to remove the member from
* @param username the username of the assignee to remove
* @param projectId the id of the project to remove the assignee from
* @param user the user making the request
* @return project member response data
* @return project assignee response data
*/
ProjectMemberResponse removeMemberFromProject(String username, long projectId, User user);
UserResponse removeMemberFromProject(String username, long projectId, User user);

/**
* Get the info of the members of a project
*
* @param projectId the id of the project to get the members info of
* @return list of project member info response data
* @return list of project assignee info response data
*/
List<ProjectMemberInfoResponse> getProjectMembersInfo(long projectId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface BacklogItemCommentRepository extends JpaRepository<BacklogItemC
Optional<BacklogItemComment> findByIdWithUserOrOwner(@Param ("id") long id, @Param("user") User user);

/**
* Finds a BacklogItemComment by id and checks if the user is a member or the owner of the project associated
* Finds a BacklogItemComment by id and checks if the user is a assignee or the owner of the project associated
* with the BacklogItemComment
* @param id id of BacklogItemComment
* @param user user requesting access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import dev.corn.cornbackend.api.backlog.item.data.BacklogItemDetails;
import dev.corn.cornbackend.api.backlog.item.data.BacklogItemResponse;
import dev.corn.cornbackend.entities.backlog.item.BacklogItem;
import dev.corn.cornbackend.entities.project.interfaces.ProjectMapper;
import dev.corn.cornbackend.entities.project.member.ProjectMember;
import dev.corn.cornbackend.entities.project.member.ProjectMemberMapperImpl;
import dev.corn.cornbackend.entities.project.member.interfaces.ProjectMemberMapper;
import dev.corn.cornbackend.entities.sprint.interfaces.SprintMapper;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand All @@ -28,4 +33,16 @@ public interface BacklogItemMapper {
*/
@Mapping(target = "sprint", source = "sprint")
BacklogItemDetails backlogItemToBacklogItemDetails(BacklogItem backlogItem);

/**
* Maps a ProjectMember to a UserResponse.
*
* @param assignee The ProjectMember to map.
* @return The mapped UserResponse.
*/
default UserResponse mapProjectMemberToUserResponse(ProjectMember assignee) {
ProjectMemberMapper projectMemberMapper = new ProjectMemberMapperImpl();

return projectMemberMapper.mapProjectMememberToUserResponse(assignee);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface BacklogItemRepository extends JpaRepository<BacklogItem, Long>
List<BacklogItem> getByProject(Project project);

/**
* Finds a BacklogItem by id and checks if the user is a member or the owner of the project associated
* Finds a BacklogItem by id and checks if the user is a assignee or the owner of the project associated
* with the BacklogItem
* @param id id of BacklogItem
* @param user user requesting access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import dev.corn.cornbackend.api.project.data.ProjectInfoResponse;
import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoResponse;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.sprint.Sprint;
import dev.corn.cornbackend.entities.sprint.SprintMapperImpl;
import dev.corn.cornbackend.entities.sprint.interfaces.SprintMapper;
import org.mapstruct.Mapper;

import java.util.List;
Expand Down Expand Up @@ -31,4 +35,10 @@ public interface ProjectMapper {
*/
ProjectInfoResponse toProjectInfoResponse(Project project, List<ProjectMemberInfoResponse> membersInfo,
long totalNumberOfUsers);

default SprintResponse sprintToSprintResponse(Sprint sprint) {
SprintMapper sprintMapper = new SprintMapperImpl();

return sprintMapper.toSprintResponse(sprint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ProjectRepository extends JpaRepository<Project, Long> {
Page<Project> findAllByOwnerOrderByName(User owner, Pageable pageable);

/**
* Finds a Project by id and checks if the user is a member or the owner of given project
* Finds a Project by id and checks if the user is a assignee or the owner of given project
*
* @param id id of Project
* @param user user requesting access
Expand All @@ -49,7 +49,7 @@ public interface ProjectRepository extends JpaRepository<Project, Long> {
*
* @param user user to check
* @param projectId id of Project
* @return true if User is owner or project member of given Project, false otherwise
* @return true if User is owner or project assignee of given Project, false otherwise
*/
@Query("SELECT CASE WHEN COUNT (p) > 0 THEN true ELSE false END FROM Project p WHERE p.projectId = :projectId AND (p.owner = :user OR :user IN (SELECT pm.user FROM ProjectMember pm where pm.project = p))")
boolean existsByProjectMemberAndProjectId(@Param("user") User user, @Param("projectId") long projectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.project.member.constants.ProjectMemberConstants;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import dev.corn.cornbackend.utils.json.JsonMapper;
import dev.corn.cornbackend.utils.json.interfaces.Jsonable;
import dev.corn.cornbackend.utils.validators.interfaces.NoNullElements;
Expand Down
Loading

0 comments on commit 1fc1a0f

Please sign in to comment.