Skip to content

Commit a5be896

Browse files
Merge pull request #142 from lemonssoju/develop-v2
[develop-v2] main merge
2 parents 77d644f + 404c622 commit a5be896

File tree

14 files changed

+126
-20
lines changed

14 files changed

+126
-20
lines changed

src/main/java/com/lesso/neverland/album/domain/Album.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.lesso.neverland.puzzle.domain.Puzzle;
66
import jakarta.persistence.*;
77
import lombok.AccessLevel;
8+
import lombok.Builder;
89
import lombok.Getter;
910
import lombok.NoArgsConstructor;
1011
import org.hibernate.annotations.DynamicInsert;
@@ -25,13 +26,17 @@ public class Album extends BaseEntity {
2526
@ManyToOne(fetch = FetchType.LAZY)
2627
@JoinColumn(name = "puzzle")
2728
private Puzzle puzzle;
28-
29-
@Column(nullable = false)
3029
private String albumImage;
3130

3231
@Column(nullable = false)
3332
private String content;
3433

3534
@OneToMany(mappedBy = "album")
3635
private List<Comment> comments = new ArrayList<>();
36+
37+
@Builder
38+
public Album(Puzzle puzzle, String content) {
39+
this.puzzle = puzzle;
40+
this.content = content;
41+
}
3742
}

src/main/java/com/lesso/neverland/common/base/BaseResponseStatus.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public enum BaseResponseStatus {
4646
INVALID_PUZZLE_IDX(false, HttpStatus.BAD_REQUEST, "잘못된 puzzle idx 입니다."),
4747
NO_PUZZLE_WRITER(false, HttpStatus.BAD_REQUEST, "해당 post의 작성자가 아닙니다."),
4848
ALREADY_DELETED_PUZZLE(false, HttpStatus.CONFLICT, "이미 삭제된 puzzle 입니다."),
49+
NO_PUZZLER(false, HttpStatus.FORBIDDEN, "해당 퍼즐의 퍼즐러가 아닙니다."),
50+
TOO_LONG_CONTENT(false, HttpStatus.BAD_REQUEST, "퍼즐피스의 길이는 100자 이하여야 합니다."),
4951

5052

5153
// group(2300-2399)

src/main/java/com/lesso/neverland/gpt/application/GptService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lesso.neverland.gpt.application;
22

3-
import com.lesso.neverland.gpt.dto.CompletePuzzleResponse;
3+
import com.lesso.neverland.gpt.dto.GptResponseDto;
44
import com.lesso.neverland.gpt.dto.GptRequest;
55
import com.lesso.neverland.gpt.dto.GptResponse;
66
import com.theokanning.openai.completion.chat.ChatCompletionResult;
@@ -39,7 +39,7 @@ public String toText(List<String> puzzleTextList) {
3939
return result.toString();
4040
}
4141

42-
public CompletePuzzleResponse parseResponse(String response) {
42+
public GptResponseDto parseResponse(String response) {
4343
// 영어 부분 추출을 위한 정규 표현식 패턴 (괄호로 묶인 부분)
4444
Pattern pattern = Pattern.compile("\\((.*?)\\)");
4545
Matcher matcher = pattern.matcher(response);
@@ -54,6 +54,6 @@ public CompletePuzzleResponse parseResponse(String response) {
5454
String koreanPart = response.replace("(" + englishPart + ")", "").trim();
5555

5656
// 추출된 한글 부분과 영어 부분으로 CompletePuzzleResponse 객체 생성
57-
return new CompletePuzzleResponse(englishPart, koreanPart);
57+
return new GptResponseDto(englishPart, koreanPart);
5858
}
5959
}

src/main/java/com/lesso/neverland/gpt/dto/CompletePuzzleResponse.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.lesso.neverland.gpt.dto;
2+
3+
public record GptResponseDto(String prompt,
4+
String description) {}

src/main/java/com/lesso/neverland/gpt/presentation/GptController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.lesso.neverland.common.base.BaseResponse;
44
import com.lesso.neverland.gpt.application.GptService;
5-
import com.lesso.neverland.gpt.dto.CompletePuzzleRequest;
6-
import com.lesso.neverland.gpt.dto.CompletePuzzleResponse;
5+
import com.lesso.neverland.gpt.dto.GptResponseDto;
6+
import com.lesso.neverland.puzzle.dto.CompletePuzzleRequest;
77
import com.lesso.neverland.gpt.dto.GptResponse;
88
import lombok.AllArgsConstructor;
99
import org.springframework.web.bind.annotation.PostMapping;
@@ -19,7 +19,7 @@ public class GptController {
1919
private final GptService gptService;
2020

2121
@PostMapping("/completePuzzle")
22-
public BaseResponse<CompletePuzzleResponse> completePuzzle(@RequestBody CompletePuzzleRequest completePuzzleRequest) {
22+
public BaseResponse<GptResponseDto> completePuzzle(@RequestBody CompletePuzzleRequest completePuzzleRequest) {
2323
GptResponse response = gptService.completion(gptService.toText(completePuzzleRequest.puzzleTextList()));
2424
return new BaseResponse<>(gptService.parseResponse(response.messages().get(0).message()));
2525
}

src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.lesso.neverland.puzzle.application;
22

3+
import com.lesso.neverland.album.domain.Album;
4+
import com.lesso.neverland.album.repository.AlbumRepository;
35
import com.lesso.neverland.common.base.BaseException;
46
import com.lesso.neverland.common.base.BaseResponse;
57
import com.lesso.neverland.common.image.ImageService;
8+
import com.lesso.neverland.gpt.application.GptService;
9+
import com.lesso.neverland.gpt.dto.GptResponseDto;
10+
import com.lesso.neverland.puzzle.dto.CompletePuzzleRequest;
11+
import com.lesso.neverland.puzzle.dto.CompletePuzzleResponse;
12+
import com.lesso.neverland.gpt.dto.GptResponse;
613
import com.lesso.neverland.group.domain.Team;
714
import com.lesso.neverland.group.dto.GroupPuzzleDto;
815
import com.lesso.neverland.group.dto.GroupPuzzleListResponse;
@@ -12,6 +19,7 @@
1219
import com.lesso.neverland.puzzle.domain.PuzzlePiece;
1320
import com.lesso.neverland.puzzle.dto.*;
1421
import com.lesso.neverland.puzzle.repository.PuzzleMemberRepository;
22+
import com.lesso.neverland.puzzle.repository.PuzzlePieceRepository;
1523
import com.lesso.neverland.puzzle.repository.PuzzleRepository;
1624
import com.lesso.neverland.user.application.UserService;
1725
import com.lesso.neverland.user.domain.User;
@@ -41,6 +49,9 @@ public class PuzzleService {
4149
private final GroupRepository groupRepository;
4250
private final ImageService imageService;
4351
private final PuzzleMemberRepository puzzleMemberRepository;
52+
private final PuzzlePieceRepository puzzlePieceRepository;
53+
private final GptService gptService;
54+
private final AlbumRepository albumRepository;
4455

4556
// 퍼즐 목록 조회
4657
public BaseResponse<GroupPuzzleListResponse> getGroupPuzzleList(Long groupIdx) {
@@ -148,13 +159,15 @@ private Puzzle createPuzzle(CreatePuzzleRequest createPuzzleRequest, Team group,
148159
return puzzle;
149160
}
150161

151-
// [작성자] 피드 수정 화면 조회
152-
public BaseResponse<PuzzleEditViewResponse> getPuzzleEditView(Long puzzleIdx) {
162+
// [작성자] 퍼즐 수정
163+
public BaseResponse<String> editPuzzle(Long groupIdx, Long puzzleIdx, MultipartFile image, EditPuzzleRequest editPuzzleRequest) {
153164
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
154165
Puzzle puzzle = puzzleRepository.findById(puzzleIdx).orElseThrow(() -> new BaseException(INVALID_PUZZLE_IDX));
155166
validateWriter(user, puzzle);
156167

157-
return new BaseResponse<>(new PuzzleEditViewResponse(puzzle.getTitle(), puzzle.getPuzzleImage(), puzzle.getContent()));
168+
169+
170+
return new BaseResponse<>(SUCCESS);
158171
}
159172

160173
// [작성자] 퍼즐 삭제
@@ -199,4 +212,50 @@ public BaseResponse<PuzzlerListResponse> getPuzzlerList(Long groupIdx) {
199212
.toList());
200213
return new BaseResponse<>(puzzlerList);
201214
}
215+
216+
// 퍼즐피스 추가
217+
@Transactional(rollbackFor = Exception.class)
218+
public BaseResponse<String> addPuzzlePiece(Long groupIdx, Long puzzleIdx, PuzzlePieceRequest puzzlePieceRequest) {
219+
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
220+
Puzzle puzzle = puzzleRepository.findById(puzzleIdx).orElseThrow(() -> new BaseException(INVALID_PUZZLE_IDX));
221+
validatePuzzler(user, puzzle);
222+
223+
if (puzzlePieceRequest.content().length() > 100) throw new BaseException(TOO_LONG_CONTENT);
224+
PuzzlePiece puzzlePiece = PuzzlePiece.builder()
225+
.puzzle(puzzle)
226+
.user(user)
227+
.content(puzzlePieceRequest.content()).build();
228+
puzzlePiece.setPuzzle(puzzle);
229+
puzzlePiece.setUser(user);
230+
puzzlePieceRepository.save(puzzlePiece);
231+
232+
return new BaseResponse<>(SUCCESS);
233+
}
234+
235+
// 퍼즐러 validation
236+
private void validatePuzzler(User user, Puzzle puzzle) {
237+
if (!puzzleMemberRepository.existsByUserAndPuzzle(user, puzzle)) throw new BaseException(NO_PUZZLER);
238+
if (puzzle.getStatus().equals(INACTIVE)) throw new BaseException(ALREADY_DELETED_PUZZLE);
239+
}
240+
241+
// [작성자] 퍼즐 완성하기
242+
@Transactional(rollbackFor = Exception.class)
243+
public BaseResponse<CompletePuzzleResponse> completePuzzle(Long groupIdx, Long puzzleIdx, CompletePuzzleRequest completePuzzleRequest) {
244+
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
245+
Puzzle puzzle = puzzleRepository.findById(puzzleIdx).orElseThrow(() -> new BaseException(INVALID_PUZZLE_IDX));
246+
validateWriter(user, puzzle);
247+
248+
// GPT 요약 수행
249+
GptResponse response = gptService.completion(gptService.toText(completePuzzleRequest.puzzleTextList()));
250+
GptResponseDto gptResponseDto = gptService.parseResponse(response.messages().get(0).message());
251+
252+
// Album 생성
253+
Album newAlbum = Album.builder()
254+
.puzzle(puzzle)
255+
.content(gptResponseDto.description()).build();
256+
albumRepository.save(newAlbum);
257+
258+
CompletePuzzleResponse completePuzzleResponse = new CompletePuzzleResponse(gptResponseDto.prompt(), gptResponseDto.description(), newAlbum.getAlbumIdx());
259+
return new BaseResponse<>(completePuzzleResponse);
260+
}
202261
}

src/main/java/com/lesso/neverland/gpt/dto/CompletePuzzleRequest.java renamed to src/main/java/com/lesso/neverland/puzzle/dto/CompletePuzzleRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lesso.neverland.gpt.dto;
1+
package com.lesso.neverland.puzzle.dto;
22

33
import java.util.List;
44

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.lesso.neverland.puzzle.dto;
2+
3+
public record CompletePuzzleResponse(String prompt,
4+
String description,
5+
Long albumIdx) {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.lesso.neverland.puzzle.dto;
2+
3+
public record EditPuzzleRequest(String content) {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.lesso.neverland.puzzle.dto;
2+
3+
public record PuzzlePieceRequest(String content) {}

src/main/java/com/lesso/neverland/puzzle/presentation/PuzzleController.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.lesso.neverland.common.base.BaseException;
44
import com.lesso.neverland.common.base.BaseResponse;
5+
import com.lesso.neverland.gpt.application.GptService;
6+
import com.lesso.neverland.puzzle.dto.CompletePuzzleRequest;
7+
import com.lesso.neverland.puzzle.dto.CompletePuzzleResponse;
58
import com.lesso.neverland.group.dto.GroupPuzzleListResponse;
69
import com.lesso.neverland.puzzle.application.PuzzleService;
710
import com.lesso.neverland.puzzle.dto.*;
@@ -19,6 +22,7 @@
1922
@RequestMapping(puzzle)
2023
public class PuzzleController {
2124
private final PuzzleService puzzleService;
25+
private final GptService gptService;
2226

2327
// 퍼즐 목록 조회
2428
@GetMapping("")
@@ -42,10 +46,10 @@ public BaseResponse<String> createPuzzle(@PathVariable Long groupIdx, @RequestPa
4246
}
4347
}
4448

45-
// [작성자] 피드 수정 화면 조회
46-
@GetMapping("/{puzzleIdx}/editView")
47-
public BaseResponse<PuzzleEditViewResponse> getPuzzleEditView(@PathVariable Long puzzleIdx) {
48-
return puzzleService.getPuzzleEditView(puzzleIdx);
49+
// [작성자] 퍼즐 수정
50+
@GetMapping("/{puzzleIdx}/edit")
51+
public BaseResponse<String> editPuzzle(@PathVariable("groupIdx") Long groupIdx, @PathVariable("puzzleIdx") Long puzzleIdx, @RequestPart MultipartFile image, @RequestPart EditPuzzleRequest editPuzzleRequest) {
52+
return puzzleService.editPuzzle(groupIdx, puzzleIdx, image, editPuzzleRequest);
4953
}
5054

5155
// [작성자] 퍼즐 삭제
@@ -60,4 +64,19 @@ public BaseResponse<PuzzlerListResponse> getPuzzlerList(@PathVariable Long group
6064
return puzzleService.getPuzzlerList(groupIdx);
6165
}
6266

67+
// [멤버] 퍼즐피스 추가
68+
@PostMapping("/{puzzleIdx}/puzzlePiece")
69+
public BaseResponse<String> addPuzzlePiece(@PathVariable("groupIdx") Long groupIdx, @PathVariable("puzzleIdx") Long puzzleIdx, @RequestBody PuzzlePieceRequest puzzlePieceRequest) {
70+
return puzzleService.addPuzzlePiece(groupIdx, puzzleIdx, puzzlePieceRequest);
71+
}
72+
73+
// [작성자] 퍼즐 완성하기
74+
@PostMapping("/{puzzleIdx}}")
75+
public BaseResponse<CompletePuzzleResponse> completePuzzle(@PathVariable("groupIdx") Long groupIdx,
76+
@PathVariable("puzzleIdx") Long puzzleIdx,
77+
@RequestBody CompletePuzzleRequest completePuzzleRequest)
78+
{
79+
return puzzleService.completePuzzle(groupIdx, puzzleIdx, completePuzzleRequest);
80+
}
81+
6382
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.lesso.neverland.puzzle.repository;
22

3+
import com.lesso.neverland.puzzle.domain.Puzzle;
34
import com.lesso.neverland.puzzle.domain.PuzzleMember;
5+
import com.lesso.neverland.user.domain.User;
46
import org.springframework.data.jpa.repository.JpaRepository;
57

68
public interface PuzzleMemberRepository extends JpaRepository<PuzzleMember, Long> {
9+
boolean existsByUserAndPuzzle(User user, Puzzle puzzle);
710
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lesso.neverland.puzzle.repository;
2+
3+
import com.lesso.neverland.puzzle.domain.PuzzlePiece;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface PuzzlePieceRepository extends JpaRepository<PuzzlePiece, Long> {
7+
}

0 commit comments

Comments
 (0)