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

Feature/dnd special #169

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.meme.ala.domain.aggregation.service;

import com.meme.ala.domain.aggregation.model.entity.Aggregation;
import com.meme.ala.domain.member.model.entity.AlaCardSettingPair;
import com.meme.ala.domain.member.model.entity.Member;

import java.io.UnsupportedEncodingException;
import java.util.List;

public interface AggregationService {
public Aggregation findByMember(Member member);
Aggregation findByMember(Member member);

public void initAggregation(Member member);
void initAggregation(Member member, List<AlaCardSettingPair> alaCardSettingPairList);

public void save(Aggregation aggregation);
void save(Aggregation aggregation);

public void submitWordList(Member member, Aggregation aggregation, List<String> wordIdList) throws UnsupportedEncodingException;
void submitWordList(Member member, Aggregation aggregation, List<String> wordIdList) throws UnsupportedEncodingException;

public Integer getUserCount();
Integer getUserCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public Aggregation findByMember(Member member) {

@Override
@Transactional
public void initAggregation(Member member) {
List<WordCount> wordCountList = member.getAlaCardSettingPairList().stream()
public void initAggregation(Member member, List<AlaCardSettingPair> alaCardSettingPairList) {
Aggregation aggregation = aggregationRepository.findByMemberId(member.getId())
.orElse(Aggregation.builder().memberId(member.getId()).wordCountList(new ArrayList<>()).build());
List<WordCount> wordCountList = alaCardSettingPairList.stream()
.map(this::toNestedWordCountList)
.flatMap(Collection::stream)
.flatMap(Collection::stream)
.collect(Collectors.toList());
Aggregation aggregation = Aggregation.builder().memberId(member.getId()).wordCountList(wordCountList).build();
aggregation.getWordCountList().addAll(wordCountList);
aggregationRepository.save(aggregation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ public ResponseEntity<ResponseDto> patchAlaCardSetting(@CurrentUser Member membe
return ResponseEntity.status(HttpStatus.NO_CONTENT)
.body(ResponseDto.of(HttpStatus.NO_CONTENT, ResponseMessage.SUCCESS));
}

@GetMapping("/specialCard")
public ResponseEntity<ResponseDto> assignSpecialCard(@RequestParam String nickname, @RequestParam int size) {
Member member = memberService.findByNickname(nickname);
memberCardService.assignSpecialCard(member, size);
return ResponseEntity.status(HttpStatus.NO_CONTENT)
.body(ResponseDto.of(HttpStatus.NO_CONTENT, ResponseMessage.SUCCESS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AlaCardRepository extends MongoRepository<AlaCard, ObjectId> {
List<AlaCard> findAllBySpecial(Boolean special);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void initMember(InitEvent event) {
Member member = memberRepository.findByEmail(event.getEmail())
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND));
memberCardService.assignCard(member, defaultCardNum);
aggregationService.initAggregation(member);
friendInfoService.initFriendInfo(member);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface MemberCardService {
List<SelectionWordDto> getWordList(String cookieId);

void saveSetting(Member member, AlaCardSettingDto alaCardSettingDto);

void assignSpecialCard(Member member, int num);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.meme.ala.core.error.ErrorCode;
import com.meme.ala.core.error.exception.EntityNotFoundException;
import com.meme.ala.domain.aggregation.service.AggregationService;
import com.meme.ala.domain.alacard.model.dto.response.AlaCardSettingDto;
import com.meme.ala.domain.alacard.model.dto.response.SelectionWordDto;
import com.meme.ala.domain.alacard.model.entity.AlaCard;
Expand All @@ -22,6 +23,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -38,12 +40,14 @@ public class MemberCardServiceImpl implements MemberCardService {
private final TemporalWordListRepository temporalWordListRepository;
private final MemberService memberService;
private final AlaCardService alaCardService;
private final AggregationService aggregationService;

@Override
@Transactional
public void assignCard(Member member, int num) {
List<AlaCard> memberAlaCardList = getAlaCardListFromMember(member);
List<AlaCard> alaCardList = getAlaCardList();
List<AlaCardSettingPair> newAlaCardList = new ArrayList<>();

List<AlaCardSetting> alaCardSettingList = alaCardService.getBackgrounds().stream()
.map(background -> AlaCardSetting.builder()
Expand All @@ -63,13 +67,15 @@ public void assignCard(Member member, int num) {
.collect(Collectors.toList());

for (int i = 0; i < num; i++) {
member.getAlaCardSettingPairList()
newAlaCardList
.add(AlaCardSettingPair.builder()
.alaCard(selectedAlaCardList.get(i))
.alaCardSetting(alaCardSettingList.get(i % alaCardSettingList.size()))
.build());
}
member.getAlaCardSettingPairList().addAll(newAlaCardList);
memberRepository.save(member);
aggregationService.initAggregation(member, newAlaCardList);
}

@Cacheable
Expand Down Expand Up @@ -131,4 +137,40 @@ public void saveSetting(Member member, AlaCardSettingDto alaCardSettingDto) {
}
memberRepository.save(member);
}

@Override
@Transactional
public void assignSpecialCard(Member member, int num) {
List<AlaCard> memberAlaCardList = getAlaCardListFromMember(member);
List<AlaCard> alaCardList = alaCardRepository.findAllBySpecial(true);
List<AlaCardSettingPair> newAlaCardList = new ArrayList<>();
int assignSize = Math.min(num, alaCardList.size());

List<AlaCardSetting> alaCardSettingList = alaCardService.getBackgrounds().stream()
.map(background -> AlaCardSetting.builder()
.background(background)
.isOpen(true)
.build())
.collect(Collectors.toList());

Collections.shuffle(alaCardList);
Collections.shuffle(alaCardSettingList);

List<AlaCard> selectedAlaCardList =
alaCardList.stream()
.filter(alaCard -> !memberAlaCardList.contains(alaCard))
.limit(assignSize)
.collect(Collectors.toList());

for (int i = 0; i < assignSize; i++) {
newAlaCardList
.add(AlaCardSettingPair.builder()
.alaCard(selectedAlaCardList.get(i))
.alaCardSetting(alaCardSettingList.get(i % alaCardSettingList.size()))
.build());
}
member.getAlaCardSettingPairList().addAll(newAlaCardList);
memberRepository.save(member);
aggregationService.initAggregation(member, newAlaCardList);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.meme.ala.domain.member.service;

import com.meme.ala.common.EntityFactory;
import com.meme.ala.domain.aggregation.service.AggregationService;
import com.meme.ala.domain.alacard.model.entity.AlaCard;
import com.meme.ala.domain.alacard.repository.AlaCardRepository;
import com.meme.ala.domain.alacard.service.AlaCardService;
Expand All @@ -21,6 +22,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;

@SpringBootTest
Expand All @@ -34,6 +36,8 @@ public class MemberCardServiceTest {
private MemberCardService memberCardService;
@MockBean
private AlaCardService alaCardService;
@MockBean
private AggregationService aggregationService;
@Value("${member.alacardnum}")
private int defaultCardNum;

Expand All @@ -49,6 +53,7 @@ public class MemberCardServiceTest {
when(memberRepository.existsMemberByMemberSettingNickname(any(String.class))).thenReturn(false);
when(alaCardRepository.findAll()).thenReturn(alaCardList);
when(alaCardService.getBackgrounds()).thenReturn(Arrays.asList(EntityFactory.testAlaCardSetting().getBackground()));
doNothing().when(aggregationService).initAggregation(any(Member.class),any(List.class));


Member testMember = Member.builder().build();
Expand Down