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 1 commit
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,27 @@
package io.mtudy.soundcloud.like.application.inputs;

import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;

@NoArgsConstructor
public class CreateLikeInput {
@NotBlank
private String type;

@NotBlank
private String typeId;

public CreateLikeInput(String type, String typeId) {
this.type = type;
this.typeId = typeId;
}

public String getType() {
return type;
}

public String getTypeId() {
return typeId;
}
}
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.CreateLikeInput;
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 CreateLikeInput input) {
return this.service.run(input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.mtudy.soundcloud.like.application.interfaces.web.graphql;

import graphql.kickstart.tools.GraphQLQueryResolver;
import io.mtudy.soundcloud.like.application.services.FindLikeByTrackIdService;
import io.mtudy.soundcloud.like.domain.entities.Like;
import org.springframework.stereotype.Component;

@Component
public class LikeQueryResolver implements GraphQLQueryResolver {
private final FindLikeByTrackIdService service;

public LikeQueryResolver(FindLikeByTrackIdService service) {
this.service = service;
}

public Like like(String id) {
return this.service.run(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.mtudy.soundcloud.like.application.services;

import io.mtudy.soundcloud.like.application.inputs.CreateLikeInput;
import io.mtudy.soundcloud.like.domain.entities.Like;
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(CreateLikeInput request) {
return this.repository.save(new Like(
UUID.randomUUID().toString(),
UUID.randomUUID().toString(),
request.getType(),
request.getTypeId()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.mtudy.soundcloud.like.application.services;

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

@Service
public class FindLikeByTrackIdService {
private final LikeRepository repository;

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

public Like run(String id) {
return this.repository.findByTrackId(id);
}
}
47 changes: 47 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,47 @@
package io.mtudy.soundcloud.like.domain.entities;

import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@NoArgsConstructor
@Entity(name = "like")
public class Like {
@Id
@Column(columnDefinition = "VARCHAR(36)")
private String id;

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

@Column
private String type;

@Column
private String typeId;

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

public String getId() {
return id;
}

public String getAuthorId() {
return authorId;
}

public String getType() {
return type;
}

public String getTypeId() {
return typeId;
}
}
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;
}
}
12 changes: 12 additions & 0 deletions src/main/resources/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ type Track {
artworkUrl: String!
}

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

type Mutation {
createTrack(input: CreateTrackInput!): Track
createLike(input: CreateLikeInput!): Like
}

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

input CreateLikeInput {
type: String!
typeId: String!
}