Skip to content

Commit

Permalink
Merge pull request #90 from SanE-Seo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Changha-dev authored May 5, 2024
2 parents d358aa8 + 93d3a01 commit 62a9f6c
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;


public interface DistrictRepository extends JpaRepository<District, Long> {
Optional<District> findByName(String districtName);

@Query("SELECT d FROM District d WHERE d.name LIKE :name%")
List<District> findByNameStartingWith(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public boolean existsByPostAndMember(Post post, Member member) {
return likeRepository.existsByPostAndMember(post, member);

}

@Override
public List<Likes> findByMemberId(Long memberId) {
return likeRepository.findByMemberId(memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.seoultech.sanEseo.like.domain.Likes;
import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.post.domain.Post;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

Expand All @@ -15,4 +12,6 @@ public interface LikeRepository extends JpaRepository<Likes, Long> {
void deleteByPostAndMember(Post post, Member member);

boolean existsByPostAndMember(Post post, Member member);

List<Likes> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface LikePort {
int countByPost(Post post);

boolean existsByPostAndMember(Post post, Member member);

List<Likes> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
Expand Down Expand Up @@ -67,4 +68,16 @@ public boolean hasMemberLikedPost(Long memberId, Long postId) {
return likePort.existsByPostAndMember(post, member);
}

public List<Post> findLikedPostsByMember(Long memberId) {
List<Likes> likes = likePort.findByMemberId(memberId);
return likes.stream()
.map(like -> postPort.getPost(like.getPost().getId()))
.collect(Collectors.toList());
}

public List<Post> filterPostsByCategory(List<Post> posts, int category) {
return posts.stream()
.filter(post -> post.getCategory().getValue() == category)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
import com.seoultech.sanEseo.global.config.web.AuthMember;
import com.seoultech.sanEseo.global.config.web.LoginMember;
import com.seoultech.sanEseo.global.response.ApiResponse;
import com.seoultech.sanEseo.like.application.service.LikeService;
import com.seoultech.sanEseo.member.adapter.in.web.dto.RegisterRequest;
import com.seoultech.sanEseo.member.adapter.in.web.dto.MemberResponse;
import com.seoultech.sanEseo.member.adapter.in.web.dto.UpdateMemberRequest;
import com.seoultech.sanEseo.member.application.port.in.AuthUseCase;
import com.seoultech.sanEseo.member.application.port.out.MemberPort;
import com.seoultech.sanEseo.member.application.service.AuthService;
import com.seoultech.sanEseo.member.application.service.MemberService;
import com.seoultech.sanEseo.post.domain.Post;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/member")
public class MemberController {

private final MemberService memberService;
private final AuthUseCase authUseCase;
private final LikeService likesService;

@PostMapping
public ResponseEntity<?> register(@RequestBody RegisterRequest request) {
Expand All @@ -39,9 +44,18 @@ public ResponseEntity<?> findMember(@LoginMember AuthMember authMember) {
return ApiResponse.ok("사용자 정보 조회 성공", MemberResponse.fromEntity(memberService.loadMember(authMember.getId())));
}



@GetMapping("/duplicate")
public ResponseEntity<?> checkDuplicateName(@RequestParam String username) {
memberService.checkDuplicateName(username);
return ApiResponse.ok(username + "은 사용 가능한 이름입니다.");
}

@GetMapping("/liked-posts/{category}")
public ResponseEntity<?> getLikedPosts(@LoginMember AuthMember authMember, @PathVariable int category) {
List<Post> posts = likesService.findLikedPostsByMember(authMember.getId());
List<Post> posts1 = likesService.filterPostsByCategory(posts, category);
return ApiResponse.ok("좋아요한 게시글 조회 성공", posts1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.seoultech.sanEseo.member.application.port.out.MemberPort;
import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.post.domain.Post;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MemberPersistenceAdapter implements MemberPort {

Expand Down Expand Up @@ -47,4 +50,6 @@ public long getNewIndex() {
return memberRepository.findFirstByOrderByIdDesc().orElse(Member.builder()
.id(0L).build()).getId() + 1;
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.seoultech.sanEseo.member.application.port.out;

import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.post.domain.Post;

import java.util.List;
import java.util.Optional;

public interface MemberPort {
Expand All @@ -15,4 +17,5 @@ public interface MemberPort {
boolean existsByEmail(final String email);

long getNewIndex();

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
package com.seoultech.sanEseo.member.application.service;

import com.seoultech.sanEseo.global.common.S3Uploader;
import com.seoultech.sanEseo.image.GetImageResponse;
import com.seoultech.sanEseo.image.ImageService;
import com.seoultech.sanEseo.image.PostImage;
import com.seoultech.sanEseo.like.application.service.LikeService;
import com.seoultech.sanEseo.member.adapter.in.web.dto.MemberResponse;
import com.seoultech.sanEseo.member.application.port.in.command.UpdateMemberCommand;
import com.seoultech.sanEseo.member.application.port.out.MemberPort;
import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.member.exception.DuplicateEmailException;
import com.seoultech.sanEseo.member.exception.DuplicateNameException;
import com.seoultech.sanEseo.post.application.port.PostPort;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MemberService {
private final MemberPort memberPort;
private final S3Uploader s3Uploader;
private final PostPort postPort;
private final PostDistrictPort postDistrictPort;
private final ImageService imageService;
private final LikeService likeService;


public MemberService(MemberPort memberPort, S3Uploader s3Uploader) {
public MemberService(MemberPort memberPort, S3Uploader s3Uploader, PostPort postPort, PostDistrictPort postDistrictPort, ImageService imageService, LikeService likeService) {
this.memberPort = memberPort;
this.s3Uploader = s3Uploader;
this.postPort = postPort;
this.postDistrictPort = postDistrictPort;
this.imageService = imageService;
this.likeService = likeService;
}

public void addMember(Member member) {
Expand Down Expand Up @@ -69,4 +89,6 @@ public MemberResponse updateMember(UpdateMemberCommand command) {

return MemberResponse.fromEntity(memberPort.save(member));
}


}
4 changes: 4 additions & 0 deletions src/main/java/com/seoultech/sanEseo/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.seoultech.sanEseo.member.domain;

import com.seoultech.sanEseo.post.domain.Post;
import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "Member")
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ public boolean existsByNameAndDescription(String title, String description) {

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import com.seoultech.sanEseo.post.application.service.GetPostResponse;
import com.seoultech.sanEseo.post.application.service.PostService;
import com.seoultech.sanEseo.post.application.service.UpdatePostRequest;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/posts")
public class PostController {
Expand Down Expand Up @@ -44,4 +48,11 @@ public ResponseEntity<?> deletePost(@LoginMember AuthMember authMember, @PathVar
postService.deletePost(authMember.getId(), postId);
return ApiResponse.ok("게시글이 삭제되었습니다.");
}

@GetMapping("/by-district-prefix")
public ResponseEntity<?> getPostsByDistrictPrefix(@RequestParam String districtName) {
List<Post> posts = postService.findPostsByDistrictNameStart(districtName);
return ApiResponse.ok("입력된 자치구를 포함하는 게시글 반환 완료.", posts);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface PostRepository extends JpaRepository<Post, Long> {
boolean existsByTitleAndDescription(String title, String description);

List<Post> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.seoultech.sanEseo.post.application.service;

import com.seoultech.sanEseo.district.adapter.DistrictRepository;
import com.seoultech.sanEseo.district.domain.District;
import com.seoultech.sanEseo.district.application.port.DistrictPort;
import com.seoultech.sanEseo.image.GetImageResponse;
import com.seoultech.sanEseo.image.ImageService;
import com.seoultech.sanEseo.image.PostImage;
import com.seoultech.sanEseo.like.application.service.LikeService;
import com.seoultech.sanEseo.member.application.port.out.MemberPort;
import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.post.adapter.PostAdapter;
import com.seoultech.sanEseo.post.application.port.PostPort;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post.exception.AuthorMismatchException;
import com.seoultech.sanEseo.post_district.adapter.PostDistrictRepository;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.public_api.application.service.dto.CoordinateRequest;
Expand All @@ -18,6 +26,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@RequiredArgsConstructor
Expand All @@ -29,6 +38,8 @@ public class PostService {
private final DistrictPort districtPort;
private final PostDistrictPort postDistrictPort;
private final CoordinateService coordinateService;
private final DistrictRepository districtRepository;
private final PostDistrictRepository postDistrictRepository;

@Transactional
public Long addPost(Long memberId, AddPostRequest request) {
Expand Down Expand Up @@ -137,4 +148,18 @@ public void deletePost(Long memberId, Long postId) {
}


public List<Post> findPostsByDistrictNameStart(String districtName) {
List<District> districts = districtRepository.findByNameStartingWith(districtName);
if (districts.isEmpty()) {
throw new RuntimeException("No districts found starting with: " + districtName);
}
List<PostDistrict> postDistricts = districts.stream()
.flatMap(district -> postDistrictRepository.findByDistrict(district).stream())
.distinct()
.collect(Collectors.toList());
return postDistricts.stream().map(PostDistrict::getPost).collect(Collectors.toList());
}



}
1 change: 1 addition & 0 deletions src/main/java/com/seoultech/sanEseo/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
private Member member;
private Category category;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seoultech.sanEseo.post_district.adapter;


import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
Expand Down Expand Up @@ -56,4 +57,5 @@ public Slice<PostDistrict> findByPostCategory(Category category, Pageable pageab
return postDistrictRepository.findByPostCategory(category, pageable);
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.seoultech.sanEseo.post_district.adapter;


import com.seoultech.sanEseo.global.config.web.AuthMember;
import com.seoultech.sanEseo.global.config.web.LoginMember;
import com.seoultech.sanEseo.global.response.ApiResponse;
import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.member.application.service.MemberService;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.post_district.application.service.PostDistrictService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -19,9 +19,11 @@
public class PostDistrictController {

private final PostDistrictService postDistrictService;
private final MemberService memberService;

public PostDistrictController(PostDistrictService postDistrictService) {
public PostDistrictController(PostDistrictService postDistrictService, MemberService memberService) {
this.postDistrictService = postDistrictService;
this.memberService = memberService;
}


Expand All @@ -47,4 +49,6 @@ public ResponseEntity<?> getPostByLikesSortedDesc(@PathVariable int category) {
List<GetPostDistrictResponse> responses = postDistrictService.getPostByLikesSortedDesc(category);
return ApiResponse.ok("좋아요 순으로 정렬된 게시글 목록 조회 성공", responses);
}


}
Loading

0 comments on commit 62a9f6c

Please sign in to comment.