Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] : 유저는 마이페이지에서 고정 스케줄을 등록할 수 있다 #93

Merged
merged 6 commits into from
Oct 23, 2024
28 changes: 28 additions & 0 deletions src/main/java/side/onetime/controller/FixedEventController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package side.onetime.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import side.onetime.dto.fixedEvent.request.CreateFixedEventRequest;
import side.onetime.global.common.ApiResponse;
import side.onetime.global.common.status.SuccessStatus;
import side.onetime.service.FixedEventService;

@RestController
@RequestMapping("/api/v1/fixed-events")
@RequiredArgsConstructor
public class FixedEventController {
private final FixedEventService fixedEventService;

// 고정 이벤트 생성 및 고정 스케줄 등록 API
@PostMapping
public ResponseEntity<ApiResponse<Object>> createFixedEvent(
@RequestHeader("Authorization") String authorizationHeader,
@Valid @RequestBody CreateFixedEventRequest createFixedEventRequest) {

fixedEventService.createFixedEvent(authorizationHeader, createFixedEventRequest);

return ApiResponse.onSuccess(SuccessStatus._CREATED_FIXED_SCHEDULE);
}
}
45 changes: 45 additions & 0 deletions src/main/java/side/onetime/domain/FixedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package side.onetime.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import side.onetime.global.common.dao.BaseEntity;

import java.util.List;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(name = "fixed_events")
public class FixedEvent extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fixed_events_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "fixed_events_fk_users_id"))
private User user;

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

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

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

@OneToMany(mappedBy = "fixedEvent",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<FixedSelection> fixedSelections;

@Builder
public FixedEvent(User user, String title, String startTime, String endTime) {
this.user = user;
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
}
}
36 changes: 36 additions & 0 deletions src/main/java/side/onetime/domain/FixedSchedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package side.onetime.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import side.onetime.global.common.dao.BaseEntity;

import java.util.List;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(name = "fixed_schedules")
public class FixedSchedule extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fixed_schedules_id")
private Long id;

@Column(name = "day", length = 10)
private String day;

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

@OneToMany(mappedBy = "fixedSchedule",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<FixedSelection> fixedSelections;

@Builder
public FixedSchedule(String day, String time) {
this.day = day;
this.time = time;
}
}
33 changes: 33 additions & 0 deletions src/main/java/side/onetime/domain/FixedSelection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package side.onetime.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import side.onetime.global.common.dao.BaseEntity;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(name = "fixed_selections")
public class FixedSelection extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fixed_selections_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fixed_events_id", foreignKey = @ForeignKey(name = "fixed_selections_fk_fixed_events_id"))
private FixedEvent fixedEvent;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fixed_schedules_id", foreignKey = @ForeignKey(name = "fixed_selections_fk_fixed_schedules_id"))
private FixedSchedule fixedSchedule;

