From 0f8e69d50e69a8ee6ae2f945e50b15ea4f45b4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EA=B0=80=EC=98=81?= Date: Wed, 23 Oct 2024 02:49:48 +0900 Subject: [PATCH] feat: bookMark --- build.gradle | 2 +- .../bookMark/domain/BookMarkEntity.java | 2 +- .../presentation/BookMarkController.java | 5 +- .../presentation/dto/res/BookMarkLoadRes.java | 29 +++++----- .../repository/BookMarkRepository.java | 6 +-- .../bookMark/service/BookMarkService.java | 2 +- .../bookMark/service/BookMarkServiceImpl.java | 54 +++++++++---------- .../query/CommunityQueryRepositoryImpl.java | 1 - .../dto/response/CommunityResponse.java | 2 + .../java/com/dear/domain/food/enums/Food.java | 2 +- .../dear/domain/food/enums/FoodDetail.java | 4 +- .../dto/response/ProfessorLoadResponse.java | 6 ++- .../query/QueryDslRepositoryImpl.java | 5 +- .../dear/domain/user/domain/UserEntity.java | 1 + .../global/properties/NeisProperties.java | 14 ++--- src/main/resources/application.yml | 2 +- 16 files changed, 73 insertions(+), 64 deletions(-) diff --git a/build.gradle b/build.gradle index 2a4ab33..086704a 100644 --- a/build.gradle +++ b/build.gradle @@ -96,7 +96,7 @@ dependencies { //health check implementation 'org.springframework.boot:spring-boot-starter-actuator' - //neis +// //neis // implementation 'org.springframework:spring-context:6.0.6' // implementation 'org.springframework:spring-web:6.0.6' // implementation 'org.springframework:spring-tx:6.0.6' diff --git a/src/main/java/com/dear/domain/bookMark/domain/BookMarkEntity.java b/src/main/java/com/dear/domain/bookMark/domain/BookMarkEntity.java index 1ec02f7..97d54ea 100644 --- a/src/main/java/com/dear/domain/bookMark/domain/BookMarkEntity.java +++ b/src/main/java/com/dear/domain/bookMark/domain/BookMarkEntity.java @@ -28,7 +28,7 @@ public class BookMarkEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "professor_id") - private ProfessorEntity professorEntity; + private UserEntity professorEntity; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "school_id") diff --git a/src/main/java/com/dear/domain/bookMark/presentation/BookMarkController.java b/src/main/java/com/dear/domain/bookMark/presentation/BookMarkController.java index 4f44d8a..7117b94 100644 --- a/src/main/java/com/dear/domain/bookMark/presentation/BookMarkController.java +++ b/src/main/java/com/dear/domain/bookMark/presentation/BookMarkController.java @@ -33,10 +33,9 @@ public Response bookMark( @Operation(description = "북마크 불러오기") @GetMapping public ResponseData> loadBookMark( - @ModelAttribute PageRequest pageRequest, - @RequestParam("type") BookMarkEnum bookMarkEnum +// @ModelAttribute PageRequest pageRequest ){ - return ResponseData.of(HttpStatus.OK, "성공", bookMarkService.loadBookMark(pageRequest, bookMarkEnum)); + return ResponseData.of(HttpStatus.OK, "성공", bookMarkService.loadBookMark()); } } diff --git a/src/main/java/com/dear/domain/bookMark/presentation/dto/res/BookMarkLoadRes.java b/src/main/java/com/dear/domain/bookMark/presentation/dto/res/BookMarkLoadRes.java index b77ac6c..a524f94 100644 --- a/src/main/java/com/dear/domain/bookMark/presentation/dto/res/BookMarkLoadRes.java +++ b/src/main/java/com/dear/domain/bookMark/presentation/dto/res/BookMarkLoadRes.java @@ -1,29 +1,34 @@ package com.dear.domain.bookMark.presentation.dto.res; -import com.dear.domain.professor.domain.ProfessorEntity; +import com.dear.domain.major.domain.MajorEntity; import com.dear.domain.school.domain.SchoolEntity; +import com.dear.domain.user.domain.UserEntity; + +import java.util.Optional; public record BookMarkLoadRes( - String id, - String name, - String content, - String type + String Id, + String Name, + Optional school, + Optional major ) { - public static BookMarkLoadRes of(ProfessorEntity professorEntity, boolean isBookMarked){ + public static BookMarkLoadRes of(UserEntity professorEntity, Optional majorEntity) { return new BookMarkLoadRes( professorEntity.getId().toString(), - professorEntity.getUser().getName(), - professorEntity.getMajor().getMClass(), - professorEntity.getUser().getRole().toString() + professorEntity.getName(), + Optional.ofNullable(professorEntity.getSchools()) + .map(SchoolEntity::getSchoolName), + Optional.ofNullable(majorEntity.orElse(null)) + .map(MajorEntity::getMClass) ); } - public static BookMarkLoadRes of(SchoolEntity schoolEntity, boolean isBookMarked){ + public static BookMarkLoadRes of(SchoolEntity schoolEntity){ return new BookMarkLoadRes( schoolEntity.getSeq(), schoolEntity.getSchoolName(), - schoolEntity.getSchoolIntro(), - "UNIVERCCITY" + null, + null ); } } diff --git a/src/main/java/com/dear/domain/bookMark/repository/BookMarkRepository.java b/src/main/java/com/dear/domain/bookMark/repository/BookMarkRepository.java index bb703d1..2d3f65f 100644 --- a/src/main/java/com/dear/domain/bookMark/repository/BookMarkRepository.java +++ b/src/main/java/com/dear/domain/bookMark/repository/BookMarkRepository.java @@ -13,10 +13,10 @@ @Repository public interface BookMarkRepository extends JpaRepository { - - BookMarkEntity findByProfessorEntityAndUser(ProfessorEntity professorEntity, UserEntity userEntity); + + BookMarkEntity findByProfessorEntityAndUser(UserEntity professor, UserEntity userEntity); @Query("SELECT CASE WHEN COUNT(b) > 0 THEN TRUE ELSE FALSE END FROM BookMarkEntity b WHERE b.professorEntity = :professor AND b.user = :user") - Boolean existsByProfessorEntityAndUser(@Param("professor") ProfessorEntity professorEntity, @Param("user") UserEntity userEntity); + Boolean existsByProfessorEntityAndUser(@Param("professor") UserEntity professor, @Param("user") UserEntity userEntity); BookMarkEntity findBySchoolEntityAndUser(SchoolEntity schoolEntity, UserEntity userEntity); @Query("SELECT CASE WHEN COUNT(b) > 0 THEN TRUE ELSE FALSE END FROM BookMarkEntity b WHERE b.schoolEntity = :university AND b.user = :user") diff --git a/src/main/java/com/dear/domain/bookMark/service/BookMarkService.java b/src/main/java/com/dear/domain/bookMark/service/BookMarkService.java index ea2a8cf..7ac1d04 100644 --- a/src/main/java/com/dear/domain/bookMark/service/BookMarkService.java +++ b/src/main/java/com/dear/domain/bookMark/service/BookMarkService.java @@ -8,5 +8,5 @@ public interface BookMarkService { void convertOfBookMark(Long id, BookMarkEnum bookMarkEnum); - List loadBookMark(PageRequest pageRequest, BookMarkEnum bookMarkEnum); + List loadBookMark(); } diff --git a/src/main/java/com/dear/domain/bookMark/service/BookMarkServiceImpl.java b/src/main/java/com/dear/domain/bookMark/service/BookMarkServiceImpl.java index f7ef98c..c43e6cd 100644 --- a/src/main/java/com/dear/domain/bookMark/service/BookMarkServiceImpl.java +++ b/src/main/java/com/dear/domain/bookMark/service/BookMarkServiceImpl.java @@ -7,6 +7,8 @@ import com.dear.domain.community.domain.CommunityEntity; import com.dear.domain.community.exception.CommunityNotFoundException; import com.dear.domain.community.domain.repository.CommunityRepository; +import com.dear.domain.major.domain.MajorEntity; +import com.dear.domain.major.repository.MajorRepository; import com.dear.domain.professor.domain.ProfessorEntity; import com.dear.domain.professor.exception.ProfessorNotFoundException; import com.dear.domain.professor.repository.ProfessorJpaRepository; @@ -15,6 +17,7 @@ import com.dear.domain.user.domain.UserEntity; import com.dear.domain.user.dto.User; import com.dear.domain.user.exception.UserNotFoundException; +import com.dear.domain.user.mapper.UserMapper; import com.dear.domain.user.repository.UserRepository; import com.dear.global.common.dto.request.PageRequest; import com.dear.global.common.image.entity.ImageEntity; @@ -24,6 +27,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -36,8 +40,9 @@ public class BookMarkServiceImpl implements BookMarkService { private final BookMarkRepository bookMarkRepository; private final UserSecurity userSecurity; private final UserRepository userRepository; - private final ProfessorJpaRepository professorJpaRepository; private final SchoolRepository schoolRepository; + private final UserMapper userMapper; + private final MajorRepository majorRepository; @Override @Transactional @@ -54,10 +59,10 @@ public void convertOfBookMark(Long id, BookMarkEnum bookMarkEnum) { } private void handleProfessorBookmark(Long id, UserEntity userEntity) { - ProfessorEntity professorEntity = professorJpaRepository.findById(id) + UserEntity user = userRepository.findById(id) .orElseThrow(() -> ProfessorNotFoundException.EXCEPTION); - toggleProfessorBookmark(professorEntity, userEntity); + toggleProfessorBookmark(user, userEntity); } private void handleSchoolBookmark(Long id, UserEntity userEntity) { @@ -67,13 +72,13 @@ private void handleSchoolBookmark(Long id, UserEntity userEntity) { toggleSchoolBookmark(schoolEntity, userEntity); } - private void toggleProfessorBookmark(ProfessorEntity professorEntity, UserEntity userEntity) { - if (bookMarkRepository.existsByProfessorEntityAndUser(professorEntity, userEntity)) { - BookMarkEntity bookMarkEntity = bookMarkRepository.findByProfessorEntityAndUser(professorEntity, userEntity); + private void toggleProfessorBookmark(UserEntity professor, UserEntity userEntity) { + if (bookMarkRepository.existsByProfessorEntityAndUser(professor, userEntity)) { + BookMarkEntity bookMarkEntity = bookMarkRepository.findByProfessorEntityAndUser(professor, userEntity); bookMarkRepository.delete(bookMarkEntity); } else { BookMarkEntity newBookMark = BookMarkEntity.builder() - .professorEntity(professorEntity) + .professorEntity(professor) .user(userEntity) .build(); bookMarkRepository.save(newBookMark); @@ -94,30 +99,23 @@ private void toggleSchoolBookmark(SchoolEntity schoolEntity, UserEntity userEnti } @Override - public List loadBookMark(PageRequest pageRequest, BookMarkEnum bookMarkEnum) { + public List loadBookMark() { + UserEntity userEntity = userMapper.toEntity(userSecurity.getUser()); List bookMarkEntity = bookMarkRepository.findAll(); - if (bookMarkEnum.equals(UNIVERSITY)){ - List bookMarkEntityList = bookMarkEntity.stream() - .map(bookMark -> { - return BookMarkLoadRes.of( - bookMark.getSchoolEntity(), - bookMarkRepository.existsBySchoolEntityAndUser(bookMark.getSchoolEntity(), bookMark.getUser()) - ); - }) - .toList(); - return bookMarkEntityList; - }else { - List bookMarkEntityList = bookMarkEntity.stream() - .map(bookMark -> { - return BookMarkLoadRes.of( - bookMark.getProfessorEntity(), - bookMarkRepository.existsByProfessorEntityAndUser(bookMark.getProfessorEntity(), bookMark.getUser()) - ); - }) - .toList(); - return bookMarkEntityList; + List bookMarkLoadRes = new ArrayList<>(); + + for (BookMarkEntity bookMark : bookMarkEntity) { + if (bookMarkRepository.existsBySchoolEntityAndUser(bookMark.getSchoolEntity(), userEntity)) { + BookMarkEntity bookMark1 = bookMarkRepository.findBySchoolEntityAndUser(bookMark.getSchoolEntity(), userEntity); + bookMarkLoadRes.add(BookMarkLoadRes.of(bookMark1.getSchoolEntity())); + }else { + BookMarkEntity bookMark1 = bookMarkRepository.findByProfessorEntityAndUser(bookMark.getProfessorEntity(), userEntity); + Optional majorEntity = majorRepository.findById(userEntity.getId()); + bookMarkLoadRes.add(BookMarkLoadRes.of(bookMark1.getProfessorEntity(), majorEntity)); + } } + return bookMarkLoadRes; } } \ No newline at end of file diff --git a/src/main/java/com/dear/domain/community/domain/repository/query/CommunityQueryRepositoryImpl.java b/src/main/java/com/dear/domain/community/domain/repository/query/CommunityQueryRepositoryImpl.java index bd74b3f..8884f2a 100644 --- a/src/main/java/com/dear/domain/community/domain/repository/query/CommunityQueryRepositoryImpl.java +++ b/src/main/java/com/dear/domain/community/domain/repository/query/CommunityQueryRepositoryImpl.java @@ -75,7 +75,6 @@ public CommunityOneResponse getCommunityByCommunityId(Long communityId) { } - @Override public List getCommunityList(PageRequest pageRequest) { diff --git a/src/main/java/com/dear/domain/community/dto/response/CommunityResponse.java b/src/main/java/com/dear/domain/community/dto/response/CommunityResponse.java index 3468435..d3d56d1 100644 --- a/src/main/java/com/dear/domain/community/dto/response/CommunityResponse.java +++ b/src/main/java/com/dear/domain/community/dto/response/CommunityResponse.java @@ -23,5 +23,7 @@ public class CommunityResponse { private Long comment; private String imagePath; private String profileImage; + private boolean isBookMark; + } diff --git a/src/main/java/com/dear/domain/food/enums/Food.java b/src/main/java/com/dear/domain/food/enums/Food.java index b9ac0bb..d4b491c 100644 --- a/src/main/java/com/dear/domain/food/enums/Food.java +++ b/src/main/java/com/dear/domain/food/enums/Food.java @@ -5,7 +5,7 @@ // //import java.io.Serializable; //import java.util.List; - +// //@Getter //@AllArgsConstructor //public class Food implements Serializable { diff --git a/src/main/java/com/dear/domain/food/enums/FoodDetail.java b/src/main/java/com/dear/domain/food/enums/FoodDetail.java index 46b5ca7..03e78b0 100644 --- a/src/main/java/com/dear/domain/food/enums/FoodDetail.java +++ b/src/main/java/com/dear/domain/food/enums/FoodDetail.java @@ -1,5 +1,5 @@ -package com.dear.domain.food.enums; - +//package com.dear.domain.food.enums; +// //import lombok.AllArgsConstructor; //import lombok.Getter; // diff --git a/src/main/java/com/dear/domain/professor/dto/response/ProfessorLoadResponse.java b/src/main/java/com/dear/domain/professor/dto/response/ProfessorLoadResponse.java index 9aa6d52..3152cab 100644 --- a/src/main/java/com/dear/domain/professor/dto/response/ProfessorLoadResponse.java +++ b/src/main/java/com/dear/domain/professor/dto/response/ProfessorLoadResponse.java @@ -1,10 +1,13 @@ package com.dear.domain.professor.dto.response; +import com.dear.domain.bookMark.domain.BookMarkEntity; import com.dear.domain.major.dto.Major; import com.dear.domain.school.dto.School; import com.dear.domain.user.dto.User; import com.dear.global.common.image.entity.ImageEntity; +import java.util.Optional; + public record ProfessorLoadResponse( Long userId, String name, @@ -12,8 +15,9 @@ public record ProfessorLoadResponse( String major, String email, String profileImage - ) { + + public static ProfessorLoadResponse of(User user, School school, Major major, ImageEntity profileImage) { return new ProfessorLoadResponse( user.getId(), diff --git a/src/main/java/com/dear/domain/professor/repository/query/QueryDslRepositoryImpl.java b/src/main/java/com/dear/domain/professor/repository/query/QueryDslRepositoryImpl.java index 9c29a40..e2fd383 100644 --- a/src/main/java/com/dear/domain/professor/repository/query/QueryDslRepositoryImpl.java +++ b/src/main/java/com/dear/domain/professor/repository/query/QueryDslRepositoryImpl.java @@ -3,7 +3,6 @@ import com.dear.domain.major.domain.MajorEntity; import com.dear.domain.professor.dto.response.ProfessorLoadResponse; import com.dear.domain.school.domain.SchoolEntity; -import com.dear.domain.user.domain.UserEntity; import com.dear.domain.user.enums.UserRole; import com.dear.global.common.dto.request.PageRequest; import com.dear.global.common.image.entity.ImageEntity; @@ -17,8 +16,8 @@ import java.util.List; +import static com.dear.domain.bookMark.domain.QBookMarkEntity.bookMarkEntity; import static com.dear.domain.major.domain.QMajorEntity.majorEntity; -import static com.dear.domain.professor.domain.QProfessorEntity.professorEntity; import static com.dear.domain.school.domain.QSchoolEntity.schoolEntity; import static com.dear.domain.user.domain.QUserEntity.userEntity; import static com.dear.global.common.image.entity.QImageEntity.imageEntity; @@ -32,6 +31,7 @@ public class QueryDslRepositoryImpl implements QueryDslRepository { @Override public List findProfessorWithPagination(PageRequest pageRequest) { + JPAQuery query = jpaQueryFactory .select(createProjection()) @@ -43,6 +43,7 @@ public List findProfessorWithPagination(PageRequest pageR .on(schoolEntity.seq.eq(userEntity.schoolId.seq)) .leftJoin(imageEntity) .on(imageEntity.profileId.eq(userEntity.id)) + .leftJoin(bookMarkEntity) .fetchJoin(); return query diff --git a/src/main/java/com/dear/domain/user/domain/UserEntity.java b/src/main/java/com/dear/domain/user/domain/UserEntity.java index 97f0a0b..caee9b3 100644 --- a/src/main/java/com/dear/domain/user/domain/UserEntity.java +++ b/src/main/java/com/dear/domain/user/domain/UserEntity.java @@ -1,5 +1,6 @@ package com.dear.domain.user.domain; +import com.dear.domain.major.domain.MajorEntity; import com.dear.domain.school.domain.SchoolEntity; import com.dear.domain.user.enums.UserRole; import com.dear.domain.user.enums.UserStatus; diff --git a/src/main/java/com/dear/global/properties/NeisProperties.java b/src/main/java/com/dear/global/properties/NeisProperties.java index fc586db..e77aef4 100644 --- a/src/main/java/com/dear/global/properties/NeisProperties.java +++ b/src/main/java/com/dear/global/properties/NeisProperties.java @@ -1,10 +1,10 @@ -package com.dear.global.properties; - -import lombok.Getter; -import lombok.Setter; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; - +//package com.dear.global.properties; +// +//import lombok.Getter; +//import lombok.Setter; +//import org.springframework.boot.context.properties.ConfigurationProperties; +//import org.springframework.context.annotation.Configuration; +// //@Getter //@Setter //@Configuration diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 78b1a53..af79b8c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -56,7 +56,7 @@ spring: dialect: org.hibernate.dialect.MySQL8Dialect format_sql: true hibernate: - ddl-auto: none + ddl-auto: update show-sql: true redis: