Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add location to posts #16

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
8 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 61 additions & 61 deletions src/main/java/com/SafeNet/Backend/InitDB.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
package com.SafeNet.Backend;

import com.SafeNet.Backend.domain.member.entity.Member;
import com.SafeNet.Backend.domain.member.repository.MemberRepository;
import com.SafeNet.Backend.domain.region.entity.Region;
import com.SafeNet.Backend.domain.region.repository.RegionRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Component
@RequiredArgsConstructor
public class InitDB {

private final InitService initService;

@PostConstruct
public void init() {
initService.userDBInit();
}

@Component
@Transactional
@RequiredArgsConstructor
static class InitService {

private final MemberRepository memberRepository;
private final RegionRepository regionRepository;

public void userDBInit() {
String cityName = "μš©μΈμ‹œ";
String memberEmail = "[email protected]";

Optional<Region> existingRegion = regionRepository.findByCity(cityName);
Region region;
if (existingRegion.isEmpty()) {
region = Region.builder()
.city(cityName)
.build();
regionRepository.save(region);
} else {
region = existingRegion.get();
}

Optional<Member> existingMember = memberRepository.findByEmail(memberEmail);
if (existingMember.isEmpty()) {
Member member = Member.builder()
.name("λˆˆμ†‘μ΄")
.phoneNumber("010-3333-4444")
.email(memberEmail)
.pwd("3333")
.region(region)
.build();
memberRepository.save(member);
}
}
}
}
//package com.SafeNet.Backend;
//
//import com.SafeNet.Backend.domain.member.entity.Member;
//import com.SafeNet.Backend.domain.member.repository.MemberRepository;
//import com.SafeNet.Backend.domain.region.entity.Region;
//import com.SafeNet.Backend.domain.region.repository.RegionRepository;
//import jakarta.annotation.PostConstruct;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.Optional;
//
//@Component
//@RequiredArgsConstructor
//public class InitDB {
//
// private final InitService initService;
//
// @PostConstruct
// public void init() {
// initService.userDBInit();
// }
//
// @Component
// @Transactional
// @RequiredArgsConstructor
// static class InitService {
//
// private final MemberRepository memberRepository;
// private final RegionRepository regionRepository;
//
// public void userDBInit() {
// String cityName = "μš©μΈμ‹œ";
// String memberEmail = "[email protected]";
//
// Optional<Region> existingRegion = regionRepository.findByCity(cityName);
// Region region;
// if (existingRegion.isEmpty()) {
// region = Region.builder()
// .city(cityName)
// .build();
// regionRepository.save(region);
// } else {
// region = existingRegion.get();
// }
//
// Optional<Member> existingMember = memberRepository.findByEmail(memberEmail);
// if (existingMember.isEmpty()) {
// Member member = Member.builder()
// .name("λˆˆμ†‘μ΄")
// .phoneNumber("010-3333-4444")
// .email(memberEmail)
// .pwd("3333")
// .region(region)
// .build();
// memberRepository.save(member);
// }
// }
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Optional;

