Skip to content

Commit

Permalink
Merge pull request #97 from Team-INSERT/refactor/#93
Browse files Browse the repository at this point in the history
refactor(thumbsup): thumbsup 도메인 리팩토링
  • Loading branch information
jacobhboy authored Apr 30, 2024
2 parents a8bd834 + db88cc8 commit feb1291
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ public Long getIdFromJwt(String token) {
public User getCurrentUser() {
return authRepository.getCurrentUser();
}

public User getNullableCurrentUser() {
return authRepository.getNullableCurrentUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public class QueryAuthService {
public User getCurrentUser() {
return authRepository.getCurrentUser();
}

public User getNullableCurrentUser() {
return authRepository.getNullableCurrentUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import java.util.List;

import com.project.bumawiki.domain.thumbsup.presentation.dto.ThumbsUpResponseDto;
import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.user.domain.User;

public interface CustomThumbsUpRepository {
List<ThumbsUpResponseDto> getUserThumbsUp(User user);

Long countThumbsUpByTitle(String title);
List<Docs> getUserThumbsUp(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface ThumbsUpRepository extends JpaRepository<ThumbsUp, Long> {
void deleteByDocsAndUser(Docs docs, User user);

List<ThumbsUp> findByDocs_Id(Long docsId);

long countByDocs_Title(String title);

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.project.bumawiki.domain.thumbsup.domain.repository;
package com.project.bumawiki.domain.thumbsup.infra;

import static com.project.bumawiki.domain.docs.domain.QDocs.*;
import static com.project.bumawiki.domain.thumbsup.domain.QThumbsUp.*;
import static com.querydsl.core.types.Projections.*;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.project.bumawiki.domain.thumbsup.presentation.dto.ThumbsUpResponseDto;
import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.thumbsup.domain.repository.CustomThumbsUpRepository;
import com.project.bumawiki.domain.user.domain.User;
import com.querydsl.jpa.impl.JPAQueryFactory;

Expand All @@ -21,23 +21,13 @@ public class CustomThumbsUpRepositoryImpl implements CustomThumbsUpRepository {
private final JPAQueryFactory jpaQueryFactory;

@Override
public List<ThumbsUpResponseDto> getUserThumbsUp(User user) {
public List<Docs> getUserThumbsUp(User user) {
return jpaQueryFactory
.select(constructor(ThumbsUpResponseDto.class, docs))
.select(docs)
.from(thumbsUp)
.leftJoin(thumbsUp.docs, docs)
.where(thumbsUp.user.eq(user))
.distinct()
.fetch();
}

@Override
public Long countThumbsUpByTitle(String title) {
return jpaQueryFactory
.select(thumbsUp.count())
.from(docs)
.join(docs, thumbsUp.docs)
.where(docs.title.eq(title))
.fetchOne();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.project.bumawiki.domain.thumbsup.presentation;

import java.util.List;

import org.springframework.http.HttpStatus;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.project.bumawiki.domain.auth.annotation.LoginOrNot;
import com.project.bumawiki.domain.auth.annotation.LoginRequired;
import com.project.bumawiki.domain.auth.service.QueryAuthService;
import com.project.bumawiki.domain.thumbsup.presentation.dto.ThumbsUpCountResponseDto;
import com.project.bumawiki.domain.thumbsup.presentation.dto.ThumbsUpRequestDto;
import com.project.bumawiki.domain.thumbsup.presentation.dto.ThumbsUpResponseDto;
import com.project.bumawiki.domain.thumbsup.service.CommandThumbsUpService;
import com.project.bumawiki.domain.thumbsup.service.QueryThumbsUpService;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@Valid
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/thumbsup")
public class ThumbsUpController {
private final QueryThumbsUpService queryThumbsUpService;
private final CommandThumbsUpService commandThumbsUpService;
private final QueryAuthService queryAuthService;

@LoginRequired
@PostMapping
public void createLike(@RequestBody ThumbsUpRequestDto thumbsUpRequestDto) {
commandThumbsUpService.createThumbsUp(queryAuthService.getCurrentUser(), thumbsUpRequestDto.docsId());
}

@LoginOrNot
@GetMapping("/like/{docsId}")
@ResponseStatus(HttpStatus.OK)
public Boolean checkYouLikeThis(@PathVariable Long docsId) {
return queryThumbsUpService.checkUserLikeThisDocs(
docsId, queryAuthService.getNullableCurrentUser()
);
}

@LoginRequired
@GetMapping("/my")
public List<ThumbsUpResponseDto> getThumbsUps() {
return queryThumbsUpService.getThumbsUp(queryAuthService.getCurrentUser())
.stream()
.map(ThumbsUpResponseDto::new)
.toList();
}

@GetMapping("/{title}")
public ThumbsUpCountResponseDto getThumbsUpCount(@PathVariable String title) {
return new ThumbsUpCountResponseDto(
queryThumbsUpService.countThumbsUpByDocsTitle(title)
);
}

@LoginRequired
@DeleteMapping
public void cancel(@RequestBody ThumbsUpRequestDto thumbsUpRequestDto) {
commandThumbsUpService.cancelThumbsUp(queryAuthService.getCurrentUser(), thumbsUpRequestDto.docsId());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.project.bumawiki.domain.thumbsup.presentation.dto;

public record ThumbsUpCountResponseDto(Long thumbsUpsCount) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Service
@Transactional
@RequiredArgsConstructor
public class ThumbsUpManipulateService {
public class CommandThumbsUpService {
private final DocsReader docsReader;
private final ThumbsUpValidator thumbsUpValidator;
private final ThumbsUpCreator thumbsUpCreator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.project.bumawiki.domain.thumbsup.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.implementation.DocsValidator;
import com.project.bumawiki.domain.thumbsup.domain.repository.CustomThumbsUpRepository;
import com.project.bumawiki.domain.thumbsup.domain.repository.ThumbsUpRepository;
import com.project.bumawiki.domain.thumbsup.implementation.ThumbsUpReader;
import com.project.bumawiki.domain.user.domain.User;

Expand All @@ -12,12 +17,22 @@
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ThumbsUpCheckService {
public class QueryThumbsUpService {
private final ThumbsUpReader thumbsUpReader;
private final DocsValidator docsValidator;
private final CustomThumbsUpRepository customThumbsUpRepository;
private final ThumbsUpRepository thumbsUpRepository;

public boolean checkUserLikeThisDocs(Long docsId, User currentUser) {
docsValidator.checkDocsExist(docsId);
return thumbsUpReader.checkDocsLike(docsId, currentUser);
}

public Long countThumbsUpByDocsTitle(String title) {
return thumbsUpRepository.countByDocs_Title(title);
}

public List<Docs> getThumbsUp(User user) {
return customThumbsUpRepository.getUserThumbsUp(user);
}
}

This file was deleted.

Loading

0 comments on commit feb1291

Please sign in to comment.