Skip to content

Commit

Permalink
Merge pull request #20 from study-hub-inu/feat/SH-110-mypage
Browse files Browse the repository at this point in the history
Feat/sh 110 mypage
  • Loading branch information
wellbeing-dough authored Sep 21, 2023
2 parents d0451ff + 956d944 commit bd6d8c6
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ public class QBookMarkEntity extends EntityPathBase<BookMarkEntity> {

public static final QBookMarkEntity bookMarkEntity = new QBookMarkEntity("bookMarkEntity");

public final kr.co.studyhubinu.studyhubserver.common.domain.QBaseTimeEntity _super = new kr.co.studyhubinu.studyhubserver.common.domain.QBaseTimeEntity(this);

//inherited
public final DateTimePath<java.time.LocalDateTime> createdDate = _super.createdDate;

public final NumberPath<Long> id = createNumber("id", Long.class);

//inherited
public final DateTimePath<java.time.LocalDateTime> modifiedDate = _super.modifiedDate;

public final NumberPath<Long> postId = createNumber("postId", Long.class);

public final NumberPath<Long> userId = createNumber("userId", Long.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.co.studyhubinu.studyhubserver.bookmark.domain;

import kr.co.studyhubinu.studyhubserver.common.domain.BaseTimeEntity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -10,7 +11,7 @@
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BookMarkEntity {
public class BookMarkEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import io.swagger.v3.oas.annotations.Operation;
import kr.co.studyhubinu.studyhubserver.study.dto.request.CreatePostRequest;
import kr.co.studyhubinu.studyhubserver.study.dto.request.UpdatePostRequest;
import kr.co.studyhubinu.studyhubserver.study.dto.response.GetBookmarkedPostsResponse;
import kr.co.studyhubinu.studyhubserver.study.dto.response.GetMyPostResponse;
import kr.co.studyhubinu.studyhubserver.study.service.StudyPostService;
import kr.co.studyhubinu.studyhubserver.user.dto.data.UserId;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -42,4 +45,16 @@ public ResponseEntity<HttpStatus> deletePost(@PathVariable("postId") Long postId
studyPostService.deletePost(postId, userId.getId());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Operation(summary = "내가 쓴 스터디 조회")
@GetMapping("/mypost")
public ResponseEntity<Slice<GetMyPostResponse>> getMyPosts(@RequestParam int page, @RequestParam int size, UserId userId) {
return ResponseEntity.ok(studyPostService.getMyPosts(page, size, userId.getId()));
}

@Operation(summary = "내가 북마크한 스터디 조회")
@GetMapping("/bookmarked")
public ResponseEntity<Slice<GetBookmarkedPostsResponse>> getBookmarkedPosts(@RequestParam int page, @RequestParam int size, UserId userId) {
return ResponseEntity.ok().body(studyPostService.getBookmarkedPosts(page, size, userId.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ public class StudyPostEntity extends BaseTimeEntity {
@Column(name = "chat_url")
private String chatUrl;

@Enumerated(EnumType.STRING)
private MajorType major;

@Column(name = "study_person")
private int studyPerson;

@Enumerated(EnumType.STRING)
@Column(name = "filtered_gender")
private GenderType filteredGender;

@Enumerated(EnumType.STRING)
@Column(name = "study_way")
private StudyWayType studyWay;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CreatePostRequest {
@NotBlank
private String chatUrl;

@Schema(description = "관련 학과", example = "COMPUTER")
@Schema(description = "관련 학과", example = "COMPUTER_SCIENCE_ENGINEERING")
private MajorType major;

@Schema(description = "스터디 정원", example = "10")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kr.co.studyhubinu.studyhubserver.study.dto.response;

import kr.co.studyhubinu.studyhubserver.user.enums.MajorType;
import lombok.*;

@Getter
@Setter // QueryDsl 때문에 필요함
@NoArgsConstructor
@AllArgsConstructor
public class GetBookmarkedPostsResponse {

private Long postId;
private MajorType major;
private String title;
private String content;
private int remainingSeat;
private boolean close;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.co.studyhubinu.studyhubserver.study.dto.response;

import kr.co.studyhubinu.studyhubserver.user.enums.MajorType;
import lombok.Getter;

@Getter
public class GetMyPostResponse {
private Long postId;
private MajorType major;
private String title;
private String content;
private int remainingSeat;
private boolean close;

public GetMyPostResponse(Long postId, MajorType major, String title, String content, int remainingSeat, boolean close) {
this.postId = postId;
this.major = major;
this.title = title;
this.content = content;
this.remainingSeat = remainingSeat;
this.close = close;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package kr.co.studyhubinu.studyhubserver.study.repository;

import kr.co.studyhubinu.studyhubserver.study.domain.StudyPostEntity;
import kr.co.studyhubinu.studyhubserver.study.dto.response.GetMyPostResponse;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface StudyPostRepository extends JpaRepository<StudyPostEntity, Long>, StudyPostRepositoryCustom {

@Query("SELECT new kr.co.studyhubinu.studyhubserver.study.dto.response.GetMyPostResponse(sp.id, sp.major, sp.title, sp.content, sp.remainingSeat, sp.close) " +
"FROM StudyPostEntity sp " +
"WHERE sp.postedUserId = :userId")
Slice<GetMyPostResponse> findByPostedUserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.co.studyhubinu.studyhubserver.study.repository;

import kr.co.studyhubinu.studyhubserver.study.domain.StudyPostEntity;
import kr.co.studyhubinu.studyhubserver.study.dto.response.*;
import kr.co.studyhubinu.studyhubserver.user.enums.MajorType;
import org.springframework.data.domain.Pageable;
Expand All @@ -17,5 +16,7 @@ public interface StudyPostRepositoryCustom {

Slice<FindPostResponseByContent> findByContent(String content, Pageable pageable);

Slice<GetBookmarkedPostsResponse> findPostsByBookmarked(Long userId, Pageable pageable);

//Slice<StudyPostEntity> findByBookMark(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Collections;
import java.util.List;

import static kr.co.studyhubinu.studyhubserver.bookmark.domain.QBookMarkEntity.bookMarkEntity;
import static kr.co.studyhubinu.studyhubserver.study.domain.QStudyPostEntity.*;

@Repository
Expand Down Expand Up @@ -81,6 +82,29 @@ public Slice<FindPostResponseByContent> findByContent(String content, Pageable p
return findByQuery(studyPostDto, pageable);
}

@Override
public Slice<GetBookmarkedPostsResponse> findPostsByBookmarked(Long userId, Pageable pageable) {
List<GetBookmarkedPostsResponse> lists = jpaQueryFactory.select(
Projections.bean(GetBookmarkedPostsResponse.class,
studyPostEntity.id.as("postId"),
studyPostEntity.major,
studyPostEntity.title,
studyPostEntity.content,
studyPostEntity.remainingSeat,
studyPostEntity.close
)
)
.from(studyPostEntity)
.innerJoin(bookMarkEntity)
.on(bookMarkEntity.postId.eq(studyPostEntity.id))
.where(bookMarkEntity.userId.eq(userId))
.orderBy(studyPostEntity.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
return toSlice(pageable, lists);
}

public <T> Slice<T> findByQuery(JPAQuery<T> query, Pageable pageable) {
Slice<T> sliceDto = toSlice(pageable, query.fetch());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -68,6 +70,18 @@ public Slice<FindPostResponseByContent> findPostResponseByContent(String content
return studyPostRepository.findByContent(content, pageable);
}

public Slice<GetMyPostResponse> getMyPosts(int page, int size, Long userId) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdDate"));
return studyPostRepository.findByPostedUserId(user.getId(), pageable);
}

public Slice<GetBookmarkedPostsResponse> getBookmarkedPosts(int page, int size, Long userId) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdDate"));
return studyPostRepository.findPostsByBookmarked(userId, pageable);
}

// public Slice<StudyPostEntity> findPostResponseByBookMark(Pageable pageable) {
// return studyPostRepository.findByBookMark(pageable);
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import lombok.NoArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;

@Entity
@Getter
Expand All @@ -33,8 +30,10 @@ public class UserEntity extends BaseTimeEntity {

private String imaUrl;

@Enumerated(EnumType.STRING)
private MajorType major;

@Enumerated(EnumType.STRING)
private GenderType gender;

private String imageUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SignUpRequest {
@NotBlank
private String email;

@Schema(description = "유저 비밀번호", example = "asd123")
@Schema(description = "유저 비밀번호", example = "asdasdasd!!")
@Pattern(
regexp = "^(?=.*[!@#$%^&*?~_]).{10,}$",
message = "비밀번호는 10자 이상이어야 하며, 하나 이상의 특수문자를 포함해야 합니다."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void updateNickname(UpdateNicknameInfo info) {
}

public void nicknameDuplicationValid(String nickname) {
userRepository.findByNickname(nickname).orElseThrow(UserNicknameDuplicateException::new);
if (userRepository.findByNickname(nickname).isPresent()) {
throw new UserNicknameDuplicateException();
}
}

@Transactional
Expand Down

0 comments on commit bd6d8c6

Please sign in to comment.