Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/DEAR-115' into DEAR-120-survey-d…
Browse files Browse the repository at this point in the history
…atamodel-1
  • Loading branch information
smuefsmuef committed Jul 18, 2024
2 parents 3429fee + ec67be7 commit bf40da3
Show file tree
Hide file tree
Showing 21 changed files with 460 additions and 15 deletions.
84 changes: 84 additions & 0 deletions database/01_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
-- Creat tables for PG-Adapter Auth.js
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE verification_token
(
identifier TEXT NOT NULL,
expires TIMESTAMPTZ NOT NULL,
token TEXT NOT NULL,

PRIMARY KEY (identifier, token)
);

CREATE TABLE accounts
(
id SERIAL,
"userId" INTEGER NOT NULL,
type VARCHAR(255) NOT NULL,
provider VARCHAR(255) NOT NULL,
"providerAccountId" VARCHAR(255) NOT NULL,
refresh_token TEXT,
access_token TEXT,
expires_at BIGINT,
id_token TEXT,
scope TEXT,
session_state TEXT,
token_type TEXT,

PRIMARY KEY (id)
);

CREATE TABLE users
(
id SERIAL,
name VARCHAR(255),
email VARCHAR(255),
"emailVerified" TIMESTAMPTZ,
image TEXT,

PRIMARY KEY (id)
);

ALTER TABLE users
ADD COLUMN username VARCHAR(32);

-- Create tables for DEAR-115-TEAM
------------------------------------------------------------------------------------------------------------------------
CREATE TABLE team
(
id SERIAL,
name VARCHAR(32),
current_sprint_id INTEGER,
config_id INTEGER,
code VARCHAR(4) NOT NULL,
created_by INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
active BOOLEAN DEFAULT TRUE NOT NULL,

CONSTRAINT pk_team PRIMARY KEY (id)
);

CREATE TABLE team_member
(
id SERIAL,
user_id INTEGER NOT NULL,
team_id INTEGER NOT NULL,
role VARCHAR(8) NOT NULL,
active BOOLEAN DEFAULT TRUE NOT NULL,

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 uq_team_member UNIQUE (user_id, team_id, role)
);

CREATE TABLE team_config
(
id SERIAL,
work_kinds VARCHAR[],

PRIMARY KEY (id)
);

-- Add constraints to the tables
ALTER TABLE team
ADD CONSTRAINT fk_team_team_config FOREIGN KEY (config_id) REFERENCES team_config (id);
15 changes: 15 additions & 0 deletions database/02_test_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
INSERT INTO public.users (id, name, email, "emailVerified", image, username)
VALUES (1, 'Hans Müller', 'hans@test.com', null, null, 'Hansi');

INSERT INTO public.team (id, name, current_sprint_id, config_id, code, created_by, created_at, active)
VALUES (1, 'Team Alpha', null, null, 'ALF1', 1, '2024-07-13 15:19:11.164000 +00:00', true);
INSERT INTO public.team (id, name, current_sprint_id, config_id, code, created_by, created_at, active)
VALUES (2, 'Team Beta', null, null, 'BET2', 1, '2024-07-13 15:20:22.162000 +00:00', true);

INSERT INTO public.team_member (id, user_id, team_id, role, active)
VALUES (1, 1, 1, 'ADMIN', true);
INSERT INTO public.team_member (id, user_id, team_id, role, active)
VALUES (2, 1, 2, 'MEMBER', true);

INSERT INTO public.team_config (id, work_kinds)
VALUES (1, '{CODING,MEETING}');
Original file line number Diff line number Diff line change
@@ -0,0 +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.*;

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

@Autowired
private TeamService teamService;

@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
Expand Up @@ -23,7 +23,7 @@ public ResponseEntity<List<User>> getAllUsers() {
}

@GetMapping("/user/{id}")
public ResponseEntity<User> getUserById(@PathVariable Integer id) {
public ResponseEntity<UserDTO> getUserById(@PathVariable Integer id) {
return ResponseEntity.ok().body(userService.getUserById(id));
}

Expand Down
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/CreateTeamDTO.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 CreateTeamDTO {
private String name;
private int userId;
}
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;
}
4 changes: 4 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
@Data
public class UserDTO {
private int id;
private String name;
private String email;
private String loginProvider;
private String username;
private Boolean hasTeam;
}
6 changes: 6 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/entities/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ch.fhnw.deardevbackend.entities;

public enum Role {
ADMIN,
MEMBER
}
42 changes: 42 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/entities/Team.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ch.fhnw.deardevbackend.entities;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.OffsetDateTime;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "team")
@Builder
@Data
public class Team {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private int id;

@Column(name = "name")
private String name;

@Column(name = "current_sprint_id")
private Integer currentSprintId;

@Column(name = "config_id")
private Integer configId;

@Column(name = "code")
private String code;

@Column(name = "created_by")
private Integer createdBy;

@Column(name = "created_at")
private OffsetDateTime createdAt;

@Column(name = "active")
private boolean active;
}
22 changes: 22 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/entities/TeamConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ch.fhnw.deardevbackend.entities;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "team_config")
@Builder
@Data
public class TeamConfig {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private int id;

@Column(name = "work_kinds")
private String workKinds;
}
32 changes: 32 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/entities/TeamMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch.fhnw.deardevbackend.entities;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "team_member")
@Builder
@Data
public class TeamMember {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private int id;

@Column(name = "user_id")
private int userId;

@Column(name = "team_id")
private int teamId;

@Enumerated(EnumType.STRING)
@Column(name = "role")
private Role role;

@Column(name = "active")
private boolean active;
}
20 changes: 20 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/mapper/CreateTeamMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ch.fhnw.deardevbackend.mapper;

import ch.fhnw.deardevbackend.dto.CreateTeamDTO;
import ch.fhnw.deardevbackend.entities.Team;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

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

@Mapping(target = "currentSprintId", ignore = true)
@Mapping(target = "configId", ignore = true)
@Mapping(target = "createdBy", source = "userId")
@Mapping(target = "createdAt", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "active", constant = "true")
Team toTeam(CreateTeamDTO createTeamDTO);
}
21 changes: 21 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.fhnw.deardevbackend.mapper;

import ch.fhnw.deardevbackend.dto.UserDTO;
import ch.fhnw.deardevbackend.entities.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

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

@Mapping(source = "user.id", target = "id")
@Mapping(source = "user.name", target = "name")
@Mapping(source = "user.email", target = "email")
@Mapping(source = "user.username", target = "username")
@Mapping(source = "provider", target = "loginProvider")
@Mapping(source = "hasTeam", target = "hasTeam")
UserDTO toDto(User user, String provider, Boolean hasTeam);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ch.fhnw.deardevbackend.repositories;

import ch.fhnw.deardevbackend.entities.TeamMember;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface TeamMemberRepository extends JpaRepository<TeamMember, Integer> {
@Query(
value = "select count(*) > 0 from team_member where user_id =:userId",
nativeQuery = true)
Boolean userIsInTeam(@Param("userId") Integer userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ch.fhnw.deardevbackend.repositories;

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);
}
Loading

0 comments on commit bf40da3

Please sign in to comment.