Skip to content

Commit

Permalink
Merge pull request #53 from softeerbootcamp4th/feat/#49-api
Browse files Browse the repository at this point in the history
Feat/#49 api
  • Loading branch information
wjddn2165 authored Aug 6, 2024
2 parents fa62f5c + f780f60 commit cdbfebc
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 123 deletions.
3 changes: 3 additions & 0 deletions Server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'org.apache.commons:commons-dbcp2:2.9.0'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
Expand All @@ -39,6 +40,8 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.projectlombok:lombok'
annotationProcessor('org.projectlombok:lombok')

implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package JGS.CasperEvent.domain.event.controller.eventController;

import JGS.CasperEvent.domain.event.dto.RequestDto.PostCasperBot;
import JGS.CasperEvent.domain.event.dto.ResponseDto.GetCasperBot;
import JGS.CasperEvent.domain.event.dto.ResponseDto.GetLotteryParticipant;
import JGS.CasperEvent.domain.event.service.RedisService.RedisService;
import JGS.CasperEvent.domain.event.service.eventService.LotteryEventService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -26,21 +29,23 @@ public LotteryEventController(LotteryEventService lotteryEventService, RedisServ
}

// 캐스퍼 봇 생성 API
//TODO: 쿠키 이름 정하기 (프론트랑 협의)
@PostMapping
public ResponseEntity<GetCasperBot> postCasperBot(@CookieValue String userData,
@RequestBody String body) {
public ResponseEntity<GetCasperBot> postCasperBot(
HttpServletRequest request,
@RequestBody @Valid PostCasperBot postCasperBot) {
String userId = request.getAttribute("userId").toString();
return ResponseEntity
.status(HttpStatus.CREATED)
.body(lotteryEventService.postCasperBot(userData, body));
.body(lotteryEventService.postCasperBot(userId, postCasperBot));
}

// 응모 여부 조회 API
@GetMapping("/applied")
public ResponseEntity<GetLotteryParticipant> GetLotteryParticipant(@CookieValue String userData) throws UserPrincipalNotFoundException {
public ResponseEntity<GetLotteryParticipant> GetLotteryParticipant(HttpServletRequest request) throws UserPrincipalNotFoundException {
String userId = request.getAttribute("userId").toString();
return ResponseEntity
.status(HttpStatus.OK)
.body(lotteryEventService.getLotteryParticipant(userData));
.body(lotteryEventService.getLotteryParticipant(userId));
}

// 최근 100개 캐스퍼 봇 조회
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package JGS.CasperEvent.domain.event.dto.RequestDto;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

public class PostCasperBot {

@NotNull(message = "눈 모양 값은 필수 필드입니다.")
@Min(value = 0, message = "눈 모양 값이 부적절합니다.")
@Max(value = 7, message = "눈 모양 값이 부적절합니다.")
private Integer eyeShape;

@NotNull(message = "눈 위치 값은 필수 필드입니다.")
@Min(value = 0, message = "눈 위치 값이 부적절합니다.")
@Max(value = 2, message = "눈 위치 값이 부적절합니다.")
private Integer eyePosition;

@NotNull(message = "입 모양 값은 필수 필드입니다.")
@Min(value = 0, message = "입 모양 값이 부적절합니다.")
@Max(value = 4, message = "입 모양 값이 부적절합니다.")
private Integer mouthShape;

@NotNull(message = "색깔 값은 필수 필드입니다.")
@Min(value = 0, message = "색깔 값이 부적절합니다.")
@Max(value = 17, message = "색깔 값이 부적절합니다.")
private Integer color;

@NotNull(message = "스티커 값은 필수 필드입니다.")
@Min(value = 0, message = "스티커 값이 부적절합니다.")
@Max(value = 4, message = "스티커 값이 부적절합니다.")
private Integer sticker;

@NotNull(message = "이름은 필수 필드입니다. ")
private String name;

private String expectation;


public Integer getEyeShape() {
return eyeShape;
}

public Integer getEyePosition() {
return eyePosition;
}

public Integer getMouthShape() {
return mouthShape;
}

public Integer getColor() {
return color;
}

public Integer getSticker() {
return sticker;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getExpectation() {
return expectation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.time.LocalDateTime;


public record GetLotteryParticipant(int linkClickedCount, int expectations, int appliedCount,
public record GetLotteryParticipant(int linkClickedCount, int expectations, int appliedCount, GetCasperBot casperBot,
LocalDateTime createdAt, LocalDateTime updatedAt) {
public static GetLotteryParticipant of(LotteryParticipants lotteryParticipants) {
public static GetLotteryParticipant of(LotteryParticipants lotteryParticipants, GetCasperBot getcasperBot) {
return new GetLotteryParticipant(
lotteryParticipants.getLinkClickedCount(),
lotteryParticipants.getExpectations(),
lotteryParticipants.getAppliedCount(),
lotteryParticipants.getCreatedAt(),
lotteryParticipants.getUpdatedAt()
getcasperBot,
lotteryParticipants.getBaseUser().getCreatedAt(),
lotteryParticipants.getBaseUser().getUpdatedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package JGS.CasperEvent.domain.event.entity.casperBot;

import JGS.CasperEvent.domain.event.dto.RequestDto.PostCasperBot;
import JGS.CasperEvent.global.entity.BaseEntity;
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.util.UserUtil;
import jakarta.persistence.*;


@Entity
public class CasperBot extends BaseEntity {
public CasperBot(int eyeShape, int eyePosition, int mouthShape, int color, int sticker, String name, String expectation,String phoneNumber) {
public CasperBot(PostCasperBot postCasperBot, String phoneNumber) {
this.casperId = UserUtil.generateId();
this.phoneNumber = phoneNumber;
this.eyeShape = eyeShape;
this.eyePosition = eyePosition;
this.mouthShape = mouthShape;
this.color = color;
this.sticker = sticker;
this.name = name;
this.expectation = expectation;
this.eyeShape = postCasperBot.getEyeShape();
this.eyePosition = postCasperBot.getEyePosition();
this.mouthShape = postCasperBot.getMouthShape();
this.color = postCasperBot.getColor();
this.sticker = postCasperBot.getSticker();
this.name = postCasperBot.getName();
this.expectation = postCasperBot.getExpectation();
}

@Id
Expand Down Expand Up @@ -72,24 +72,6 @@ public String getExpectation() {
return expectation;
}

public void validateEnumFields() throws CustomException {
// if (eyeShape == null) {
// throw new CustomException("eyeShape cannot be null", CustomErrorCode.INVALID_CASPERBOT_PARAMETER);
// }
// if (eyePosition == null) {
// throw new CustomException("EyePosition cannot be null", CustomErrorCode.INVALID_CASPERBOT_PARAMETER);
// }
// if (mouthShape == null) {
// throw new CustomException("MouthShape cannot be null", CustomErrorCode.INVALID_CASPERBOT_PARAMETER);
// }
// if (color == null) {
// throw new CustomException("Color cannot be null", CustomErrorCode.INVALID_CASPERBOT_PARAMETER);
// }
// if (sticker == null) {
// throw new CustomException("Sticker cannot be null", CustomErrorCode.INVALID_CASPERBOT_PARAMETER);
// }
}

@Override
public String toString() {
return "CasperBot{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package JGS.CasperEvent.domain.event.entity.participants;

import JGS.CasperEvent.global.entity.BaseUser;
import JGS.CasperEvent.global.enums.Role;
import jakarta.persistence.Entity;
import jakarta.persistence.*;
import lombok.Getter;

@Getter
@Entity
public class LotteryParticipants extends BaseUser {
public class LotteryParticipants {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne
@JoinColumn(name = "base_user_id")
private BaseUser baseUser;

private int linkClickedCount;
private int expectations;
private int appliedCount;

public String getPhoneNumber() {
return getId();
private Long casperId;

public void updateCasperId(Long casperId){
this.casperId = casperId;
}

public LotteryParticipants() {
Expand All @@ -24,8 +33,8 @@ public void expectationAdded(){
expectations++;
}

public LotteryParticipants(String phoneNumber) {
super(phoneNumber, Role.USER);
public LotteryParticipants(BaseUser baseUser) {
this.baseUser = baseUser;
this.appliedCount = 1;
this.linkClickedCount = 0;
this.expectations = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package JGS.CasperEvent.domain.event.repository.participantsRepository;

import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
import JGS.CasperEvent.global.entity.BaseUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface LotteryParticipantsRepository extends JpaRepository<LotteryParticipants, String> {
Optional<LotteryParticipants> findByBaseUser(BaseUser baseUser);
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package JGS.CasperEvent.domain.event.service.eventService;

import JGS.CasperEvent.domain.event.dto.RequestDto.PostCasperBot;
import JGS.CasperEvent.domain.event.dto.ResponseDto.GetCasperBot;
import JGS.CasperEvent.domain.event.dto.ResponseDto.GetLotteryParticipant;
import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;
Expand All @@ -8,22 +9,21 @@
import JGS.CasperEvent.domain.event.repository.eventRepository.LotteryEventRepository;
import JGS.CasperEvent.domain.event.repository.participantsRepository.LotteryParticipantsRepository;
import JGS.CasperEvent.domain.event.service.RedisService.RedisService;
import JGS.CasperEvent.global.entity.BaseUser;
import JGS.CasperEvent.global.enums.CustomErrorCode;
import JGS.CasperEvent.global.error.exception.CustomException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import JGS.CasperEvent.global.jwt.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.attribute.UserPrincipalNotFoundException;

import static JGS.CasperEvent.global.util.UserUtil.getDecodedPhoneNumber;

@Service
@Transactional
public class LotteryEventService {

private final UserRepository userRepository;
private LotteryEventRepository lotteryEventRepository;
private LotteryParticipantsRepository lotteryParticipantsRepository;
private CasperBotRepository casperBotRepository;
Expand All @@ -33,30 +33,18 @@ public class LotteryEventService {
public LotteryEventService(LotteryEventRepository lotteryEventRepository,
LotteryParticipantsRepository lotteryParticipantsRepository,
CasperBotRepository casperBotRepository,
RedisService redisService) {
RedisService redisService, UserRepository userRepository) {
this.lotteryEventRepository = lotteryEventRepository;
this.lotteryParticipantsRepository = lotteryParticipantsRepository;
this.casperBotRepository = casperBotRepository;
this.redisService = redisService;
this.userRepository = userRepository;
}

public GetCasperBot postCasperBot(String userData, String body) throws CustomException {
LotteryParticipants participants = registerUserIfNeed(userData);

JsonParser jsonParser = new JsonParser();

JsonObject casperBotObject = (JsonObject) jsonParser.parse(body);

int eyeShape = casperBotObject.get("eyeShape").getAsInt();
int eyePosition = casperBotObject.get("eyePosition").getAsInt();
int mouthShape = casperBotObject.get("mouthShape").getAsInt();
int color = casperBotObject.get("color").getAsInt();
int sticker = casperBotObject.get("sticker").getAsInt();
String name = casperBotObject.get("name").getAsString();
String expectation = casperBotObject.get("expectation").getAsString();
public GetCasperBot postCasperBot(String userId, PostCasperBot postCasperBot) throws CustomException {
LotteryParticipants participants = registerUserIfNeed(userId);

CasperBot casperBot = new CasperBot(eyeShape, eyePosition, mouthShape, color, sticker, name, expectation, participants.getPhoneNumber());
casperBot.validateEnumFields();
CasperBot casperBot = new CasperBot(postCasperBot, participants.getBaseUser().getId());

if (casperBot.getExpectation() != null) participants.expectationAdded();
lotteryParticipantsRepository.save(participants);
Expand All @@ -68,34 +56,27 @@ public GetCasperBot postCasperBot(String userData, String body) throws CustomExc
}


public GetLotteryParticipant getLotteryParticipant(String userData) throws UserPrincipalNotFoundException {
String phoneNumber = getDecodedPhoneNumber(userData);

LotteryParticipants participant = lotteryParticipantsRepository.findById(phoneNumber).orElse(null);
// TODO: 응모 횟수 로직 작성
public GetLotteryParticipant getLotteryParticipant(String userId) throws UserPrincipalNotFoundException {
LotteryParticipants participant = lotteryParticipantsRepository.findById(userId).orElse(null);

if (participant == null) throw new UserPrincipalNotFoundException("응모 내역이 없습니다.");
else return GetLotteryParticipant.of(participant);

return GetLotteryParticipant.of(participant, getCasperBot(participant.getCasperId()));
}

public GetCasperBot getCasperBot(Long casperId){
public GetCasperBot getCasperBot(Long casperId) {
CasperBot casperBot = casperBotRepository.findById(casperId).orElse(null);
if(casperBot == null) throw new CustomException("캐스퍼 봇이 없음", CustomErrorCode.CASPERBOT_NOT_FOUND);
if (casperBot == null) throw new CustomException("캐스퍼 봇이 없음", CustomErrorCode.CASPERBOT_NOT_FOUND);
return GetCasperBot.of(casperBot);
}


public LotteryParticipants registerUserIfNeed(String userData) {
String phoneNumber = getDecodedPhoneNumber(userData);

LotteryParticipants participants = lotteryParticipantsRepository.findById(phoneNumber)
.orElse(null);

if (participants == null) {
participants = new LotteryParticipants(phoneNumber);
lotteryParticipantsRepository.save(participants);
}
public LotteryParticipants registerUserIfNeed(String userId) {
BaseUser baseUser = userRepository.findById(userId).get();

return participants;
return lotteryParticipantsRepository.findByBaseUser(baseUser)
.orElseGet(() -> lotteryParticipantsRepository.save(new LotteryParticipants(baseUser)));
}

}
11 changes: 0 additions & 11 deletions Server/src/main/java/JGS/CasperEvent/global/auth/AdminCheck.java

This file was deleted.

Loading

0 comments on commit cdbfebc

Please sign in to comment.