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

Support comments for team request #12

Merged
merged 2 commits into from
Nov 22, 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 @@ -3,13 +3,16 @@
import io.flexwork.modules.usermanagement.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -46,9 +49,17 @@ public class Comment {
updatable = false)
private LocalDateTime createdAt;

@Enumerated(EnumType.STRING)
@Column(name = "entity_type", nullable = false, length = 20)
private String entityType;
private EntityType entityType;

@Column(name = "entity_id", nullable = false)
private Long entityId;

@PrePersist
private void prePersist() {
if (createdAt == null) {
createdAt = LocalDateTime.now();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.flexwork.modules.collab.domain;

public enum EntityType {
Team_Request
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.flexwork.modules.collab.repository;

import io.flexwork.modules.collab.domain.Comment;
import io.flexwork.modules.collab.domain.EntityType;
import java.util.List;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findByEntityTypeAndEntityId(String entityType, Long entityId);
@EntityGraph(attributePaths = "createdBy")
List<Comment> findByEntityTypeAndEntityIdOrderByCreatedAtDesc(
EntityType entityType, Long entityId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.flexwork.modules.collab.service;

import io.flexwork.modules.collab.domain.Comment;
import io.flexwork.modules.collab.domain.EntityType;
import io.flexwork.modules.collab.repository.CommentRepository;
import io.flexwork.modules.collab.service.dto.CommentDTO;
import io.flexwork.modules.collab.service.mapper.CommentMapper;
import jakarta.transaction.Transactional;
import java.util.List;
import org.springframework.stereotype.Service;
Expand All @@ -12,12 +15,15 @@ public class CommentService {

private final CommentRepository commentRepository;

public CommentService(CommentRepository commentRepository) {
private final CommentMapper commentMapper;

public CommentService(CommentRepository commentRepository, CommentMapper commentMapper) {
this.commentRepository = commentRepository;
this.commentMapper = commentMapper;
}

public Comment saveComment(Comment comment) {
return commentRepository.save(comment);
public CommentDTO saveComment(CommentDTO comment) {
return commentMapper.toDTO(commentRepository.save(commentMapper.toEntity(comment)));
}

public Comment getCommentById(Long id) {
Expand All @@ -27,8 +33,12 @@ public Comment getCommentById(Long id) {
() -> new IllegalArgumentException("Comment not found with id: " + id));
}

public List<Comment> getCommentsForEntity(String entityType, Long entityId) {
return commentRepository.findByEntityTypeAndEntityId(entityType, entityId);
public List<CommentDTO> getCommentsForEntity(EntityType entityType, Long entityId) {
return commentRepository
.findByEntityTypeAndEntityIdOrderByCreatedAtDesc(entityType, entityId)
.stream()
.map(commentMapper::toDTO)
.toList();
}

public void deleteComment(Long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.flexwork.modules.collab.service.dto;

import io.flexwork.modules.collab.domain.EntityType;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class CommentDTO {

private Long id;
private String content;
private Long createdById;
private String createdByName;
private String createdByImageUrl;
private LocalDateTime createdAt;
private EntityType entityType;
private Long entityId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.flexwork.modules.collab.service.mapper;

import io.flexwork.modules.collab.domain.Comment;
import io.flexwork.modules.collab.service.dto.CommentDTO;
import io.flexwork.modules.usermanagement.domain.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface CommentMapper {
@Mapping(source = "createdBy.id", target = "createdById")
@Mapping(target = "createdByName", expression = "java(mapFullName(comment.getCreatedBy()))")
@Mapping(source = "createdBy.imageUrl", target = "createdByImageUrl")
CommentDTO toDTO(Comment comment);

@Mapping(source = "createdById", target = "createdBy.id")
Comment toEntity(CommentDTO commentDTO);

default String mapFullName(User user) {
if (user == null) {
return null;
}
return user.getFirstName() + " " + user.getLastName();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.flexwork.modules.collab.web.rest;

import io.flexwork.modules.collab.domain.Comment;
import io.flexwork.modules.collab.domain.EntityType;
import io.flexwork.modules.collab.service.CommentService;
import io.flexwork.modules.collab.service.dto.CommentDTO;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comments")
@RequestMapping("/api/comments")
public class CommentController {

private final CommentService commentService;
Expand All @@ -17,27 +19,24 @@ public CommentController(CommentService commentService) {
}

@PostMapping
public ResponseEntity<Comment> saveComment(@RequestBody Comment comment) {
Comment savedComment = commentService.saveComment(comment);
public ResponseEntity<CommentDTO> saveComment(@RequestBody CommentDTO comment) {
CommentDTO savedComment = commentService.saveComment(comment);
return ResponseEntity.ok(savedComment);
}

// Get a Comment by ID
@GetMapping("/{id}")
public ResponseEntity<Comment> getCommentById(@PathVariable Long id) {
Comment comment = commentService.getCommentById(id);
return ResponseEntity.ok(comment);
}

// Get Comments for an Entity
@GetMapping
public ResponseEntity<List<Comment>> getCommentsForEntity(
@RequestParam String entityType, @RequestParam Long entityId) {
List<Comment> comments = commentService.getCommentsForEntity(entityType, entityId);
public ResponseEntity<List<CommentDTO>> getCommentsForEntity(
@RequestParam EntityType entityType, @RequestParam Long entityId) {
List<CommentDTO> comments = commentService.getCommentsForEntity(entityType, entityId);
return ResponseEntity.ok(comments);
}

// Delete a Comment
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteComment(@PathVariable Long id) {
commentService.deleteComment(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Activity {

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private EntityType entityType;
private CrmEntityType crmEntityType;

@Column(nullable = false)
private Long entityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ActivityLog {

@Enumerated(EnumType.STRING)
@Column(name = "entity_type", nullable = false)
private EntityType entityType;
private CrmEntityType crmEntityType;

@Column(name = "entity_id", nullable = false)
private Long entityId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.flexwork.modules.crm.domain;

public enum EntityType {
public enum CrmEntityType {
ACCOUNT,
CONTACT,
OPPORTUNITY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface AccountMapper {
void updateFromDto(AccountDTO accountDTO, @MappingTarget Account account);

@Mapping(target = "id", ignore = true)
@Mapping(target = "entityType", constant = "ACCOUNT")
@Mapping(target = "crmEntityType", constant = "ACCOUNT")
@Mapping(target = "entityId", source = "account.id")
@Mapping(target = "user", source = "updatedUserId", qualifiedByName = "toUser")
ActivityLog accountEntityToActivityLog(Account account, Action action, Long updatedUserId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface TeamRequestRepository
extends JpaRepository<TeamRequest, Long>, JpaSpecificationExecutor<TeamRequest> {

@EntityGraph(attributePaths = {"team", "requestUser", "assignUser", "workflow"})
@Query("SELECT tr FROM TeamRequest tr")
Page<TeamRequest> findAllWithEagerRelationships(
Specification<TeamRequest> spec, Pageable pageable);
Page<TeamRequest> findAll(Specification<TeamRequest> spec, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.flexwork.modules.teams.service.mapper.TeamRequestMapper;
import io.flexwork.query.QueryDTO;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;
import org.jclouds.rest.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -42,11 +43,12 @@ public Page<TeamRequestDTO> getAllTeamRequests(Pageable pageable) {
return teamRequestRepository.findAll(pageable).map(teamRequestMapper::toDto);
}

public Page<TeamRequestDTO> findTeamRequests(Optional<QueryDTO> queryDTO, Pageable pageable) {
Specification<TeamRequest> spec = createSpecification(queryDTO);
return teamRequestRepository
.findAllWithEagerRelationships(spec, pageable)
.map(teamRequestMapper::toDto);
public Page<TeamRequestDTO> findTeamRequests(QueryDTO queryDTO, Pageable pageable) {
if (!hasTeamIdFilter(queryDTO)) {
throw new ResourceNotFoundException("No team id found");
}
Specification<TeamRequest> spec = createSpecification(Optional.of(queryDTO));
return teamRequestRepository.findAll(spec, pageable).map(teamRequestMapper::toDto);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -93,4 +95,12 @@ public void deleteTeamRequest(Long id) {
}
teamRequestRepository.deleteById(id);
}

private static boolean hasTeamIdFilter(QueryDTO queryDTO) {
return queryDTO != null
&& queryDTO.getFilters() != null
&& queryDTO.getFilters().stream()
.filter(Objects::nonNull)
.anyMatch(filter -> "team.id".equals(filter.getField()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.flexwork.modules.teams.service.dto.TeamRequestDTO;
import io.flexwork.query.QueryDTO;
import jakarta.validation.Valid;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -36,7 +35,7 @@ public ResponseEntity<Page<TeamRequestDTO>> getAllTeamRequests(Pageable pageable

@PostMapping("/search")
public ResponseEntity<Page<TeamRequestDTO>> findTeamRequests(
@Valid @RequestBody Optional<QueryDTO> queryDTO, Pageable pageable) {
@Valid @RequestBody QueryDTO queryDTO, Pageable pageable) {
Page<TeamRequestDTO> teamRequests = teamRequestService.findTeamRequests(queryDTO, pageable);
return new ResponseEntity<>(teamRequests, HttpStatus.OK);
}
Expand Down
Loading