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: