22
33import com .amcamp .domain .image .application .ImageService ;
44import com .amcamp .domain .member .domain .Member ;
5+ import com .amcamp .domain .track .dao .TrackRepository ;
56import com .amcamp .domain .wishlist .dao .WishlistRepository ;
67import com .amcamp .domain .wishlist .domain .Wishlist ;
78import com .amcamp .domain .wishlist .dto .response .WishlistInfoResponse ;
9+ import com .amcamp .global .error .exception .CustomException ;
10+ import com .amcamp .global .error .exception .ErrorCode ;
811import com .amcamp .global .util .MemberUtil ;
912import lombok .RequiredArgsConstructor ;
1013import org .springframework .stereotype .Service ;
1114import org .springframework .transaction .annotation .Transactional ;
1215import org .springframework .web .multipart .MultipartFile ;
1316
17+ import java .util .List ;
18+
1419@ Service
1520@ Transactional
1621@ RequiredArgsConstructor
@@ -19,6 +24,7 @@ public class WishlistService {
1924 private final WishlistRepository wishlistRepository ;
2025 private final MemberUtil memberUtil ;
2126 private final ImageService imageService ;
27+ private final TrackRepository trackRepository ;
2228
2329 public void createWishlist (String title , MultipartFile file ) {
2430 Member member = memberUtil .getCurrentMember ();
@@ -30,5 +36,51 @@ public void createWishlist(String title, MultipartFile file) {
3036
3137 new WishlistInfoResponse (wishlist .getId (), wishlist .getTitle (), wishlist .getImageUrl ());
3238 }
39+
40+ public void deleteWishlist (Long wishlistId ) {
41+ Member currentMember = memberUtil .getCurrentMember ();
42+ Wishlist wishlist = findWishlistById (wishlistId );
43+
44+ validateOwnership (wishlist , currentMember );
45+
46+ wishlistRepository .delete (wishlist );
47+ }
48+
49+ public List <WishlistInfoResponse > findAllWishlist () {
50+ Member currentMember = memberUtil .getCurrentMember ();
51+
52+ return wishlistRepository .findByMember (currentMember )
53+ .stream ()
54+ .map (WishlistInfoResponse ::from )
55+ .toList ();
56+ }
57+
58+ public void editWishlist (Long wishlistId , String title , MultipartFile file ) {
59+ Member currentMember = memberUtil .getCurrentMember ();
60+ Wishlist wishlist = findWishlistById (wishlistId );
61+
62+ validateOwnership (wishlist , currentMember );
63+
64+ if (title != null && !title .isBlank ()) {
65+ wishlist .updateTitle (title );
66+ }
67+
68+ if (file != null && !file .isEmpty ()) {
69+ String imageUrl = imageService .uploadInitWishlistImage (file );
70+ wishlist .updateImageUrl (imageUrl );
71+ imageService .storeImageInfo (imageUrl , wishlist .getId ().toString ());
72+ }
73+ }
74+
75+ private void validateOwnership (Wishlist wishlist , Member currentMember ) {
76+ if (!wishlist .getMember ().equals (currentMember )) {
77+ throw new CustomException (ErrorCode .WISHLIST_MEMBER_MISMATCH );
78+ }
79+ }
80+
81+ private Wishlist findWishlistById (Long wishlistId ) {
82+ return wishlistRepository .findById (wishlistId )
83+ .orElseThrow (() -> new CustomException ((ErrorCode .WISHLIST_NOT_FOUND )));
84+ }
3385}
3486
0 commit comments