Skip to content

Commit

Permalink
Merge pull request #127 from Dear-project/feat/#120
Browse files Browse the repository at this point in the history
feat: bookMark
  • Loading branch information
GayeongKimm authored Oct 22, 2024
2 parents 4529cbe + 0f8e69d commit f2b107f
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 64 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public Response bookMark(
@Operation(description = "북마크 불러오기")
@GetMapping
public ResponseData<List<BookMarkLoadRes>> 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());
}

}
Original file line number Diff line number Diff line change
@@ -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<String> school,
Optional<String> major
) {
public static BookMarkLoadRes of(ProfessorEntity professorEntity, boolean isBookMarked){
public static BookMarkLoadRes of(UserEntity professorEntity, Optional<MajorEntity> 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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

@Repository
public interface BookMarkRepository extends JpaRepository<BookMarkEntity, Long> {
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

public interface BookMarkService {
void convertOfBookMark(Long id, BookMarkEnum bookMarkEnum);
List<BookMarkLoadRes> loadBookMark(PageRequest pageRequest, BookMarkEnum bookMarkEnum);
List<BookMarkLoadRes> loadBookMark();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -94,30 +99,23 @@ private void toggleSchoolBookmark(SchoolEntity schoolEntity, UserEntity userEnti
}

@Override
public List<BookMarkLoadRes> loadBookMark(PageRequest pageRequest, BookMarkEnum bookMarkEnum) {
public List<BookMarkLoadRes> loadBookMark() {
UserEntity userEntity = userMapper.toEntity(userSecurity.getUser());
List<BookMarkEntity> bookMarkEntity = bookMarkRepository.findAll();

if (bookMarkEnum.equals(UNIVERSITY)){
List<BookMarkLoadRes> bookMarkEntityList = bookMarkEntity.stream()
.map(bookMark -> {
return BookMarkLoadRes.of(
bookMark.getSchoolEntity(),
bookMarkRepository.existsBySchoolEntityAndUser(bookMark.getSchoolEntity(), bookMark.getUser())
);
})
.toList();
return bookMarkEntityList;
}else {
List<BookMarkLoadRes> bookMarkEntityList = bookMarkEntity.stream()
.map(bookMark -> {
return BookMarkLoadRes.of(
bookMark.getProfessorEntity(),
bookMarkRepository.existsByProfessorEntityAndUser(bookMark.getProfessorEntity(), bookMark.getUser())
);
})
.toList();
return bookMarkEntityList;
List<BookMarkLoadRes> 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> majorEntity = majorRepository.findById(userEntity.getId());
bookMarkLoadRes.add(BookMarkLoadRes.of(bookMark1.getProfessorEntity(), majorEntity));
}
}

return bookMarkLoadRes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public CommunityOneResponse getCommunityByCommunityId(Long communityId) {

}


@Override
public List<CommunityResponse> getCommunityList(PageRequest pageRequest) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public class CommunityResponse {
private Long comment;
private String imagePath;
private String profileImage;
private boolean isBookMark;


}
2 changes: 1 addition & 1 deletion src/main/java/com/dear/domain/food/enums/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
//import java.io.Serializable;
//import java.util.List;

//
//@Getter
//@AllArgsConstructor
//public class Food implements Serializable {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dear/domain/food/enums/FoodDetail.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.dear.domain.food.enums;

//package com.dear.domain.food.enums;
//
//import lombok.AllArgsConstructor;
//import lombok.Getter;
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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,
String school,
String major,
String email,
String profileImage

) {


public static ProfessorLoadResponse of(User user, School school, Major major, ImageEntity profileImage) {
return new ProfessorLoadResponse(
user.getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -32,6 +31,7 @@ public class QueryDslRepositoryImpl implements QueryDslRepository {

@Override
public List<ProfessorLoadResponse> findProfessorWithPagination(PageRequest pageRequest) {

JPAQuery<ProfessorLoadResponse> query =
jpaQueryFactory
.select(createProjection())
Expand All @@ -43,6 +43,7 @@ public List<ProfessorLoadResponse> findProfessorWithPagination(PageRequest pageR
.on(schoolEntity.seq.eq(userEntity.schoolId.seq))
.leftJoin(imageEntity)
.on(imageEntity.profileId.eq(userEntity.id))
.leftJoin(bookMarkEntity)
.fetchJoin();

return query
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/dear/domain/user/domain/UserEntity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/dear/global/properties/NeisProperties.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ spring:
dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: true
hibernate:
ddl-auto: none
ddl-auto: update
show-sql: true

redis:
Expand Down

0 comments on commit f2b107f

Please sign in to comment.