Skip to content

Commit

Permalink
Merge pull request #111 from szymonpoltorak/DEV-174-User-permissions-…
Browse files Browse the repository at this point in the history
…frontend

Dev 174 user permissions frontend
  • Loading branch information
szymonpoltorak authored May 7, 2024
2 parents 86e15ba + 1aa63c5 commit fe8f7db
Show file tree
Hide file tree
Showing 27 changed files with 96 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final BacklogItemCommentResponse addNewComment(BacklogItemCommentRequest

log.info(RETURNING_RESPONSE_FOR, savedComment);

return backLogItemCommentMapper.toBacklogItemCommentResponse(savedComment);
return backLogItemCommentMapper.toBacklogItemCommentResponse(savedComment, user);
}

@Override
Expand All @@ -82,7 +82,7 @@ public final BacklogItemCommentResponse updateComment(long commentId, String com

log.info(RETURNING_RESPONSE_FOR, savedComment);

return backLogItemCommentMapper.toBacklogItemCommentResponse(savedComment);
return backLogItemCommentMapper.toBacklogItemCommentResponse(savedComment, user);
}

@Override
Expand All @@ -98,7 +98,7 @@ public final BacklogItemCommentResponse deleteComment(long commentId, User user)

log.info(RETURNING_RESPONSE_FOR, backlogItemComment);

return backLogItemCommentMapper.toBacklogItemCommentResponse(backlogItemComment);
return backLogItemCommentMapper.toBacklogItemCommentResponse(backlogItemComment, user);
}

@Override
Expand All @@ -110,7 +110,7 @@ public final BacklogItemCommentResponse getComment(long commentId, User user) {

log.info(RETURNING_RESPONSE_FOR, backlogItemComment);

return backLogItemCommentMapper.toBacklogItemCommentResponse(backlogItemComment);
return backLogItemCommentMapper.toBacklogItemCommentResponse(backlogItemComment, user);
}

@Override
Expand All @@ -137,7 +137,7 @@ public BacklogItemCommentResponseList getCommentsForBacklogItem(long backlogItem

return BacklogItemCommentResponseList.builder()
.comments(comments.stream()
.map(backLogItemCommentMapper::toBacklogItemCommentResponse)
.map((comment) -> backLogItemCommentMapper.toBacklogItemCommentResponse(comment, user))
.toList())
.totalNumber(comments.getTotalElements())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public record BacklogItemCommentResponse(String comment,
UserResponse user,
LocalDateTime commentTime,
LocalDateTime lastEditTime,
long backlogItemCommentId) {
long backlogItemCommentId,
boolean canEdit) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public BacklogItemDetails getDetailsById(long id, User user) {

log.info(RETURNING_RESPONSE_OF, backlogItem);

return backlogItemMapper.backlogItemToBacklogItemDetails(backlogItem);
return backlogItemMapper.backlogItemToBacklogItemDetails(backlogItem, user);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final ProjectInfoResponse addNewProject(String name, User user) {

log.info("Created project member: {}", newProjectMember);

return mapToProjectInfoResponse(newProject, NEW_PROJECT_MEMBER_NUMBER);
return mapToProjectInfoResponse(newProject, NEW_PROJECT_MEMBER_NUMBER, user);
}

@Override
Expand All @@ -74,7 +74,7 @@ public final List<ProjectInfoResponse> getProjectsOnPage(int page, User user) {

return projects
.stream()
.map(this::mapToProjectInfoResponse)
.map((project) -> this.mapToProjectInfoResponse(project, user))
.toList();
}

Expand All @@ -93,7 +93,7 @@ public final ProjectResponse updateProjectsName(String name, long projectId, Use

log.info("Updated project: {}", updatedProject);

return projectMapper.toProjectResponse(updatedProject);
return projectMapper.toProjectResponse(updatedProject, user);
}

@Override
Expand All @@ -107,18 +107,18 @@ public final ProjectResponse deleteProject(long projectId, User user) {

projectRepository.deleteById(projectId);

return projectMapper.toProjectResponse(projectToDelete);
return projectMapper.toProjectResponse(projectToDelete, user);
}

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

return mapToProjectInfoResponse(project, totalNumberOfUsers);
return mapToProjectInfoResponse(project, totalNumberOfUsers, user);
}

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

return projectMapper.toProjectInfoResponse(project, members, totalNumberOfUsers);
return projectMapper.toProjectInfoResponse(project, members, totalNumberOfUsers, user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

@Builder
public record ProjectInfoResponse(long projectId, String name, List<SprintResponse> sprints, long totalNumberOfUsers,
List<UserResponse> membersInfo) {
List<UserResponse> membersInfo, boolean isOwner) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import java.util.List;

@Builder
public record ProjectResponse(String name, List<SprintResponse> sprints) {
public record ProjectResponse(String name, List<SprintResponse> sprints, boolean isOwner) {
}
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.ProjectMemberList;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberInfoExtendedResponse;
import dev.corn.cornbackend.api.project.member.data.ProjectMemberList;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public void run(String... args) {
)).map(backlogItemRepository::save).toList());
}

