Skip to content

Commit 0c3a922

Browse files
authored
Merge pull request #17 from KONKUK-MAP-Service/feat-Bookmark
💡[Feat] : 북마크 기능 구현 #17
2 parents affad5c + 0e73c80 commit 0c3a922

12 files changed

+179
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.cona.KUsukKusuk.bookmark.controller;
2+
3+
import com.cona.KUsukKusuk.bookmark.dto.BookmarkDeleteRequest;
4+
import com.cona.KUsukKusuk.bookmark.dto.BookmarkRequest;
5+
import com.cona.KUsukKusuk.bookmark.service.BookmarkService;
6+
import com.cona.KUsukKusuk.global.response.HttpResponse;
7+
import io.swagger.v3.oas.annotations.Operation;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.web.bind.annotation.DeleteMapping;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
@RestController
16+
@RequestMapping("/bookmark")
17+
@RequiredArgsConstructor
18+
public class BookmarkController {
19+
private final BookmarkService bookmarkService;
20+
21+
@PostMapping("/add")
22+
@Operation(summary = "북마트 등록", description = "북마크 등록을 수행합니다.")
23+
24+
public HttpResponse<String> addBookmark(@RequestBody BookmarkRequest request) {
25+
bookmarkService.addBookmark(request.spotId());
26+
return HttpResponse.okBuild("장소를 북마크했습니다.");
27+
}
28+
29+
@DeleteMapping("/delete")
30+
public HttpResponse<String> deleteBookmark(@RequestBody BookmarkDeleteRequest request) {
31+
bookmarkService.deleteBookmark(request.bookmarkId());
32+
return HttpResponse.okBuild("장소 북마크를 삭제했습니다.");
33+
}
34+
}

src/main/java/com/cona/KUsukKusuk/bookmark/domain/Bookmark.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cona.KUsukKusuk.bookmark.domain;
22

