Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat#11 create casper bot #23

Merged
merged 19 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
10e3d2c
feat: validateEnumField ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
k000927 Jul 30, 2024
97d7f87
chore: CustomException ์ž‘์„ฑ
k000927 Jul 30, 2024
7be3af5
chore: @Repository ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€
k000927 Jul 30, 2024
588cda4
chore: createdAt ์ž‘๋™ ์œ„ํ•ด @EnableJpaAuditing ์ถ”๊ฐ€
k000927 Jul 30, 2024
86b4707
chore: lotteryEvent์™€ LotteryParticipants ์‚ฌ์ด์˜ ์—ฐ๊ด€๊ด€๊ณ„ ์ œ๊ฑฐ
k000927 Jul 30, 2024
7dd8993
chore: ์œ ์ € ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋”๋ฏธ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
k000927 Jul 30, 2024
8ad2bb8
chore: ์˜คํƒ€ ์ˆ˜์ •
k000927 Jul 31, 2024
5890833
chore: Getter์™€ ์‘๋ชจ ํšŸ์ˆ˜ ์ €์žฅ์šฉ ์นผ๋Ÿผ appliedCount ์ถ”๊ฐ€
k000927 Jul 31, 2024
c2fc4d4
feat: postCasperBot ๋ฉ”์„œ๋“œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ๋ฐ registerUserIfNeed ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
k000927 Jul 31, 2024
4a52c50
test: ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€ - ์ฟ ํ‚ค์— ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด๊ฐ€ ์—†์„ ๋•Œ ํ…Œ์ŠคํŠธ
k000927 Jul 31, 2024
41ec642
chore: JpaAuditingConfiguration config๋กœ ๋ถ„๋ฆฌ
k000927 Jul 31, 2024
6853da8
feat: GlobalExceptionHandler ์ถ”๊ฐ€
k000927 Jul 31, 2024
0ebf49b
feat: ์บ์Šคํผ ๋ด‡ ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ๋ฐ phoneNumber ์—…๋ฐ์ดํŠธ ๋ฉ”์„œ๋“œ ์ž‘์„ฑ
k000927 Jul 31, 2024
7bec1e1
chore: ๋ฐ˜ํ™˜๊ฐ’ ResponseEntity๋กœ ์ˆ˜์ •
k000927 Jul 31, 2024
71b9e1b
chore: ์‚ฌ์†Œํ•œ ์ˆ˜์ •..
k000927 Jul 31, 2024
da87a20
chore: ํ…Œ์ŠคํŠธ์ฝ”๋“œ ํ…์ŠคํŠธ๋ธ”๋ก์œผ๋กœ ๋ณ€๊ฒฝ
k000927 Jul 31, 2024
e7cfc0b
chore: ์œ ์ € ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋”๋ฏธ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€
k000927 Jul 30, 2024
d6ca23f
Merge branch 'feat/#12-check-if-user-registered' into feat#11-Create-โ€ฆ
k000927 Jul 31, 2024
a429e0c
Merge remote-tracking branch 'origin/feat#11-Create-Casper-Bot' into โ€ฆ
k000927 Jul 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication
public class CasperEventApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@
import JGS.CasperEvent.domain.event.service.eventService.LotteryEventService;
import JGS.CasperEvent.global.response.CustomResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/event/lottery")
public class LotteryEventController {

private final LotteryEventService lotteryEventService;

@Autowired
public LotteryEventController(LotteryEventService lotteryEventService) {
this.lotteryEventService = lotteryEventService;
}
private LotteryEventService lotteryEventService;

@PostMapping
public CustomResponse<GetCasperBot> postCasperBot(@RequestBody String body) {
return CustomResponse.create(lotteryEventService.postCasperBot(body));
public ResponseEntity<CustomResponse<GetCasperBot>> postCasperBot(@CookieValue String userData,
@RequestBody String body) {
return new ResponseEntity<>(CustomResponse.create(lotteryEventService.postCasperBot(userData, body)), HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import JGS.CasperEvent.domain.event.entity.casperBot.casperEnum.*;
import JGS.CasperEvent.global.entity.BaseEntity;
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.error.exception.ErrorCode;
import com.google.gson.annotations.SerializedName;
import jakarta.persistence.*;

Expand Down Expand Up @@ -73,4 +75,26 @@ public String getName() {
public String getExpectation() {
return expectation;
}

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

public void updatePhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package JGS.CasperEvent.domain.event.entity.event;

import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
import jakarta.persistence.*;

import java.util.Set;

@Entity
public class LotteryEvent extends BaseEvent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lotteryEventId;

@OneToMany(mappedBy = "lotteryEvent", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<LotteryParticipants> lotteryParticipants;

public Long getLotteryEventId() {
return lotteryEventId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package JGS.CasperEvent.domain.event.entity.event;

import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
import JGS.CasperEvent.domain.event.entity.participants.RushParticipants;
import jakarta.persistence.*;

import java.util.Set;
Expand All @@ -23,7 +23,7 @@ public class RushEvent extends BaseEvent {
private RushOption rightOption;

@OneToMany(mappedBy = "rushEvent", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<LotteryParticipants> rushParticipants;
private Set<RushParticipants> rushParticipants;

public String getPrizeImageUrl() {
return prizeImageUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package JGS.CasperEvent.domain.event.entity.participants;

import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;
import JGS.CasperEvent.global.entity.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

@Entity
public class LotteryParticipants extends BaseEntity {
private int linkClickedCount;
private int expectations;

@ManyToOne
@JoinColumn(name="lottery_event_id")
private LotteryEvent lotteryEvent;
private int appliedCount;

@Id
private String phoneNumber;

public String getPhoneNumber() {
return phoneNumber;
}

public LotteryParticipants() {

}

public void expectationAdded(){
expectations++;
}

public LotteryParticipants(String phoneNumber){
this.phoneNumber = phoneNumber;
this.appliedCount = 1;
this.linkClickedCount = 0;
this.expectations = 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package JGS.CasperEvent.domain.event.repository;

import org.springframework.stereotype.Repository;

@Repository
public interface AdminRepository {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package JGS.CasperEvent.domain.event.repository;

public interface CasperBotRepository {
import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CasperBotRepository extends JpaRepository<CasperBot, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package JGS.CasperEvent.domain.event.repository.eventRepository;

public interface LotteryEventRepository {
import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LotteryEventRepository extends JpaRepository<LotteryEvent, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package JGS.CasperEvent.domain.event.repository.eventRepository;

import org.springframework.stereotype.Repository;

@Repository
public interface RushEventRepository {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package JGS.CasperEvent.domain.event.repository.eventRepository;

import org.springframework.stereotype.Repository;

@Repository
public interface RushOptionRepository {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package JGS.CasperEvent.domain.event.repository.participantsRepository;

public interface LotteryParticipantsRepository {
import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
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> findByPhoneNumber(String phoneNumber);
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package JGS.CasperEvent.domain.event.repository.participantsRepository;

import org.springframework.stereotype.Repository;

@Repository
public interface RushParticipantsRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,56 @@

import JGS.CasperEvent.domain.event.dto.GetCasperBot;
import JGS.CasperEvent.domain.event.entity.casperBot.CasperBot;
import JGS.CasperEvent.domain.event.entity.participants.LotteryParticipants;
import JGS.CasperEvent.domain.event.repository.CasperBotRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.LotteryEventRepository;
import JGS.CasperEvent.domain.event.repository.participantsRepository.LotteryParticipantsRepository;
import JGS.CasperEvent.global.error.exception.CustomException;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class LotteryEventService {

@Autowired
private LotteryEventRepository lotteryEventRepository;
@Autowired
private LotteryParticipantsRepository lotteryParticipantsRepository;
@Autowired
private CasperBotRepository casperBotRepository;

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

public GetCasperBot postCasperBot(String body) {
Gson gson = getGson();
CasperBot casperBot = gson.fromJson(body, CasperBot.class);
casperBot.validateEnumFields();
casperBot.updatePhoneNumber(participants.getPhoneNumber());

if (casperBot.getEyePosition() != null) participants.expectationAdded();

casperBotRepository.save(casperBot);
lotteryParticipantsRepository.save(participants);
return GetCasperBot.of(casperBot);
}

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

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

if (participants == null) {
participants = new LotteryParticipants(phoneNumber);
lotteryParticipantsRepository.save(participants);
}

return participants;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package JGS.CasperEvent.domain.health.api;

import JGS.CasperEvent.global.response.CustomResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -10,7 +12,7 @@
public class HealthController {

@GetMapping
public CustomResponse<Boolean> health(){
return CustomResponse.success(true);
public ResponseEntity<CustomResponse<Boolean>> health(){
return new ResponseEntity<>(CustomResponse.success(true), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package JGS.CasperEvent.global.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package JGS.CasperEvent.global.error;

import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.response.CustomErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingRequestCookieException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static JGS.CasperEvent.global.response.CustomErrorResponse.returnError;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(CustomException.class)
public ResponseEntity<CustomErrorResponse> handler(CustomException e){
return new ResponseEntity<>(returnError(e), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(MissingRequestCookieException.class)
public ResponseEntity<CustomErrorResponse> missingCookieHandler(MissingRequestCookieException e){
return new ResponseEntity<>(returnError("์œ ์ € ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."), HttpStatus.UNAUTHORIZED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package JGS.CasperEvent.global.error.exception;

public class CustomException extends RuntimeException {
private final ErrorCode errorCode;

public CustomException(String message, ErrorCode errorCode) {
super(message);
this.errorCode = errorCode;
}

public ErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package JGS.CasperEvent.global.error.exception;

public enum ErrorCode {
AUTHENTICATION_REQUIRED(401),
ACCESS_DENIED(403),
INTERNAL_SERVER_ERROR(500),
USER_NOT_FOUND(404),
INVALID_REQUEST_ERROR(400);

private final int status;

ErrorCode(int status) {
this.status = status;
}

public int getStatus(){
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package JGS.CasperEvent.global.response;

import JGS.CasperEvent.global.error.exception.CustomException;

public record CustomErrorResponse(String message) {
public static CustomErrorResponse returnError(CustomException e){
return new CustomErrorResponse(e.getMessage());
}

public static CustomErrorResponse returnError(String message){
return new CustomErrorResponse(message);
}
}
Loading
Loading