@Builder
public FixedSelection(FixedEvent fixedEvent, FixedSchedule fixedSchedule) {
this.fixedEvent = fixedEvent;
this.fixedSchedule = fixedSchedule;
}
}
3 changes: 3 additions & 0 deletions src/main/java/side/onetime/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class User extends BaseEntity {
@OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<EventParticipation> eventParticipations;

@OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<FixedEvent> fixedEvents;

@Builder
public User(String name, String email, String nickname, String provider, String providerId) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import side.onetime.domain.Schedule;

import java.time.LocalTime;
import java.util.List;

import static side.onetime.util.DateUtil.addThirtyMinutes;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record GetMostPossibleTime(
Expand All @@ -22,7 +23,7 @@ public static GetMostPossibleTime dayOf(Schedule schedule, List<String> possible
return new GetMostPossibleTime(
schedule.getDay(),
schedule.getTime(),
String.valueOf(LocalTime.parse(schedule.getTime()).plusMinutes(30)),
addThirtyMinutes(schedule.getTime()),
possibleNames.size(),
possibleNames,
impossibleNames
Expand All @@ -33,18 +34,18 @@ public static GetMostPossibleTime dateOf(Schedule schedule, List<String> possibl
return new GetMostPossibleTime(
schedule.getDate(),
schedule.getTime(),
String.valueOf(LocalTime.parse(schedule.getTime()).plusMinutes(30)),
addThirtyMinutes(schedule.getTime()),
possibleNames.size(),
possibleNames,
impossibleNames
);
}

public GetMostPossibleTime updateEndTime(String endTime) {
return new GetMostPossibleTime(
public void updateEndTime(String endTime) {
new GetMostPossibleTime(
this.timePoint,
this.startTime,
String.valueOf(LocalTime.parse(endTime).plusMinutes(30)),
addThirtyMinutes(endTime),
this.possibleCount,
this.possibleNames,
this.impossibleNames
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package side.onetime.dto.fixedEvent.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;
import side.onetime.dto.fixedEvent.response.FixedScheduleResponse;

import java.util.List;

import static side.onetime.util.DateUtil.addThirtyMinutes;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record CreateFixedEventRequest(
@NotBlank(message = "제목은 필수 값입니다.") String title,
@NotNull(message = "스케줄 목록은 필수 값입니다.") List<FixedScheduleResponse> schedules
) {
public FixedEvent toEntity(User user, String startTime, String endTime) {
return FixedEvent.builder()
.user(user)
.title(title)
.startTime(startTime)
.endTime(addThirtyMinutes(endTime))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package side.onetime.dto.fixedEvent.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import java.util.List;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record FixedScheduleResponse(
String timePoint,
List<String> times
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package side.onetime.exception.status;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import side.onetime.global.common.code.BaseErrorCode;
import side.onetime.global.common.dto.ErrorReasonDto;

@Getter
@RequiredArgsConstructor
public enum FixedScheduleErrorStatus implements BaseErrorCode {
_NOT_FOUND_FIXED_SCHEDULES(HttpStatus.NOT_FOUND, "FIXED-SCHEDULE-001", "스케줄을 가져오는 데 실패했습니다."),
;

private final HttpStatus httpStatus;
private final String code;
private final String message;

@Override
public ErrorReasonDto getReason() {
return ErrorReasonDto.builder()
.isSuccess(false)
.code(code)
.message(message)
.build();
}

@Override
public ErrorReasonDto getReasonHttpStatus() {
return ErrorReasonDto.builder()
.isSuccess(false)
.httpStatus(httpStatus)
.code(code)
.message(message)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public enum SuccessStatus implements BaseCode {
_GET_USER_PROFILE(HttpStatus.OK, "200", "유저 정보 조회에 성공했습니다."),
_UPDATE_USER_PROFILE(HttpStatus.OK, "200", "유저 정보 수정에 성공했습니다."),
_WITHDRAW_SERVICE(HttpStatus.OK, "200", "유저 서비스 탈퇴에 성공했습니다."),
// FixedEvent
_CREATED_FIXED_SCHEDULE(HttpStatus.CREATED, "201", "고정 스케줄 등록에 성공했습니다."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package side.onetime.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import side.onetime.domain.FixedEvent;

public interface FixedEventRepository extends JpaRepository<FixedEvent, Long> {
}
11 changes: 11 additions & 0 deletions src/main/java/side/onetime/repository/FixedScheduleRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package side.onetime.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import side.onetime.domain.FixedSchedule;

import java.util.List;
import java.util.Optional;

public interface FixedScheduleRepository extends JpaRepository<FixedSchedule, Long> {
Optional<List<FixedSchedule>> findAllByDay(String day);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package side.onetime.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import side.onetime.domain.FixedSelection;

public interface FixedSelectionRepository extends JpaRepository<FixedSelection, Long> {
}
33 changes: 33 additions & 0 deletions src/main/java/side/onetime/service/FixedEventService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package side.onetime.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;
import side.onetime.dto.fixedEvent.request.CreateFixedEventRequest;
import side.onetime.repository.FixedEventRepository;
import side.onetime.util.JwtUtil;

import java.util.List;


@Service
@RequiredArgsConstructor
public class FixedEventService {
private final FixedScheduleService fixedScheduleService;
private final FixedEventRepository fixedEventRepository;
private final JwtUtil jwtUtil;

// 고정 이벤트 생성 메서드
@Transactional
public void createFixedEvent(String authorizationHeader, CreateFixedEventRequest createFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
List<String> times = createFixedEventRequest.schedules().get(0).times();
String startTime = times.get(0);
String endTime = times.get(times.size() - 1);
FixedEvent fixedEvent = createFixedEventRequest.toEntity(user, startTime, endTime);
fixedEventRepository.save(fixedEvent);
fixedScheduleService.createFixedSchedules(createFixedEventRequest, fixedEvent);
}
}
Loading
Loading