Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create team requests (#9)
Browse files Browse the repository at this point in the history
haiphucnguyen authored Nov 20, 2024
1 parent 9a47205 commit ce2c4f7
Showing 9 changed files with 219 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -6,44 +6,38 @@
import io.flexwork.modules.crm.service.dto.AccountDTO;
import io.flexwork.modules.usermanagement.domain.User;
import org.mapstruct.*;
import org.mapstruct.Named;

@Mapper(componentModel = "spring")
public interface AccountMapper {
// Mapping from entity to DTO

@Mapping(target = "parentAccountId", source = "parentAccount.id")
@Mapping(target = "parentAccountName", source = "parentAccount.name")
@Mapping(target = "assignedToUserId", source = "assignedToUser.id")
AccountDTO toDto(Account account);

// Mapping from DTO to entity
@Mapping(
target = "assignedToUser",
expression = "java(ofUser(accountDTO.getAssignedToUserId()))")
@Mapping(
target = "parentAccount",
expression = "java(ofParentAccount(accountDTO.getParentAccountId()))")
@Mapping(target = "assignedToUser", source = "assignedToUserId", qualifiedByName = "toUser")
@Mapping(target = "parentAccount", source = "parentAccountId", qualifiedByName = "toAccount")
Account toEntity(AccountDTO accountDTO);

@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
@Mapping(
target = "assignedToUser",
expression = "java(ofUser(accountDTO.getAssignedToUserId()))")
@Mapping(
target = "parentAccount",
expression = "java(ofParentAccount(accountDTO.getParentAccountId()))")
@Mapping(target = "assignedToUser", source = "assignedToUserId", qualifiedByName = "toUser")
@Mapping(target = "parentAccount", source = "parentAccountId", qualifiedByName = "toAccount")
void updateFromDto(AccountDTO accountDTO, @MappingTarget Account account);

@Mapping(target = "id", ignore = true)
@Mapping(target = "entityType", constant = "ACCOUNT")
@Mapping(target = "entityId", source = "account.id")
@Mapping(target = "user", expression = "java(ofUser(updatedUserId))")
@Mapping(target = "user", source = "updatedUserId", qualifiedByName = "toUser")
ActivityLog accountEntityToActivityLog(Account account, Action action, Long updatedUserId);

default User ofUser(Long userId) {
@Named("toUser")
default User toUser(Long userId) {
return (userId == null) ? null : User.builder().id(userId).build();
}

default Account ofParentAccount(Long parentAccountId) {
@Named("toAccount")
default Account toAccount(Long parentAccountId) {
return (parentAccountId == null) ? null : Account.builder().id(parentAccountId).build();
}
}
Original file line number Diff line number Diff line change
@@ -6,20 +6,26 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;

@Mapper(componentModel = "spring")
public interface ContactMapper {

@Mapping(source = "account.id", target = "accountId")
@Mapping(source = "account.name", target = "accountName")
ContactDTO toDto(Contact contact);

@Mapping(target = "account", expression = "java(ofAccount(contactDTO.getAccountId()))")
@Mapping(target = "account", source = "accountId", qualifiedByName = "toAccount")
Contact toEntity(ContactDTO contactDTO);

@Mapping(target = "account", expression = "java(ofAccount(contactDTO.getAccountId()))")
@Mapping(target = "account", source = "accountId", qualifiedByName = "toAccount")
void updateFromDto(ContactDTO contactDTO, @MappingTarget Contact contact);

default Account ofAccount(Long accountId) {
@Named("toAccount")
default Account toAccount(Long accountId) {
if (accountId == null) {
return null;
}
Account account = new Account();
account.setId(accountId);
return account;
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.flexwork.modules.teams.service;

import io.flexwork.modules.teams.domain.TeamRequest;
import io.flexwork.modules.teams.repository.TeamRequestRepository;
import io.flexwork.modules.teams.service.dto.TeamRequestDTO;
import io.flexwork.modules.teams.service.mapper.TeamRequestMapper;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import org.jclouds.rest.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TeamRequestService {

private final TeamRequestRepository teamRequestRepository;
private final TeamRequestMapper teamRequestMapper; // Assume a MapStruct mapper

@Autowired
public TeamRequestService(
TeamRequestRepository teamRequestRepository, TeamRequestMapper teamRequestMapper) {
this.teamRequestRepository = teamRequestRepository;
this.teamRequestMapper = teamRequestMapper;
}

@Transactional(readOnly = true)
public List<TeamRequestDTO> getAllTeamRequests() {
return teamRequestRepository.findAll().stream()
.map(teamRequestMapper::toDto)
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public TeamRequestDTO getTeamRequestById(Long id) {
TeamRequest teamRequest =
teamRequestRepository
.findById(id)
.orElseThrow(
() ->
new ResourceNotFoundException(
"TeamRequest not found with id: " + id));
return teamRequestMapper.toDto(teamRequest);
}

@Transactional
public TeamRequestDTO createTeamRequest(TeamRequestDTO teamRequestDTO) {
TeamRequest teamRequest = teamRequestMapper.toEntity(teamRequestDTO);
teamRequest.setCreatedDate(LocalDateTime.now()); // Example of setting default value
teamRequest = teamRequestRepository.save(teamRequest);
return teamRequestMapper.toDto(teamRequest);
}

@Transactional
public TeamRequestDTO updateTeamRequest(Long id, TeamRequestDTO teamRequestDTO) {
TeamRequest existingTeamRequest =
teamRequestRepository
.findById(id)
.orElseThrow(
() ->
new ResourceNotFoundException(
"TeamRequest not found with id: " + id));

teamRequestMapper.updateEntity(teamRequestDTO, existingTeamRequest); // Update fields
existingTeamRequest = teamRequestRepository.save(existingTeamRequest);
return teamRequestMapper.toDto(existingTeamRequest);
}

@Transactional
public void deleteTeamRequest(Long id) {
if (!teamRequestRepository.existsById(id)) {
throw new ResourceNotFoundException("TeamRequest not found with id: " + id);
}
teamRequestRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.flexwork.modules.teams.service.dto;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TeamRequestDTO {
private Long id;
private Long teamId;
private String teamName;
private Long workflowId;
private String workflowName;
private Long requestUserId;
private String requestUserName;
private Long assignUserId;
private String assignUserName;
private String requestTitle;
private String requestDescription;
private LocalDateTime createdDate;
private String currentState;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.flexwork.modules.teams.service.mapper;

import io.flexwork.modules.teams.domain.TeamRequest;
import io.flexwork.modules.teams.domain.Workflow;
import io.flexwork.modules.teams.service.dto.TeamRequestDTO;
import io.flexwork.modules.usermanagement.domain.Team;
import io.flexwork.modules.usermanagement.domain.User;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.mapstruct.NullValuePropertyMappingStrategy;

@Mapper(componentModel = "spring")
public interface TeamRequestMapper {

TeamRequestDTO toDto(TeamRequest teamRequest);

@Mapping(target = "team", source = "teamId", qualifiedByName = "toTeam")
@Mapping(target = "workflow", source = "workflowId", qualifiedByName = "toWorkflow")
@Mapping(target = "requestUser", source = "requestUserId", qualifiedByName = "toUser")
@Mapping(target = "assignUser", source = "assignUserId", qualifiedByName = "toUser")
TeamRequest toEntity(TeamRequestDTO teamRequestDTO);

@Named("toTeam")
default Team toTeam(Long teamId) {
return (teamId == null) ? null : Team.builder().id(teamId).build();
}

@Named("toWorkflow")
default Workflow toWorkflow(Long workflowId) {
return (workflowId == null) ? null : Workflow.builder().id(workflowId).build();
}

@Named("toUser")
default User toUser(Long userId) {
return (userId == null) ? null : User.builder().id(userId).build();
}

@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
void updateEntity(TeamRequestDTO dto, @MappingTarget TeamRequest entity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.flexwork.modules.teams.web.rest;

import io.flexwork.modules.teams.service.TeamRequestService;
import io.flexwork.modules.teams.service.dto.TeamRequestDTO;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/team-requests")
public class TeamRequestController {

private final TeamRequestService teamRequestService;

public TeamRequestController(TeamRequestService teamRequestService) {
this.teamRequestService = teamRequestService;
}

@GetMapping
public ResponseEntity<List<TeamRequestDTO>> getAllTeamRequests() {
return ResponseEntity.ok(teamRequestService.getAllTeamRequests());
}

@GetMapping("/{id}")
public ResponseEntity<TeamRequestDTO> getTeamRequestById(@PathVariable Long id) {
return ResponseEntity.ok(teamRequestService.getTeamRequestById(id));
}

@PostMapping
public ResponseEntity<TeamRequestDTO> createTeamRequest(
@RequestBody TeamRequestDTO teamRequestDTO) {
TeamRequestDTO createdTeamRequest = teamRequestService.createTeamRequest(teamRequestDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(createdTeamRequest);
}

@PutMapping("/{id}")
public ResponseEntity<TeamRequestDTO> updateTeamRequest(
@PathVariable Long id, @RequestBody TeamRequestDTO teamRequestDTO) {
return ResponseEntity.ok(teamRequestService.updateTeamRequest(id, teamRequestDTO));
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteTeamRequest(@PathVariable Long id) {
teamRequestService.deleteTeamRequest(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ List<User> findUsersNotInTeam(
@Query(
"SELECT new io.flexwork.modules.usermanagement.service.dto.UserWithTeamRoleDTO(u.id, u.email, u.firstName, u.lastName, u.timezone, u.imageUrl, u.title, ut.team.id, ut.role.name) "
+ "FROM User u JOIN u.userTeams ut WHERE ut.team.id = :teamId")
Page<UserWithTeamRoleDTO> findUsersByTeamId(@Param("teamId") Long teamId, Pageable pageable);
List<UserWithTeamRoleDTO> findUsersByTeamId(@Param("teamId") Long teamId);

/**
* Return the team role of user, return default value is 'Guest'
Original file line number Diff line number Diff line change
@@ -106,8 +106,8 @@ public List<TeamDTO> findAllTeamsByUserId(Long userId) {
return teamRepository.findAllTeamsByUserId(userId).stream().map(teamMapper::toDto).toList();
}

public Page<UserWithTeamRoleDTO> getUsersByTeam(Long teamId, Pageable pageable) {
return teamRepository.findUsersByTeamId(teamId, pageable);
public List<UserWithTeamRoleDTO> getUsersByTeam(Long teamId) {
return teamRepository.findUsersByTeamId(teamId);
}

@Transactional(readOnly = true)
Original file line number Diff line number Diff line change
@@ -126,9 +126,8 @@ public ResponseEntity<Page<TeamDTO>> findTeams(
}

@GetMapping("/{teamId}/members")
public ResponseEntity<Page<UserWithTeamRoleDTO>> findUsersByTeamId(
@PathVariable Long teamId, Pageable pageable) {
return new ResponseEntity<>(teamService.getUsersByTeam(teamId, pageable), HttpStatus.OK);
public ResponseEntity<List<UserWithTeamRoleDTO>> findUsersByTeamId(@PathVariable Long teamId) {
return new ResponseEntity<>(teamService.getUsersByTeam(teamId), HttpStatus.OK);
}

@GetMapping("/users/{userId}")

0 comments on commit ce2c4f7

Please sign in to comment.