Skip to content

Commit

Permalink
Merge pull request #195 from Kim-s-Crew/dev
Browse files Browse the repository at this point in the history
팀 생성 & 일정 생성
  • Loading branch information
EUNCHAEv1006 authored Jun 3, 2024
2 parents 3edc127 + 842417f commit 54dbf48
Show file tree
Hide file tree
Showing 25 changed files with 459 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wercsmik.spaghetticodingclub.domain.schedule.controller;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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 wercsmik.spaghetticodingclub.domain.schedule.dto.SchedulerCreationRequestDTO;
import wercsmik.spaghetticodingclub.domain.schedule.dto.SchedulerCreationResponseDTO;
import wercsmik.spaghetticodingclub.domain.schedule.service.SchedulerService;
import wercsmik.spaghetticodingclub.global.common.CommonResponse;
import wercsmik.spaghetticodingclub.global.security.UserDetailsImpl;

@RestController
@AllArgsConstructor
@RequestMapping("/schedules")
public class SchedulerController {

private final SchedulerService schedulerService;

@PostMapping
public ResponseEntity<CommonResponse<SchedulerCreationResponseDTO>> createSchedule(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody SchedulerCreationRequestDTO requestDTO) {

SchedulerCreationResponseDTO schedulerResponseDTO = schedulerService.createSchedule(userDetails, requestDTO);

return ResponseEntity.status(HttpStatus.CREATED)
.body(CommonResponse.of("일정 생성 성공", schedulerResponseDTO));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package wercsmik.spaghetticodingclub.domain.schedule.dto;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SchedulerCreationRequestDTO {

private String title;

private LocalDateTime startTime;

private LocalDateTime endTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package wercsmik.spaghetticodingclub.domain.schedule.dto;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import wercsmik.spaghetticodingclub.domain.schedule.entity.Scheduler;

@Getter
@Setter
@AllArgsConstructor
public class SchedulerCreationResponseDTO {

private Long schedulerId;

private Long userId;

private String title;

private LocalDateTime startTime;

private LocalDateTime endTime;

private LocalDateTime createdAt;

private LocalDateTime modifiedAt;

public static SchedulerCreationResponseDTO of(Scheduler scheduler) {
return new SchedulerCreationResponseDTO(
scheduler.getSchedulerId(),
scheduler.getUserId().getUserId(),
scheduler.getTitle(),
scheduler.getStartTime(),
scheduler.getEndTime(),
scheduler.getCreatedAt(),
scheduler.getModifiedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package wercsmik.spaghetticodingclub.domain.schedule.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import wercsmik.spaghetticodingclub.domain.user.entity.User;
import wercsmik.spaghetticodingclub.global.auditing.BaseTimeEntity;

@Entity
@Getter
@Builder
@AllArgsConstructor
@RequiredArgsConstructor
public class Scheduler extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long schedulerId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User userId;

@Column
private String title;

@Column(nullable = false)
private LocalDateTime startTime;

@Column(nullable = false)
private LocalDateTime endTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package wercsmik.spaghetticodingclub.domain.schedule.repository;

import java.time.LocalDateTime;
import org.springframework.data.jpa.repository.JpaRepository;
import wercsmik.spaghetticodingclub.domain.schedule.entity.Scheduler;
import wercsmik.spaghetticodingclub.domain.user.entity.User;

public interface SchedulerRepository extends JpaRepository<Scheduler, Long> {

boolean existsByUserIdAndStartTimeLessThanAndEndTimeGreaterThan(User user, LocalDateTime endTime, LocalDateTime startTime);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package wercsmik.spaghetticodingclub.domain.schedule.service;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wercsmik.spaghetticodingclub.domain.schedule.dto.SchedulerCreationRequestDTO;
import wercsmik.spaghetticodingclub.domain.schedule.dto.SchedulerCreationResponseDTO;
import wercsmik.spaghetticodingclub.domain.schedule.entity.Scheduler;
import wercsmik.spaghetticodingclub.domain.schedule.repository.SchedulerRepository;
import wercsmik.spaghetticodingclub.domain.user.entity.User;
import wercsmik.spaghetticodingclub.global.exception.CustomException;
import wercsmik.spaghetticodingclub.global.exception.ErrorCode;
import wercsmik.spaghetticodingclub.global.security.UserDetailsImpl;

@Service
@AllArgsConstructor
public class SchedulerService {

private final SchedulerRepository schedulerRepository;

@Transactional
public SchedulerCreationResponseDTO createSchedule(UserDetailsImpl userDetails, SchedulerCreationRequestDTO requestDTO) {

if (userDetails == null) {
throw new CustomException(ErrorCode.USER_NOT_FOUND);
}

User user = userDetails.getUser();

LocalDateTime startTime = requestDTO.getStartTime();
LocalDateTime endTime = requestDTO.getEndTime();

// 종료 시간이 시작 시간보다 이전이면 예외 발생
if (endTime.isBefore(startTime)) {
throw new CustomException(ErrorCode.INVALID_DATE_RANGE);
}

// 사용자가 이미 일정이 겹치는지 확인
boolean hasOverlap = schedulerRepository.existsByUserIdAndStartTimeLessThanAndEndTimeGreaterThan(user, endTime, startTime);
if (hasOverlap) {
throw new CustomException(ErrorCode.SCHEDULE_OVERLAP);
}

Scheduler scheduler = Scheduler.builder()
.userId(user)
.title(requestDTO.getTitle())
.startTime(requestDTO.getStartTime())
.endTime(requestDTO.getEndTime())
.build();

Scheduler savedSchedule = schedulerRepository.save(scheduler);

return SchedulerCreationResponseDTO.of(savedSchedule);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package wercsmik.spaghetticodingclub.domain.team.controller;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import wercsmik.spaghetticodingclub.domain.team.dto.TeamCreationRequestDTO;
import wercsmik.spaghetticodingclub.domain.team.dto.TeamCreationResponseDTO;
import wercsmik.spaghetticodingclub.domain.team.service.TeamService;
import wercsmik.spaghetticodingclub.global.common.CommonResponse;

@RestController
@RequestMapping("/tracks/{trackId}/trackWeeks/{trackWeekId}/teams")
@AllArgsConstructor
public class TeamController {

private final TeamService teamService;

@PostMapping
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
public ResponseEntity<CommonResponse<TeamCreationResponseDTO>> createTeam(
@PathVariable Long trackId,
@PathVariable Long trackWeekId,
@RequestBody TeamCreationRequestDTO requestDTO) {

TeamCreationResponseDTO responseDTO = teamService.createTeam(trackId, trackWeekId, requestDTO);

return ResponseEntity.status(HttpStatus.CREATED)
.body(CommonResponse.of("팀 생성 성공", responseDTO));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package wercsmik.spaghetticodingclub.domain.team.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class TeamCreationRequestDTO {

private String teamName;

private List<Long> memberIds;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package wercsmik.spaghetticodingclub.domain.team.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;
import java.util.Map;

@Getter
@AllArgsConstructor
public class TeamCreationResponseDTO {

private Long teamId;

private String teamName;

private List<Map<String, Object>> members;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
package wercsmik.spaghetticodingclub.domain.team.entity;

public class Team {

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wercsmik.spaghetticodingclub.domain.track.entity.TrackWeek;
import wercsmik.spaghetticodingclub.domain.user.entity.User;
import wercsmik.spaghetticodingclub.global.auditing.BaseTimeEntity;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Team extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long teamId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "trackWeekId", nullable = false)
private TrackWeek trackWeek;

@Column(length = 50, nullable = false)
private String teamName;

@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, orphanRemoval = true)
private List<User> members = new ArrayList<>();

//TODO(은채): 팀리더 설정

public static class TeamBuilder {
public TeamBuilder members(List<User> members) {
if (this.members == null) {
this.members = new ArrayList<>();
}
this.members.addAll(members);
return this;
}
}
}
Loading

0 comments on commit 54dbf48

Please sign in to comment.