-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from UMC-WOWMARKET/feat/like
[Feat] 프로젝트 Like 및 Unlike API 생성 #134
- Loading branch information
Showing
8 changed files
with
143 additions
and
8 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
55 changes: 47 additions & 8 deletions
55
src/main/java/wowmarket/wow_server/detail/project/service/ProjectService.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 |
---|---|---|
@@ -1,36 +1,75 @@ | ||
package wowmarket.wow_server.detail.project.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import wowmarket.wow_server.detail.project.dto.ProjectImgResponseDto; | ||
import wowmarket.wow_server.detail.project.dto.ProjectInfoResponseDto; | ||
import wowmarket.wow_server.domain.Likes; | ||
import wowmarket.wow_server.domain.Project; | ||
import wowmarket.wow_server.domain.User; | ||
import wowmarket.wow_server.repository.ItemRepository; | ||
import wowmarket.wow_server.repository.LikesRepository; | ||
import wowmarket.wow_server.repository.ProjectRepository; | ||
import wowmarket.wow_server.repository.UserRepository; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProjectService { | ||
private final UserRepository userRepository; | ||
private final ProjectRepository projectRepository; | ||
private final ItemRepository itemRepository; | ||
|
||
public ProjectService(ProjectRepository projectRepository, UserRepository userRepository, ItemRepository itemRepository) { | ||
this.userRepository = userRepository; | ||
this.projectRepository = projectRepository; | ||
this.itemRepository = itemRepository; | ||
} | ||
private final LikesRepository likesRepository; | ||
|
||
//주문폼: 굿즈 소개(이미지 3개) 조회 | ||
public ProjectImgResponseDto getProjectImg(Long project_id){ | ||
public ProjectImgResponseDto getProjectImg(Long project_id) { | ||
Project project = projectRepository.findByProject_Id(project_id); | ||
return new ProjectImgResponseDto(project); | ||
} | ||
|
||
//주문폼: 상세 정보 조회 | ||
public ProjectInfoResponseDto getProjectInfo(Long project_id){ | ||
public ProjectInfoResponseDto getProjectInfo(Long project_id) { | ||
Project project = projectRepository.findByProject_Id(project_id); | ||
projectRepository.updateView(project_id); //상세정보 조회할 때마다 조회수 +1 | ||
return new ProjectInfoResponseDto(project, itemRepository.getTotalOrderCountByProject(project), | ||
itemRepository.getTotalGoalByProject(project)); | ||
} | ||
|
||
@Transactional | ||
public ResponseEntity<?> likeProject(Long projectId, User user) { | ||
Optional<Likes> likeExist = likesRepository.findByUserAndProject(user, projectId); | ||
if (likeExist.isEmpty()) { | ||
likesRepository.save( | ||
Likes.builder().user(user).project(projectRepository.findById(projectId).get()).build() | ||
); | ||
projectRepository.updateProjectLike(projectId); | ||
userRepository.updateProjectLike(user); | ||
} else { | ||
throw new ResponseStatusException(HttpStatus.CONFLICT, "이미 like한 프로젝트"); | ||
} | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@Transactional | ||
public ResponseEntity<?> unLikeProject(Long projectId, User user) { | ||
Optional<Likes> likeExist = likesRepository.findByUserAndProject(user, projectId); | ||
if (likeExist.isPresent()) { | ||
if (projectRepository.findById(projectId).get().getLikeCnt() > 0 | ||
&& user.getProjectLike() > 0) { | ||
likesRepository.deleteLikes(likeExist.get().getId()); | ||
projectRepository.updateProjectUnLike(projectId); | ||
userRepository.updateProjectUnLike(user); | ||
} else { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "like 수가 0이라 unlike할 수 없음"); | ||
} | ||
} else { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "like하지 않아서 unlike할 수 없음"); | ||
} | ||
return ResponseEntity.ok().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package wowmarket.wow_server.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Likes extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "like_id") | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "project_id") | ||
private Project project; | ||
|
||
@Builder | ||
public Likes(User user, Project project) { | ||
this.user = user; | ||
this.project = project; | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/main/java/wowmarket/wow_server/repository/LikesRepository.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,22 @@ | ||
package wowmarket.wow_server.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
import wowmarket.wow_server.domain.Likes; | ||
import wowmarket.wow_server.domain.User; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface LikesRepository extends JpaRepository<Likes, Long> { | ||
@Query("SELECT l FROM Likes l " + | ||
"WHERE l.user = :user AND l.project.id = :projectId") | ||
Optional<Likes> findByUserAndProject(@Param("user") User user, @Param("projectId") Long projectId); | ||
|
||
@Modifying | ||
@Query("DELETE FROM Likes l WHERE l.id = :likesId") | ||
void deleteLikes(@Param("likesId") Long likesId); | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/wowmarket/wow_server/repository/UserRepository.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 |
---|---|---|
@@ -1,10 +1,22 @@ | ||
package wowmarket.wow_server.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import wowmarket.wow_server.domain.User; | ||
import java.util.Optional; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
Optional<User> findByEmail(String email); | ||
|
||
@Modifying | ||
@Query("UPDATE User u SET u.projectLike = u.projectLike + 1 " + | ||
"WHERE u = :user") | ||
void updateProjectLike(@Param("user") User user); | ||
|
||
@Modifying | ||
@Query("UPDATE User u SET u.projectLike = u.projectLike - 1 " + | ||
"WHERE u = :user") | ||
void updateProjectUnLike(@Param("user") User user); | ||
} |