-
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.
Merge pull request #30 from f-lab-edu/issue29
[Issue29] 영상 메타데이터 관련 기능 구현
- Loading branch information
Showing
12 changed files
with
498 additions
and
11 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
61 changes: 61 additions & 0 deletions
61
mitube-app/src/main/java/com/misim/controller/VideoMetadataController.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.misim.controller; | ||
|
||
import com.misim.controller.model.Response.MetadataResponse; | ||
import com.misim.entity.VideoMetadata; | ||
import com.misim.exception.CommonResponse; | ||
import com.misim.service.VideoMetadataService; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/videoMetadata") | ||
public class VideoMetadataController { | ||
|
||
private final VideoMetadataService videoMetadataService; | ||
|
||
public VideoMetadataController(VideoMetadataService videoMetadataService) { | ||
this.videoMetadataService = videoMetadataService; | ||
} | ||
|
||
@GetMapping("/{videoMetadataId}") | ||
public CommonResponse<MetadataResponse> getVideoMetadata(@PathVariable Long videoMetadataId) { | ||
|
||
VideoMetadata metadata = videoMetadataService.read(videoMetadataId); | ||
|
||
MetadataResponse response = new MetadataResponse(metadata.getViewCount(), metadata.getLikeCount(), metadata.getDislikeCount()); | ||
|
||
return CommonResponse | ||
.<MetadataResponse>builder() | ||
.body(response) | ||
.build(); | ||
} | ||
|
||
@PostMapping("/{videoMetadataId}/view") | ||
public void addVideoMetadataViewCount(@PathVariable Long videoMetadataId) { | ||
|
||
videoMetadataService.updateViewCount(videoMetadataId); | ||
} | ||
|
||
@PostMapping("/{videoMetadataId}/like") | ||
public void addVideoMetadataLikeCount(@PathVariable Long videoMetadataId, @RequestParam Boolean isChecked) { | ||
|
||
videoMetadataService.updateLikeCount(videoMetadataId, isChecked); | ||
} | ||
|
||
@PostMapping("/{videoMetadataId}/dislike") | ||
public void addVideoMetadataDislikeCount(@PathVariable Long videoMetadataId, @RequestParam Boolean isChecked) { | ||
|
||
videoMetadataService.updateDislikeCount(videoMetadataId, isChecked); | ||
} | ||
|
||
@DeleteMapping("/{videoMetadataId}") | ||
public void deleteVideoMetadata(@PathVariable Long videoMetadataId) { | ||
|
||
videoMetadataService.delete(videoMetadataId); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
mitube-app/src/main/java/com/misim/controller/model/Response/MetadataResponse.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,6 @@ | ||
package com.misim.controller.model.Response; | ||
|
||
public record MetadataResponse ( | ||
Long viewCount, Long likeCount, Long dislikeCount | ||
) { | ||
} |
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
56 changes: 56 additions & 0 deletions
56
mitube-app/src/main/java/com/misim/entity/VideoMetadata.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,56 @@ | ||
package com.misim.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class VideoMetadata extends BaseTimeEntity{ | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private Long viewCount; | ||
|
||
private Long likeCount; | ||
|
||
private Long dislikeCount; | ||
|
||
@Builder | ||
public VideoMetadata(Long viewCount, Long likeCount, Long dislikeCount) { | ||
this.viewCount = viewCount; | ||
this.likeCount = likeCount; | ||
this.dislikeCount = dislikeCount; | ||
} | ||
|
||
public void incrementViewCount() { | ||
this.viewCount++; | ||
} | ||
|
||
public void incrementLikeCount() { | ||
this.likeCount++; | ||
} | ||
|
||
public void incrementDislikeCount() { | ||
this.dislikeCount++; | ||
} | ||
|
||
public void decrementLikeCount() { | ||
if (this.likeCount > 0) { | ||
this.likeCount--; | ||
} | ||
} | ||
|
||
public void decrementDislikeCount() { | ||
if (this.dislikeCount > 0) { | ||
this.dislikeCount--; | ||
} | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
mitube-app/src/main/java/com/misim/repository/VideoMetadataRepository.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,10 @@ | ||
package com.misim.repository; | ||
|
||
import com.misim.entity.VideoMetadata; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface VideoMetadataRepository extends JpaRepository<VideoMetadata, Long> { | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
mitube-app/src/main/java/com/misim/service/VideoMetadataService.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,95 @@ | ||
package com.misim.service; | ||
|
||
import com.misim.entity.VideoMetadata; | ||
import com.misim.repository.VideoMetadataRepository; | ||
import java.util.NoSuchElementException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class VideoMetadataService { | ||
|
||
private final VideoMetadataRepository videoMetadataRepository; | ||
|
||
public VideoMetadata create() { | ||
|
||
VideoMetadata metadata = VideoMetadata.builder() | ||
.viewCount(0L) | ||
.likeCount(0L) | ||
.dislikeCount(0L) | ||
.build(); | ||
|
||
return videoMetadataRepository.save(metadata); | ||
} | ||
|
||
public VideoMetadata read(Long id) { | ||
|
||
return videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new); | ||
} | ||
|
||
public Long readViewCount(Long id) { | ||
|
||
return videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new) | ||
.getViewCount(); | ||
} | ||
|
||
public Long readLikeCount(Long id) { | ||
|
||
return videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new) | ||
.getLikeCount(); | ||
} | ||
|
||
public Long readDislikeCount(Long id) { | ||
|
||
return videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new) | ||
.getDislikeCount(); | ||
} | ||
|
||
public void updateViewCount(Long id) { | ||
|
||
VideoMetadata metadata = videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new); | ||
|
||
metadata.incrementViewCount(); | ||
|
||
videoMetadataRepository.save(metadata); | ||
} | ||
|
||
public void updateLikeCount(Long id, boolean isChecked) { | ||
|
||
VideoMetadata metadata = videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new); | ||
|
||
if (isChecked) { | ||
metadata.incrementLikeCount(); | ||
} else { | ||
metadata.decrementLikeCount(); | ||
} | ||
|
||
videoMetadataRepository.save(metadata); | ||
} | ||
|
||
public void updateDislikeCount(Long id, boolean isChecked) { | ||
|
||
VideoMetadata metadata = videoMetadataRepository.findById(id) | ||
.orElseThrow(NoSuchElementException::new); | ||
|
||
if (isChecked) { | ||
metadata.incrementDislikeCount(); | ||
} else { | ||
metadata.decrementDislikeCount(); | ||
} | ||
|
||
videoMetadataRepository.save(metadata); | ||
} | ||
|
||
public void delete(Long id) { | ||
|
||
videoMetadataRepository.deleteById(id); | ||
} | ||
} |
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
Oops, something went wrong.