for (int i = 0; i < SAMPLE_BACKLOG_ITEMS.length / 4; i++) {
for (int j = 0; j < random.nextInt(4); j++) {
for (int i = 0; i < SAMPLE_BACKLOG_ITEMS.length / 2; i++) {
for (int j = 0; j < random.nextInt(6); j++) {
long backlogItemId = drawRandom(backlogItems).getBacklogItemId();
User commenter = drawRandom(users);
for (int k = 0; k < 5; k++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import dev.corn.cornbackend.api.backlog.comment.data.BacklogItemCommentResponse;
import dev.corn.cornbackend.entities.backlog.comment.BacklogItemComment;
import dev.corn.cornbackend.entities.user.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

/**
* Mapper for converting BacklogItemComment to BacklogItemCommentResponse
Expand All @@ -16,6 +18,8 @@ public interface BacklogItemCommentMapper {
* @param backlogItemComment The BacklogItemComment to convert
* @return The converted BacklogItemCommentResponse
*/
@Mapping(source = "commentDate", target = "commentTime")
BacklogItemCommentResponse toBacklogItemCommentResponse(BacklogItemComment backlogItemComment);
@Mapping(source = "backlogItemComment.commentDate", target = "commentTime")
@Mapping(target = "canEdit", expression = "java(user.equals(backlogItemComment.getUser()) || user.equals(backlogItemComment.getBacklogItem().getProject().getOwner()))")
@Named("toBacklogItemCommentResponse")
BacklogItemCommentResponse toBacklogItemCommentResponse(BacklogItemComment backlogItemComment, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -32,8 +33,8 @@ public interface BacklogItemMapper {
* @param backlogItem The BacklogItem to map.
* @return The mapped BacklogItemDetails.
*/
@Mapping(target = "sprint", source = "sprint")
BacklogItemDetails backlogItemToBacklogItemDetails(BacklogItem backlogItem);
@Mapping(target = "sprint", source = "backlogItem.sprint")
BacklogItemDetails backlogItemToBacklogItemDetails(BacklogItem backlogItem, User user);

/**
* Maps a ProjectMember to a UserResponse.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import dev.corn.cornbackend.entities.sprint.Sprint;
import dev.corn.cornbackend.entities.sprint.SprintMapperImpl;
import dev.corn.cornbackend.entities.sprint.interfaces.SprintMapper;
import dev.corn.cornbackend.entities.user.User;
import dev.corn.cornbackend.entities.user.data.UserResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;

Expand All @@ -24,7 +26,9 @@ public interface ProjectMapper {
* @param project the project to map
* @return the project response
*/
ProjectResponse toProjectResponse(Project project);
@Mapping(target = "isOwner", expression = "java(user.equals(project.getOwner()))")
@Mapping(target = "name", source = "project.name")
ProjectResponse toProjectResponse(Project project, User user);

/**
* Map a project to a project info response
Expand All @@ -33,8 +37,10 @@ public interface ProjectMapper {
* @param membersInfo the members info of the project
* @return the project info response
*/
@Mapping(target = "name", source = "project.name")
@Mapping(target = "isOwner", expression = "java(user.equals(project.getOwner()))")
ProjectInfoResponse toProjectInfoResponse(Project project, List<UserResponse> membersInfo,
long totalNumberOfUsers);
long totalNumberOfUsers, User user);

default SprintResponse sprintToSprintResponse(Sprint sprint) {
SprintMapper sprintMapper = new SprintMapperImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final void test_addNewCommentShouldReturnCorrectBacklogItemCommentResponse() {
when(backlogItemCommentRepository.save(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentToSave()))
.thenReturn(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment());

when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment()))
when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment(), BACKLOG_ITEM_COMMENT_TEST_DATA.user()))
.thenReturn(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse());

BacklogItemCommentResponse expected = BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse();
Expand Down Expand Up @@ -100,7 +100,8 @@ final void test_updateBacklogItemComentShouldReturnCorrectBacklogItemCommentResp
when(backlogItemCommentRepository.save(UPDATE_BACKLOG_ITEM_TEST_DATA.updatedBacklogItemComment()))
.thenReturn(UPDATE_BACKLOG_ITEM_TEST_DATA.updatedBacklogItemComment());

when(backlogItemCommentMapper.toBacklogItemCommentResponse(UPDATE_BACKLOG_ITEM_TEST_DATA.updatedBacklogItemComment()))
when(backlogItemCommentMapper.toBacklogItemCommentResponse(UPDATE_BACKLOG_ITEM_TEST_DATA.updatedBacklogItemComment(),
UPDATE_BACKLOG_ITEM_TEST_DATA.user()))
.thenReturn(UPDATE_BACKLOG_ITEM_TEST_DATA.backlogItemCommentResponse());

BacklogItemCommentResponse expected = UPDATE_BACKLOG_ITEM_TEST_DATA.backlogItemCommentResponse();
Expand Down Expand Up @@ -136,7 +137,8 @@ final void test_deleteCommentShouldReturnCorrectBacklogItemCommentResponse() {
when(backlogItemCommentRepository.findByIdWithUserOrOwner(commentId, UPDATE_BACKLOG_ITEM_TEST_DATA.user()))
.thenReturn(Optional.of(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment()));

when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment()))
when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment(),
UPDATE_BACKLOG_ITEM_TEST_DATA.user()))
.thenReturn(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse());

BacklogItemCommentResponse expected = BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse();
Expand Down Expand Up @@ -171,7 +173,8 @@ final void test_getCommentShouldReturnCorrectBacklogItemCommentResponse() {
when(backlogItemCommentRepository.findByIdWithProjectMember(commentId, UPDATE_BACKLOG_ITEM_TEST_DATA.user()))
.thenReturn(Optional.of(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment()));

when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment()))
when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment(),
UPDATE_BACKLOG_ITEM_TEST_DATA.user()))
.thenReturn(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse());

