Skip to content

Commit

Permalink
Merge pull request #33 from 9oormthon-univ/hyundong
Browse files Browse the repository at this point in the history
fix :: 발표에 맞춰 기능 수정
  • Loading branch information
hyundong-L authored Nov 23, 2024
2 parents ae327a2 + b8b1b6b commit 501f50d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package com.danpoong.onchung.domain.auth.controller;

import com.danpoong.onchung.domain.auth.dto.LoginResponseDto;
import com.danpoong.onchung.domain.auth.dto.SignUpRequestDto;
import com.danpoong.onchung.domain.auth.dto.AccessTokenDto;
import com.danpoong.onchung.domain.auth.service.AuthService;
import com.danpoong.onchung.global.security.jwt.dto.TokenDto;
import com.danpoong.onchung.global.template.ResponseTemplate;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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;

Expand All @@ -23,21 +11,21 @@
public class AuthController {
private final AuthService authService;

@Operation(summary = "로그인", description = "만약 엑세스 토큰이 존재하지 않으면 쿠키에서 리프레시 토큰을 찾아 로그인")
@PostMapping("/login")
public ResponseTemplate<LoginResponseDto> login(@AuthenticationPrincipal Long userId, HttpServletRequest request, HttpServletResponse response) {
return new ResponseTemplate<>(HttpStatus.OK, "로그인 성공", authService.login(userId, request, response));
}

@Operation(summary = "회원가입", description = "아이디 중복 확인 버튼 클릭 시 닉네임, 아이디 같이 보내면 중복 검사 밑 회원가입 진행")
@PostMapping("/signup")
public ResponseTemplate<AccessTokenDto> signup(@RequestBody SignUpRequestDto sign, HttpServletResponse response) {
return new ResponseTemplate<>(HttpStatus.CREATED, "회원가입 성공", authService.signUp(sign, response));
}

@Operation(summary = "토큰 재발급")
@PostMapping("/reissue")
public ResponseTemplate<AccessTokenDto> reissue(@RequestBody TokenDto tokenRequestDto, HttpServletRequest request, HttpServletResponse response) {
return new ResponseTemplate<>(HttpStatus.OK, "토큰 재발급 성공", authService.reissue(request, response, tokenRequestDto));
}
// @Operation(summary = "로그인", description = "만약 엑세스 토큰이 존재하지 않으면 쿠키에서 리프레시 토큰을 찾아 로그인")
// @PostMapping("/login")
// public ResponseTemplate<LoginResponseDto> login(@AuthenticationPrincipal Long userId, HttpServletRequest request, HttpServletResponse response) {
// return new ResponseTemplate<>(HttpStatus.OK, "로그인 성공", authService.login(userId, request, response));
// }
//
// @Operation(summary = "회원가입", description = "아이디 중복 확인 버튼 클릭 시 닉네임, 아이디 같이 보내면 중복 검사 밑 회원가입 진행")
// @PostMapping("/signup")
// public ResponseTemplate<AccessTokenDto> signup(@RequestBody SignUpRequestDto sign, HttpServletResponse response) {
// return new ResponseTemplate<>(HttpStatus.CREATED, "회원가입 성공", authService.signUp(sign, response));
// }
//
// @Operation(summary = "토큰 재발급")
// @PostMapping("/reissue")
// public ResponseTemplate<AccessTokenDto> reissue(@RequestBody TokenDto tokenRequestDto, HttpServletRequest request, HttpServletResponse response) {
// return new ResponseTemplate<>(HttpStatus.OK, "토큰 재발급 성공", authService.reissue(request, response, tokenRequestDto));
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,14 +25,13 @@ public ResponseTemplate<PolicyResponseDto> getPolicy(@PathVariable("policy_id")
return new ResponseTemplate<>(HttpStatus.OK, "정책 단일 조회 성공", policyService.getPolicy(policyId, request, response));
}

@Operation(summary = "정책 즐겨찾기", description = "정책이 존재하면 삭제, 아니라면 추가")
@PatchMapping("/favorite/{policy_id}")
public ResponseTemplate<?> favoritePolicy(
@AuthenticationPrincipal Long userId,
@PathVariable("policy_id") Long policyId
) {
policyService.favoritePolicy(userId, policyId);
return new ResponseTemplate<>(HttpStatus.OK, "즐겨찾기 추가 / 삭제 성공");
}

// @Operation(summary = "정책 즐겨찾기", description = "정책이 존재하면 삭제, 아니라면 추가")
// @PatchMapping("/favorite/{policy_id}")
// public ResponseTemplate<?> favoritePolicy(
// @AuthenticationPrincipal Long userId,
// @PathVariable("policy_id") Long policyId
// ) {
// policyService.favoritePolicy(userId, policyId);
// return new ResponseTemplate<>(HttpStatus.OK, "즐겨찾기 추가 / 삭제 성공");
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.danpoong.onchung.domain.policy.dto.PolicyResponseDto;
import com.danpoong.onchung.domain.policy.repository.PolicyRepository;
import com.danpoong.onchung.domain.public_office.domain.PublicOffice;
import com.danpoong.onchung.domain.user.domain.UserInfo;
import com.danpoong.onchung.domain.user.repository.UserRepository;
import com.danpoong.onchung.global.security.util.CookieUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,12 +27,12 @@ public class PolicyService {
public PolicyResponseDto getPolicy(Long policyId, HttpServletRequest request, HttpServletResponse response) {
Policy policy = policyRepository.findById(policyId).orElseThrow(() -> new RuntimeException("해당 ID의 정책이 존재하지 않습니다."));

List<Long> recentPolicyList = CookieUtil.getLongListFromCookie(request, RECENT_POLICY_COOKIE_NAME);
// List<Long> recentPolicyList = CookieUtil.getLongListFromCookie(request, RECENT_POLICY_COOKIE_NAME);

if (recentPolicyList == null) {
recentPolicyList = new ArrayList<>();
}
CookieUtil.setLongListCookie(response, RECENT_POLICY_COOKIE_NAME, recentPolicyList, COOKIE_EXPIRE_SECONDS);
// if (recentPolicyList == null) {
// recentPolicyList = new ArrayList<>();
// }
// CookieUtil.setLongListCookie(response, RECENT_POLICY_COOKIE_NAME, recentPolicyList, COOKIE_EXPIRE_SECONDS);

return PolicyResponseDto.builder()
.policyId(policy.getId())
Expand All @@ -53,20 +51,20 @@ public PolicyResponseDto getPolicy(Long policyId, HttpServletRequest request, Ht
.build();
}

@Transactional
public void favoritePolicy(Long userId, Long policyId) {
UserInfo userInfo = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("사용자가 존재하지 않습니다."));

Policy policy = policyRepository.findById(policyId).orElseThrow(() -> new RuntimeException("해당 ID의 정책이 존재하지 않습니다."));

boolean isPresent = userInfo.getFavoritePolicies().contains(policy);

if (!isPresent) {
userInfo.addFavoritePolicy(policy);
} else {
userInfo.removeFavoritePolicy(policy);
}
}
// @Transactional
// public void favoritePolicy(Long userId, Long policyId) {
// UserInfo userInfo = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("사용자가 존재하지 않습니다."));
//
// Policy policy = policyRepository.findById(policyId).orElseThrow(() -> new RuntimeException("해당 ID의 정책이 존재하지 않습니다."));
//
// boolean isPresent = userInfo.getFavoritePolicies().contains(policy);
//
// if (!isPresent) {
// userInfo.addFavoritePolicy(policy);
// } else {
// userInfo.removeFavoritePolicy(policy);
// }
// }

private List<String> getPublicOfficeName(List<PublicOffice> publicOffices) {
List<String> publicOfficeNames = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -22,11 +21,11 @@ public class WordController {
@Operation(summary = "카테고리 별 단어 조회", description = "한 페이지의 데이터 개수 설정 가능")
@GetMapping("/category/{page_num}")
public ResponseTemplate<Page<WordSummaryResponseDto>> getWords(
@AuthenticationPrincipal Long userId,
// @AuthenticationPrincipal Long userId,
@RequestParam String category,
@PathVariable("page_num") int pageNum
) {
return new ResponseTemplate<>(HttpStatus.OK, "단어 카테고리 조회 성공", wordService.getWordsByCategory(userId, category, pageNum));
return new ResponseTemplate<>(HttpStatus.OK, "단어 카테고리 조회 성공", wordService.getWordsByCategory(category, pageNum));
}

@Operation(summary = "단어 상세 조회")
Expand All @@ -35,32 +34,32 @@ public ResponseTemplate<WordResponseDto> getWord(@PathVariable("word_id") Long w
return new ResponseTemplate<>(HttpStatus.OK, "단어 상세 조회 성공", wordService.getWord(wordId));
}

@Operation(summary = "단어 북마크", description = "만약 기존에 북마크에 있다면 삭제, 아니라면 추가")
@PatchMapping("/bookmark/{word_id}")
public ResponseTemplate<?> favoriteWord(
@AuthenticationPrincipal Long userId,
@PathVariable("word_id") Long wordId
) {
wordService.favoriteWord(userId, wordId);
return new ResponseTemplate<>(HttpStatus.OK, "북마크 추가 / 삭제 성공");
}

@Operation(summary = "단어 북마크 조회")
@GetMapping("/book-mark/{page_num}")
public ResponseTemplate<Page<WordSummaryResponseDto>> getBookmarks(
@AuthenticationPrincipal Long userId,
@PathVariable("page_num") int pageNum
) {
return new ResponseTemplate<>(HttpStatus.OK, "북마크 단어 조회 성공", wordService.getBookmarkedWords(userId, pageNum));
}
// @Operation(summary = "단어 북마크", description = "만약 기존에 북마크에 있다면 삭제, 아니라면 추가")
// @PatchMapping("/bookmark/{word_id}")
// public ResponseTemplate<?> favoriteWord(
// @AuthenticationPrincipal Long userId,
// @PathVariable("word_id") Long wordId
// ) {
// wordService.favoriteWord(userId, wordId);
// return new ResponseTemplate<>(HttpStatus.OK, "북마크 추가 / 삭제 성공");
// }
//
// @Operation(summary = "단어 북마크 조회")
// @GetMapping("/book-mark/{page_num}")
// public ResponseTemplate<Page<WordSummaryResponseDto>> getBookmarks(
// @AuthenticationPrincipal Long userId,
// @PathVariable("page_num") int pageNum
// ) {
// return new ResponseTemplate<>(HttpStatus.OK, "북마크 단어 조회 성공", wordService.getBookmarkedWords(userId, pageNum));
// }

@Operation(summary = "단어 검색", description = "type에는 카테고리 종류 중 하나 / 공백 / 북마크 중 하나가 들어갈 수 있다.")
@Operation(summary = "단어 검색", description = "type에는 카테고리 종류 중 하나 / 공백 중 하나가 들어갈 수 있다.")
@GetMapping("/search")
public ResponseTemplate<WordSummaryResponseDto> searchWord(
@AuthenticationPrincipal Long userId,
// @AuthenticationPrincipal Long userId,
@RequestParam String type,
@RequestParam String word
) {
return new ResponseTemplate<>(HttpStatus.OK, "용어 검색 성공", wordService.searchWord(userId, type, word));
return new ResponseTemplate<>(HttpStatus.OK, "용어 검색 성공", wordService.searchWord(type, word));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public class WordService {

private static final int WORD_PAGE_SIZE = 10;

public Page<WordSummaryResponseDto> getWordsByCategory(Long userId, String category, int page) {
public Page<WordSummaryResponseDto> getWordsByCategory(String category, int page) {
Page<Word> words = wordRepository.findByCategory(WordCategory.checkCategory(category), PageRequest.of(page, WORD_PAGE_SIZE));

return words.map(word -> {
boolean isBookmarked = false;

if (userId != null) {
UserInfo userInfo = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("해당 ID의 사용자가 존재하지 않습니다."));

isBookmarked = userInfo.getFavoriteWords().contains(word);
}
// if (userId != null) {
// UserInfo userInfo = userRepository.findById(userId)
// .orElseThrow(() -> new RuntimeException("해당 ID의 사용자가 존재하지 않습니다."));
//
// isBookmarked = userInfo.getFavoriteWords().contains(word);
// }

return WordSummaryResponseDto.builder()
.wordId(word.getId())
Expand Down Expand Up @@ -94,27 +94,27 @@ public Page<WordSummaryResponseDto> getBookmarkedWords(Long userId, int page) {
);
}

public WordSummaryResponseDto searchWord(Long userId, String type, String term) {
UserInfo userInfo = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("해당 ID의 사용자가 존재하지 않습니다."));
public WordSummaryResponseDto searchWord(String type, String term) {
// UserInfo userInfo = userRepository.findById(userId)
// .orElseThrow(() -> new RuntimeException("해당 ID의 사용자가 존재하지 않습니다."));

Word word = wordRepository.findByTerm(term).orElse(null);
if (word == null) {
return WordSummaryResponseDto.empty();
}

// 북마크 검색
if (type.equals("북마크")) {
return searchBookmark(userInfo, word);
}

// // 북마크 검색
// if (type.equals("북마크")) {
// return searchBookmark(userInfo, word);
// }
//
// 전체 검색
if (type.isEmpty()) {
return searchAll(userInfo, word);
return searchAll(word);
}

// 카테고리 검색
return searchByCategory(userInfo, type, word);
return searchByCategory(type, word);
}

// 북마크 내 검색
Expand All @@ -128,21 +128,27 @@ private WordSummaryResponseDto searchBookmark(UserInfo userInfo, Word word) {
}

// 전체 검색
private WordSummaryResponseDto searchAll(UserInfo userInfo, Word word) {
boolean isBookmark = userInfo.getFavoriteWords().contains(word);
return createWordSummaryResponse(word, isBookmark);
private WordSummaryResponseDto searchAll(Word word) {
// boolean isBookmark = userInfo.getFavoriteWords().contains(word);
return createWordSummaryResponse(word, false);
}

// 카테고리 내 검색
private WordSummaryResponseDto searchByCategory(UserInfo userInfo, String type, Word word) {
private WordSummaryResponseDto searchByCategory(String type, Word word) {
WordCategory wordCategoryEng = WordCategory.checkCategory(type);
List<Word> wordList = wordRepository.findAllByCategory(wordCategoryEng);

if (wordList.contains(word) && userInfo.getFavoriteWords().contains(word)) {
return createWordSummaryResponse(word, true);
if (wordList.contains(word)) {
return createWordSummaryResponse(word, false);
}

return WordSummaryResponseDto.empty();

// if (wordList.contains(word) && userInfo.getFavoriteWords().contains(word)) {
// return createWordSummaryResponse(word, true);
// }
//
// return WordSummaryResponseDto.empty();
}

// 응답 객체 생성
Expand Down
Loading

0 comments on commit 501f50d

Please sign in to comment.