Skip to content

Commit

Permalink
feat: 좋아요 등록 삭제
Browse files Browse the repository at this point in the history
spot 좋아요 등록 및 삭제 구현

#15
  • Loading branch information
2hy2on committed Jul 4, 2024
1 parent d0bbd20 commit a5583de
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
}
}
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));
}

}
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 src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java
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 src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java
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;

}
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);
}
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();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/spot/entity/Spot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
import java.util.Optional;

public interface SpotRepository extends JpaRepository<Spot, Long> {
@Override
void delete(Spot entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, Long> {
Optional<User> findByUsername(String username);

User findUserById(Long id);
}

4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit a5583de

Please sign in to comment.