3+
import com.cona.KUsukKusuk.global.domain.BaseEntity;
34
import com.cona.KUsukKusuk.spot.domain.Spot;
45
import com.cona.KUsukKusuk.user.domain.User;
56
import jakarta.persistence.Entity;
@@ -18,7 +19,7 @@
1819
@AllArgsConstructor
1920
@Getter
2021
@Setter
21-
public class Bookmark {
22+
public class Bookmark extends BaseEntity {
2223
@Id
2324
@GeneratedValue(strategy = GenerationType.IDENTITY)
2425
private Long id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.cona.KUsukKusuk.bookmark.dto;
2+
3+
public record BookmarkDeleteRequest(
4+
Long bookmarkId
5+
) {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.cona.KUsukKusuk.bookmark.dto;
2+
3+
public record BookmarkRequest(
4+
Long spotId
5+
) {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.cona.KUsukKusuk.bookmark.dto;
2+
3+
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
4+
import com.cona.KUsukKusuk.spot.domain.Spot;
5+
import java.time.LocalDateTime;
6+
import lombok.Builder;
7+
8+
@Builder
9+
public record BookmarkResponseDto(
10+
Long bookmarkId,
11+
String spotname,
12+
String review,
13+
LocalDateTime createDate,
14+
String SpotImageurl
15+
16+
) {
17+
public static BookmarkResponseDto of(Bookmark bookmark, Spot spot) {
18+
19+
return BookmarkResponseDto.builder()
20+
.bookmarkId(bookmark.getId())
21+
.spotname(spot.getSpotName())
22+
.review(spot.getReview())
23+
.createDate(bookmark.createdDate)
24+
.build();
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.cona.KUsukKusuk.bookmark.dto;
2+
3+
public record UserBookmarkResponse(
4+
5+
Long bookmarkId,
6+
Long spotId,
7+
String spotName
8+
) {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.cona.KUsukKusuk.bookmark.exception;
2+
3+
import com.cona.KUsukKusuk.global.exception.HttpExceptionCode;
4+
import org.springframework.http.HttpStatus;
5+
6+
public class BookmarkException extends RuntimeException {
7+
private final HttpStatus httpStatus;
8+
9+
public BookmarkException(HttpExceptionCode exceptionCode) {
10+
super(exceptionCode.getMessage());
11+
this.httpStatus = exceptionCode.getHttpStatus();
12+
}
13+
14+
public BookmarkException() {
15+
this(HttpExceptionCode.BOOK_MARK_ERR);
16+
}
17+
18+
}

src/main/java/com/cona/KUsukKusuk/bookmark/repository/BookmarkRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
44
import com.cona.KUsukKusuk.user.domain.User;
5+
import java.util.List;
6+
import java.util.Optional;
57
import org.springframework.data.jpa.repository.JpaRepository;
68

79
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
810
void deleteAllByUser(User user);
11+
List<Bookmark> findByUser(User user);
912

1013
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.cona.KUsukKusuk.bookmark.service;
2+
3+
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
4+
import com.cona.KUsukKusuk.bookmark.dto.BookmarkResponseDto;
5+
import com.cona.KUsukKusuk.bookmark.dto.UserBookmarkResponse;
6+
import com.cona.KUsukKusuk.bookmark.exception.BookmarkException;
7+
import com.cona.KUsukKusuk.bookmark.repository.BookmarkRepository;
8+
import com.cona.KUsukKusuk.global.exception.HttpExceptionCode;
9+
import com.cona.KUsukKusuk.spot.domain.Spot;
10+
import com.cona.KUsukKusuk.spot.exception.SpotNotFoundException;
11+
import com.cona.KUsukKusuk.spot.repository.SpotRepository;
12+
import com.cona.KUsukKusuk.user.domain.User;
13+
import com.cona.KUsukKusuk.user.service.UserService;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
import lombok.RequiredArgsConstructor;
17+
import org.springframework.stereotype.Service;
18+
19+
@Service
20+
@RequiredArgsConstructor
21+
public class BookmarkService {
22+
private final BookmarkRepository bookmarkRepository;
23+
private final UserService userService;
24+
private final SpotRepository spotRepository;
25+
26+
public void addBookmark(Long spotId) {
27+
String username = userService.getUsernameBySecurityContext();
28+
User user = userService.findUserByUserid(username);
29+
30+
Spot spot = spotRepository.findById(spotId)
31+
.orElseThrow(() -> new SpotNotFoundException(HttpExceptionCode.SPOT_NOT_FOUND));
32+
33+
Bookmark bookmark = new Bookmark();
34+
bookmark.setUser(user);
35+
bookmark.setSpot(spot);
36+
37+
bookmarkRepository.save(bookmark);
38+
}
39+
40+
public void deleteBookmark(Long bookmarkId) {
41+
bookmarkRepository.deleteById(bookmarkId);
42+
}
43+
44+
45+
public List<BookmarkResponseDto> getUserBookmarks() {
46+
String username = userService.getUsernameBySecurityContext();
47+
User user = userService.findUserByUserid(username);
48+
List<Bookmark> userBookmarks = bookmarkRepository.findByUser(user);
49+
if (userBookmarks == null || userBookmarks.isEmpty()) {
50+
throw new BookmarkException(HttpExceptionCode.BOOK_MARK_NOT_EXIST);
51+
}
52+
53+
54+
return userBookmarks.stream()
55+
.map(bookmark -> BookmarkResponseDto.of(bookmark,bookmark.getSpot()))
56+
.collect(Collectors.toList());
57+
}
58+
}

src/main/java/com/cona/KUsukKusuk/global/exception/HttpExceptionCode.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public enum HttpExceptionCode {
3939
IMAGE_UPLOAD_FAILED(HttpStatus.NOT_FOUND, "이미지 업로드에 실패하였습니다."),
4040
SPOT_NOT_FOUND(HttpStatus.NOT_FOUND,"해당 spot ID가 DB에 존재하지 않습니다."),
4141
USER_LOGIN_PERMIT_FAIL(HttpStatus.FORBIDDEN,"로그인한 사용자만 이용할 수 있습니다. "),
42-
USER_NOT_MATCH(HttpStatus.BAD_REQUEST, "해당 사용자가 작성한 글이 아닙니다.");
42+
USER_NOT_MATCH(HttpStatus.BAD_REQUEST, "해당 사용자가 작성한 글이 아닙니다."),
43+
BOOK_MARK_ERR(HttpStatus.BAD_REQUEST, "북마크 등록에 실피하였습니다."),
44+
BOOK_MARK_NOT_EXIST(HttpStatus.NOT_FOUND, "등록된 북마크가 존재하지 않습니다.");
4345

4446

4547

src/main/java/com/cona/KUsukKusuk/spot/dto/SpotGetResponse.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
44
import com.cona.KUsukKusuk.spot.domain.Spot;
55
import io.swagger.v3.oas.annotations.media.Schema;
6+
import java.time.LocalDateTime;
67
import java.util.List;
78
import lombok.Builder;
89

@@ -15,7 +16,8 @@ public record SpotGetResponse(
1516
List<String>images,
1617
String longtitude,
1718
String latitude,
18-
String review
19+
String review,
20+
LocalDateTime createDate
1921
) {
2022
public static SpotGetResponse of(Spot spot,Boolean bool) {
2123
return SpotGetResponse.builder()
@@ -26,6 +28,7 @@ public static SpotGetResponse of(Spot spot,Boolean bool) {
2628
.longtitude(spot.getLongitude())
2729
.latitude(spot.getLatitude())
2830
.review(spot.getReview())
31+
.createDate(spot.getCreatedDate())
2932
.build();
3033
}
3134
}

src/main/java/com/cona/KUsukKusuk/user/controller/UserController.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.cona.KUsukKusuk.user.controller;
22

3+
import com.cona.KUsukKusuk.bookmark.dto.BookmarkResponseDto;
4+
import com.cona.KUsukKusuk.bookmark.dto.UserBookmarkResponse;
5+
import com.cona.KUsukKusuk.bookmark.service.BookmarkService;
36
import com.cona.KUsukKusuk.global.response.HttpResponse;
47
import com.cona.KUsukKusuk.global.security.JWTUtil;
58
import com.cona.KUsukKusuk.user.domain.User;
@@ -19,6 +22,7 @@
1922
import io.swagger.v3.oas.annotations.Operation;
2023
import io.swagger.v3.oas.annotations.tags.Tag;
2124
import jakarta.validation.Valid;
25+
import java.util.List;
2226
import lombok.RequiredArgsConstructor;
2327
import org.springframework.web.bind.annotation.DeleteMapping;
2428
import org.springframework.web.bind.annotation.GetMapping;
@@ -36,6 +40,7 @@
3640
public class UserController {
3741
private final UserService userService;
3842
private final JWTUtil jwtUtil;
43+
private final BookmarkService bookmarkService;
3944

4045
@PostMapping("/join")
4146
@Operation(summary = "회원가입", description = "회원가입을 수행합니다.")
@@ -117,6 +122,11 @@ public HttpResponse<UserProfileResponse> getCurrentUserProfile() {
117122
UserProfileResponse userProfile = userService.getCurrentUserProfile();
118123
return HttpResponse.okBuild(userProfile);
119124
}
125+
@GetMapping("/bookmarks")
126+
public HttpResponse<List<BookmarkResponseDto>> getUserBookmarks() {
127+
List<BookmarkResponseDto> bookmarks = bookmarkService.getUserBookmarks();
128+
return HttpResponse.okBuild(bookmarks);
129+
}
120130

121131

122132
}

0 commit comments

Comments
 (0)