Skip to content

Commit

Permalink
[merge] RecordService 2차 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jinkonu authored Jul 12, 2024
2 parents 23867cb + d6ce04f commit ba630e6
Show file tree
Hide file tree
Showing 66 changed files with 1,241 additions and 320 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.recordy.server.external.config;
package org.recordy.server.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.recordy.server.external.controller;
package org.recordy.server.common.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.recordy.server.external.controller;
package org.recordy.server.common.controller;

import lombok.RequiredArgsConstructor;
import org.recordy.server.external.service.impl.S3ServiceImpl;
import org.recordy.server.common.service.impl.S3ServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.recordy.server.external.exception;
package org.recordy.server.common.exception;

import org.recordy.server.common.exception.RecordyException;
import org.recordy.server.common.message.ErrorMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.recordy.server.external.service;
package org.recordy.server.common.service;

import org.springframework.web.multipart.MultipartFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.recordy.server.external.service.impl;
package org.recordy.server.common.service.impl;

import org.recordy.server.common.config.S3Config;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.external.config.S3Config;
import org.recordy.server.external.exception.ExternalException;
import org.recordy.server.external.service.S3Service;
import org.recordy.server.common.exception.ExternalException;
import org.recordy.server.common.service.S3Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/recordy/server/keyword/domain/Keyword.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.recordy.server.keyword.domain;

import java.util.List;

