diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/RushEventController.java b/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/RushEventController.java index 627a1f0e..82cae4fa 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/RushEventController.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/controller/eventController/RushEventController.java @@ -3,6 +3,7 @@ import JGS.CasperEvent.domain.event.dto.ResponseDto.RushEventListAndServerTimeResponse; import JGS.CasperEvent.domain.event.service.eventService.RushEventService; +import JGS.CasperEvent.global.entity.BaseUser; import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -26,7 +27,16 @@ public ResponseEntity getRushEventListAndSer @GetMapping("/{eventId}/applied") public ResponseEntity checkUserParticipationInRushEvent(HttpServletRequest httpServletRequest, @PathVariable("eventId") Long eventId) { - String userId = httpServletRequest.getAttribute("userId").toString(); - return ResponseEntity.ok(rushEventService.isExists(eventId, userId)); + BaseUser user = (BaseUser) httpServletRequest.getAttribute("user"); + return ResponseEntity.ok(rushEventService.isExists(eventId, user.getId())); + } + + // 밸런스 게임 응모 + @PostMapping("/{eventId}/options/{optionId}/apply") + public ResponseEntity applyRushEvent(HttpServletRequest httpServletRequest, @PathVariable("eventId") Long eventId, @PathVariable("optionId") int optionId) { + BaseUser user = (BaseUser) httpServletRequest.getAttribute("user"); + rushEventService.apply(user, eventId, optionId); + + return ResponseEntity.noContent().build(); } } diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/RushParticipants.java b/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/RushParticipants.java index 9d0bb1a8..f160d1ff 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/RushParticipants.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/entity/participants/RushParticipants.java @@ -2,23 +2,29 @@ import JGS.CasperEvent.domain.event.entity.event.RushEvent; import JGS.CasperEvent.global.entity.BaseUser; -import JGS.CasperEvent.global.enums.Role; -import jakarta.persistence.Entity; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; @Entity -public class RushParticipants extends BaseUser { - private int choice; +public class RushParticipants { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private int optionId; + @OneToOne + @JoinColumn(name = "base_user_id") + private BaseUser baseUser; + @ManyToOne @JoinColumn(name = "rush_event_id") private RushEvent rushEvent; - public RushParticipants(String id, Role role) { - super(id, role); + public RushParticipants(BaseUser baseUser, RushEvent rushEvent, int optionId) { + this.baseUser = baseUser; + this.rushEvent = rushEvent; + this.optionId = optionId; } public RushParticipants() { - super(); + } } diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/RushParticipantsRepository.java b/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/RushParticipantsRepository.java index 8bbeca70..4184c6a9 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/RushParticipantsRepository.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/repository/participantsRepository/RushParticipantsRepository.java @@ -10,6 +10,6 @@ public interface RushParticipantsRepository extends JpaRepository { @Query("SELECT CASE WHEN COUNT(rp) > 0 THEN TRUE ELSE FALSE END " + "FROM RushParticipants rp " + - "WHERE rp.rushEvent.rushEventId = :eventId AND rp.id = :userId") + "WHERE rp.rushEvent.rushEventId = :eventId AND rp.baseUser.id = :userId") boolean existsByRushEventIdAndUserId(@Param("eventId") Long eventId, @Param("userId") String userId); } diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java index 7d873b3e..013b011e 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java @@ -3,8 +3,13 @@ import JGS.CasperEvent.domain.event.dto.ResponseDto.GetRushEvent; import JGS.CasperEvent.domain.event.dto.ResponseDto.RushEventListAndServerTimeResponse; import JGS.CasperEvent.domain.event.entity.event.RushEvent; +import JGS.CasperEvent.domain.event.entity.participants.RushParticipants; import JGS.CasperEvent.domain.event.repository.eventRepository.RushEventRepository; import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository; +import JGS.CasperEvent.global.entity.BaseUser; +import JGS.CasperEvent.global.enums.CustomErrorCode; +import JGS.CasperEvent.global.error.exception.CustomException; +import JGS.CasperEvent.global.util.RepositoryErrorHandler; import org.springframework.stereotype.Service; import java.time.LocalDateTime; @@ -34,4 +39,17 @@ public RushEventListAndServerTimeResponse getAllRushEvents() { public boolean isExists(Long eventId, String userId) { return rushParticipantsRepository.existsByRushEventIdAndUserId(eventId, userId); } + + public void apply(BaseUser user, Long eventId, int optionId) { + if (isExists(eventId, user.getId())) { + throw new CustomException("이미 응모한 회원입니다.", CustomErrorCode.CONFLICT); + } + + // eventId 를 이용하여 rushEvent 를 꺼냄 + RushEvent rushEvent = RepositoryErrorHandler.findByIdOrElseThrow(rushEventRepository, eventId, CustomErrorCode.NO_RUSH_EVENT); + + // 새로운 RushParticipants 를 생성하여 DB 에 저장 + RushParticipants rushParticipants = new RushParticipants(user, rushEvent, optionId); + rushParticipantsRepository.save(rushParticipants); + } } diff --git a/Server/src/main/java/JGS/CasperEvent/global/entity/BaseUser.java b/Server/src/main/java/JGS/CasperEvent/global/entity/BaseUser.java index 698d64f5..debab0dc 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/entity/BaseUser.java +++ b/Server/src/main/java/JGS/CasperEvent/global/entity/BaseUser.java @@ -1,6 +1,7 @@ package JGS.CasperEvent.global.entity; import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants; +import JGS.CasperEvent.domain.event.entity.participants.RushParticipants; import JGS.CasperEvent.global.enums.Role; import jakarta.persistence.*; import lombok.Getter; @@ -13,9 +14,12 @@ public class BaseUser extends BaseEntity { String id; Role role; - @OneToOne(mappedBy = "baseUser", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToOne(mappedBy = "baseUser", cascade = CascadeType.ALL) private LotteryParticipants lotteryParticipants; + @OneToOne(mappedBy = "baseUser", cascade = CascadeType.ALL) + private RushParticipants rushParticipants; + public BaseUser(String id, Role role) { this.id = id; this.role = role; diff --git a/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java b/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java index eef0a59c..c3cc173a 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java +++ b/Server/src/main/java/JGS/CasperEvent/global/jwt/filter/JwtAuthorizationFilter.java @@ -56,7 +56,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha BaseUser user = getAuthenticateUser(token); verifyAuthorization(requestUri, user); log.info("값 : {}", user.getId()); - httpServletRequest.setAttribute("userId", user.getId()); + httpServletRequest.setAttribute("user", user); chain.doFilter(request, response); } catch (JsonParseException e) { log.error("JsonParseException"); diff --git a/Server/src/main/resources/application-local.yml b/Server/src/main/resources/application-local.yml index c0554dff..9fa337dd 100644 --- a/Server/src/main/resources/application-local.yml +++ b/Server/src/main/resources/application-local.yml @@ -1,21 +1,15 @@ spring: datasource: - url: jdbc:h2:mem:testdb - driver-class-name: org.h2.Driver - username: sa + url: "jdbc:mysql://localhost:3306/jgs?serverTimezone=UTC" + driver-class-name: com.mysql.cj.jdbc.Driver + username: root password: jpa: hibernate: - ddl-auto: update + ddl-auto: create-drop properties: hibernate: - dialect: org.hibernate.dialect.H2Dialect - h2: - console: - enabled: true - path: /h2 - settings: - web-allow-others: true + dialect: org.hibernate.dialect.MySQLDialect config: activate: on-profile: local