Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

좋아요 스킴 초안. Ref #1 #6

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.mtudy.soundcloud.like.application.inputs;

import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;

@NoArgsConstructor
public class CreatePlaylistLikeInput {
@NotBlank
private String playlistId;

public CreatePlaylistLikeInput(String playlistId) {
this.playlistId = playlistId;
}

public String getPlaylistId() {
return playlistId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.mtudy.soundcloud.like.application.interfaces.web.graphql;

import graphql.kickstart.tools.GraphQLMutationResolver;
import io.mtudy.soundcloud.like.application.inputs.CreatePlaylistLikeInput;
import io.mtudy.soundcloud.like.application.services.CreateLikeService;
import io.mtudy.soundcloud.like.domain.entities.Like;
import org.springframework.stereotype.Component;

import javax.validation.Valid;

@Component
public class CreateLikeMutationResolver implements GraphQLMutationResolver {
private final CreateLikeService service;

public CreateLikeMutationResolver(CreateLikeService service) {
this.service = service;
}

public Like createLike(@Valid CreatePlaylistLikeInput input) {
return this.service.run(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.mtudy.soundcloud.like.application.services;

import io.mtudy.soundcloud.like.application.inputs.CreatePlaylistLikeInput;
import io.mtudy.soundcloud.like.domain.entities.Like;
import io.mtudy.soundcloud.like.domain.entities.PlaylistLike;
import io.mtudy.soundcloud.like.domain.repotitories.LikeRepository;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class CreateLikeService {
private final LikeRepository repository;

public CreateLikeService(LikeRepository repository) {
this.repository = repository;
}

public Like run(CreatePlaylistLikeInput request) {
var builder = new PlaylistLike.Builder(
UUID.randomUUID().toString(),
request.getPlaylistId(),
UUID.randomUUID().toString()
);

return this.repository.save(builder.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.mtudy.soundcloud.like.domain.entities;

import io.mtudy.soundcloud.like.domain.enums.LikeType;
import lombok.NoArgsConstructor;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@NoArgsConstructor
@Entity(name = "albumLike")
JeongmyeongzZ marked this conversation as resolved.
Show resolved Hide resolved
@DiscriminatorValue(LikeType.Values.ALBUM)
JeongmyeongzZ marked this conversation as resolved.
Show resolved Hide resolved
public final class AlbumLike extends Like {
private AlbumLike(Builder builder) {
JeongmyeongzZ marked this conversation as resolved.
Show resolved Hide resolved
this.id = builder.id;
this.typeId = builder.typeId;
this.authorId = builder.authorId;
}

public final static class Builder {
private final String id;

private final String typeId;

private final String authorId;

public Builder(String id, String typeId, String authorId) {
this.id = id;
this.typeId = typeId;
this.authorId = authorId;
}

public AlbumLike build() {
return new AlbumLike(this);
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/io/mtudy/soundcloud/like/domain/entities/Like.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.mtudy.soundcloud.like.domain.entities;

import lombok.NoArgsConstructor;

import javax.persistence.*;

@NoArgsConstructor
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type",
discriminatorType = DiscriminatorType.STRING)
JeongmyeongzZ marked this conversation as resolved.
Show resolved Hide resolved
public abstract class Like {
@Id
@Column(columnDefinition = "VARCHAR(36)")
protected String id;

@Column(columnDefinition = "VARCHAR(36)")
protected String authorId;

@Column(columnDefinition = "VARCHAR(36)")
protected String typeId;

public String getId() {
return id;
}

public String getAuthorId() {
return authorId;
}

public String getTypeId() {
return typeId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.mtudy.soundcloud.like.domain.entities;

import io.mtudy.soundcloud.like.domain.enums.LikeType;
import lombok.NoArgsConstructor;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@NoArgsConstructor
@Entity(name = "playlistLike")
@DiscriminatorValue(LikeType.Values.PLAYLIST)
public final class PlaylistLike extends Like {
private PlaylistLike(Builder builder) {
this.id = builder.id;
this.typeId = builder.typeId;
this.authorId = builder.authorId;
}

public final static class Builder {
private final String id;

private final String typeId;

private final String authorId;

public Builder(String id, String typeId, String authorId) {
this.id = id;
this.typeId = typeId;
this.authorId = authorId;
}

public PlaylistLike build() {
return new PlaylistLike(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.mtudy.soundcloud.like.domain.entities;

import io.mtudy.soundcloud.like.domain.enums.LikeType;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@NoArgsConstructor
@Entity(name = "trackLike")
@DiscriminatorValue(LikeType.Values.TRACK)
public final class TrackLike extends Like {
private TrackLike(Builder builder) {
this.id = builder.id;
this.typeId = builder.typeId;
this.authorId = builder.authorId;
}

public final static class Builder {
private final String id;

private final String typeId;

private final String authorId;

public Builder(String id, String typeId, String authorId) {
this.id = id;
this.typeId = typeId;
this.authorId = authorId;
}

public TrackLike build() {
return new TrackLike(this);
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/mtudy/soundcloud/like/domain/enums/LikeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.mtudy.soundcloud.like.domain.enums;

public enum LikeType {
TRACK(Values.TRACK), ALBUM(Values.ALBUM), PLAYLIST(Values.PLAYLIST);

LikeType (String val) {}

public static class Values {
public static final String TRACK = "TRACK";
public static final String ALBUM = "ALBUM";
public static final String PLAYLIST = "PLAYLIST";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.mtudy.soundcloud.like.domain.repotitories;

import io.mtudy.soundcloud.like.domain.entities.Like;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LikeRepository extends CrudRepository<Like, String> {
public default Like findByTrackId(String trackId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 어떤 코드인지 설명해주실 수 있나요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

별도로 얘기했으나, 남기는 게 좋을 거 같아 코멘트 남겨둡니다.

현재 trackId가 현재 엔티티 컬럼에 존재하지 않고, 보통 인터페이스에서 컬럼명으로 whereColumn으로 메서드를 정의하면 컴파일 시점에 알아서 관련 코드를 만들어 줍니다.

public Like findByTrackId(String trackId);

그리고, 보통 단일 객체를 찾을 땐 JPA 인터페이스에서 기본적으로 Optional<Entity>를 리턴하는데 이렇게 추가되는 메서드에도 옵셔널을 쓰는 게 좋을 거 같은데 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코멘트 남겨주셨던 부분은 우선, 삭제했습니다 !

return null;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/db/migration/V1__create_like_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create table `like` (
id varchar(36) not null,
authorId varchar(36) not null,
type varchar(36) not null,
typeId varchar(36) not null,
primary key (id)
) engine=InnoDB collate=utf8mb4_general_ci
11 changes: 11 additions & 0 deletions src/main/resources/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ type Track {
artworkUrl: String!
}

type Like {
id : ID!
type : String!
typeId: String!
}

type Mutation {
createTrack(input: CreateTrackInput!): Track
likePlaylist(input: likePlaylistInput!): Like
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분은 지금 용어가 혼용 돼 있는데 정리가 필요합니당

플레이리스트를 좋아요 누른다 => like playlist로 접근했ㄴ었는데, like 라는 자원을 생성하는 Create playlist like 뭐 이런식으로 섞여있는데 용어를 어떤식으로 정리하면 좋을까요 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 이건 저도 잘 몰라서 레퍼런스를 좀 찾아봤습니다.

해당 질문에 대해선 두 번째 글이 알찬데, 동사를 먼저 쓰라고 하네요.

지금 케이스에선 likePlaylist면 괜찮지않나 싶습니다.

}

input CreateTrackInput {
title: String!
artworkUrl: String!
}

input likePlaylistInput {
playlistId: String!
}