Skip to content

Commit

Permalink
DEAR-129: add team config getter
Browse files Browse the repository at this point in the history
  • Loading branch information
baurnick committed Jul 30, 2024
1 parent f9d0861 commit 245a2c6
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 12 deletions.
14 changes: 12 additions & 2 deletions database/01_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ CREATE TABLE team_member

CREATE TABLE team_config
(
id SERIAL,
work_kinds VARCHAR[],
id SERIAL,
happiness_survey BOOLEAN,
work_kind_survey BOOLEAN,
emotion_survey BOOLEAN,

PRIMARY KEY (id)
);

CREATE TABLE team_config_work_kinds
(
team_config_id INTEGER NOT NULL,
work_kind_id INTEGER NOT NULL,

FOREIGN KEY (team_config_id) REFERENCES team_config (id)
);

-- Add constraints to the tables
ALTER TABLE team
ADD CONSTRAINT fk_team_team_config FOREIGN KEY (config_id) REFERENCES team_config (id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.fhnw.deardevbackend.dto.CreateTeamDTO;
import ch.fhnw.deardevbackend.dto.JoinTeamDTO;
import ch.fhnw.deardevbackend.dto.TeamAndRoleDTO;
import ch.fhnw.deardevbackend.dto.TeamConfigDTO;
import ch.fhnw.deardevbackend.entities.Team;
import ch.fhnw.deardevbackend.services.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -31,6 +32,12 @@ public ResponseEntity<Team> createTeam(@RequestBody CreateTeamDTO request) {
return ResponseEntity.ok().body(createdTeam);
}

@GetMapping("/{teamId}/config")
public ResponseEntity<TeamConfigDTO> getTeamConfigByTeamId(@PathVariable Integer teamId) {
TeamConfigDTO config = teamService.getTeamConfigByTeamId(teamId);
return ResponseEntity.ok().body(config);
}

@PostMapping("/join")
public ResponseEntity<Team> joinTeam(@RequestBody JoinTeamDTO request) {
Team team = teamService.joinTeam(request);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/TeamConfigDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.fhnw.deardevbackend.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TeamConfigDTO {
private Integer id;
private String teamName;
private List<WorkKindDTO> workKinds;
private Boolean happinessSurvey;
private Boolean workKindSurvey;
private Boolean emotionSurvey;
}
15 changes: 15 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/WorkKindDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ch.fhnw.deardevbackend.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WorkKindDTO {
private Integer id;
private String name;
}
20 changes: 18 additions & 2 deletions src/main/java/ch/fhnw/deardevbackend/entities/TeamConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Entity
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -17,6 +19,20 @@ public class TeamConfig {
@Id
private Integer id;

@Column(name = "work_kinds")
private String workKinds;
@ElementCollection
@CollectionTable(name = "team_config_work_kinds", joinColumns = @JoinColumn(name = "team_config_id"))
@Column(name = "work_kind_id")
private List<Integer> workKindIds;

@Transient
private List<WorkKind> workKinds;

@Column(name = "happiness_survey")
private Boolean happinessSurvey;

@Column(name = "work_kind_survey")
private Boolean workKindSurvey;

@Column(name = "emotion_survey")
private Boolean emotionSurvey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WorkKind {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "name")
@Column(name = "name", nullable = false)
private String name;

@Column(name = "team_id")
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/mapper/TeamConfigMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ch.fhnw.deardevbackend.mapper;

import ch.fhnw.deardevbackend.dto.TeamConfigDTO;
import ch.fhnw.deardevbackend.dto.WorkKindDTO;
import ch.fhnw.deardevbackend.entities.Team;
import ch.fhnw.deardevbackend.entities.TeamConfig;
import ch.fhnw.deardevbackend.entities.WorkKind;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper(componentModel = "spring", unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE)
public interface TeamConfigMapper {
TeamConfigMapper INSTANCE = Mappers.getMapper(TeamConfigMapper.class);

@Mapping(source = "team.name", target = "teamName")
@Mapping(source = "config.id", target = "id")
@Mapping(source = "workKinds", target = "workKinds")
@Mapping(source = "config.happinessSurvey", target = "happinessSurvey")
@Mapping(source = "config.workKindSurvey", target = "workKindSurvey")
@Mapping(source = "config.emotionSurvey", target = "emotionSurvey")
TeamConfigDTO toTeamConfigDTO(TeamConfig config, Team team, List<WorkKindDTO> workKinds);

WorkKindDTO toWorkKindDTO(WorkKind workKind);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.fhnw.deardevbackend.repositories;

import ch.fhnw.deardevbackend.entities.TeamConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TeamConfigRepository extends JpaRepository<TeamConfig, Integer> {
}
41 changes: 35 additions & 6 deletions src/main/java/ch/fhnw/deardevbackend/services/TeamService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package ch.fhnw.deardevbackend.services;

import ch.fhnw.deardevbackend.controller.exceptions.YappiException;
import ch.fhnw.deardevbackend.dto.CreateTeamDTO;
import ch.fhnw.deardevbackend.dto.JoinTeamDTO;
import ch.fhnw.deardevbackend.dto.TeamAndRoleDTO;
import ch.fhnw.deardevbackend.entities.Role;
import ch.fhnw.deardevbackend.entities.Team;
import ch.fhnw.deardevbackend.entities.TeamMember;
import ch.fhnw.deardevbackend.dto.*;
import ch.fhnw.deardevbackend.entities.*;
import ch.fhnw.deardevbackend.mapper.CreateTeamMapper;
import ch.fhnw.deardevbackend.mapper.TeamAndRoleMapper;
import ch.fhnw.deardevbackend.mapper.TeamConfigMapper;
import ch.fhnw.deardevbackend.repositories.TeamConfigRepository;
import ch.fhnw.deardevbackend.repositories.TeamMemberRepository;
import ch.fhnw.deardevbackend.repositories.TeamRepository;
import ch.fhnw.deardevbackend.repositories.WorkKindRepository;
import ch.fhnw.deardevbackend.util.TeamCodeGenerator;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -30,9 +30,15 @@ public class TeamService {
@Autowired
private TeamMemberRepository teamMemberRepository;
@Autowired
private TeamConfigRepository teamConfigRepository;
@Autowired
private WorkKindRepository workKindRepository;
@Autowired
private CreateTeamMapper createTeamMapper;
@Autowired
private TeamAndRoleMapper teamAndRoleMapper;
@Autowired
private TeamConfigMapper teamConfigMapper;

public List<TeamAndRoleDTO> getTeamsAndRoleByUserId(Integer userId) {
List<TeamMember> teamMembers = teamMemberRepository.findByUserId(userId);
Expand All @@ -49,8 +55,18 @@ public List<TeamAndRoleDTO> getTeamsAndRoleByUserId(Integer userId) {
public Team createTeam(CreateTeamDTO teamDTO) {
String uniqueCode = generateUniqueTeamCode();

TeamConfig config = TeamConfig.builder()
.workKindIds(Arrays.asList(1, 2))
.happinessSurvey(true)
.workKindSurvey(true)
.emotionSurvey(true)
.build();

TeamConfig savedTeamConfig = teamConfigRepository.save(config);

Team team = createTeamMapper.toTeam(teamDTO);
team.setCode(uniqueCode);
team.setConfigId(savedTeamConfig.getId());

Team savedTeam = teamRepository.save(team);

Expand Down Expand Up @@ -84,6 +100,19 @@ public Team joinTeam(JoinTeamDTO joinTeamDTO) {
return team;
}

@Transactional(readOnly = true)
public TeamConfigDTO getTeamConfigByTeamId(Integer teamId) {
Team team = teamRepository.findById(teamId).orElseThrow(() -> new YappiException("Team not found with id: " + teamId));
Integer configId = team.getConfigId();
TeamConfig config = teamConfigRepository.findById(configId).orElseThrow(() -> new YappiException("Team config not found with id: " + configId));
List<WorkKind> workKinds = workKindRepository.findAllById(config.getWorkKindIds());
List<WorkKindDTO> workKindDTOs = workKinds.stream()
.map(teamConfigMapper::toWorkKindDTO)
.toList();

return teamConfigMapper.toTeamConfigDTO(config, team, workKindDTOs);
}

private String generateUniqueTeamCode() {
String code;
do {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring.application.name=dear-dev-backend
## Database Configuration
spring.datasource.url=jdbc:postgresql://yappi_db:5432/postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/yappi_db
spring.datasource.username=yappi_db_admin
spring.datasource.password=${DATABASE_PASSWORD}
spring.datasource.driver-class-name=org.postgresql.Driver
Expand Down

0 comments on commit 245a2c6

Please sign in to comment.