Skip to content

Commit

Permalink
Merge pull request #54 from softeerbootcamp4th/feature/#35-rush-event…
Browse files Browse the repository at this point in the history
…-apply2

Feature/#35 rush event apply2
  • Loading branch information
k000927 authored Aug 7, 2024
2 parents cdbfebc + 3564124 commit 41fdc9e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -26,7 +27,16 @@ public ResponseEntity<RushEventListAndServerTimeResponse> getRushEventListAndSer
@GetMapping("/{eventId}/applied")
public ResponseEntity<Boolean> 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<Void> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public interface RushParticipantsRepository extends JpaRepository<RushParticipants, String> {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
16 changes: 5 additions & 11 deletions Server/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 41fdc9e

Please sign in to comment.