Skip to content

Commit

Permalink
Merge branch 'develop' into feat/16
Browse files Browse the repository at this point in the history
  • Loading branch information
kmjenny authored Feb 17, 2024
2 parents d996fdf + 02d6806 commit 5d6ea77
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.harang.server.dto.request.MatchingRequest;
import org.harang.server.dto.type.SuccessMessage;
import org.harang.server.service.MatchingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -38,4 +39,9 @@ public ApiResponse<?> finishMatching(final @NotNull @PathVariable("postId") Long
matchingService.finishMatching(postId);
return ApiResponse.success(SuccessMessage.OK);
}

@GetMapping("/{postId}/waitings")
public ApiResponse<?> getWaitinglist(final @NotNull @PathVariable("postId") Long postId) {
return ApiResponse.success(matchingService.getWaitingList(postId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.harang.server.domain.Post;
import org.harang.server.dto.common.ApiResponse;
import org.harang.server.dto.request.PostRequest;
import org.harang.server.dto.response.PostResponse;
import org.harang.server.dto.type.SuccessMessage;
import org.harang.server.service.PostService;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
Expand All @@ -31,5 +33,9 @@ public ApiResponse<?> createPost(@MemberId Long memberId, @Valid @RequestBody Po
public ApiResponse<?> deletePost(@MemberId Long memberId, @PathVariable Long postId) {
postService.deletePost(memberId, postId);
return ApiResponse.success(SuccessMessage.OK);

@GetMapping
public ApiResponse<List<PostResponse>> getAllPosts() {
return ApiResponse.success(postService.getAllPosts());
}
}
10 changes: 9 additions & 1 deletion src/main/java/org/harang/server/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class Member {
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "type", nullable = false)
@Enumerated(EnumType.STRING)
private Type type;
Expand All @@ -28,15 +31,20 @@ public class Member {
@Column(name = "age", nullable = false)
private Long age;


/* Relation Parent Mapping */
@OneToMany(mappedBy = "member")
private List<Matching> matchingList = new ArrayList<>();

@OneToMany(mappedBy = "member")
private List<Post> postList = new ArrayList<>();

@OneToMany(mappedBy = "member")
private List<Waiting> waitingList = new ArrayList<>();

@Builder
public Member(Type type, Gender gender, Long age) {
public Member(String name, Type type, Gender gender, Long age) {
this.name = name;
this.type = type;
this.gender = gender;
this.age = age;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/harang/server/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class Post {
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<PostCategory> postCategoryList = new ArrayList<>();

@OneToMany(mappedBy = "post")
private List<Waiting> waitingList = new ArrayList<>();

@Builder
public Post(Member member, LocalDateTime createdAt, String title, String content, String chatLink, Gender preferredGender, int preferredAge, LocalDate preferredStartAt, LocalDate preferredEndAt, Status status) {
this.member = member;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/harang/server/domain/Waiting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.harang.server.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Waiting {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Builder
public Waiting(Post post, Member member) {
this.post = post;
this.member = member;
}
}
27 changes: 27 additions & 0 deletions src/main/java/org/harang/server/dto/response/PostResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.harang.server.dto.response;

import lombok.Builder;
import org.harang.server.domain.Category;
import org.harang.server.domain.Post;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Builder
public record PostResponse(Long postId, String title, String author, String createdAt, String contentAbstract, List<String> category) {
public static PostResponse of(Post post) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

String createdAt = post.getCreatedAt().format(dateFormatter);
String contentAbstract = post.getContent().substring(0, Math.min(post.getContent().length(), 70)) + "...";

List<String> categories = post.getPostCategoryList().stream()
.map(postCategory -> postCategory.getCategory().getName())
.collect(Collectors.toList());

return new PostResponse(post.getId(), post.getTitle(), post.getMember().getName(), createdAt, contentAbstract, categories);
}

}
16 changes: 16 additions & 0 deletions src/main/java/org/harang/server/dto/response/WaitingResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.harang.server.dto.response;

import lombok.Builder;
import lombok.Getter;

@Getter
public class WaitingResponse {
private Long memberId;
private String nickname;

@Builder
public WaitingResponse(Long memberId, String nickname) {
this.memberId = memberId;
this.nickname = nickname;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/harang/server/dto/type/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public enum ErrorMessage {
MEMBER_NOT_FOUND("40402", HttpStatus.NOT_FOUND, "사용자가 존재하지 않습니다."),
POST_NOT_FOUND("40403", HttpStatus.NOT_FOUND, "게시글이 존재하지 않습니다."),
MATCHING_NOT_FOUND("40404", HttpStatus.NOT_FOUND, "매칭이 존재하지 않습니다."),
MEMBER_INFO_NOT_FOUND("40405", HttpStatus.NOT_FOUND, "사용자의 정보가 존재하지 않습니다."),


// method not allowed - 409
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.harang.server.repository;

import java.util.Optional;
import org.harang.server.domain.MemberInfo;
import org.harang.server.dto.type.ErrorMessage;
import org.harang.server.exception.CustomException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberInfoRepository extends JpaRepository<MemberInfo, Long> {
default MemberInfo findByMemberIdOrThrow(Long memberId) {
return findByMemberId(memberId).orElseThrow(() -> new CustomException(ErrorMessage.MEMBER_INFO_NOT_FOUND));
}

Optional<MemberInfo> findByMemberId(Long memberId);
}
12 changes: 12 additions & 0 deletions src/main/java/org/harang/server/repository/WaitingRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.harang.server.repository;

import java.util.List;
import org.harang.server.domain.Member;
import org.harang.server.domain.Waiting;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface WaitingRepository extends JpaRepository<Waiting, Long> {
List<Waiting> findAllByPostId(Long postId);
}
18 changes: 18 additions & 0 deletions src/main/java/org/harang/server/service/MatchingService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package org.harang.server.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.harang.server.domain.Matching;
import org.harang.server.domain.Member;
import org.harang.server.domain.Post;
import org.harang.server.domain.Waiting;
import org.harang.server.domain.enums.Status;
import org.harang.server.domain.enums.Type;
import org.harang.server.dto.request.MatchingRequest;
import org.harang.server.dto.response.WaitingResponse;
import org.harang.server.dto.type.ErrorMessage;
import org.harang.server.exception.CustomException;
import org.harang.server.repository.MatchingRepository;
import org.harang.server.repository.MemberInfoRepository;
import org.harang.server.repository.MemberRepository;
import org.harang.server.repository.PostRepository;
import org.harang.server.repository.WaitingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,6 +26,8 @@ public class MatchingService {
private final MatchingRepository matchingRepository;
private final MemberRepository memberRepository;
private final PostRepository postRepository;
private final WaitingRepository waitingRepository;
private final MemberInfoRepository memberInfoRepository;

@Transactional
public void createMatching(Long memberId, MatchingRequest matchingRequest) {
Expand Down Expand Up @@ -76,4 +83,15 @@ public void finishMatching(Long postId) {
postRepository.save(post);
matchingRepository.save(matching);
}

public List<WaitingResponse> getWaitingList(Long postId) {
List<Waiting> waitingList = waitingRepository.findAllByPostId(postId);

List<WaitingResponse> waitingListResponse = waitingList.stream()
.map(w -> WaitingResponse.builder()
.memberId(w.getMember().getId())
.nickname(memberInfoRepository.findByMemberIdOrThrow(w.getMember().getId()).getNickname())
.build()).toList();
return waitingListResponse;
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/harang/server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import org.harang.server.domain.*;
import org.harang.server.domain.enums.Status;
import org.harang.server.dto.request.PostRequest;
import org.harang.server.dto.response.PostResponse;
import org.harang.server.repository.*;
import org.hibernate.event.spi.PostDeleteEventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -72,6 +76,12 @@ public void deletePost(Long memberId, Long postId) {

locationRepository.deleteByPostId(postId);
postRepository.delete(post);

public List<PostResponse> getAllPosts() {
return postRepository.findAll()
.stream()
.map(PostResponse::of)
.collect(Collectors.toList());
}

}

0 comments on commit 5d6ea77

Please sign in to comment.