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] 한판승부 생성하기 API 구현 #33

Merged
merged 9 commits into from
Jul 12, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.universe.uni.controller;

import javax.validation.Valid;

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 com.universe.uni.dto.CreateShortGameRequestDto;
import com.universe.uni.dto.CreateShortGameResponseDto;
import com.universe.uni.service.GameService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

game관련이면 /api/game으로 둬도 될거같은디

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

애초에 컨트롤러 명 자체도 shortGame 이기 때문에 별도의 /game 에 대한 endpoint 를 다루지 않는다면

컨트롤러 이름과 매칭되는 역할과 endpoint Path 를 주면 좋을 것 같아요:)

public class ShortGameController {

private final GameService gameService;

@PostMapping("/game/short")
public CreateShortGameResponseDto createShortGame(@RequestBody @Valid final CreateShortGameRequestDto createShortGameRequestDto) {
Copy link
Collaborator

@2zerozu 2zerozu Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 나 @Valid 안 넣었다 .. 고맙습니다
근데 바디도 final로 넣어야하나 ..???

return gameService.createShortGame(createShortGameRequestDto);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/universe/uni/domain/GameResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Objects;

public enum GameResult {
WIN, LOSE, DRAW;
WIN, LOSE, DRAW, UNDECIDED;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고뇌의 흔적이 보입니다...


public static GameResult findMatchResultBy(String gameResultName) {
return Arrays.stream(values())
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/universe/uni/domain/entity/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public class Game {

@Column(name = "finish_at")
private LocalDateTime finishAt;

public Game(Couple couple) {
this.couple = couple;
this.enable = Boolean.TRUE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그냥 true랑 다른 게 있나용????

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import javax.persistence.Table;

import com.universe.uni.domain.MissionType;
import com.universe.uni.domain.entity.convertor.GameResultAttributeConverter;
import com.universe.uni.domain.entity.convertor.MissionTypeAttributeConverter;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "mission_category")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MissionCategory {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/universe/uni/domain/entity/RoundGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.persistence.Table;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -39,4 +40,11 @@ public class RoundGame {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_game_history_id")
private UserGameHistory userGameHistory;

@Builder
public RoundGame(Game game, MissionCategory missionCategory) {
this.game = game;
this.missionCategory = missionCategory;
this.enable = Boolean.TRUE;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/universe/uni/domain/entity/RoundMission.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.universe.uni.domain.entity.convertor.GameResultAttributeConverter;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -53,4 +54,13 @@ public class RoundMission {
@Column(name = "final_result")
@Convert(converter = GameResultAttributeConverter.class)
private GameResult finalResult;

@Builder
public RoundMission(RoundGame roundGame, MissionContent missionContent, User user) {
this.roundGame = roundGame;
this.missionContent = missionContent;
this.user = user;
this.result = GameResult.UNDECIDED;
this.finalResult = GameResult.UNDECIDED;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/universe/uni/domain/entity/ShortGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.persistence.PrimaryKeyJoinColumn;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -15,4 +16,8 @@
@PrimaryKeyJoinColumn(name = "game_id")
public class ShortGame extends Game {

@Builder
public ShortGame(Couple couple) {
super(couple);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/universe/uni/domain/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.universe.uni.domain.SnsType;
import com.universe.uni.domain.entity.convertor.SnsTypeAttributeConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static lombok.AccessLevel.PROTECTED;

@Entity
@Table(name = "user_match_history")
@Table(name = "user_game_history")
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/universe/uni/domain/entity/WishCoupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class WishCoupon {
private LocalDateTime usedAt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -61,15 +61,12 @@ public class WishCoupon {
private GameType gameType;

@Builder
public WishCoupon(Long id, String image, String content, boolean isVisible, boolean isUsed, LocalDateTime usedAt,
User user, Game game, GameType gameType) {
this.id = id;
public WishCoupon(String image, String content, boolean isVisible,
Game game, GameType gameType) {
this.image = image;
this.content = content;
this.isVisible = isVisible;
this.isUsed = isUsed;
this.usedAt = usedAt;
this.user = user;
this.isUsed = Boolean.FALSE;
this.game = game;
this.gameType = gameType;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/universe/uni/dto/CreateShortGameRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.universe.uni.dto;

import javax.validation.constraints.NotNull;

import lombok.Getter;

@Getter
public class CreateShortGameRequestDto {

@NotNull
private Long missionCategoryId;

@NotNull
private String wishContent;
}
18 changes: 18 additions & 0 deletions src/main/java/com/universe/uni/dto/CreateShortGameResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.universe.uni.dto;

import com.universe.uni.domain.entity.RoundMission;
import com.universe.uni.domain.entity.ShortGame;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CreateShortGameResponseDto {
private ShortGameVO shortGame;
private RoundMissionVO roundMission;

public static CreateShortGameResponseDto of(ShortGame shortGame, RoundMission roundMission) {
return new CreateShortGameResponseDto(new ShortGameVO(shortGame), new RoundMissionVO(roundMission));
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/universe/uni/dto/RoundMissionVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.universe.uni.dto;

import java.time.LocalDate;
import java.time.LocalDateTime;

import com.universe.uni.domain.GameResult;
import com.universe.uni.domain.entity.MissionCategory;
import com.universe.uni.domain.entity.MissionContent;
import com.universe.uni.domain.entity.RoundMission;

import lombok.Getter;

@Getter
public class RoundMissionVO {
private long roundMissionId;
private MissionContent missionContent;
private GameResult result;
private GameResult finalResult;
private LocalDateTime updatedAt;

public RoundMissionVO(RoundMission roundMission){
this.roundMissionId = roundMission.getId();
this.missionContent = roundMission.getMissionContent();
this.result = roundMission.getResult();
this.finalResult = roundMission.getFinalResult();
this.updatedAt = roundMission.getUpdatedAt();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/universe/uni/dto/ShortGameVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.universe.uni.dto;

import java.time.LocalDateTime;

import com.universe.uni.domain.entity.ShortGame;

import lombok.Getter;

@Getter
public class ShortGameVO {
private Long id;
private Boolean enable;
private LocalDateTime finishAt;

public ShortGameVO(ShortGame shortGame) {
this.id = shortGame.getId();
this.enable = shortGame.getEnable();
this.finishAt = shortGame.getFinishAt();
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/universe/uni/exception/dto/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum ErrorType {
"요청 방식이 잘못된 경우입니다. 요청 방식 자체가 잘못된 경우입니다."),
VALIDATION_TOKEN_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "UE1002",
"요청 시 토큰이 누락되어 토큰 값이 없는 경우입니다."),
ALREADY_GAME_CREATED(HttpStatus.BAD_REQUEST, "UE1003",
"이미 생성된 승부가 있습니다."),

/**
* 401 Unauthorized
Expand All @@ -32,6 +34,9 @@ public enum ErrorType {
"잘못된 endpoint에 요청한 경우입니다."),
USER_NOT_FOUND_EXCEPTION(HttpStatus.NOT_FOUND, "UE5002",
"조회한 유저가 존재하지 않는 경우 입니다."),
NOT_FOUND_MISSION_CATEGORY_EXCEPTION(HttpStatus.NOT_FOUND, "UE5003", "존재하지 않는 미션 카테고리입니다"),
NOT_FOUND_MISSION_CONTENT(HttpStatus.NOT_FOUND, "UE5004", "해당 카테고리 미션이 존재하지 않습니다."),
NOT_FOUND_ROUND_MISSION(HttpStatus.NOT_FOUND, "UE5005", "해당 라운드 미션이 존재하지 않습니다."),

/**
* 406 Not Acceptable
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/universe/uni/repository/GameRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.universe.uni.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.Couple;
import com.universe.uni.domain.entity.Game;

public interface GameRepository extends JpaRepository<Game, Long> {

boolean existsByCoupleAndEnable(Couple couple, boolean enable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.universe.uni.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.MissionCategory;

public interface MissionCategoryRepository extends JpaRepository<MissionCategory, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.universe.uni.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.MissionCategory;
import com.universe.uni.domain.entity.MissionContent;

public interface MissionContentRepository extends JpaRepository<MissionContent, Long> {
List<MissionContent> findByMissionCategory(MissionCategory missionCategory);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.universe.uni.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.RoundGame;

public interface RoundGameRepository extends JpaRepository<RoundGame, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.universe.uni.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.RoundGame;
import com.universe.uni.domain.entity.RoundMission;
import com.universe.uni.domain.entity.User;

public interface RoundMissionRepository extends JpaRepository<RoundMission, Long> {
Optional<RoundMission> findByRoundGameAndUser(RoundGame roundGame, User user);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.universe.uni.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.entity.Couple;
import com.universe.uni.domain.entity.User;

public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByCouple(Couple couple);

}
Loading