public enum Keyword {

EXOTIC("이색적인"),
Expand All @@ -23,7 +25,9 @@ public enum Keyword {
this.name = name;
}

public static Keyword fromString(String keyword) {
return Keyword.valueOf(keyword.toUpperCase());
public static List<Keyword> from(List<String> keywords) {
return keywords.stream()
.map(Keyword::valueOf)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.recordy.server.common.domain.JpaMetaInfoEntity;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(name = "keywords")
@Entity
public class KeywordEntity {
public class KeywordEntity extends JpaMetaInfoEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@

import lombok.RequiredArgsConstructor;
import org.recordy.server.auth.security.UserId;
import org.recordy.server.keyword.domain.Keyword;
import org.recordy.server.record.controller.dto.RecordCreateRequest;
import org.recordy.server.record.controller.dto.request.RecordCreateRequest;
import org.recordy.server.record.domain.File;
import org.recordy.server.record.domain.Record;

import org.recordy.server.record.domain.usecase.RecordCreate;
import org.recordy.server.record.service.RecordService;
import org.springframework.data.domain.Slice;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@RequestMapping("/api/v1/records")
Expand Down Expand Up @@ -55,8 +52,8 @@ public ResponseEntity<Void> deleteRecord(
@GetMapping("/recent")
public ResponseEntity<Slice<Record>> getRecentRecords(
@RequestParam(required = false) List<String> keywords,
@RequestParam long cursorId,
@RequestParam int size
@RequestParam(required = false, defaultValue = "0L") long cursorId,
@RequestParam(required = false, defaultValue = "10") int size
) {
Slice<Record> records = recordService.getRecentRecords(keywords, cursorId, size);

Expand All @@ -65,11 +62,33 @@ public ResponseEntity<Slice<Record>> getRecentRecords(
.body(records);
}

@GetMapping("/famous")
public ResponseEntity<Slice<Record>> getFamousRecords(
@RequestParam(required = false) List<String> keywords,
@RequestParam(required = false, defaultValue = "0") int pageNumber,
@RequestParam(required = false, defaultValue = "10") int pageSize
){
return ResponseEntity
.ok()
.body(recordService.getFamousRecords(keywords, pageNumber, pageSize));
}

@PostMapping("/watch")
public ResponseEntity<Void> watch(
@UserId long userId,
@RequestParam long recordId
) {
recordService.watch(userId, recordId);
return ResponseEntity
.ok()
.build();
}

@GetMapping("/user")
public ResponseEntity<Slice<Record>> getRecentRecordsByUser(
@UserId long userId,
@RequestParam long cursorId,
@RequestParam int size
@RequestParam(required = false, defaultValue = "0L") long cursorId,
@RequestParam(required = false, defaultValue = "10") int size
) {
Slice<Record> records = recordService.getRecentRecordsByUser(userId, cursorId, size);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.recordy.server.record.controller.dto;
package org.recordy.server.record.controller.dto.request;

import java.util.List;

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/recordy/server/record/domain/Record.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.recordy.server.record.domain;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.recordy.server.keyword.domain.Keyword;
import org.recordy.server.record.controller.dto.FileUrl;
import org.recordy.server.record.service.dto.FileUrl;
import org.recordy.server.user.domain.User;

import java.util.List;
Expand All @@ -20,11 +21,10 @@ public class Record {
String content;
List<Keyword> keywords;
User uploader;
private LocalDateTime createdAt;
long bookmarkCount;

public boolean isUploader(long userId) {
if (uploader.getId() == userId) {
return true;
}
return false;
return uploader.getId() == userId;
}
}
38 changes: 29 additions & 9 deletions src/main/java/org/recordy/server/record/domain/RecordEntity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.recordy.server.record.domain;

import jakarta.persistence.*;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.recordy.server.common.domain.JpaMetaInfoEntity;
import org.recordy.server.keyword.domain.KeywordEntity;
import org.recordy.server.record.controller.dto.FileUrl;
import org.recordy.server.record.service.dto.FileUrl;
import org.recordy.server.record_stat.domain.BookmarkEntity;
import org.recordy.server.record_stat.domain.ViewEntity;
import org.recordy.server.user.domain.UserEntity;

import java.util.ArrayList;
Expand All @@ -16,7 +20,7 @@
@Getter
@Table(name = "records")
@Entity
public class RecordEntity {
public class RecordEntity extends JpaMetaInfoEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -31,11 +35,17 @@ public class RecordEntity {
@JoinColumn(name = "user_id")
private UserEntity user;

@OneToMany(mappedBy = "record")
@OneToMany(mappedBy = "record", cascade = CascadeType.ALL, orphanRemoval = true)
private List<UploadEntity> uploads = new ArrayList<>();

@OneToMany(mappedBy = "record", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ViewEntity> views = new ArrayList<>();

@OneToMany(mappedBy = "record", cascade = CascadeType.ALL, orphanRemoval = true)
private List<BookmarkEntity> bookmarks = new ArrayList<>();

@Builder
public RecordEntity(Long id, String videoUrl, String thumbnailUrl, String location, String content, UserEntity user) {
public RecordEntity(Long id, String videoUrl, String thumbnailUrl, String location, String content, UserEntity user, LocalDateTime createdAt) {
this.id = id;
this.videoUrl = videoUrl;
this.thumbnailUrl = thumbnailUrl;
Expand All @@ -51,14 +61,11 @@ public static RecordEntity from(Record record) {
record.getFileUrl().thumbnailUrl(),
record.getLocation(),
record.getContent(),
UserEntity.from(record.getUploader())
UserEntity.from(record.getUploader()),
record.getCreatedAt()
);
}

private void addUpload(UploadEntity upload) {
uploads.add(upload);
}

public Record toDomain() {
return Record.builder()
.id(id)
Expand All @@ -73,6 +80,19 @@ public Record toDomain() {
.map(KeywordEntity::toDomain)
.toList())
.uploader(user.toDomain())
.bookmarkCount(bookmarks.size())
.build();
}

public void addUpload(UploadEntity upload) {
uploads.add(upload);
}

public void addView(ViewEntity view) {
views.add(view);
}

public void addBookmark(BookmarkEntity bookmark) {
bookmarks.add(bookmark);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public UploadEntity(Long id, RecordEntity record, KeywordEntity keyword) {
}

public static UploadEntity of(RecordEntity record, KeywordEntity keyword) {
return UploadEntity.builder()
UploadEntity upload = UploadEntity.builder()
.record(record)
.keyword(keyword)
.build();
record.addUpload(upload);

return upload;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.recordy.server.record.domain.usecase;

import org.recordy.server.keyword.domain.Keyword;
import org.recordy.server.record.controller.dto.RecordCreateRequest;
import org.recordy.server.record.controller.dto.request.RecordCreateRequest;

import java.util.List;
import java.util.stream.Collectors;

public record RecordCreate(
long uploaderId,
Expand All @@ -13,17 +12,11 @@ public record RecordCreate(
List<Keyword> keywords
) {

public static RecordCreate of(Long uploaderId, String location, String content, List<Keyword> keywords){
return new RecordCreate(uploaderId, location, content, keywords);
}

public static RecordCreate from(Long uploaderId, RecordCreateRequest recordCreateRequest) {
List<Keyword> keywords = recordCreateRequest.keywords().stream()
.map(Keyword::valueOf)
.collect(Collectors.toList());
return new RecordCreate(uploaderId, recordCreateRequest.location(), recordCreateRequest.content(), keywords);
return new RecordCreate(
uploaderId,
recordCreateRequest.location(),
recordCreateRequest.content(),
Keyword.from(recordCreateRequest.keywords()));
}



}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.recordy.server.record.repository;

import java.util.Optional;
import org.recordy.server.keyword.domain.Keyword;
import org.recordy.server.record.domain.Record;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface RecordRepository {
Expand All @@ -16,9 +17,11 @@ public interface RecordRepository {
void deleteById(long recordId);

// query
Optional<Record> findById(long recordId);
Slice<Record> findAllOrderByPopularity(long cursor, Pageable pageable);
Optional<Record> findById(long id);
Slice<Record> findAllOrderByPopularity(Pageable pageable);
Slice<Record> findAllByKeywordsOrderByPopularity(List<Keyword> keywords, Pageable pageable);
Slice<Record> findAllByIdAfterOrderByIdDesc(long cursor, Pageable pageable);
Slice<Record> findAllByIdAfterAndKeywordsOrderByIdDesc(List<Keyword> keywords, long cursor, Pageable pageable);
Slice<Record> findAllByUserIdOrderByIdDesc(long userId, long cursor, Pageable pageable);
Map<Keyword, Long> countAllByUserIdGroupByKeyword(long userId);
}
Loading

0 comments on commit ba630e6

Please sign in to comment.