Skip to content

Commit

Permalink
✨ [Feature] 전체일정 수정 API 작성 #1053 (#1085)
Browse files Browse the repository at this point in the history
Co-authored-by: wonie <wonies@github.com>
  • Loading branch information
wonies and wonie authored Dec 30, 2024
1 parent 608f83a commit b85ba43
Show file tree
Hide file tree
Showing 14 changed files with 553 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package gg.calendar.api.user.schedule.publicschedule.controller;


import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gg.auth.UserDto;
import gg.auth.argumentresolver.Login;
import gg.calendar.api.user.schedule.publicschedule.controller.request.PublicScheduleCreateReqDto;
import gg.calendar.api.user.schedule.publicschedule.controller.request.PublicScheduleUpdateReqDto;
import gg.calendar.api.user.schedule.publicschedule.controller.response.PublicScheduleUpdateResDto;
import gg.calendar.api.user.schedule.publicschedule.service.PublicScheduleService;
import gg.data.calendar.PublicSchedule;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,5 +33,13 @@ public ResponseEntity<Void> publicScheduleCreate(@RequestBody @Valid PublicSched
publicScheduleService.createPublicSchedule(req, userDto.getId());
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PutMapping("/{id}")
public ResponseEntity<PublicScheduleUpdateResDto> publicScheduleUpdate(@PathVariable Long id,
@RequestBody @Valid PublicScheduleUpdateReqDto req,
@Login @Parameter(hidden = true) UserDto userDto) {
PublicSchedule updateSchedule = publicScheduleService.updatePublicSchedule(id, req, userDto.getId());
return ResponseEntity.ok(PublicScheduleUpdateResDto.toDto(updateSchedule));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.time.LocalDateTime;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;
Expand All @@ -26,12 +28,18 @@ public class PublicScheduleCreateReqDto {
private EventTag eventTag;
private JobTag jobTag;
private TechTag techTag;
@NotNull
@NotBlank
private String author;
@NotNull

@NotBlank
@Size(max = 50, message = "제목은 50자이하로 입력해주세요.")
private String title;

@Size(max = 2000, message = "내용은 2000자이하로 입력해주세요.")
private String content;

private String link;

@NotNull
private LocalDateTime startTime;
@NotNull
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
package gg.calendar.api.user.schedule.publicschedule.controller.request;

import java.time.LocalDateTime;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;

import gg.data.calendar.type.DetailClassification;
import gg.data.calendar.type.EventTag;
import gg.data.calendar.type.JobTag;
import gg.data.calendar.type.ScheduleStatus;
import gg.data.calendar.type.TechTag;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PublicScheduleUpdateReqDto {
@NotNull
private DetailClassification classification;
private EventTag eventTag;
private JobTag jobTag;
private TechTag techTag;
@NotBlank
private String author;

@NotBlank
@Size(max = 50, message = "제목은 50자이하로 입력해주세요.")
private String title;

@Size(max = 2000, message = "내용은 2000자이하로 입력해주세요.")
private String content;
private String link;

@NotNull
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime startTime;

@NotNull
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime endTime;
private ScheduleStatus status;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
package gg.calendar.api.user.schedule.publicschedule.controller.response;

import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;
import gg.data.calendar.type.EventTag;
import gg.data.calendar.type.JobTag;
import gg.data.calendar.type.TechTag;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PublicScheduleUpdateResDto {
private Long id;
private DetailClassification classification;
private EventTag eventTag;
private JobTag jobTag;
private TechTag techTag;
private String author;
private String title;
private String content;
private String link;
private String startTime;
private String endTime;
private String status;

@Builder
private PublicScheduleUpdateResDto(Long id, DetailClassification classification, EventTag eventTag, JobTag jobTag,
TechTag techTag, String author, String title, String content, String link, String startTime, String endTime,
String status) {
this.id = id;
this.classification = classification;
this.eventTag = eventTag;
this.jobTag = jobTag;
this.techTag = techTag;
this.author = author;
this.title = title;
this.content = content;
this.link = link;
this.startTime = startTime;
this.endTime = endTime;
this.status = status;
}

public static PublicScheduleUpdateResDto toDto(PublicSchedule publicSchedule) {
return PublicScheduleUpdateResDto.builder()
.id(publicSchedule.getId())
.classification(publicSchedule.getClassification())
.eventTag(publicSchedule.getEventTag())
.jobTag(publicSchedule.getJobTag())
.techTag(publicSchedule.getTechTag())
.author(publicSchedule.getAuthor())
.title(publicSchedule.getTitle())
.content(publicSchedule.getContent())
.link(publicSchedule.getLink())
.startTime(publicSchedule.getStartTime().toString())
.endTime(publicSchedule.getEndTime().toString())
.status(publicSchedule.getStatus().name())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package gg.calendar.api.user.schedule.publicschedule.service;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import gg.calendar.api.user.schedule.publicschedule.controller.request.PublicScheduleCreateReqDto;
import gg.calendar.api.user.schedule.publicschedule.controller.request.PublicScheduleUpdateReqDto;
import gg.data.calendar.PublicSchedule;
import gg.data.user.User;
import gg.repo.calendar.PublicScheduleRepository;
import gg.repo.user.UserRepository;
import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.CustomRuntimeException;
import gg.utils.exception.custom.ForbiddenException;
import gg.utils.exception.custom.InvalidParameterException;
import gg.utils.exception.custom.NotExistException;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -23,12 +28,35 @@ public class PublicScheduleService {
public void createPublicSchedule(PublicScheduleCreateReqDto req, Long userId) {
User user = userRepository.getById(userId);
if (!user.getIntraId().equals(req.getAuthor())) {
throw new CustomRuntimeException(ErrorCode.CALENDAR_AUTHOR_NOT_MATCH);
}
if (req.getStartTime().isAfter(req.getEndTime())) {
throw new CustomRuntimeException(ErrorCode.CALENDAR_BEFORE_DATE);
throw new ForbiddenException(ErrorCode.CALENDAR_AUTHOR_NOT_MATCH);
}
validateTimeRange(req.getStartTime(), req.getEndTime());
PublicSchedule publicSchedule = PublicScheduleCreateReqDto.toEntity(user.getIntraId(), req);
publicScheduleRepository.save(publicSchedule);
}

@Transactional
public PublicSchedule updatePublicSchedule(Long scheduleId, PublicScheduleUpdateReqDto req, Long userId) {
User user = userRepository.getById(userId);
PublicSchedule existingSchedule = publicScheduleRepository.findById(scheduleId)
.orElseThrow(() -> new NotExistException(ErrorCode.PUBLIC_SCHEDULE_NOT_FOUND));
checkAuthor(existingSchedule.getAuthor(), user);
checkAuthor(req.getAuthor(), user);
validateTimeRange(req.getStartTime(), req.getEndTime());
existingSchedule.update(req.getClassification(), req.getEventTag(), req.getJobTag(), req.getTechTag(),
req.getTitle(), req.getContent(), req.getLink(), req.getStartTime(), req.getEndTime(), req.getStatus());
return existingSchedule;
}

private static void checkAuthor(String author, User user) {
if (!user.getIntraId().equals(author)) {
throw new ForbiddenException(ErrorCode.CALENDAR_AUTHOR_NOT_MATCH);
}
}

public void validateTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
if (endTime.isBefore(startTime)) {
throw new InvalidParameterException(ErrorCode.CALENDAR_BEFORE_DATE);
}
}
}
Loading

0 comments on commit b85ba43

Please sign in to comment.