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

Refactor | CAKK-57 | 케이크 도메인 로직 리팩토링 #203

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 14 additions & 23 deletions cakk-api/src/main/java/com/cakk/api/service/cake/CakeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
import com.cakk.domain.mysql.dto.param.cake.CakeImageResponseParam;
import com.cakk.domain.mysql.dto.param.cake.CakeUpdateParam;
import com.cakk.domain.mysql.entity.cake.Cake;
import com.cakk.domain.mysql.entity.cake.CakeCategory;
import com.cakk.domain.mysql.entity.cake.Tag;
import com.cakk.domain.mysql.entity.shop.CakeShop;
import com.cakk.domain.mysql.entity.user.User;
import com.cakk.domain.mysql.facade.cake.CakeManagerFacade;
import com.cakk.domain.mysql.repository.reader.CakeReader;
import com.cakk.domain.mysql.repository.reader.CakeShopReader;
import com.cakk.domain.mysql.repository.reader.TagReader;
import com.cakk.domain.mysql.repository.writer.CakeWriter;
import com.cakk.domain.mysql.repository.writer.TagWriter;
import com.cakk.domain.redis.repository.CakeViewsRedisRepository;

@Transactional(readOnly = true)
Expand All @@ -41,10 +42,9 @@ public class CakeService {
private final CakeReader cakeReader;
private final CakeWriter cakeWriter;
private final TagReader tagReader;
private final TagWriter tagWriter;
private final CakeShopReader cakeShopReader;
private final CakeViewsRedisRepository cakeViewsRedisRepository;

private final CakeManagerFacade cakeManagerFacade;
private final ApplicationEventPublisher publisher;

public CakeImageListResponse findCakeImagesByCursorAndCategory(final CakeSearchByCategoryRequest dto) {
Expand Down Expand Up @@ -92,37 +92,28 @@ public CakeDetailResponse findCakeDetailById(Long cakeId) {

@Transactional
public void createCake(CakeCreateParam param) {
CakeShop cakeShop = cakeShopReader.searchByIdAndOwner(param.cakeShopId(), param.owner());
Cake cake = param.cake();
List<Tag> tags = param.tagNames()
.stream()
.map(tagName -> tagReader.findByTagName(tagName).orElseGet(() -> tagWriter.saveTag(tagName)))
.toList();

cake.registerTags(tags);
cake.registerCategories(param.cakeCategories());
cakeShop.registerCake(cake);
final CakeShop cakeShop = cakeShopReader.searchByIdAndOwner(param.cakeShopId(), param.owner());
final Cake cake = param.cake();
final List<Tag> tags = tagReader.getTagsByTagName(param.tagNames());
final List<CakeCategory> cakeCategories = param.cakeCategories();

cakeManagerFacade.createCake(cakeShop, cake, tags, cakeCategories);
}

@Transactional
public void updateCake(CakeUpdateParam param) {
final Cake cake = cakeReader.findWithCakeTagsAndCakeCategories(param.cakeId(), param.owner());
List<Tag> tags = param.tagNames()
.stream()
.map(tagName -> tagReader.findByTagName(tagName).orElseGet(() -> tagWriter.saveTag(tagName)))
.toList();

cake.updateCakeImageUrl(param.cakeImageUrl());
cake.updateCakeCategories(param.cakeCategories());
cake.updateCakeTags(tags);
final List<Tag> tags = tagReader.getTagsByTagName(param.tagNames());
final String cakeImageUrl = param.cakeImageUrl();
final List<CakeCategory> cakeCategories = param.cakeCategories();

cakeManagerFacade.updateCake(cake, cakeImageUrl, tags, cakeCategories);
}

@Transactional
public void deleteCake(User owner, Long cakeId) {
final Cake cake = cakeReader.findWithCakeTagsAndCakeCategories(cakeId, owner);

cake.removeCakeCategories();
cake.removeCakeTags();
cakeWriter.deleteCake(cake);
Comment on lines 115 to 117
Copy link
Collaborator

Choose a reason for hiding this comment

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

해당 로직을 삭제한 이유는 뭘까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

orphanremoval 옵션이 작동하고 있기 때문에 필요 없는 로직이 되었습니다

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public CakeShopByMineResponse getMyBusinessId(final User user) {
return ShopMapper.supplyCakeShopByMineResponseBy(result);
}

@Transactional(readOnly = true)
@Transactional
Copy link
Collaborator

Choose a reason for hiding this comment

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

readOnly를 제거한 이유는 뭘까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

인증 정책을 통해 BusinessOwner인증 요청을 보내다보니 엔티티 변경 감지로 Update될 수 있기 때문입니다

public void requestCertificationBusinessOwner(final CertificationParam param) {
final BusinessInformation businessInformation = cakeShopReader.findBusinessInformationByCakeShopId(param.cakeShopId());
final CertificationEvent certificationEvent = verificationPolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@

import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
* Indicates that an annotated class is a "Service" (e.g. a domain service object).
*
* <p>This annotation serves as a specialization of {@link Service @Service},
* <p>This annotation serves as a specialization of {@link Component @Component},
* allowing for implementation classes to be autodetected through classpath scanning.
*
* @author komment
* @see Component
* @see Service
*/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
@Component
public @interface DomainFacade {

/**
* Alias for {@link Service#value}.
* Alias for {@link Component#value}.
*/
@AliasFor(annotation = Service.class)
@AliasFor(annotation = Component.class)
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cakk.domain.mysql.facade.cake;

import java.util.List;

import com.cakk.domain.mysql.annotation.DomainFacade;
import com.cakk.domain.mysql.entity.cake.Cake;
import com.cakk.domain.mysql.entity.cake.CakeCategory;
import com.cakk.domain.mysql.entity.cake.Tag;
import com.cakk.domain.mysql.entity.shop.CakeShop;

@DomainFacade
public class CakeManagerFacade {

public void createCake(CakeShop cakeShop, Cake cake, List<Tag> tags, List<CakeCategory> cakeCategories) {
cake.registerTags(tags);
cake.registerCategories(cakeCategories);
cakeShop.registerCake(cake);
}

public void updateCake(Cake cake, String cakeImageUrl, List<Tag> tags, List<CakeCategory> cakeCategories) {
cake.updateCakeImageUrl(cakeImageUrl);
cake.updateCakeCategories(cakeCategories);
cake.updateCakeTags(tags);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cakk.domain.mysql.repository.jpa;

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

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -11,4 +12,6 @@
public interface TagJpaRepository extends JpaRepository<Tag, Long> {

Optional<Tag> findTagByTagName(String tagName);

List<Tag> findTagsByTagNameIsIn(List<String> tagNames);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.cakk.domain.mysql.repository.reader;

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

import lombok.RequiredArgsConstructor;

import com.cakk.domain.mysql.annotation.Reader;
import com.cakk.domain.mysql.entity.cake.Tag;
import com.cakk.domain.mysql.mapper.TagMapper;
import com.cakk.domain.mysql.repository.jpa.TagJpaRepository;

@Reader
Expand All @@ -17,4 +19,16 @@ public class TagReader {
public Optional<Tag> findByTagName(final String tagName) {
return tagJpaRepository.findTagByTagName(tagName);
}

public List<Tag> getTagsByTagName(final List<String> tagNames) {
List<Tag> tags = tagJpaRepository.findTagsByTagNameIsIn(tagNames);

return tagNames.stream()
.map(tagName -> tags
.stream()
.filter(tag -> tag.getTagName().equals(tagName))
Copy link
Collaborator

Choose a reason for hiding this comment

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

tag에 대한 equals와 hashcode를 overriding 하여 비교하는건 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

반영하겠습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lcomment 근데 이 부분은 tagName:String 타입과 tag: Tag 타입 때문에 흠,, 해야 하는 이유가 있을까요?

Copy link
Collaborator

Choose a reason for hiding this comment

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

아 제가 잘못 봤네요 ! 필요 없을 것 같습니다

.findAny()
.orElse(tagJpaRepository.save(TagMapper.supplyTagBy(tagName))))
.toList();
}
}
Loading