BacklogItemCommentResponse expected = BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse();
Expand Down Expand Up @@ -214,7 +217,8 @@ final void getCommentsForBacklogItemShouldReturnCorrectResponse() {
BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment().getBacklogItem(), pageable))
.thenReturn(page);

when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment()))
when(backlogItemCommentMapper.toBacklogItemCommentResponse(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemComment(),
UPDATE_BACKLOG_ITEM_TEST_DATA.user()))
.thenReturn(BACKLOG_ITEM_COMMENT_TEST_DATA.backlogItemCommentResponse());

//then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ final void getDetailsById_shouldReturnCorrectBacklogItemDetailsResponseOnCorrect
when(backlogItemRepository.findByIdWithProjectMember(id, SAMPLE_USER))
.thenReturn(Optional.of(BACKLOG_ITEM_DETAILS_TEST_DATA.backlogItem()));

when(backlogItemMapper.backlogItemToBacklogItemDetails(BACKLOG_ITEM_DETAILS_TEST_DATA.backlogItem()))
when(backlogItemMapper.backlogItemToBacklogItemDetails(BACKLOG_ITEM_DETAILS_TEST_DATA.backlogItem(), SAMPLE_USER))
.thenReturn(BACKLOG_ITEM_DETAILS_TEST_DATA.backlogItemDetails());

BacklogItemDetails expected = BACKLOG_ITEM_DETAILS_TEST_DATA.backlogItemDetails();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ProjectControllerTest {
final void test_addNewProject_shouldAddNewProject() {
// given
Project project = ADD_PROJECT_DATA.project();
ProjectInfoResponse expected = MAPPER.toProjectInfoResponse(project, Collections.emptyList(), 0L);
ProjectInfoResponse expected = MAPPER.toProjectInfoResponse(project, Collections.emptyList(), 0L,
ADD_PROJECT_DATA.owner());

// when
when(projectService.addNewProject(ADD_PROJECT_DATA.project().getName(), ADD_PROJECT_DATA.owner()))
Expand All @@ -55,7 +56,8 @@ final void test_getProjectsOnPage_shouldGetProjects() {
// given
Project project = ADD_PROJECT_DATA.project();
List<UserResponse> members = List.of(new UserResponse(1, "full", "name", "username"));
List<ProjectInfoResponse> expected = List.of(MAPPER.toProjectInfoResponse(project, members, 1L));
List<ProjectInfoResponse> expected = List.of(MAPPER.toProjectInfoResponse(project, members, 1L,
ADD_PROJECT_DATA.owner()));
int page = 0;

// when
Expand All @@ -73,7 +75,7 @@ final void test_getProjectsOnPage_shouldGetProjects() {
final void test_updateProjectsName_shouldUpdateName() {
// given
Project project = ADD_PROJECT_DATA.project();
ProjectResponse expected = MAPPER.toProjectResponse(project);
ProjectResponse expected = MAPPER.toProjectResponse(project, ADD_PROJECT_DATA.owner());

// when
when(projectService.updateProjectsName(ADD_PROJECT_DATA.project().getName(), 0L, ADD_PROJECT_DATA.owner()))
Expand All @@ -91,7 +93,7 @@ final void test_deleteProject_shouldDeleteProject() {
// given
long projectId = 0L;
Project project = ADD_PROJECT_DATA.project();
ProjectResponse expected = MAPPER.toProjectResponse(project);
ProjectResponse expected = MAPPER.toProjectResponse(project, ADD_PROJECT_DATA.owner());

// when
when(projectService.deleteProject(projectId, ADD_PROJECT_DATA.owner()))
Expand Down
Loading

0 comments on commit fe8f7db

Please sign in to comment.