From cbdf6c6531efab40296a11682d2bfbd2c0d53592 Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 01:31:43 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20Category=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #24 --- .../controller/CategoryController.java | 32 +++++++++++++++++++ .../category/dto/CategoryRequestDto.java | 18 +++++++++++ .../snapspot/category/entity/Category.java | 23 +++++++++++++ .../repository/CategoryRepository.java | 7 ++++ .../category/service/CategoryService.java | 17 ++++++++++ 5 files changed, 97 insertions(+) create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/dto/CategoryRequestDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java diff --git a/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java b/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java new file mode 100644 index 0000000..9ee47e9 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java @@ -0,0 +1,32 @@ +package com.umc.hackaton.snapspot.category.controller; + +import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto; +import com.umc.hackaton.snapspot.category.service.CategoryService; +import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +@Slf4j +@RequestMapping("/api/v1/categories") +public class CategoryController { + private final CategoryService categoryService; + + @PostMapping("/") + public ResponseEntity createCategory(@RequestBody CategoryRequestDto category) { + try { + categoryService.save(category); + return ResponseEntity.ok().body(category); + } catch (Exception e){ + log.info("카테고리 생성에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리 생성에 실패하였습니다."); + } + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/category/dto/CategoryRequestDto.java b/src/main/java/com/umc/hackaton/snapspot/category/dto/CategoryRequestDto.java new file mode 100644 index 0000000..85880b6 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/dto/CategoryRequestDto.java @@ -0,0 +1,18 @@ +package com.umc.hackaton.snapspot.category.dto; + +import com.umc.hackaton.snapspot.category.entity.Category; +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class CategoryRequestDto { + @NotNull private String categoryName; + + @Builder + public Category toEntity() { + return Category.builder() + .categoryName(categoryName) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java b/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java new file mode 100644 index 0000000..6557694 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java @@ -0,0 +1,23 @@ +package com.umc.hackaton.snapspot.category.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class Category extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "category_name", nullable = false) + private String categoryName; +} diff --git a/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java new file mode 100644 index 0000000..38ca5be --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java @@ -0,0 +1,7 @@ +package com.umc.hackaton.snapspot.category.repository; + +import com.umc.hackaton.snapspot.category.entity.Category; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CategoryRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java new file mode 100644 index 0000000..0372857 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java @@ -0,0 +1,17 @@ +package com.umc.hackaton.snapspot.category.service; + +import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto; +import com.umc.hackaton.snapspot.category.repository.CategoryRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CategoryService { + private final CategoryRepository categoryRepository; + + + public void save(CategoryRequestDto category) { + categoryRepository.save(category.toEntity()); + } +} From a5583de431eacc7ca8c76f6d81089f68009f6aea Mon Sep 17 00:00:00 2001 From: mashin2002 Date: Fri, 5 Jul 2024 01:43:32 +0900 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit spot 좋아요 등록 및 삭제 구현 #15 --- build.gradle | 2 +- .../like/controller/LikeController.java | 27 ++++++++ .../like/converter/LikeConverter.java | 14 +++++ .../hackaton/snapspot/like/dto/LikeDto.java | 21 +++++++ .../hackaton/snapspot/like/entity/Like.java | 34 +++++++++++ .../like/repository/LikeRepository.java | 13 ++++ .../snapspot/like/service/LikeService.java | 61 +++++++++++++++++++ .../hackaton/snapspot/spot/entity/Spot.java | 8 +++ .../spot/repository/SpotRepository.java | 2 + .../user/repository/UserRepository.java | 3 + src/main/resources/application.yml | 4 +- 11 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/umc/hackaton/snapspot/like/controller/LikeController.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/like/converter/LikeConverter.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/like/repository/LikeRepository.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/like/service/LikeService.java diff --git a/build.gradle b/build.gradle index 6632356..5f6a3fb 100644 --- a/build.gradle +++ b/build.gradle @@ -39,4 +39,4 @@ dependencies { tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/src/main/java/com/umc/hackaton/snapspot/like/controller/LikeController.java b/src/main/java/com/umc/hackaton/snapspot/like/controller/LikeController.java new file mode 100644 index 0000000..85ac2a3 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/like/controller/LikeController.java @@ -0,0 +1,27 @@ +package com.umc.hackaton.snapspot.like.controller; + +import com.umc.hackaton.snapspot.like.dto.LikeDto; +import com.umc.hackaton.snapspot.like.service.LikeService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/v1/spots/like") +@RequiredArgsConstructor +public class LikeController { + + private final LikeService likeService; + + @PostMapping("") + public ResponseEntity registerLike(@RequestBody LikeDto.RequestDto likeDto){ + return ResponseEntity.ok(likeService.registerLike(likeDto)); + } + + + @DeleteMapping("") + public ResponseEntity deleteLike(@RequestBody LikeDto.RequestDto likeDto){ + return ResponseEntity.ok(likeService.deleteLike(likeDto)); + } + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/like/converter/LikeConverter.java b/src/main/java/com/umc/hackaton/snapspot/like/converter/LikeConverter.java new file mode 100644 index 0000000..b6d7169 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/like/converter/LikeConverter.java @@ -0,0 +1,14 @@ +package com.umc.hackaton.snapspot.like.converter; + +import com.umc.hackaton.snapspot.like.entity.Like; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; + +public class LikeConverter { + public static Like toEntity(User user, Spot spot){ + return Like.builder() + .spot(spot) + .user(user) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java b/src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java new file mode 100644 index 0000000..21015bd --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.like.dto; + +import lombok.Builder; +import lombok.Getter; + +public class LikeDto { + @Builder + @Getter + public static class RequestDto{ + Long userId; + Long spotId; + + } + + @Builder + @Getter + public static class ResponseDto{ + String message; + + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java b/src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java new file mode 100644 index 0000000..5e64065 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java @@ -0,0 +1,34 @@ +package com.umc.hackaton.snapspot.like.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; + +@Entity +@Table(name = "`like`") +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class Like extends BaseEntity { + + + @Id + @Column(name="id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "spot_id", nullable = false) + private Spot spot; + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/like/repository/LikeRepository.java b/src/main/java/com/umc/hackaton/snapspot/like/repository/LikeRepository.java new file mode 100644 index 0000000..1580625 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/like/repository/LikeRepository.java @@ -0,0 +1,13 @@ +package com.umc.hackaton.snapspot.like.repository; + +import com.umc.hackaton.snapspot.like.entity.Like; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface LikeRepository extends JpaRepository { + + Like findByUserAndSpot(User user, Spot spot); +} diff --git a/src/main/java/com/umc/hackaton/snapspot/like/service/LikeService.java b/src/main/java/com/umc/hackaton/snapspot/like/service/LikeService.java new file mode 100644 index 0000000..622ed0f --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/like/service/LikeService.java @@ -0,0 +1,61 @@ +package com.umc.hackaton.snapspot.like.service; + +import com.umc.hackaton.snapspot.like.converter.LikeConverter; +import com.umc.hackaton.snapspot.like.dto.LikeDto; +import com.umc.hackaton.snapspot.like.entity.Like; +import com.umc.hackaton.snapspot.like.repository.LikeRepository; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class LikeService { + + private final LikeRepository likeRepository; + private final SpotRepository spotRepository; + private final UserRepository userRepository; + + public LikeDto.ResponseDto registerLike(LikeDto.RequestDto likeDto) { + User user = userRepository.findById(likeDto.getUserId()).orElseThrow(()->new IllegalArgumentException("존재하지 않는 사용자입니다.")); + + Spot spot = spotRepository.findById(likeDto.getSpotId()).orElseThrow(()->new IllegalArgumentException("존재하지 않는 스팟입니다.")); + Like likeEntity = likeRepository.findByUserAndSpot(user, spot); + + if(likeEntity == null){ + Like saveLike = LikeConverter.toEntity(user, spot); + likeRepository.save(saveLike); + //like 하나 증가 + spot.increaseLike(); + spotRepository.save(spot); + return LikeDto.ResponseDto.builder() + .message("좋아요 등록 완료").build(); + } + return LikeDto.ResponseDto.builder() + .message("좋아요 등록 실패").build(); + } + + + public LikeDto.ResponseDto deleteLike(LikeDto.RequestDto likeDto) { + User user = userRepository.findById(likeDto.getUserId()).orElseThrow(()->new IllegalArgumentException("존재하지 않는 사용자입니다.")); + + Spot spot = spotRepository.findById(likeDto.getSpotId()).orElseThrow(()->new IllegalArgumentException("존재하지 않는 스팟입니다.")); + + Like likeEntity = likeRepository.findByUserAndSpot(user, spot); + + if(likeEntity != null){ + likeRepository.delete(likeEntity); + spot.decreaseLike(); + spotRepository.save(spot); + return LikeDto.ResponseDto.builder() + .message("삭제 완료").build(); + } + return LikeDto.ResponseDto.builder() + .message("좋아요 삭제에 실패했습니다.").build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index 7defc85..1ae198d 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -46,4 +46,12 @@ public class Spot extends BaseEntity { @Column(name = "description", nullable = false, columnDefinition = "TEXT") private String description; + + public void increaseLike() { + this.likeNum -= 1; + } + + public void decreaseLike() { + this.likeNum += 1; + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java index 0079df6..94ffc77 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java @@ -6,4 +6,6 @@ import java.util.Optional; public interface SpotRepository extends JpaRepository { + @Override + void delete(Spot entity); } diff --git a/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java b/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java index a6b441a..210ca30 100644 --- a/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java @@ -2,11 +2,14 @@ import com.umc.hackaton.snapspot.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import java.util.Optional; +@Repository public interface UserRepository extends JpaRepository { Optional findByUsername(String username); User findUserById(Long id); } + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d9b562f..84660ba 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,7 @@ spring: datasource: -# driver-class-name: com.mysql.cj.jdbc.Driver -# url: jdbc:mysql://localhost:3306/spot?useSSL=false&allowPublicKeyRetrieval=true + # driver-class-name: com.mysql.cj.jdbc.Driver + # url: jdbc:mysql://localhost:3306/spot?useSSL=false&allowPublicKeyRetrieval=true url: jdbc:h2:mem:spotdb username: root password: From 065a90c5605b1b4f3dee0e0adb38c077178e0a31 Mon Sep 17 00:00:00 2001 From: MJJ Date: Fri, 5 Jul 2024 01:52:38 +0900 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20Visit=20=EC=A0=80=EC=9E=A5/?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../visit/controller/VisitController.java | 26 +++++++++ .../hackaton/snapspot/visit/dto/VisitDto.java | 21 ++++++++ .../snapspot/visit/dto/VisitRequestDto.java | 12 +++++ .../snapspot/visit/dto/VisitResponseDto.java | 21 ++++++++ .../hackaton/snapspot/visit/entity/Visit.java | 30 +++++++++++ .../visit/repository/VisitRepository.java | 11 ++++ .../snapspot/visit/service/VisitService.java | 54 +++++++++++++++++++ 7 files changed, 175 insertions(+) create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java b/src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java new file mode 100644 index 0000000..5aab69f --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/controller/VisitController.java @@ -0,0 +1,26 @@ +package com.umc.hackaton.snapspot.visit.controller; + +import com.umc.hackaton.snapspot.visit.dto.VisitRequestDto; +import com.umc.hackaton.snapspot.visit.service.VisitService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/v1/visit") +@RequiredArgsConstructor +public class VisitController { + + private final VisitService visitService; + + @PostMapping + public ResponseEntity saveVisit(@RequestBody VisitRequestDto dto) { + try { + visitService.saveVisit(dto); + return ResponseEntity.ok().body("스팟 방문 저장 성공."); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 방문 저장에 실패하였습니다."); + } + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java new file mode 100644 index 0000000..bff014a --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.visit.dto; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.visit.entity.Visit; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class VisitDto { + private Spot spot; + private User user; + + public Visit toEntity() { + return Visit.builder() + .spot(spot) + .user(user) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java new file mode 100644 index 0000000..2ca697d --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitRequestDto.java @@ -0,0 +1,12 @@ +package com.umc.hackaton.snapspot.visit.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class VisitRequestDto { + private Long spotId; + private Long userId; + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java new file mode 100644 index 0000000..860f71b --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/dto/VisitResponseDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.visit.dto; + +import com.umc.hackaton.snapspot.visit.entity.Visit; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class VisitResponseDto { + private Long id; + private Long spotId; + private Long userId; + + public static VisitResponseDto fromEntity(Visit visit) { + return VisitResponseDto.builder() + .id(visit.getId()) + .spotId(visit.getSpot().getId()) + .userId(visit.getUser().getId()) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java b/src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java new file mode 100644 index 0000000..6c9beea --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/entity/Visit.java @@ -0,0 +1,30 @@ +package com.umc.hackaton.snapspot.visit.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class Visit extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "spot_id") + private Spot spot; + + @ManyToOne + @JoinColumn(name = "user_id") + private User user; +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java b/src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java new file mode 100644 index 0000000..9f72371 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/repository/VisitRepository.java @@ -0,0 +1,11 @@ +package com.umc.hackaton.snapspot.visit.repository; + +import com.umc.hackaton.snapspot.visit.entity.Visit; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface VisitRepository extends JpaRepository { + + Optional findBySpotId(Long spotId); +} diff --git a/src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java b/src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java new file mode 100644 index 0000000..e0790a6 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/visit/service/VisitService.java @@ -0,0 +1,54 @@ +package com.umc.hackaton.snapspot.visit.service; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import com.umc.hackaton.snapspot.visit.dto.VisitDto; +import com.umc.hackaton.snapspot.visit.dto.VisitRequestDto; +import com.umc.hackaton.snapspot.visit.dto.VisitResponseDto; +import com.umc.hackaton.snapspot.visit.entity.Visit; +import com.umc.hackaton.snapspot.visit.repository.VisitRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class VisitService { + + private final VisitRepository visitRepository; + private final UserRepository userRepository; + private final SpotRepository spotRepository; + + @Transactional + public void saveVisit(VisitRequestDto dto) { + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다.")); + + Spot spot = spotRepository.findById(dto.getSpotId()) + .orElseThrow(() -> new IllegalArgumentException("스팟을 찾을 수 없습니다.")); + + VisitDto visitDto = new VisitDto(); + visitDto.setUser(user); + visitDto.setSpot(spot); + + visitRepository.save(visitDto.toEntity()); + + } + + @Transactional + public VisitResponseDto getVisit(Long spotId) { + Optional visit = visitRepository.findBySpotId(spotId); + + if (visit.isEmpty()) { + throw new IllegalArgumentException("해당 스팟에 대한 방문 정보가 없습니다."); + } + + return VisitResponseDto.fromEntity(visit.get()); + } +} From a958a3b0d12d159066718daab65a2c2b19755b30 Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 01:58:01 +0900 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20Category=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 전체 카테고리 조회 categorySpots 필드는 추후 삭제하는 방향으로 일단 Spot 연결 후 테스트 #24 --- .../controller/CategoryController.java | 20 ++++++++--- .../snapspot/category/entity/Category.java | 15 +++++--- .../category/entity/CategorySpot.java | 35 +++++++++++++++++++ .../repository/CategoryRepository.java | 5 +++ .../category/service/CategoryService.java | 11 ++++++ 5 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java diff --git a/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java b/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java index 9ee47e9..8a5ccc3 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java @@ -1,16 +1,16 @@ package com.umc.hackaton.snapspot.category.controller; import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto; +import com.umc.hackaton.snapspot.category.entity.Category; import com.umc.hackaton.snapspot.category.service.CategoryService; import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @RequiredArgsConstructor @RestController @@ -29,4 +29,16 @@ public ResponseEntity createCategory(@RequestBody CategoryRequestDto category return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리 생성에 실패하였습니다."); } } + + + @GetMapping("/") + public ResponseEntity showAllCategories() { + try { + List categories = categoryService.showAll(); + return ResponseEntity.ok().body(categories); + } catch (Exception e){ + log.info("카테고리 리스트 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리 리스트 조회에 실패하였습니다."); + } + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java b/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java index 6557694..79ad7b0 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java @@ -1,11 +1,12 @@ package com.umc.hackaton.snapspot.category.entity; +import com.fasterxml.jackson.annotation.JsonManagedReference; import com.umc.hackaton.snapspot.config.entity.BaseEntity; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; + +import java.util.LinkedHashSet; +import java.util.Set; @Entity @Getter @@ -20,4 +21,10 @@ public class Category extends BaseEntity { @Column(name = "category_name", nullable = false) private String categoryName; + + @ToString.Exclude + @JsonManagedReference + @OrderBy("id") + @OneToMany(mappedBy = "category") + private final Set categorySpots = new LinkedHashSet<>(); } diff --git a/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java b/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java new file mode 100644 index 0000000..2fc5b98 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java @@ -0,0 +1,35 @@ +package com.umc.hackaton.snapspot.category.entity; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class CategorySpot extends BaseEntity{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "spot_id") + @JsonBackReference + private Spot spot; + + @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "category_id") + @JsonBackReference + private Category category; +} diff --git a/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java index 38ca5be..84aefcb 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java @@ -2,6 +2,11 @@ import com.umc.hackaton.snapspot.category.entity.Category; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; public interface CategoryRepository extends JpaRepository { + @Query("SELECT c.id, c.categoryName FROM Category c") + List findAllCategories(); } diff --git a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java index 0372857..30933d2 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java @@ -1,10 +1,14 @@ package com.umc.hackaton.snapspot.category.service; import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto; +import com.umc.hackaton.snapspot.category.entity.Category; import com.umc.hackaton.snapspot.category.repository.CategoryRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.stream.Collectors; + @Service @RequiredArgsConstructor public class CategoryService { @@ -14,4 +18,11 @@ public class CategoryService { public void save(CategoryRequestDto category) { categoryRepository.save(category.toEntity()); } + + public List showAll() { + List objects = categoryRepository.findAllCategories(); + return objects.stream() + .map(object -> new Category((Long) object[0], (String) object[1])) + .collect(Collectors.toList()); + } } From f1d3e72351ea228408c1d420e23c33a96bc4008b Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 02:20:52 +0900 Subject: [PATCH 05/13] =?UTF-8?q?feat:=20Spot=EC=83=9D=EC=84=B1=EC=8B=9C?= =?UTF-8?q?=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EC=84=A0=ED=83=9D?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EB=B0=98=EC=98=81=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #24 --- .../category/entity/CategorySpot.java | 2 ++ .../repository/CategorySpotRepository.java | 8 ++++++ .../snapspot/spot/dto/SpotRequestDto.java | 3 +++ .../snapspot/spot/service/SpotService.java | 27 ++++++++++++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java diff --git a/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java b/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java index 2fc5b98..f3d2c1d 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java @@ -32,4 +32,6 @@ public class CategorySpot extends BaseEntity{ @JoinColumn(name = "category_id") @JsonBackReference private Category category; + + } diff --git a/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java new file mode 100644 index 0000000..5cb1b4b --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java @@ -0,0 +1,8 @@ +package com.umc.hackaton.snapspot.category.repository; + +import com.umc.hackaton.snapspot.category.entity.CategorySpot; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CategorySpotRepository extends JpaRepository { + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java index 8ba1b8c..399973c 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/dto/SpotRequestDto.java @@ -4,6 +4,8 @@ import lombok.Builder; import lombok.Getter; +import java.util.List; + @Getter public class SpotRequestDto { private Long userId; @@ -12,5 +14,6 @@ public class SpotRequestDto { private String title; private String description; private String imgUrl; + private List categoryNums; } diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java index dc87610..ce13d75 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java @@ -1,5 +1,9 @@ package com.umc.hackaton.snapspot.spot.service; +import com.umc.hackaton.snapspot.category.entity.Category; +import com.umc.hackaton.snapspot.category.entity.CategorySpot; +import com.umc.hackaton.snapspot.category.repository.CategoryRepository; +import com.umc.hackaton.snapspot.category.repository.CategorySpotRepository; import com.umc.hackaton.snapspot.spot.dto.SpotDto; import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; import com.umc.hackaton.snapspot.spot.entity.Spot; @@ -11,11 +15,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + @Service @RequiredArgsConstructor public class SpotService { private final SpotRepository spotRepository; private final UserRepository userRepository; + private final CategoryRepository categoryRepository; + private final CategorySpotRepository categorySpotRepository; @Transactional public void upload(SpotRequestDto dto) { @@ -30,7 +40,22 @@ public void upload(SpotRequestDto dto) { spotDto.setDescription(dto.getDescription()); spotDto.setImgUrl(dto.getImgUrl()); - spotRepository.save(spotDto.toEntity()); + Spot savedSpot = spotRepository.save(spotDto.toEntity()); + + List categorySpots = new ArrayList<>(); + for (Long categoryId : dto.getCategoryNums()) { + Optional categoryOptional = categoryRepository.findById(categoryId); + categoryOptional.ifPresent(category -> { + CategorySpot categorySpot = CategorySpot.builder() + .category(category) + .spot(savedSpot) + .build(); + categorySpots.add(categorySpot); + }); + } + + // CategorySpot 저장 + categorySpotRepository.saveAll(categorySpots); } } From 8806b602975678423e0480e3e405be26e78a56e8 Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 03:02:58 +0900 Subject: [PATCH 06/13] =?UTF-8?q?feat:=20category=EB=B3=84=20Spot=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B0=9B=EC=95=84=EC=98=A4?= =?UTF-8?q?=EA=B8=B0=20=EC=8B=A4=ED=8C=A8,=EC=A4=91=EA=B0=84=20=EC=BB=A4?= =?UTF-8?q?=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 이후 카테고리별 스팟 리스트 받아오기 따로 진행 #24 --- .../controller/CategoryController.java | 14 ++++++++ .../repository/CategorySpotRepository.java | 5 ++- .../category/service/CategoryService.java | 32 ++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java b/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java index 8a5ccc3..eb82659 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java @@ -4,6 +4,7 @@ import com.umc.hackaton.snapspot.category.entity.Category; import com.umc.hackaton.snapspot.category.service.CategoryService; import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -41,4 +42,17 @@ public ResponseEntity showAllCategories() { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리 리스트 조회에 실패하였습니다."); } } + + @GetMapping("/spots") + public ResponseEntity showSpotsByCategoryId( + @RequestParam List categoryIds + ) { + try { + List spots = categoryService.showSpotsByCategoryIds(categoryIds); + return ResponseEntity.ok().body(spots); + } catch (Exception e){ + log.info("카테고리별 스팟 리스트 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리별 스팟 리스트 조회에 실패하였습니다."); + } + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java index 5cb1b4b..a15fbda 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java @@ -1,8 +1,11 @@ package com.umc.hackaton.snapspot.category.repository; +import com.umc.hackaton.snapspot.category.entity.Category; import com.umc.hackaton.snapspot.category.entity.CategorySpot; import org.springframework.data.jpa.repository.JpaRepository; -public interface CategorySpotRepository extends JpaRepository { +import java.util.List; +public interface CategorySpotRepository extends JpaRepository { + List findAllByCategory(Category category); } diff --git a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java index 30933d2..5e37f27 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java @@ -2,17 +2,23 @@ import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto; import com.umc.hackaton.snapspot.category.entity.Category; +import com.umc.hackaton.snapspot.category.entity.CategorySpot; import com.umc.hackaton.snapspot.category.repository.CategoryRepository; +import com.umc.hackaton.snapspot.category.repository.CategorySpotRepository; +import com.umc.hackaton.snapspot.spot.dto.SpotDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class CategoryService { private final CategoryRepository categoryRepository; + private final CategorySpotRepository categorySpotRepository; public void save(CategoryRequestDto category) { @@ -25,4 +31,28 @@ public List showAll() { .map(object -> new Category((Long) object[0], (String) object[1])) .collect(Collectors.toList()); } + + public List showSpotsByCategoryIds(List categoryIds) { + Set spots = new HashSet<>(); + for (Long categoryId : categoryIds) { + Category category = categoryRepository.findById(categoryId).orElse(null); + if (category != null) { + List categorySpots = categorySpotRepository.findAllByCategory(category); + for (CategorySpot categorySpot : categorySpots) { + SpotDto spotDto = new SpotDto(); + User user = categorySpot.getSpot().getUser(); // 이 부분에서 User 엔티티를 Eager로 로딩 + spotDto.setUser(user); + spotDto.setLatitude(categorySpot.getSpot().getLatitude()); + spotDto.setLongitude(categorySpot.getSpot().getLongitude()); + spotDto.setTitle(categorySpot.getSpot().getTitle()); + spotDto.setDescription(categorySpot.getSpot().getDescription()); + spotDto.setImgUrl(categorySpot.getSpot().getImgUrl()); + + Spot spot = spotDto.toEntity(); + spots.add(spot); + } + } + } + return new ArrayList<>(spots); + } } From 1e84a0e073c29400427c8bed87345a6ea89c3b6e Mon Sep 17 00:00:00 2001 From: mashin2002 Date: Fri, 5 Jul 2024 03:10:43 +0900 Subject: [PATCH 07/13] =?UTF-8?q?feat:=20=EB=82=B4=EA=B0=80=20=EC=98=AC?= =?UTF-8?q?=EB=A6=B0=20=EC=8A=A4=ED=8C=9F=20=EC=A1=B0=ED=9A=8C=20=EB=B0=8F?= =?UTF-8?q?=20=EB=82=B4=20=EC=A0=95=EB=B3=B4=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 내가 올린 스팟 조회 및 내 정보 조회 구현 #29 --- .../mypage/controller/MypageController.java | 27 +++++++++++++++ .../mypage/converter/MypageConverter.java | 31 +++++++++++++++++ .../snapspot/mypage/dto/MypageDto.java | 21 ++++++++++++ .../mypage/service/MypageService.java | 33 +++++++++++++++++++ .../spot/repository/SpotRepository.java | 3 ++ 5 files changed, 115 insertions(+) create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java b/src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java new file mode 100644 index 0000000..f54c0dc --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/controller/MypageController.java @@ -0,0 +1,27 @@ +package com.umc.hackaton.snapspot.mypage.controller; + +import com.umc.hackaton.snapspot.mypage.dto.MypageDto; +import com.umc.hackaton.snapspot.mypage.service.MypageService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/mypage") +public class MypageController { + final private MypageService mypageService; + + @GetMapping("/{userId}") + public ResponseEntity> readAllMySpot(@PathVariable Long userId){ + return ResponseEntity.ok(mypageService.readAllMySpot(userId)); + } + + + @GetMapping("/{userId}/profile") + public ResponseEntity readMyProfile(@PathVariable Long userId){ + return ResponseEntity.ok(mypageService.readMyProfile(userId)); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java b/src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java new file mode 100644 index 0000000..9fcc5b6 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/converter/MypageConverter.java @@ -0,0 +1,31 @@ +package com.umc.hackaton.snapspot.mypage.converter; + +import com.umc.hackaton.snapspot.mypage.dto.MypageDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; + +import java.util.List; +import java.util.stream.Collectors; + +public class MypageConverter { + + public static List toDtoList(List spotList){ + return spotList.stream() + .map(MypageConverter::toMySptDto) + .collect(Collectors.toList()); + } + + public static MypageDto.MySptDto toMySptDto(Spot spot){ + return MypageDto.MySptDto.builder() + .imageUrl(spot.getImgUrl()) + .likeNum(spot.getLikeNum()) + .spotId(spot.getId()) + .build(); + } + public static MypageDto.MyProfile toMyProfileDto(User user){ + return MypageDto.MyProfile.builder() + .imageUrl(user.getProfileImg()) + .nickName(user.getNickname()) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java b/src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java new file mode 100644 index 0000000..4e3513d --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/dto/MypageDto.java @@ -0,0 +1,21 @@ +package com.umc.hackaton.snapspot.mypage.dto; + +import lombok.Builder; +import lombok.Getter; + +public class MypageDto { + @Builder + @Getter + public static class MySptDto{ + Long spotId; + String imageUrl; + Long likeNum; + } + + @Builder + @Getter + public static class MyProfile{ + String nickName; + String imageUrl; + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java b/src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java new file mode 100644 index 0000000..6c17b60 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/mypage/service/MypageService.java @@ -0,0 +1,33 @@ +package com.umc.hackaton.snapspot.mypage.service; + +import com.umc.hackaton.snapspot.mypage.converter.MypageConverter; +import com.umc.hackaton.snapspot.mypage.dto.MypageDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class MypageService { + + private SpotRepository spotRepository; + private UserRepository userRepository; + + public List readAllMySpot(Long userId) { + User user = userRepository.findById(userId).orElseThrow(()-> new IllegalArgumentException("존재하지 않는 사용자입니다.")); + List spotList = spotRepository.findAllByUser(user); + + return MypageConverter.toDtoList(spotList); + } + + public MypageDto.MyProfile readMyProfile(Long userId) { + User user = userRepository.findById(userId).orElseThrow(()-> new IllegalArgumentException("존재하지 않는 사용자입니다.")); + + return MypageConverter.toMyProfileDto(user); + } + + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java index 0079df6..9104f7b 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/repository/SpotRepository.java @@ -1,9 +1,12 @@ package com.umc.hackaton.snapspot.spot.repository; import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface SpotRepository extends JpaRepository { + List findAllByUser(User user); } From b04585c6590abf2ddf35b0a98ba738d8aafeeff7 Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 03:32:47 +0900 Subject: [PATCH 08/13] =?UTF-8?q?feat:=20dev=20=EB=B0=98=EC=98=81=20?= =?UTF-8?q?=EC=A0=84=20=EC=A4=91=EA=B0=84=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #24 --- .../category/entity/CategorySpot.java | 2 +- .../category/service/CategoryService.java | 33 +++++++++++-------- .../hackaton/snapspot/spot/entity/Spot.java | 16 ++++++++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java b/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java index f3d2c1d..0281bbd 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java @@ -22,7 +22,7 @@ public class CategorySpot extends BaseEntity{ private Long id; @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "spot_id") @JsonBackReference private Spot spot; diff --git a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java index 5e37f27..442a735 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java @@ -38,20 +38,27 @@ public List showSpotsByCategoryIds(List categoryIds) { Category category = categoryRepository.findById(categoryId).orElse(null); if (category != null) { List categorySpots = categorySpotRepository.findAllByCategory(category); - for (CategorySpot categorySpot : categorySpots) { - SpotDto spotDto = new SpotDto(); - User user = categorySpot.getSpot().getUser(); // 이 부분에서 User 엔티티를 Eager로 로딩 - spotDto.setUser(user); - spotDto.setLatitude(categorySpot.getSpot().getLatitude()); - spotDto.setLongitude(categorySpot.getSpot().getLongitude()); - spotDto.setTitle(categorySpot.getSpot().getTitle()); - spotDto.setDescription(categorySpot.getSpot().getDescription()); - spotDto.setImgUrl(categorySpot.getSpot().getImgUrl()); - - Spot spot = spotDto.toEntity(); - spots.add(spot); - } + List categorySpotsMapped = categorySpots.stream() + .map(CategorySpot::getSpot) + .collect(Collectors.toList()); + spots.addAll(categorySpotsMapped); } +// if (category != null) { +// List categorySpots = categorySpotRepository.findAllByCategory(category); +// for (CategorySpot categorySpot : categorySpots) { +// SpotDto spotDto = new SpotDto(); +// User user = categorySpot.getSpot().getUser(); +// spotDto.setUser(user); +// spotDto.setLatitude(categorySpot.getSpot().getLatitude()); +// spotDto.setLongitude(categorySpot.getSpot().getLongitude()); +// spotDto.setTitle(categorySpot.getSpot().getTitle()); +// spotDto.setDescription(categorySpot.getSpot().getDescription()); +// spotDto.setImgUrl(categorySpot.getSpot().getImgUrl()); +// +// Spot spot = spotDto.toEntity(); +// spots.add(spot); +// } +// } } return new ArrayList<>(spots); } diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index 7defc85..a29379c 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; import java.time.LocalDateTime; +import java.util.Objects; @Entity @Getter @@ -22,7 +23,7 @@ public class Spot extends BaseEntity { @Column(name = "id") private Long id; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "user_id", nullable = false) private User user; @@ -46,4 +47,17 @@ public class Spot extends BaseEntity { @Column(name = "description", nullable = false, columnDefinition = "TEXT") private String description; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Spot spot = (Spot) o; + return Objects.equals(id, spot.id); + } + + @Override + public int hashCode() { + return Objects.hashCode(id); + } } From 0d5e8cd214090d704fd209016e2dfc8990f7eddf Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 03:36:32 +0900 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20dev=20=EB=A8=B8=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #24 --- .../java/com/umc/hackaton/snapspot/spot/entity/Spot.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index a29379c..252c1e0 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -48,6 +48,14 @@ public class Spot extends BaseEntity { @Column(name = "description", nullable = false, columnDefinition = "TEXT") private String description; + public void increaseLike() { + this.likeNum -= 1; + } + + public void decreaseLike() { + this.likeNum += 1; + } + @Override public boolean equals(Object o) { if (this == o) return true; From caed91b8456ff5eb2c98a2d35859d51e9f0ceb2d Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 03:50:26 +0900 Subject: [PATCH 10/13] =?UTF-8?q?feat:=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #24 --- .../category/service/CategoryService.java | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java index 442a735..ba4b8ec 100644 --- a/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java +++ b/src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java @@ -5,9 +5,7 @@ import com.umc.hackaton.snapspot.category.entity.CategorySpot; import com.umc.hackaton.snapspot.category.repository.CategoryRepository; import com.umc.hackaton.snapspot.category.repository.CategorySpotRepository; -import com.umc.hackaton.snapspot.spot.dto.SpotDto; import com.umc.hackaton.snapspot.spot.entity.Spot; -import com.umc.hackaton.snapspot.user.entity.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -43,22 +41,6 @@ public List showSpotsByCategoryIds(List categoryIds) { .collect(Collectors.toList()); spots.addAll(categorySpotsMapped); } -// if (category != null) { -// List categorySpots = categorySpotRepository.findAllByCategory(category); -// for (CategorySpot categorySpot : categorySpots) { -// SpotDto spotDto = new SpotDto(); -// User user = categorySpot.getSpot().getUser(); -// spotDto.setUser(user); -// spotDto.setLatitude(categorySpot.getSpot().getLatitude()); -// spotDto.setLongitude(categorySpot.getSpot().getLongitude()); -// spotDto.setTitle(categorySpot.getSpot().getTitle()); -// spotDto.setDescription(categorySpot.getSpot().getDescription()); -// spotDto.setImgUrl(categorySpot.getSpot().getImgUrl()); -// -// Spot spot = spotDto.toEntity(); -// spots.add(spot); -// } -// } } return new ArrayList<>(spots); } From e94fa998cc82ada1830acd3992eeb0813c141950 Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 04:26:31 +0900 Subject: [PATCH 11/13] =?UTF-8?q?feat:=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #33 --- .../spot/controller/SpotController.java | 51 +++++++++++++++++-- .../hackaton/snapspot/spot/entity/Spot.java | 8 +++ .../snapspot/spot/service/SpotService.java | 14 +++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java b/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java index b0f394f..249985c 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/controller/SpotController.java @@ -1,18 +1,20 @@ package com.umc.hackaton.snapspot.spot.controller; import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; +import com.umc.hackaton.snapspot.spot.entity.Spot; import com.umc.hackaton.snapspot.spot.service.SpotService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @RequiredArgsConstructor @RestController +@Slf4j @RequestMapping("/api/v1/spots") public class SpotController { @@ -28,4 +30,45 @@ public ResponseEntity upload(@RequestBody SpotRequestDto dto){ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 업로드에 실패하였습니다."); } } + + @GetMapping("/{spotId}") + public ResponseEntity showSpot( + @PathVariable("spotId") Long spotId + ) { + try { + Spot spot = spotService.getSpot(spotId); + return ResponseEntity.ok().body(spot); + } catch (Exception e){ + log.info("스팟 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 조회에 실패하였습니다."); + } + } + + @DeleteMapping("/{spotId}") + public ResponseEntity deleteSpot( + @PathVariable("spotId") Long spotId + ) { + try { + spotService.deleteSpot(spotId); + return ResponseEntity.ok().body("success"); + } catch (Exception e){ + log.info("스팟 삭제에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 삭제에 실패하였습니다."); + } + } + + + @PatchMapping("/{spotId}") + public ResponseEntity patchSpot( + @PathVariable("spotId") Long spotId, + @RequestBody SpotRequestDto dto + ) { + try { + Spot spot = spotService.updateSpot(spotId, dto); + return ResponseEntity.ok().body(spot); + } catch (Exception e){ + log.info("스팟 수정에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 수정에 실패하였습니다."); + } + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index 252c1e0..d07a61b 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -1,6 +1,7 @@ package com.umc.hackaton.snapspot.spot.entity; import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto; import com.umc.hackaton.snapspot.user.entity.User; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -56,6 +57,13 @@ public void decreaseLike() { this.likeNum += 1; } + public Spot update(SpotRequestDto dto){ + this.description = dto.getDescription(); + this.imgUrl = dto.getImgUrl();; + this.title = dto.getTitle(); + return this; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java index ce13d75..dcd5376 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/service/SpotService.java @@ -13,6 +13,7 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -58,4 +59,17 @@ public void upload(SpotRequestDto dto) { categorySpotRepository.saveAll(categorySpots); } + + public Spot getSpot(Long spotId) { + return spotRepository.findById(spotId).orElse(null); + } + + public void deleteSpot(Long spotId) { + spotRepository.deleteById(spotId); + } + + public Spot updateSpot(Long spotId, SpotRequestDto dto) { + Spot spot = spotRepository.findById(spotId).orElse(null); + return spot.update(dto); + } } From a2402ae2e8e4862ab777f393616311255bfa068a Mon Sep 17 00:00:00 2001 From: wonjun0120 Date: Fri, 5 Jul 2024 04:52:17 +0900 Subject: [PATCH 12/13] =?UTF-8?q?feat:=20s3=20=EB=A7=81=ED=81=AC=EC=9A=A9?= =?UTF-8?q?=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EA=B8=B8=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #33 --- src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java index d07a61b..3cd75ef 100644 --- a/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java +++ b/src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java @@ -34,7 +34,7 @@ public class Spot extends BaseEntity { @Column(name = "longitude", nullable = false) private Double longitude; - @Column(name = "img_url", nullable = false) + @Column(name = "img_url", nullable = false, length = 1000) private String imgUrl; @Column(name = "like_num") From 3ea33de212db07166e786f59515ec3b286ed210b Mon Sep 17 00:00:00 2001 From: MJJ Date: Fri, 5 Jul 2024 04:57:56 +0900 Subject: [PATCH 13/13] =?UTF-8?q?feat:=20=ED=8F=B4=EB=8D=94=EC=97=90=20?= =?UTF-8?q?=EC=8A=A4=ED=8C=9F=20=EC=A0=80=EC=9E=A5/=EC=82=AD=EC=A0=9C/?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SaveSpotController.java | 50 +++++++++++++++++ .../savespot/dto/SpotSaveReqeustDto.java | 22 ++++++++ .../savespot/dto/SpotSaveResponseDto.java | 23 ++++++++ .../snapspot/savespot/entity/SpotSave.java | 30 +++++++++++ .../repository/SpotSaveRepository.java | 11 ++++ .../repository/UserFolderRepository.java | 2 + .../savespot/service/SpotSaveService.java | 53 +++++++++++++++++++ .../savespot/service/UserFolderService.java | 13 +++++ 8 files changed, 204 insertions(+) create mode 100644 src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java create mode 100644 src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java index f9c02ce..0262fef 100644 --- a/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/controller/SaveSpotController.java @@ -1,4 +1,54 @@ package com.umc.hackaton.snapspot.savespot.controller; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveReqeustDto; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; +import com.umc.hackaton.snapspot.savespot.service.SpotSaveService; +import com.umc.hackaton.snapspot.savespot.service.UserFolderService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/savespot") +@Slf4j public class SaveSpotController { + + private final SpotSaveService spotSaveService; + private final UserFolderService userFolderService; + + @PostMapping + public ResponseEntity saveSpotSave(@RequestBody SpotSaveReqeustDto spotSaveRequestDto) { + try { + spotSaveService.save(spotSaveRequestDto); + return ResponseEntity.ok().body("Userfolder에 Spot 저장 성공"); + } catch (Exception e) { + log.error("스팟 저장에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Userfolder에 Spot 저장에 실패하였습니다."); + } + } + + @DeleteMapping + public ResponseEntity deleteSpotSave(@RequestParam Long spotSaveId) { + try { + spotSaveService.delete(spotSaveId); + return ResponseEntity.ok().body("Userfolder에 Spot 삭제 성공"); + } catch (Exception e) { + log.info("스팟 삭제에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Userfolder에 Spot 삭제에 실패하였습니다."); + } + } + + @GetMapping("/{id}") + public ResponseEntity getSpotSave(@PathVariable("id") Long id) { + try { + SpotSaveResponseDto spotSaveList = spotSaveService.getSpotsByFolderId(id); + return ResponseEntity.ok().body(spotSaveList); + } catch (Exception e) { + log.info("스팟 조회에 실패하였습니다.", e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Userfolder에 Spot 조회에 실패하였습니다."); + } + } } diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java new file mode 100644 index 0000000..55b353a --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveReqeustDto.java @@ -0,0 +1,22 @@ +package com.umc.hackaton.snapspot.savespot.dto; + +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Getter; + +@Getter +public class SpotSaveReqeustDto { + @NotNull private Long folderId; + @NotNull private Long spotId; + + @Builder + public SpotSave toEntity(UserFolder userFolder, Spot spot){ + return SpotSave.builder() + .userFolder(userFolder) + .spot(spot) + .build(); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java new file mode 100644 index 0000000..7c0fcf9 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/dto/SpotSaveResponseDto.java @@ -0,0 +1,23 @@ +package com.umc.hackaton.snapspot.savespot.dto; + +import com.umc.hackaton.snapspot.spot.entity.Spot; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Builder +@Getter +@Setter +public class SpotSaveResponseDto { + private Long folderId; + private List spots; + + public static SpotSaveResponseDto fromEntity(Long folderId, List spots) { + return SpotSaveResponseDto.builder() + .folderId(folderId) + .spots(spots) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java b/src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java new file mode 100644 index 0000000..acda4f1 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/entity/SpotSave.java @@ -0,0 +1,30 @@ +package com.umc.hackaton.snapspot.savespot.entity; + +import com.umc.hackaton.snapspot.config.entity.BaseEntity; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class SpotSave extends BaseEntity { + + @Id + @GeneratedValue(strategy = jakarta.persistence.GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "userfolder_id") + private UserFolder userFolder; + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "spot_id") + private Spot spot; + +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java new file mode 100644 index 0000000..437e1e7 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/SpotSaveRepository.java @@ -0,0 +1,11 @@ +package com.umc.hackaton.snapspot.savespot.repository; + +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface SpotSaveRepository extends JpaRepository { + List findAllByUserFolder_Id(Long userFolderId); +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java index f7dc6ab..30114d7 100644 --- a/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/repository/UserFolderRepository.java @@ -1,10 +1,12 @@ package com.umc.hackaton.snapspot.savespot.repository; import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.user.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserFolderRepository extends JpaRepository { List findAllByUserIdAndIsDeletedFalse(Long userId); + UserFolder findByUser(User user); } diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java b/src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java new file mode 100644 index 0000000..3f4b5b8 --- /dev/null +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/service/SpotSaveService.java @@ -0,0 +1,53 @@ +package com.umc.hackaton.snapspot.savespot.service; + +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveReqeustDto; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; +import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.savespot.repository.SpotSaveRepository; +import com.umc.hackaton.snapspot.savespot.repository.UserFolderRepository; +import com.umc.hackaton.snapspot.spot.entity.Spot; +import com.umc.hackaton.snapspot.spot.repository.SpotRepository; +import com.umc.hackaton.snapspot.user.entity.User; +import com.umc.hackaton.snapspot.user.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@Service +public class SpotSaveService { + + private final SpotSaveRepository spotSaveRepository; + private final UserFolderRepository userFolderRepository; + private final SpotRepository spotRepository; + private final UserRepository userRepository; + + @Transactional + public SpotSaveResponseDto save(SpotSaveReqeustDto dto) { + UserFolder userFolder = userFolderRepository.findById(dto.getFolderId()) + .orElseThrow(() -> new IllegalArgumentException("Invalid folder ID")); + Spot spot = spotRepository.findById(dto.getSpotId()) + .orElseThrow(() -> new IllegalArgumentException("Invalid spot ID")); + SpotSave spotSave = dto.toEntity(userFolder, spot); + SpotSave savedSpotSave = spotSaveRepository.save(spotSave); + return SpotSaveResponseDto.fromEntity(userFolder.getId(), List.of(spot)); + } + + @Transactional + public void delete(Long spotSaveId) { + spotSaveRepository.deleteById(spotSaveId); + } + + @Transactional + public SpotSaveResponseDto getSpotsByFolderId(Long folderId) { + List spotSaves = spotSaveRepository.findAllByUserFolder_Id(folderId); + List spots = spotSaves.stream() + .map(SpotSave::getSpot) + .collect(Collectors.toList()); + return SpotSaveResponseDto.fromEntity(folderId, spots); + } +} diff --git a/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java b/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java index e7dc67e..1ea3f2b 100644 --- a/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java +++ b/src/main/java/com/umc/hackaton/snapspot/savespot/service/UserFolderService.java @@ -1,8 +1,12 @@ package com.umc.hackaton.snapspot.savespot.service; +import com.umc.hackaton.snapspot.savespot.dto.SpotSaveResponseDto; import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto; +import com.umc.hackaton.snapspot.savespot.entity.SpotSave; import com.umc.hackaton.snapspot.savespot.entity.UserFolder; +import com.umc.hackaton.snapspot.savespot.repository.SpotSaveRepository; import com.umc.hackaton.snapspot.savespot.repository.UserFolderRepository; +import com.umc.hackaton.snapspot.spot.entity.Spot; import com.umc.hackaton.snapspot.user.entity.User; import com.umc.hackaton.snapspot.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -10,12 +14,14 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class UserFolderService { private final UserFolderRepository userFolderRepository; private final UserRepository userRepository; + private final SpotSaveRepository spotSaveRepository; @Transactional public void save(UserFolderRequestDto userFolder) { @@ -39,4 +45,11 @@ public UserFolder update(Long folderId, UserFolderRequestDto userFolderRequestDt public List getAllFoldersByUserId(Long userId) { return userFolderRepository.findAllByUserIdAndIsDeletedFalse(userId); } + + public List getSpotsByFolderId(Long folderId) { + List spotSaves = spotSaveRepository.findAllByUserFolder_Id(folderId); + return spotSaves.stream() + .map(SpotSave::getSpot) + .collect(Collectors.toList()); + } }