Skip to content

Commit

Permalink
Merge pull request #50 from Central-MakeUs/dev
Browse files Browse the repository at this point in the history
fix: 수정사항 반영
  • Loading branch information
KarmaPol authored Feb 13, 2024
2 parents f0a1c96 + 0ff19c5 commit 1023e92
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 278 deletions.
174 changes: 86 additions & 88 deletions api/src/main/java/com/mm/api/domain/dib/service/DibService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.mm.api.domain.dib.service;

import static com.mm.api.exception.ErrorCode.*;

import java.util.List;

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

import com.mm.api.domain.dib.dto.response.DibListResponse;
import com.mm.api.domain.item.dto.response.ItemResponse;
import com.mm.api.exception.CustomException;
Expand All @@ -19,91 +12,96 @@
import com.mm.coredomain.repository.MemberRepository;
import com.mm.coreinfraqdsl.repository.DibCustomRepository;
import com.mm.coresecurity.oauth.OAuth2UserDetails;

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

import java.util.List;

import static com.mm.api.exception.ErrorCode.*;

@Service
@Transactional
@RequiredArgsConstructor
public class DibService {
private final DibRepository dibRepository;
private final MemberRepository memberRepository;
private final ItemRepository itemRepository;
private final DibCustomRepository dibCustomRepository;

@Transactional(readOnly = true)
public List<Boolean> getDib(List<Item> items, OAuth2UserDetails userDetails) {
if (userDetails == null) {
return items.stream()
.map(item -> false)
.toList();
}

Long memberId = userDetails.getId();
Member member = getMember(memberId);
return items.stream()
.map(item -> isDibExist(member, item))
.toList();
}

public void postDib(Long itemId, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
Item item = getItem(itemId);

if (isDibExist(member, item)) {
throw new CustomException(DIB_ALREADY_EXIST);
}

Dib dib = Dib.builder()
.member(member)
.item(item)
.build();

dibRepository.save(dib);
}

public void deleteDib(List<Long> itemIds, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
List<Item> items = itemIds.stream()
.map(this::getItem)
.toList();

items
.forEach(item -> {
Dib dib = dibRepository.findByMemberAndItem(member, item)
.orElseThrow(() -> new CustomException(DIB_NOT_FOUND));
dibRepository.delete(dib);
});
}

@Transactional(readOnly = true)
public DibListResponse getDibsMe(Integer page, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
List<Dib> dibs = dibCustomRepository.getDibsByPage(page, member);
List<ItemResponse> itemResponses = dibs.stream()
.map(dib ->
ItemResponse.of(dib.getItem(), true))
.toList();

Long pageNum = dibCustomRepository.getPageNum(member);
Boolean isLastPage = pageNum.equals(page.longValue());

long count = dibRepository.count();

return new DibListResponse(isLastPage, count, itemResponses);
}

private boolean isDibExist(Member member, Item item) {
return dibRepository.findByMemberAndItem(member, item).isPresent();
}

private Item getItem(Long id) {
return itemRepository.findById(id)
.orElseThrow(() -> new CustomException(ITEM_NOT_FOUND));
}

private Member getMember(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
}
private final DibRepository dibRepository;
private final MemberRepository memberRepository;
private final ItemRepository itemRepository;
private final DibCustomRepository dibCustomRepository;

@Transactional(readOnly = true)
public List<Boolean> getDib(List<Item> items, OAuth2UserDetails userDetails) {
if (userDetails == null) {
return items.stream()
.map(item -> false)
.toList();
}

Long memberId = userDetails.getId();
Member member = getMember(memberId);
return items.stream()
.map(item -> isDibExist(member, item))
.toList();
}

public void postDib(Long itemId, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
Item item = getItem(itemId);

if (isDibExist(member, item)) {
throw new CustomException(DIB_ALREADY_EXIST);
}

Dib dib = Dib.builder()
.member(member)
.item(item)
.build();

dibRepository.save(dib);
}

public void deleteDib(List<Long> itemIds, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
List<Item> items = itemIds.stream()
.map(this::getItem)
.toList();

items
.forEach(item -> {
Dib dib = dibRepository.findByMemberAndItem(member, item)
.orElseThrow(() -> new CustomException(DIB_NOT_FOUND));
dibRepository.delete(dib);
});
}

@Transactional(readOnly = true)
public DibListResponse getDibsMe(Integer page, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
List<Dib> dibs = dibCustomRepository.getDibsByPage(page, member);
List<ItemResponse> itemResponses = dibs.stream()
.map(dib ->
ItemResponse.of(dib.getItem(), true))
.toList();

Long pageNum = dibCustomRepository.getPageNum(member);
Boolean isLastPage = pageNum.equals(page.longValue());

long count = dibRepository.countByMember(member);

return new DibListResponse(isLastPage, count, itemResponses);
}

private boolean isDibExist(Member member, Item item) {
return dibRepository.findByMemberAndItem(member, item).isPresent();
}

private Item getItem(Long id) {
return itemRepository.findById(id)
.orElseThrow(() -> new CustomException(ITEM_NOT_FOUND));
}

private Member getMember(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.mm.coredomain.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.mm.coredomain.domain.Dib;
import com.mm.coredomain.domain.Item;
import com.mm.coredomain.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface DibRepository extends JpaRepository<Dib, Long> {
Optional<Dib> findByMemberAndItem(Member member, Item item);
Optional<Dib> findByMemberAndItem(Member member, Item item);

Long countByMember(Member member);
}
Loading

0 comments on commit 1023e92

Please sign in to comment.