-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,4 @@ dependencies { | |
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/umc/hackaton/snapspot/like/controller/LikeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<LikeDto.ResponseDto> registerLike(@RequestBody LikeDto.RequestDto likeDto){ | ||
return ResponseEntity.ok(likeService.registerLike(likeDto)); | ||
} | ||
|
||
|
||
@DeleteMapping("") | ||
public ResponseEntity<LikeDto.ResponseDto> deleteLike(@RequestBody LikeDto.RequestDto likeDto){ | ||
return ResponseEntity.ok(likeService.deleteLike(likeDto)); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/umc/hackaton/snapspot/like/converter/LikeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/umc/hackaton/snapspot/like/repository/LikeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, Long> { | ||
|
||
Like findByUserAndSpot(User user, Spot spot); | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/umc/hackaton/snapspot/like/service/LikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters