Skip to content

Commit

Permalink
fix : reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixtar committed Apr 1, 2024
1 parent 1749b9d commit 8233e95
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 49 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'

implementation('org.springframework.boot:spring-boot-starter-data-mongodb')

implementation 'redis.clients:jedis:4.3.1'

}

tasks.named('test') {
Expand Down
15 changes: 15 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
app:
image: cross-puzzle
build:
context: .
dockerfile: ./Dockerfile
ports:
- 8080:8080
depends_on:
- redis

redis:
image: redis
ports: # 바인딩할 포트:내부 포트
- 8379:8379
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.example.crosspuzzleserver.repository.CrossWordsRepository;
import com.example.crosspuzzleserver.repository.QuestionInfoRepository;
import com.example.crosspuzzleserver.repository.WordsRepository;
import com.example.crosspuzzleserver.service.PuzzleGen.PuzzleGenService;
import com.example.crosspuzzleserver.service.PuzzleGen.WordPuzzle;
import com.example.crosspuzzleserver.service.puzzle.PuzzleGen.PuzzleGenService;
import com.example.crosspuzzleserver.service.puzzle.PuzzleGen.WordPuzzle;
import com.example.crosspuzzleserver.util.error.Error;
import com.example.crosspuzzleserver.util.exception.BadRequestException;
import com.example.crosspuzzleserver.util.response.ApiResponse;
Expand All @@ -24,7 +24,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -116,17 +115,8 @@ public ResponseEntity<ApiResponse> testError() {
}

@GetMapping("/testGen")
public String testGen() {

List<ObjectId> categories = new ArrayList<>();
categories.add(categoryRepository.findAll().get(0).getId());
System.out.println("@@@" + categories.get(0));
wordPuzzle.generateCrossWord(categories);

if (puzzleGenService.generatePuzzle()) {
return "success";
}
return "duplicated";
public void testGen() {
puzzleGenService.generatePuzzle();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.crosspuzzleserver.controller.puzzle;


import com.example.crosspuzzleserver.service.spi.PuzzleService;
import com.example.crosspuzzleserver.service.puzzle.spi.PuzzleService;
import com.example.crosspuzzleserver.util.error.Error;
import com.example.crosspuzzleserver.util.response.ApiResponse;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import com.example.crosspuzzleserver.service.dto.IsAnswerDto;
import com.example.crosspuzzleserver.service.dto.puzzle.PuzzleDto;
import com.example.crosspuzzleserver.service.dto.puzzle.PuzzleListDto;
import com.example.crosspuzzleserver.service.puzzle.spi.Hits;
import com.example.crosspuzzleserver.service.spi.CategoryService;
import com.example.crosspuzzleserver.service.spi.PuzzleService;
import com.example.crosspuzzleserver.service.puzzle.spi.PuzzleService;
import com.example.crosspuzzleserver.service.spi.WordsService;
import com.example.crosspuzzleserver.util.cookie.CookieService;
import com.example.crosspuzzleserver.util.response.ApiResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -32,6 +36,8 @@ public class PuzzleQueryController {
private final PuzzleService puzzleService;
private final WordsService wordsService;
private final CategoryService categoryService;
private final CookieService cookieService;
private final Hits hits;

private final static String DEFAULT_PAGE = "0";
private final static String DEFAULT_LIMIT = "12";
Expand All @@ -40,10 +46,19 @@ public class PuzzleQueryController {
@GetMapping("")
public ResponseEntity<ApiResponse> getPuzzleById(
@RequestParam(value = "id", required = true) String id,
@RequestParam(value = "answer", required = false, defaultValue = "false") String answer
@RequestParam(value = "answer", required = false, defaultValue = "false") String answer,
@CookieValue(value = "userCookie", required = false) String cookieValue
) {
if (cookieValue == null) {
cookieValue = cookieService.getCookie();
}
if (!hits.isHit(cookieValue, id)) {
puzzleService.updatePuzzleHits(id);
}

PuzzleDto puzzleDto = puzzleService.getPuzzleById(id, answer);
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.SET_COOKIE, cookieValue)
.body(ApiResponse.success(puzzleDto));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.crosspuzzleserver.service.puzzle;

import com.example.crosspuzzleserver.service.puzzle.spi.Hits;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPooled;

@Slf4j
@Service
@RequiredArgsConstructor
public class HitsImpl implements Hits {

JedisPooled jedis = new JedisPooled("redis", 6379);

private static final int EXPIRE_TIME = 60 * 60 * 24; //하루

@Override
public boolean isHit(String cookieValue, String boardId) {
String key = cookieValue + boardId;
if (jedis.get(key) == null) {
jedis.set(key, boardId);
jedis.expire(key, EXPIRE_TIME);
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.crosspuzzleserver.service.PuzzleGen;
package com.example.crosspuzzleserver.service.puzzle.PuzzleGen;

import com.example.crosspuzzleserver.domain.AnswersInfo;
import com.example.crosspuzzleserver.domain.Category;
Expand Down Expand Up @@ -30,7 +30,7 @@ public class PuzzleGenImpl implements PuzzleGenService {

@Override
@Scheduled(cron = "0 0 3 * * *")
public boolean generatePuzzle() {
public void generatePuzzle() {

List<ObjectId> randomCategoryId = getRandomCategoryIds();
CrossWords crossWords = wordPuzzle.generateCrossWord(randomCategoryId);
Expand All @@ -39,13 +39,13 @@ public boolean generatePuzzle() {
//중복 체크 성공시 저장, 실패시 리트 or 종료
if (isDuplicated(crossWords)) {
log.info("duplicated puzzle");
return false;
return;
}
log.info("create puzzle success");
answersInfoRepository.saveAll(crossWords.getAnswersInfo());
questionInfoRepository.save(crossWords.getQuestionInfos());
crossWordsRepository.save(crossWords);
return true;
return;
}

private boolean isDuplicated(CrossWords crossWords) {
Expand Down Expand Up @@ -78,8 +78,7 @@ private List<ObjectId> getRandomCategoryIds() {
private boolean isDuplicatedWords(CrossWords cw, CrossWords origin) {
Set<String> cwWordsSet = new HashSet<>(getWordsValueList(cw.getAnswersInfo()));
Set<String> originWordsSet = new HashSet<>(getWordsValueList(origin.getAnswersInfo()));

return cwWordsSet.size() == originWordsSet.size();
return cwWordsSet.equals(originWordsSet);
}

private List<String> getWordsValueList(List<AnswersInfo> answersInfoList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.crosspuzzleserver.service.puzzle.PuzzleGen;


public interface PuzzleGenService {

public void generatePuzzle();

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.crosspuzzleserver.service.PuzzleGen;
package com.example.crosspuzzleserver.service.puzzle.PuzzleGen;

import com.example.crosspuzzleserver.domain.AnswersInfo;
import com.example.crosspuzzleserver.domain.Category;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.crosspuzzleserver.service;
package com.example.crosspuzzleserver.service.puzzle;

import com.example.crosspuzzleserver.domain.AnswersInfo;
import com.example.crosspuzzleserver.domain.Category;
Expand All @@ -13,7 +13,7 @@
import com.example.crosspuzzleserver.service.dto.puzzle.PuzzleDtoWithoutWords;
import com.example.crosspuzzleserver.service.dto.puzzle.PuzzleListDto;
import com.example.crosspuzzleserver.service.dto.puzzle.WordDto;
import com.example.crosspuzzleserver.service.spi.PuzzleService;
import com.example.crosspuzzleserver.service.puzzle.spi.PuzzleService;
import com.example.crosspuzzleserver.util.error.Error;
import com.example.crosspuzzleserver.util.exception.BadRequestException;
import com.example.crosspuzzleserver.util.exception.NotFoundException;
Expand All @@ -40,10 +40,6 @@ public class PuzzleServiceImpl implements PuzzleService {
@Transactional
public PuzzleDto getPuzzleById(String puzzleId, String answer) {
CrossWords crossWords = getCrossWordsById(puzzleId);

if (!Boolean.parseBoolean(answer)) {
updateViewCount(crossWords);
}
return crossWordsToPuzzleDto(crossWords, Boolean.parseBoolean(answer));
}

Expand All @@ -54,12 +50,6 @@ private CrossWords getCrossWordsById(String id) {
);
}

private void updateViewCount(CrossWords crossWords) {
QuestionInfos questionInfos = crossWords.getQuestionInfos();
questionInfos.addViewCount();
questionInfoRepository.save(questionInfos);
}

private PuzzleDto crossWordsToPuzzleDto(CrossWords crossWords, boolean includeValue) {
return PuzzleDto.builder()
.id(String.valueOf(crossWords.getId()))
Expand Down Expand Up @@ -166,4 +156,16 @@ private Direction getDirection(String sort) {
}
throw new BadRequestException(Error.ILLEGAL_SORT_REQUEST.getMessage());
}

@Override
public void updatePuzzleHits(String puzzleId) {
updateViewCount(puzzleId);
}

private void updateViewCount(String puzzleId) {
CrossWords crossWords = getCrossWordsById(puzzleId);
QuestionInfos questionInfos = crossWords.getQuestionInfos();
questionInfos.addViewCount();
questionInfoRepository.save(questionInfos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.crosspuzzleserver.service.puzzle.spi;

public interface Hits {

public boolean isHit(String cookieValue, String boardId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.crosspuzzleserver.service.spi;
package com.example.crosspuzzleserver.service.puzzle.spi;

import com.example.crosspuzzleserver.service.dto.puzzle.PuzzleDto;
import com.example.crosspuzzleserver.service.dto.puzzle.PuzzleListDto;
Expand All @@ -12,4 +12,6 @@ public interface PuzzleService {

boolean updatePuzzleSuccessCount(String puzzleId);

void updatePuzzleHits(String puzzleId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.crosspuzzleserver.util.cookie;

import java.time.Duration;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CookieService {

public String getCookie() {
String uuid = UUID.randomUUID().toString();
ResponseCookie responseCookie = ResponseCookie
.from("userCookie", uuid)
.domain("api.cross-word.online")
// .domain("localhost")
.path("/")
.httpOnly(true)
.secure(false)
.maxAge(Duration.ofDays(14))
.sameSite("Strict")
.build();
return responseCookie.toString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
@SpringBootTest
class CrossPuzzleServerApplicationTests {

@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.example.crosspuzzleserver.repository.CrossWordsCustomQuery;
import com.example.crosspuzzleserver.repository.CrossWordsRepository;
import com.example.crosspuzzleserver.service.spi.PuzzleService;
import com.example.crosspuzzleserver.service.puzzle.spi.PuzzleService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down

0 comments on commit 8233e95

Please sign in to comment.