Skip to content
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

Mapper fixes #50

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 +1,6 @@
package dev.corn.cornbackend.api.project;

import dev.corn.cornbackend.api.project.data.ProjectInfoResponse;
import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.api.project.interfaces.ProjectController;
import dev.corn.cornbackend.api.project.interfaces.ProjectService;
Expand Down Expand Up @@ -33,15 +34,15 @@ public class ProjectControllerImpl implements ProjectController {
@Override
@PostMapping(value = ADD_PROJECT)
@ResponseStatus(value = HttpStatus.CREATED)
public final ProjectResponse addNewProject(@RequestParam String name,
public final ProjectInfoResponse addNewProject(@RequestParam String name,
@JwtAuthed User user) {
return projectService.addNewProject(name, user);
}

@Override
@GetMapping(value = GET_PROJECTS_ON_PAGE)
public final List<ProjectResponse> getProjectsOnPage(@RequestParam int page,
@JwtAuthed User user) {
public final List<ProjectInfoResponse> getProjectsOnPage(@RequestParam int page,
@JwtAuthed User user) {
return projectService.getProjectsOnPage(page, user);
}

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

import dev.corn.cornbackend.api.project.data.ProjectInfoResponse;
import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.api.project.interfaces.ProjectService;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoResponse;
import dev.corn.cornbackend.api.project.member.interfaces.ProjectMemberService;
import dev.corn.cornbackend.entities.project.Project;
import dev.corn.cornbackend.entities.project.interfaces.ProjectMapper;
import dev.corn.cornbackend.entities.project.interfaces.ProjectRepository;
Expand All @@ -12,7 +15,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Collections;
Expand All @@ -23,12 +25,14 @@
@RequiredArgsConstructor
public class ProjectServiceImpl implements ProjectService {
private static final int PROJECTS_PER_PAGE = 20;
private static final long NEW_PROJECT_MEMBER_NUMBER = 0L;
private static final String PROJECT_DOES_NOT_EXIST = "Project does not exist!";
private final ProjectRepository projectRepository;
private final ProjectMapper projectMapper;
private final ProjectMemberService projectMemberService;

@Override
public final ProjectResponse addNewProject(String name, User user) {
public final ProjectInfoResponse addNewProject(String name, User user) {
log.info("Adding new project with name: {} and user: {}", name, user);

Project project = Project
Expand All @@ -43,11 +47,11 @@ public final ProjectResponse addNewProject(String name, User user) {

log.info("Saved project: {}", newProject);

return projectMapper.toProjectResponse(newProject);
return mapToProjectInfoResponse(newProject, NEW_PROJECT_MEMBER_NUMBER);
}

@Override
public final List<ProjectResponse> getProjectsOnPage(int page, User user) {
public final List<ProjectInfoResponse> getProjectsOnPage(int page, User user) {
log.info("Getting projects on page: {} for user: {}", page, user);

Pageable pageable = PageRequest.of(page, PROJECTS_PER_PAGE);
Expand All @@ -57,7 +61,7 @@ public final List<ProjectResponse> getProjectsOnPage(int page, User user) {

return projects
.stream()
.map(projectMapper::toProjectResponse)
.map(this::mapToProjectInfoResponse)
.toList();
}

Expand Down Expand Up @@ -92,4 +96,16 @@ public final ProjectResponse deleteProject(long projectId, User user) {

return projectMapper.toProjectResponse(projectToDelete);
}

private ProjectInfoResponse mapToProjectInfoResponse(Project project) {
long totalNumberOfUsers = projectMemberService.getTotalNumberOfMembers(project.getProjectId());

return mapToProjectInfoResponse(project, totalNumberOfUsers);
}

private ProjectInfoResponse mapToProjectInfoResponse(Project project, long totalNumberOfUsers) {
List<ProjectMemberInfoResponse> members = projectMemberService.getProjectMembersInfo(project.getProjectId());

return projectMapper.toProjectInfoResponse(project, members, totalNumberOfUsers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.corn.cornbackend.api.project.data;

import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoResponse;
import dev.corn.cornbackend.api.sprint.data.SprintResponse;
import lombok.Builder;

import java.util.List;

@Builder
public record ProjectInfoResponse(String name, List<SprintResponse> sprints, long totalNumberOfUsers,
List<ProjectMemberInfoResponse> membersInfo) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.corn.cornbackend.api.project.interfaces;

import dev.corn.cornbackend.api.project.data.ProjectInfoResponse;
import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.entities.user.User;

Expand All @@ -14,9 +15,9 @@ public interface ProjectController {
*
* @param name the name of the project
* @param user the user that is adding the project
* @return the response of the project
* @return the response of the project info
*/
ProjectResponse addNewProject(String name, User user);
ProjectInfoResponse addNewProject(String name, User user);

/**
* Gets a list of projects on a page
Expand All @@ -25,7 +26,7 @@ public interface ProjectController {
* @param user the user that is getting the projects
* @return the list of projects
*/
List<ProjectResponse> getProjectsOnPage(int page, User user);
List<ProjectInfoResponse> getProjectsOnPage(int page, User user);

/**
* Updates the name of the project
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.corn.cornbackend.api.project.interfaces;

import dev.corn.cornbackend.api.project.data.ProjectInfoResponse;
import dev.corn.cornbackend.api.project.data.ProjectResponse;
import dev.corn.cornbackend.entities.user.User;

Expand All @@ -14,18 +15,18 @@ public interface ProjectService {
*
* @param name the name of the project
* @param user the user who is adding the project
* @return the response of the project
* @return the response of the project info
*/
ProjectResponse addNewProject(String name, User user);
ProjectInfoResponse addNewProject(String name, User user);

/**
* Gets a list of projects on a page
*
* @param page the page number
* @param user the user who is getting the projects
* @return the list of project responses
* @return the list of project info responses
*/
List<ProjectResponse> getProjectsOnPage(int page, User user);
List<ProjectInfoResponse> getProjectsOnPage(int page, User user);

/**
* Updates the name of a project
Expand Down
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,13 +1,14 @@
package dev.corn.cornbackend.api.project.member;

import dev.corn.cornbackend.api.project.member.data.ProjectMemberResponse;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoResponse;
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 @@ -37,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 @@ -60,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 @@ -79,29 +80,62 @@ 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
public final List<ProjectMemberInfoResponse> getProjectMembersInfo(long projectId) {
Pageable pageable = PageRequest.of(0, 5);

log.info("Getting project members info for projectId: {}", projectId);

Page<ProjectMember> members = projectMemberRepository.findMembersByProjectIdForProjectInfo(projectId, pageable);

log.info("Found members: {}", members);

return members
.stream()
.map(ProjectMember::getUser)
.map(user -> ProjectMemberInfoResponse
.builder()
.fullName(user.getFullName())
.userId(user.getUserId())
.build()
)
.toList();
}

@Override
public final long getTotalNumberOfMembers(long projectId) {
log.info("Getting total number of members for projectId: {}", projectId);

long totalNumberOfMembers = projectMemberRepository.countByProjectProjectId(projectId);

log.info("Total number of members: {}", totalNumberOfMembers);

return totalNumberOfMembers;
}

private User getUserFromRepository(String username) {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.corn.cornbackend.api.project.member.data;

import lombok.Builder;

@Builder
public record ProjectMemberInfoResponse(String fullName, long userId) {
}

This file was deleted.

Loading
Loading