@RestController
@RequestMapping("/api/v2/posts")
@RequestMapping("/api/v3/posts")
@RequiredArgsConstructor
@Tag(name = "Post", description = "κ²Œμ‹œλ¬Ό CRUD API")
public class PostController {
Expand All @@ -36,7 +36,7 @@ public class PostController {
@ApiResponse(responseCode = "201", description = "κ²Œμ‹œλ¬Όμ΄ μ„±κ³΅μ μœΌλ‘œ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€"),
@ApiResponse(responseCode = "400", description = "잘λͺ»λœ μš”μ²­μž…λ‹ˆλ‹€")
})
public ResponseEntity<String> createPost(
public ResponseEntity<String> createPost( // v3: μ‚¬μš©μžμ™€ μ‚¬μš©μžμ˜ 지역 정보 μΆ”κ°€
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@RequestPart(value = "post") String postRequestDtoJson,
Expand All @@ -50,7 +50,7 @@ public ResponseEntity<String> createPost(

@GetMapping
@Operation(summary = "κ²Œμ‹œλ¬Ό 리슀트 뢈러였기", description = "전체 κ²Œμ‹œλ¬Όμ„ 리슀트둜 λ³Ό λ•Œ μ‚¬μš©ν•˜λŠ” API")
public ResponseEntity<?> getPosts(
public ResponseEntity<?> getPosts( // v3: μ‚¬μš©μžμ™€ μ‚¬μš©μžμ˜ 지역 정보 μΆ”κ°€
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken
) {
Expand All @@ -67,8 +67,8 @@ public ResponseEntity<?> getPosts(
public ResponseEntity<?> getPostById(
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@PathVariable("postId") Long id) {
Optional<PostResponseDto> postById = postService.getPostById(id);
@PathVariable("postId") Long postId) {
Optional<PostResponseDto> postById = postService.getPostById(postId);
if (postById.isPresent()) {
return ResponseEntity.ok(postById.get());
} else {
Expand Down Expand Up @@ -108,7 +108,8 @@ public ResponseEntity<String> updatePostStatusToTrading(
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@PathVariable("postId") Long id) {
postService.updatePostStatusToTrading(id);
String email = getUserEmail();
postService.updatePostStatusToTrading(id, email);
return ResponseEntity.ok("Post status updated to trading");
}

Expand All @@ -118,7 +119,8 @@ public ResponseEntity<String> updatePostStatusToCompleted(
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@PathVariable("postId") Long id) {
postService.updatePostStatusToCompleted(id);
String email = getUserEmail();
postService.updatePostStatusToCompleted(id, email);
return ResponseEntity.ok("Post status updated to completed");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import java.util.List;

@RestController
@RequestMapping("/api/v1/posts")
@RequestMapping("/api/v3/posts")
@RequiredArgsConstructor
@Tag(name = "Post Query", description = "κ²Œμ‹œκΈ€ νŠΉμ • 쑰건 쑰회 및 μ •λ ¬ API")
public class PostSearchAndSortController {
private final PostSearchAndSortService postSearchAndSortService;

@GetMapping("/keyword")
public ResponseEntity<List<PostResponseDto>> searchByKeyWord(
public ResponseEntity<List<PostResponseDto>> searchByKeyWord( // v3: μ‚¬μš©μžμ™€ μ‚¬μš©μžμ˜ 지역 정보 μΆ”κ°€
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@RequestParam("keyword") String keyword) {
Expand All @@ -31,7 +31,7 @@ public ResponseEntity<List<PostResponseDto>> searchByKeyWord(
}

@GetMapping("/category")
public ResponseEntity<List<PostResponseDto>> searchByCategory(
public ResponseEntity<List<PostResponseDto>> searchByCategory( // v3: μ‚¬μš©μžμ™€ μ‚¬μš©μžμ˜ 지역 정보 μΆ”κ°€
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken,
@RequestParam("category") Category category) {
Expand All @@ -41,7 +41,7 @@ public ResponseEntity<List<PostResponseDto>> searchByCategory(
}

@GetMapping("/sort/created")
public ResponseEntity<List<PostResponseDto>> sortByCreated(
public ResponseEntity<List<PostResponseDto>> sortByCreated( // v3: μ‚¬μš©μžμ™€ μ‚¬μš©μžμ˜ 지역 정보 μΆ”κ°€
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken
) {
Expand All @@ -51,7 +51,7 @@ public ResponseEntity<List<PostResponseDto>> sortByCreated(
}

@GetMapping("/sort/buyDate")
public ResponseEntity<List<PostResponseDto>> sortByBuyDate(
public ResponseEntity<List<PostResponseDto>> sortByBuyDate( // v3: μ‚¬μš©μžμ™€ μ‚¬μš©μžμ˜ 지역 정보 μΆ”κ°€
@RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken,
@RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class PostResponseDto {
private Long postId;
private Category category;
private boolean likeCount; // ν˜„μž¬ μ‚¬μš©μžκ°€ ν•˜νŠΈλ₯Ό λˆŒλ €λŠ”μ§€ μ—¬λΆ€
private boolean isLikedByCurrentUser; // ν˜„μž¬ μ‚¬μš©μžκ°€ ν•˜νŠΈλ₯Ό λˆŒλ €λŠ”μ§€ μ—¬λΆ€
private String productImageUrl; // μƒν’ˆ 이미지 URL
private String receiptImageUrl; // 영수증 이미지 URL
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByMember_Id(Long memberId);

List<Post> findByTitleContainingOrContentsContaining(String title, String contents);
List<Post> findByRegion_IdAndCategory(Long memberRegionId, Category category);

List<Post> findByCategory(Category category);
List<Post> findByRegion_IdOrderByCreatedDesc(Long memberRegionId);

List<Post> findAllByOrderByCreatedDesc();
List<Post> findByRegion_IdOrderByBuyDateDesc(Long memberRegionId);

List<Post> findByRegion_Id(Long regionId);
List<Post> findByRegion_IdAndTitleContainingOrContentsContaining(Long regionId, String title, String contents);

List<Post> findAllByOrderByBuyDateDesc();
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.SafeNet.Backend.domain.post.entity.Post;
import com.SafeNet.Backend.domain.post.exception.PostException;
import com.SafeNet.Backend.domain.post.repository.PostRepository;
import com.SafeNet.Backend.domain.postLike.entity.PostLike;
import com.SafeNet.Backend.domain.postLike.repository.PostLikeRepository;
import com.SafeNet.Backend.domain.post.util.PostDtoConverter;
import com.SafeNet.Backend.domain.region.entity.Region;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +28,11 @@ public class CommonPostService {
public Member getMemberByEmail(String email) {
return memberRepository.findByEmail(email).orElseThrow(() -> new PostException("Member not found with email: " + email, HttpStatus.NOT_FOUND));
}
private Long getMemberRegionId(String email) {
Member member = getMemberByEmail(email);
Region region = member.getRegion();
return region.getId();
}

public List<PostResponseDto> convertPostsToDto(List<Post> posts, Long memberId) {
return posts.stream().map(post -> {
Expand All @@ -34,54 +41,69 @@ public List<PostResponseDto> convertPostsToDto(List<Post> posts, Long memberId)
}).collect(Collectors.toList());
}

// κ²Œμ‹œλ¬Ό 리슀트 쑰회
public List<PostResponseDto> getAllPosts(String email) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postRepository.findAll();
Long memberId = member.getId(); // MemberId μΆ”μΆœ
Region region = member.getRegion();
Long memberRegionId = region.getId(); // λ©€λ²„μ˜ RegionId μΆ”μΆœ
List<Post> posts = postRepository.findByRegion_Id(memberRegionId);
return convertPostsToDto(posts, memberId);
}

// μ‚¬μš©μžκ°€ λ“±λ‘ν•œ κ²Œμ‹œλ¬Ό 쑰회
public List<PostResponseDto> getPostsByMemberId(String email) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postRepository.findByMember_Id(memberId);
return convertPostsToDto(posts, memberId);
}

// μ‚¬μš©μžκ°€ μ’‹μ•„μš” λˆ„λ₯Έ κ²Œμ‹œλ¬Ό 쑰회
public List<PostResponseDto> getLikedPostsByMemberId(String email) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postLikeRepository.findByMember_Id(memberId).stream()
.map(postLike -> postLike.getPost())
.map(PostLike::getPost)
.collect(Collectors.toList());
return convertPostsToDto(posts, memberId);
}

// ν‚€μ›Œλ“œλ‘œ κ²Œμ‹œλ¬Ό 쑰회
public List<PostResponseDto> searchPostsByKeyword(String keyword, String email) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postRepository.findByTitleContainingOrContentsContaining(keyword, keyword);
return convertPostsToDto(posts, memberId);
int MAX_LENGTH = 50;
if (keyword.length() > MAX_LENGTH) { // μž…λ ₯ν•œ keywordκ°€ 50κΈ€μžλ³΄λ‹€ κΈΈλ‹€λ©΄
throw new PostException("검색어가 λ„ˆλ¬΄ κΉλ‹ˆλ‹€. " + MAX_LENGTH + " κΈ€μž μ΄ν•˜λ‘œ μž…λ ₯ν•΄μ£Όμ„Έμš”.", HttpStatus.BAD_REQUEST);
}
Long memberRegionId = getMemberRegionId(email);
List<Post> posts = postRepository.findByRegion_IdAndTitleContainingOrContentsContaining(memberRegionId, keyword, keyword);
return getPostResponseDtos(email, posts);
}

// μΉ΄ν…Œκ³ λ¦¬λ‘œ κ²Œμ‹œλ¬Ό 쑰회
public List<PostResponseDto> searchPostsByCategory(Category category, String email) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postRepository.findByCategory(category);
return convertPostsToDto(posts, memberId);
Long memberRegionId = getMemberRegionId(email);
List<Post> posts = postRepository.findByRegion_IdAndCategory(memberRegionId, category);
return getPostResponseDtos(email, posts);
}

// κ²Œμ‹œλ¬Ό 생성 일자 μ—­μˆœμœΌλ‘œ κ²Œμ‹œλ¬Ό 쑰회
public List<PostResponseDto> sortPostsByCreated(String email) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postRepository.findAllByOrderByCreatedDesc();
return convertPostsToDto(posts, memberId);
Long memberRegionId = getMemberRegionId(email);
List<Post> posts = postRepository.findByRegion_IdOrderByCreatedDesc(memberRegionId);
return getPostResponseDtos(email, posts);
}

// ꡬ맀일자 μ—­μˆœμœΌλ‘œ κ²Œμ‹œλ¬Ό 쑰회
public List<PostResponseDto> sortPostsByBuyDate(String email) {
Long memberRegionId = getMemberRegionId(email);
List<Post> posts = postRepository.findByRegion_IdOrderByBuyDateDesc(memberRegionId);
return getPostResponseDtos(email, posts);
}

private List<PostResponseDto> getPostResponseDtos(String email, List<Post> posts) {
Member member = getMemberByEmail(email);
Long memberId = member.getId();
List<Post> posts = postRepository.findAllByOrderByBuyDateDesc();
return convertPostsToDto(posts, memberId);
}
}
Expand Down
Loading
Loading