Skip to content

Commit

Permalink
�Feat: ContentCommandImplService 구현 (#24)
Browse files Browse the repository at this point in the history
* Feat: ContentCommandImplService 구현
* Chore:  Content 엔티티의 필드를 int에서 Long으로 변경
  • Loading branch information
yhk313 authored Oct 26, 2023
1 parent 3e414e4 commit 7c25e99
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 49 deletions.
39 changes: 33 additions & 6 deletions src/main/java/beforespring/socialfeed/content/domain/Content.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package beforespring.socialfeed.content.domain;


import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import lombok.*;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Table(
Expand All @@ -17,8 +17,35 @@
)
}
)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Content {
@Id
@Column(name = "memberId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Enumerated(EnumType.STRING)
private ContentSourceType contentSourceType;

private String title;
private String content;
private Long viewCount;
private Long likeCount;
private Long shareCount;

@EmbeddedId
private ContentIdTuple idTuple;
private LocalDateTime updatedAt;
private LocalDateTime createdAt;

public void incrementViewCount() {
this.viewCount++;
}

public void incrementLikeCount() {
this.likeCount++;
}

public void incrementShareCount() {
this.shareCount++;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package beforespring.socialfeed.content.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ContentRepository extends JpaRepository<Content, Long> {

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package beforespring.socialfeed.content.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.persistence.*;

/**
* 해시태그 검색용 테이블
Expand All @@ -30,5 +25,4 @@ public class HashtagContent {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; // UUID 적용 고려할것.
private String hashtag;
private ContentIdTuple contentId; // one to many 고려
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package beforespring.socialfeed.content.service;

import beforespring.socialfeed.content.domain.Content;
import beforespring.socialfeed.content.domain.ContentRepository;
import beforespring.socialfeed.content.domain.ContentSourceType;
import beforespring.socialfeed.content.service.dto.ContentSpecificData;
import beforespring.socialfeed.content.service.exception.ContentNotFoundException;
import beforespring.socialfeed.member.domain.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSetPersister;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

@Service
@Transactional
@RequiredArgsConstructor
public class ContentCommandImplServiceImpl implements ContentCommandService {
private final ContentRepository contentRepository;

@Override
public ContentSpecificData getContentSpecific(Long contentId) {
Optional<Content> optionalContent = contentRepository.findById(contentId);

if (optionalContent.isPresent()) {
Content content = optionalContent.get();

content.incrementViewCount();

contentRepository.save(content);

ContentSpecificData contentData = new ContentSpecificData(
contentId,
content.getContentSourceType(),
content.getTitle(),
content.getContent(),
content.getViewCount(),
content.getLikeCount(),
content.getShareCount(),
content.getUpdatedAt(),
content.getCreatedAt()
);

return contentData;
} else {
throw new ContentNotFoundException();
}
}


@Override
public void like(Long contentId, Member member) {
Content content = contentRepository.findById(contentId)
.orElseThrow(() -> new ContentNotFoundException());

content.incrementLikeCount();

contentRepository.save(content);
}

@Override
public void share(Long contentId, Member member) {
Content content = contentRepository.findById(contentId)
.orElseThrow(() -> new ContentNotFoundException());

content.incrementShareCount();

contentRepository.save(content);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package beforespring.socialfeed.content.service;

import beforespring.socialfeed.content.domain.ContentIdTuple;
import beforespring.socialfeed.content.service.dto.ContentSpecificData;
import beforespring.socialfeed.member.domain.Member;

Expand All @@ -20,10 +19,10 @@ public interface ContentCommandService {
* 횟수 제한이 없습니다.
* </li>
*
* @param contentIdTuple 찾을 게시물 ID
* @param contentId 찾을 게시물 ID
* @return ContentSpecificData
*/
ContentSpecificData getContentSpecific(ContentIdTuple contentIdTuple);
ContentSpecificData getContentSpecific(Long contentId);

/**
* <p>
Expand All @@ -33,10 +32,10 @@ public interface ContentCommandService {
* 게시물들은 본 서비스가 아닌 외부 서비스에서 관리됩니다. 그렇기에 좋아요 클릭 시 각 SNS 별 아래 명시된 API 를 호출합니다.
* </li>
*
* @param contentIdTuple 좋아요할 게시물 ID
* @param contentId 좋아요할 게시물 ID
* @param member 공유하는 멤버
*/
void like(ContentIdTuple contentIdTuple, Member member);
void like(Long contentId, Member member);

/**
* <p>
Expand All @@ -52,8 +51,8 @@ public interface ContentCommandService {
* 횟수 제한이 없습니다.
* </li>
*
* @param contentIdTuple 공유할 게시물 아이디
* @param contentId 공유할 게시물 아이디
* @param member 공유하는 멤버
*/
void share(ContentIdTuple contentIdTuple, Member member);
void share(Long contentId, Member member);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package beforespring.socialfeed.content.service.dto;

import beforespring.socialfeed.content.domain.ContentSourceType;

import java.time.LocalDateTime;

public record ContentSpecificData(
Long contentId,
ContentSourceType contentSourceType,
String title,
String content,
Long viewCount,
Long likeCount,
Long shareCount,
LocalDateTime updatedAt,
LocalDateTime createdAt

) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package beforespring.socialfeed.content.service.exception;

public class ContentNotFoundException extends RuntimeException {
public ContentNotFoundException() {
super("Content Not Found");
}
}

0 comments on commit 7c25e99

Please sign in to comment.