Skip to content

Commit

Permalink
Merge pull request #55 from onetime-with-members/feature/#54/get-all
Browse files Browse the repository at this point in the history
[feat] : 이벤트 생성 및 참여 시 이벤트 참여여부에 저장된다
  • Loading branch information
bbbang105 authored Oct 1, 2024
2 parents c91933f + 18c0ab3 commit 637d0cb
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 19 deletions.
11 changes: 1 addition & 10 deletions src/main/java/side/onetime/domain/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -48,16 +44,11 @@ public class Event extends BaseEntity {
private List<Schedule> 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;
}
}
39 changes: 39 additions & 0 deletions src/main/java/side/onetime/domain/EventParticipation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 0 additions & 3 deletions src/main/java/side/onetime/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event> events;

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

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/side/onetime/domain/enums/EventStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package side.onetime.domain.enums;

public enum EventStatus {
CREATOR, // 이벤트 생성자
PARTICIPANT // 이벤트 참여자
}
25 changes: 25 additions & 0 deletions src/main/java/side/onetime/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,4 +55,27 @@ public ResponseEntity<ApiResponse<BaseErrorCode>> handleTokenException(TokenExce
TokenErrorResult errorResult = e.getTokenErrorResult();
return ApiResponse.onFailure(errorResult);
}

// AccessDeniedException 등 보안 관련 에러 처리
@ExceptionHandler(SecurityException.class)
public ResponseEntity<ErrorReasonDto> handleSecurityException(SecurityException e) {
log.error("SecurityException: {}", e.getMessage());
return ResponseEntity.status(ErrorStatus._UNAUTHORIZED.getHttpStatus())
.body(ErrorStatus._UNAUTHORIZED.getReasonHttpStatus());
}

// 기타 Exception 처리
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorReasonDto> 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());
}
}
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.EventParticipation;

public interface EventParticipationRepository extends JpaRepository<EventParticipation,Long> {
}
1 change: 0 additions & 1 deletion src/main/java/side/onetime/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findById(Long id);

User findByProviderId(String providerId);
}
10 changes: 9 additions & 1 deletion src/main/java/side/onetime/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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))) {
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/side/onetime/service/ScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down

0 comments on commit 637d0cb

Please sign in to comment.