Skip to content

Commit

Permalink
DEAR-115: add join team endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
baurnick committed Jul 19, 2024
1 parent ede5cd7 commit fc25f56
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
3 changes: 2 additions & 1 deletion database/01_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ CREATE TABLE team_member

CONSTRAINT pk_team_member PRIMARY KEY (id),
CONSTRAINT fk_team_member_user FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_team_member_team FOREIGN KEY (team_id) REFERENCES team (id)
CONSTRAINT fk_team_member_team FOREIGN KEY (team_id) REFERENCES team (id),
CONSTRAINT uq_team_member UNIQUE (user_id, team_id, role)
);

CREATE TABLE team_config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
package ch.fhnw.deardevbackend.controller;

import ch.fhnw.deardevbackend.controller.exceptions.ErrorResponse;
import ch.fhnw.deardevbackend.controller.exceptions.YappiException;
import ch.fhnw.deardevbackend.dto.CreateTeamDTO;
import ch.fhnw.deardevbackend.dto.JoinTeamDTO;
import ch.fhnw.deardevbackend.entities.Team;
import ch.fhnw.deardevbackend.services.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("v1/")
@RequestMapping("v1/team")
public class TeamController {

@Autowired
private TeamService teamService;

@PostMapping("/team/create")
@PostMapping("/create")
public ResponseEntity<Team> createTeam(@RequestBody CreateTeamDTO request) {
Team createdTeam = teamService.createTeam(request);
return ResponseEntity.ok().body(createdTeam);
}

@PostMapping("/join")
public ResponseEntity<Team> joinTeam(@RequestBody JoinTeamDTO request) {
Team team = teamService.joinTeam(request);
return ResponseEntity.ok().body(team);
}

@ExceptionHandler(YappiException.class)
public ResponseEntity<ErrorResponse> handleYappiException(YappiException ex) {
return ResponseEntity.badRequest().body(new ErrorResponse(ex.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.fhnw.deardevbackend.controller.exceptions;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ErrorResponse {
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ch.fhnw.deardevbackend.controller.exceptions;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
public class YappiException extends RuntimeException {
private final String message;
}
9 changes: 9 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/JoinTeamDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.fhnw.deardevbackend.dto;

import lombok.Data;

@Data
public class JoinTeamDTO {
private String code;
private int userId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import ch.fhnw.deardevbackend.entities.Team;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface TeamRepository extends JpaRepository<Team, Integer> {

boolean existsByCode(String code);

Optional<Team> findByCode(String code);
}
22 changes: 22 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/services/TeamService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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.entities.Role;
import ch.fhnw.deardevbackend.entities.Team;
import ch.fhnw.deardevbackend.entities.TeamMember;
Expand All @@ -10,6 +12,7 @@
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;

Expand Down Expand Up @@ -44,6 +47,25 @@ public Team createTeam(CreateTeamDTO teamDTO) {
return savedTeam;
}

@Transactional
public Team joinTeam(JoinTeamDTO joinTeamDTO) {
Team team = teamRepository.findByCode(joinTeamDTO.getCode()).orElseThrow(() -> new YappiException("Team not found with code: " + joinTeamDTO.getCode()));

try {
TeamMember teamMember = TeamMember.builder()
.userId(joinTeamDTO.getUserId())
.teamId(team.getId())
.role(Role.MEMBER)
.active(true)
.build();

teamMemberRepository.save(teamMember);
} catch (DataIntegrityViolationException ex) {
throw new YappiException("User with id: " + joinTeamDTO.getUserId() + " is already a member of the team with id: " + team.getId());
}
return team;
}

private String generateUniqueTeamCode() {
String code;
do {
Expand Down

0 comments on commit fc25f56

Please sign in to comment.