diff --git a/src/main/java/side/onetime/domain/Event.java b/src/main/java/side/onetime/domain/Event.java index 88a069a..f3ebf5d 100644 --- a/src/main/java/side/onetime/domain/Event.java +++ b/src/main/java/side/onetime/domain/Event.java @@ -24,10 +24,6 @@ public class Event extends BaseEntity { @Column(name = "events_uuid", columnDefinition = "BINARY(16)", unique = true) private UUID eventId; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "events_fk_users_id")) - private User user; - @Column(name = "title", nullable = false, length = 30) private String title; @@ -48,16 +44,11 @@ public class Event extends BaseEntity { private List schedules; @Builder - public Event(UUID eventId, User user, String title, String startTime, String endTime, Category category) { + public Event(UUID eventId, String title, String startTime, String endTime, Category category) { this.eventId = eventId; - this.user = user; this.title = title; this.startTime = startTime; this.endTime = endTime; this.category = category; } - - public void addUser(User user) { - this.user = user; - } } \ No newline at end of file diff --git a/src/main/java/side/onetime/domain/EventParticipation.java b/src/main/java/side/onetime/domain/EventParticipation.java new file mode 100644 index 0000000..2a7fc0d --- /dev/null +++ b/src/main/java/side/onetime/domain/EventParticipation.java @@ -0,0 +1,39 @@ +package side.onetime.domain; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import side.onetime.domain.enums.EventStatus; +import side.onetime.global.common.dao.BaseEntity; + +@Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +@Table(name = "event_participations") +public class EventParticipation extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "event_participations_id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "events_id", foreignKey = @ForeignKey(name = "event_participations_fk_events_id")) + private Event event; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "event_participations_fk_users_id")) + private User user; + + @Enumerated(EnumType.STRING) + @Column(name = "event_status", nullable = false) + private EventStatus eventStatus; + + @Builder + public EventParticipation(Event event, User user, EventStatus eventStatus) { + this.event = event; + this.user = user; + this.eventStatus = eventStatus; + } +} \ No newline at end of file diff --git a/src/main/java/side/onetime/domain/User.java b/src/main/java/side/onetime/domain/User.java index c7a9729..20f7091 100644 --- a/src/main/java/side/onetime/domain/User.java +++ b/src/main/java/side/onetime/domain/User.java @@ -34,9 +34,6 @@ public class User extends BaseEntity { @Column(name = "provider_id", nullable = false, length = 50) private String providerId; - @OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private List events; - @OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List selections; diff --git a/src/main/java/side/onetime/domain/enums/EventStatus.java b/src/main/java/side/onetime/domain/enums/EventStatus.java new file mode 100644 index 0000000..9f6328b --- /dev/null +++ b/src/main/java/side/onetime/domain/enums/EventStatus.java @@ -0,0 +1,6 @@ +package side.onetime.domain.enums; + +public enum EventStatus { + CREATOR, // 이벤트 생성자 + PARTICIPANT // 이벤트 참여자 +} \ No newline at end of file diff --git a/src/main/java/side/onetime/exception/GlobalExceptionHandler.java b/src/main/java/side/onetime/exception/GlobalExceptionHandler.java index 4e9f736..fa2a7d6 100644 --- a/src/main/java/side/onetime/exception/GlobalExceptionHandler.java +++ b/src/main/java/side/onetime/exception/GlobalExceptionHandler.java @@ -7,6 +7,8 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import side.onetime.global.common.ApiResponse; import side.onetime.global.common.code.BaseErrorCode; +import side.onetime.global.common.constant.ErrorStatus; +import side.onetime.global.common.dto.ErrorReasonDto; @RestControllerAdvice @Slf4j @@ -53,4 +55,27 @@ public ResponseEntity> handleTokenException(TokenExce TokenErrorResult errorResult = e.getTokenErrorResult(); return ApiResponse.onFailure(errorResult); } + + // AccessDeniedException 등 보안 관련 에러 처리 + @ExceptionHandler(SecurityException.class) + public ResponseEntity handleSecurityException(SecurityException e) { + log.error("SecurityException: {}", e.getMessage()); + return ResponseEntity.status(ErrorStatus._UNAUTHORIZED.getHttpStatus()) + .body(ErrorStatus._UNAUTHORIZED.getReasonHttpStatus()); + } + + // 기타 Exception 처리 + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception e) { + log.error("Exception: {}", e.getMessage()); + + if (e instanceof IllegalArgumentException) { + return ResponseEntity.status(ErrorStatus._BAD_REQUEST.getHttpStatus()) + .body(ErrorStatus._BAD_REQUEST.getReasonHttpStatus()); + } + + // 그 외 내부 서버 오류로 처리 + return ResponseEntity.status(ErrorStatus._INTERNAL_SERVER_ERROR.getHttpStatus()) + .body(ErrorStatus._INTERNAL_SERVER_ERROR.getReasonHttpStatus()); + } } \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/EventParticipationRepository.java b/src/main/java/side/onetime/repository/EventParticipationRepository.java new file mode 100644 index 0000000..2b51da6 --- /dev/null +++ b/src/main/java/side/onetime/repository/EventParticipationRepository.java @@ -0,0 +1,7 @@ +package side.onetime.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import side.onetime.domain.EventParticipation; + +public interface EventParticipationRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/UserRepository.java b/src/main/java/side/onetime/repository/UserRepository.java index fce5e2b..f22d4a1 100644 --- a/src/main/java/side/onetime/repository/UserRepository.java +++ b/src/main/java/side/onetime/repository/UserRepository.java @@ -7,6 +7,5 @@ public interface UserRepository extends JpaRepository { Optional findById(Long id); - User findByProviderId(String providerId); } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/EventService.java b/src/main/java/side/onetime/service/EventService.java index cc37c19..a3ea72d 100644 --- a/src/main/java/side/onetime/service/EventService.java +++ b/src/main/java/side/onetime/service/EventService.java @@ -4,12 +4,14 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import side.onetime.domain.*; +import side.onetime.domain.enums.EventStatus; import side.onetime.dto.EventDto; import side.onetime.exception.EventErrorResult; import side.onetime.exception.EventException; import side.onetime.exception.ScheduleErrorResult; import side.onetime.exception.ScheduleException; import side.onetime.global.common.constant.Category; +import side.onetime.repository.EventParticipationRepository; import side.onetime.repository.EventRepository; import side.onetime.repository.ScheduleRepository; import side.onetime.repository.SelectionRepository; @@ -25,6 +27,7 @@ public class EventService { private static final int MAX_MOST_POSSIBLE_TIMES_SIZE = 6; private final EventRepository eventRepository; + private final EventParticipationRepository eventParticipationRepository; private final ScheduleRepository scheduleRepository; private final SelectionRepository selectionRepository; private final JwtUtil jwtUtil; @@ -55,8 +58,13 @@ public EventDto.CreateEventResponse createEventForAnonymousUser(EventDto.CreateE public EventDto.CreateEventResponse createEventForAuthenticatedUser(EventDto.CreateEventRequest createEventRequest, String authorizationHeader) { User user = jwtUtil.getUserFromHeader(authorizationHeader); Event event = createEventRequest.to(); - event.addUser(user); + EventParticipation eventParticipation = EventParticipation.builder() + .user(user) + .event(event) + .eventStatus(EventStatus.CREATOR) + .build(); eventRepository.save(event); + eventParticipationRepository.save(eventParticipation); if (createEventRequest.getCategory().equals(Category.DATE)) { if (!isDateFormat(createEventRequest.getRanges().get(0))) { diff --git a/src/main/java/side/onetime/service/ScheduleService.java b/src/main/java/side/onetime/service/ScheduleService.java index 0998a74..abd82fc 100644 --- a/src/main/java/side/onetime/service/ScheduleService.java +++ b/src/main/java/side/onetime/service/ScheduleService.java @@ -6,10 +6,7 @@ import side.onetime.domain.*; import side.onetime.dto.ScheduleDto; import side.onetime.exception.*; -import side.onetime.repository.EventRepository; -import side.onetime.repository.MemberRepository; -import side.onetime.repository.ScheduleRepository; -import side.onetime.repository.SelectionRepository; +import side.onetime.repository.*; import side.onetime.util.JwtUtil; import java.util.*;