-
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.
Browse files
Browse the repository at this point in the history
…icle 게시글 좋아요 기능 구현
- Loading branch information
Showing
13 changed files
with
203 additions
and
18 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
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
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/api/trip/domain/interestarticle/controller/InterestArticleController.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,29 @@ | ||
package com.api.trip.domain.interestarticle.controller; | ||
|
||
import com.api.trip.domain.interestarticle.controller.dto.CreateInterestArticleRequest; | ||
import com.api.trip.domain.interestarticle.service.InterestArticleService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/interest-articles") | ||
@RequiredArgsConstructor | ||
public class InterestArticleController { | ||
|
||
private final InterestArticleService interestArticleService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Long> createInterestArticle(@RequestBody CreateInterestArticleRequest request) { | ||
String email = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
return ResponseEntity.ok(interestArticleService.createInterestArticle(request, email)); | ||
} | ||
|
||
@DeleteMapping("/{interestArticleId}") | ||
public ResponseEntity<Void> deleteInterestArticle(@PathVariable Long interestArticleId) { | ||
String email = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
interestArticleService.deleteInterestArticle(interestArticleId, email); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...java/com/api/trip/domain/interestarticle/controller/dto/CreateInterestArticleRequest.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,19 @@ | ||
package com.api.trip.domain.interestarticle.controller.dto; | ||
|
||
import com.api.trip.domain.article.model.Article; | ||
import com.api.trip.domain.interestarticle.model.InterestArticle; | ||
import com.api.trip.domain.member.model.Member; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CreateInterestArticleRequest { | ||
|
||
private Long articleId; | ||
|
||
public InterestArticle toEntity(Member member, Article article) { | ||
return InterestArticle.builder() | ||
.member(member) | ||
.article(article) | ||
.build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/api/trip/domain/interestarticle/model/InterestArticle.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,33 @@ | ||
package com.api.trip.domain.interestarticle.model; | ||
|
||
import com.api.trip.common.auditing.entity.BaseTimeEntity; | ||
import com.api.trip.domain.article.model.Article; | ||
import com.api.trip.domain.member.model.Member; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"member_id", "article_id"})}) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class InterestArticle extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Member member; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Article article; | ||
|
||
@Builder | ||
private InterestArticle(Member member, Article article) { | ||
this.member = member; | ||
this.article = article; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/api/trip/domain/interestarticle/repository/InterestArticleRepository.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,11 @@ | ||
package com.api.trip.domain.interestarticle.repository; | ||
|
||
import com.api.trip.domain.article.model.Article; | ||
import com.api.trip.domain.interestarticle.model.InterestArticle; | ||
import com.api.trip.domain.member.model.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface InterestArticleRepository extends JpaRepository<InterestArticle, Long> { | ||
|
||
InterestArticle findByMemberAndArticle(Member member, Article article); | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/api/trip/domain/interestarticle/service/InterestArticleService.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,52 @@ | ||
package com.api.trip.domain.interestarticle.service; | ||
|
||
import com.api.trip.domain.article.model.Article; | ||
import com.api.trip.domain.article.repository.ArticleRepository; | ||
import com.api.trip.domain.interestarticle.controller.dto.CreateInterestArticleRequest; | ||
import com.api.trip.domain.interestarticle.model.InterestArticle; | ||
import com.api.trip.domain.interestarticle.repository.InterestArticleRepository; | ||
import com.api.trip.domain.member.model.Member; | ||
import com.api.trip.domain.member.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class InterestArticleService { | ||
|
||
private final InterestArticleRepository interestArticleRepository; | ||
private final ArticleRepository articleRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
public Long createInterestArticle(CreateInterestArticleRequest request, String email) { | ||
Member member = memberRepository.findByEmail(email).orElseThrow(); | ||
|
||
Article article = articleRepository.findById(request.getArticleId()).orElseThrow(); | ||
|
||
InterestArticle interestArticle = interestArticleRepository.findByMemberAndArticle(member, article); | ||
if (interestArticle != null) { | ||
throw new RuntimeException("잘못된 요청입니다."); | ||
} | ||
|
||
interestArticle = request.toEntity(member, article); | ||
|
||
articleRepository.increaseLikeCount(interestArticle.getArticle()); | ||
|
||
return interestArticleRepository.save(interestArticle).getId(); | ||
} | ||
|
||
public void deleteInterestArticle(Long interestArticleId, String email) { | ||
Member member = memberRepository.findByEmail(email).orElseThrow(); | ||
|
||
InterestArticle interestArticle = interestArticleRepository.findById(interestArticleId).orElseThrow(); | ||
if (interestArticle.getMember() != member) { | ||
throw new RuntimeException("삭제 권한이 없습니다."); | ||
} | ||
|
||
articleRepository.decreaseLikeCount(interestArticle.getArticle()); | ||
|
||
interestArticleRepository.delete(interestArticle); | ||
} | ||
} |