Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -36,11 +36,12 @@ public ResponseEntity<Object> postCommunity(@RequestBody CommunityRequestDto com
@GetMapping()
public ResponseEntity<Object> getCommunity(@RequestParam(value = "query", required = false, defaultValue = "all") String query,
@RequestParam(value = "tag", required = false, defaultValue = "") String tag,
@RequestParam(value = "userId", required = false, defaultValue = "") String userId,
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
log.info("[CommunityController] 글 목록 조회 시작");
try{
return ResponseEntity.ok(communityService.getCommunityList(query, tag, size, page));
return ResponseEntity.ok(communityService.getCommunityList(query, tag, userId, size, page));
} catch (CustomException e) {
log.error(e.getMessage());
return ResponseEntity.badRequest().body(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ResponseEntity<Object> getAllContent(@RequestParam(value = "query", requi
} else if(userId.isEmpty()){
return ResponseEntity.ok(contentService.getContentQueryWithLevel(query, level, size, page));
} else {
return ResponseEntity.ok(contentService.getContentWithUserId(userId));
return ResponseEntity.ok(contentService.getContentWithUserId(userId, query, level, size, page));
}
} catch (CustomException e) {
log.error("[getAllContent] 컨텐츠 조회 실패", e);
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/example/prdoit/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.example.prdoit.controller;

import com.example.prdoit.dto.user.ChangePasswordDto;
import com.example.prdoit.dto.user.LoginDto;
import com.example.prdoit.dto.user.UserDto;
import com.example.prdoit.exception.CustomException;
import com.example.prdoit.service.user.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;


Expand Down Expand Up @@ -88,4 +90,55 @@ public ResponseEntity<Object> login(@RequestBody LoginDto loginDto) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@GetMapping("/findId/{email}")
public ResponseEntity<Object> findId(@PathVariable String email){
log.info("[findId] 아이디 찾기 시작");

try{
return ResponseEntity.ok(userService.findId(email));
} catch (CustomException e) {
log.error("[findId] 아이디 찾기 실패 - 이유: {}", e.getMessage());
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@GetMapping("/confirm/{userId}/{email}")
public ResponseEntity<Object> checkIdAndEmail(@PathVariable String userId, @PathVariable String email){
log.info("[checkIdAndEmail] 아이디와 이메일 확인 시작");

try{
userService.checkIdAndEmail(userId, email);
return ResponseEntity.ok("아이디와 이메일이 일치합니다.");
} catch (CustomException e) {
log.error("[checkIdAndEmail] 아이디와 이메일 확인 실패 - 이유: {}", e.getMessage());
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@PatchMapping("/changePassword")
public ResponseEntity<Object> changePassword(@RequestBody ChangePasswordDto changePasswordDto){
log.info("[changePassword] 비밀번호 변경 시작");
try {
userService.changePassword(changePasswordDto.getUserId(), changePasswordDto.getNewPassword());
return ResponseEntity.ok("비밀번호 변경에 성공했습니다.");
} catch (CustomException e) {
log.error("[changePassword] 비밀번호 변경 실패 - 이유: {}", e.getMessage());
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@PostMapping("/auth/password")
public ResponseEntity<Object> checkPassword(@RequestBody LoginDto loginDto) {
log.info("[checkPassword] 비밀번호 확인 시작");

try {
userService.checkPassword(loginDto.getUserId(), loginDto.getPassword());
return ResponseEntity.ok("비밀번호가 일치합니다.");
} catch (CustomException e) {
log.error("[checkPassword] 비밀번호 불일치 - 이유: {}", e.getMessage());
return ResponseEntity.badRequest().body(e.getMessage());
}
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/example/prdoit/dto/user/ChangePasswordDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.prdoit.dto.user;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ChangePasswordDto {

private String userId;
private String newPassword;
}
12 changes: 1 addition & 11 deletions src/main/java/com/example/prdoit/dto/user/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,5 @@ public class UserDto {
private String name;
private String email;
private String nickname;
//
@Override
public String toString() {
return "UserDto{" +
"id='" + id + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", email='" + email + '\'' +
", nickname='" + nickname + '\'' +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface CommunityTableRepository extends JpaRepository<CommunityTable,
@Query("SELECT c FROM CommunityTable c WHERE c.communityTitle LIKE %:query% OR c.communityContent LIKE %:query%")
Page<CommunityTable> findAllByCommunityTitleContaining(String query, Pageable pageable);

@Query("SELECT c FROM CommunityTable c WHERE c.id.id = :userId")
Page<CommunityTable> findAllByUserId(String userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ public interface ContentTableRepository extends JpaRepository<ContentTable, Stri
@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId")
List<ContentResponseDto> findAllByUserId(@Param("userId") String userId);
Page<ContentResponseDto> findAllByUserIdPagination(@Param("userId") String userId, Pageable pageable);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId")
Page<ContentResponseDto> findAllByUserIdPagination(@Param("userId") String userId, Pageable pageable);
"FROM ContentTable c WHERE c.userId.id = :userId AND c.contentLevel = :level")
Page<ContentResponseDto> findAllByUserIdAndContentLevel(String userId, int level, Pageable pageable);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId AND c.contentTitle LIKE %:query%")
Page<ContentResponseDto> findAllByUserIdAndQueryContaining(String userId, String query, Pageable pageable);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId AND c.contentLevel = :level AND c.contentTitle LIKE %:query%")
Page<ContentResponseDto> findAllByUserIdAndContentLevelAndQueryContaining(String userId, int level, String query, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.example.prdoit.model.IdTable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface IdTableRepository extends JpaRepository<IdTable, String> {
IdTable findByEmail(String userEmail);
Optional<IdTable> findByEmail(String userEmail);
IdTable findByNickname(String userNickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface CommunityService {

void postCommunity(CommunityRequestDto communityRequestDto);

List<CommunityResponseDto> getCommunityList(String query, String tag, int size, int page);
List<CommunityResponseDto> getCommunityList(String query, String tag, String userId, int size, int page);

void patchCommunity(CommunityPatchDto communityPatchDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void postCommunity(CommunityRequestDto communityRequestDto) {
}

@Override
public List<CommunityResponseDto> getCommunityList(String query, String tag, int size, int page){
public List<CommunityResponseDto> getCommunityList(String query, String tag, String userId, int size, int page){
log.info("[CommunityServiceImpl] 글 목록 조회 시작");
Pageable pageable = PageRequest.of(page - 1, size);
try {
Expand Down Expand Up @@ -98,6 +98,25 @@ public List<CommunityResponseDto> getCommunityList(String query, String tag, int
.toList();
}

if(!userId.isEmpty()) {
List<CommunityTable> communityTableList = communityTableRepository.findAllByUserId(userId, pageable).toList();
return communityTableList.stream()
.map(communityTable -> CommunityResponseDto.builder()
.communityId(communityTable.getCommunityId())
.communityTitle(communityTable.getCommunityTitle())
.commentCount(communityTable.getCommentTable().size())
.userNickname(communityTable.getId().getNickname())
.communityCreatedDate(communityTable.getCommunityDate())
.communityTagList(communityTable.getCommunityTagTable().stream()
.map(CommunityTagTable -> CommunityTagResponseDto.builder()
.communityTagId(CommunityTagTable.getCommunityTagId())
.communityTag(CommunityTagTable.getCommunityTag())
.build())
.toList())
.build())
.toList();
}

return communityTableRepository.findAll(pageable).stream()
.map(communityTable -> CommunityResponseDto.builder()
.communityId(communityTable.getCommunityId())
Expand Down Expand Up @@ -163,6 +182,8 @@ public CommunityDetailResponseDto getCommunityDetail(String communityId) {
log.info("[CommunityServiceImpl] 글 상세 조회 시작");
CommunityTable communityTable = communityTableRepository.findById(communityId).orElseThrow(() -> new IllegalArgumentException("해당 글이 없습니다."));
try {
communityTable.setCommunityView(communityTable.getCommunityView() + 1);
communityTableRepository.save(communityTable);
return CommunityDetailResponseDto.builder()
.communityId(communityTable.getCommunityId())
.communityTitle(communityTable.getCommunityTitle())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ContentService {

List<ContentResponseDto> getContentQueryWithLevel(String query, int level, int size, int page);

List<ContentResponseDto> getContentWithUserId(String userId);
List<ContentResponseDto> getContentWithUserId(String userId, String query, int level, int size, int page);

void updateContent(ContentUpdateDto contentUpdateDto);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.prdoit.service.content;

import com.example.prdoit.dto.content.*;
import com.example.prdoit.exception.CustomException;
import com.example.prdoit.model.ContentTable;
import com.example.prdoit.model.IdTable;
import com.example.prdoit.repository.ContentTableRepository;
Expand Down Expand Up @@ -31,17 +32,17 @@ public List<ContentResponseDto> getContentQuery(String query, int size, int page
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
}
try {
if(query.equals("all")) {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllList(size, offset);
if(contentResponseDtoList.isEmpty()){
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
if(!query.equals("all")){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByQueryContaining(query, size, offset);
if (contentResponseDtoList.isEmpty()) {
throw new RuntimeException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
}
} else {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByQueryContaining(query, size, offset);
if(contentResponseDtoList.isEmpty()){
throw new RuntimeException("검색 결과가 없습니다.");
} else{
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllList(size, offset);
if (contentResponseDtoList.isEmpty()) {
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
} else {
return contentResponseDtoList;
}
Expand All @@ -60,17 +61,17 @@ public List<ContentResponseDto> getContentQueryWithLevel(String query, int level
}
int offset = (page - 1) * size;
try{
if(query.equals("all")) {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByContentLevel(level, size, offset);
if(!query.equals("all")){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllQueryContainingAndContentLevel(query, level, size, offset);
if(contentResponseDtoList.isEmpty()){
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
throw new RuntimeException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
}
} else {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllQueryContainingAndContentLevel(query, level, size, offset);
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByContentLevel(level, size, offset);
if(contentResponseDtoList.isEmpty()){
throw new RuntimeException("검색 결과가 없습니다.");
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
} else {
return contentResponseDtoList;
}
Expand All @@ -82,24 +83,49 @@ public List<ContentResponseDto> getContentQueryWithLevel(String query, int level
}

@Override
public List<ContentResponseDto> getContentWithUserId(String userId){
public List<ContentResponseDto> getContentWithUserId(String userId, String query, int level, int size, int page){
IdTable idTable = IdTableRepository.findById(userId).orElseThrow
(() -> new RuntimeException("존재하지 않는 사용자입니다."));
if(idTable.getContentTable().isEmpty()) {
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
}
int offset = (page - 1) * size;
try{
return idTable.getContentTable().stream().map(contentTable -> ContentResponseDto.builder()
.contentId(contentTable.getContentId())
.contentTitle(contentTable.getContentTitle())
.contentView(contentTable.getContentView())
.contentLike(contentTable.getContentLike())
.contentLevel(contentTable.getContentLevel())
.writer(contentTable.getUserId().getNickname())
.build()).toList();
} catch (Exception e) {
if(query.equals("all") && level == -1){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdPagination(userId, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
}
} else if(!query.equals("all") && level == -1){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdAndQueryContaining(userId, query, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
}
} else if(query.equals("all")){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdAndContentLevel(userId, level, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
}
} else {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdAndContentLevelAndQueryContaining(userId, level, query, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
}
}
} catch (CustomException e) {
log.error(e.getMessage());
throw new RuntimeException("쿼리 조회에 실패했습니다.");
throw new CustomException("검색 결과가 없습니다.");
} catch (RuntimeException e) {
log.error(e.getMessage());
throw new RuntimeException("조회 실패");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public interface UserService {

void login(String userId, String password);

String findId(String email);

void checkIdAndEmail(String userId, String email);

void changePassword(String userId, String password);

void checkPassword(String userId, String password);

}
Loading