Search in sources :

Example 1 with StudylogScrap

use of wooteco.prolog.studylog.domain.StudylogScrap in project prolog by woowacourse.

the class StudylogScrapService method registerScrap.

@Transactional
public MemberScrapResponse registerScrap(Long memberId, Long studylogId) {
    if (studylogScrapRepository.countByMemberIdAndScrapStudylogId(memberId, studylogId) > 0) {
        throw new StudylogScrapAlreadyRegisteredException();
    }
    Studylog studylog = studylogRepository.findById(studylogId).orElseThrow(StudylogNotFoundException::new);
    Member member = memberRepository.findById(memberId).orElseThrow(MemberNotFoundException::new);
    StudylogScrap studylogScrap = new StudylogScrap(member, studylog);
    studylogScrapRepository.save(studylogScrap);
    return MemberScrapResponse.of(studylogScrap);
}
Also used : StudylogNotFoundException(wooteco.prolog.studylog.exception.StudylogNotFoundException) StudylogScrapAlreadyRegisteredException(wooteco.prolog.studylog.exception.StudylogScrapAlreadyRegisteredException) StudylogScrap(wooteco.prolog.studylog.domain.StudylogScrap) Member(wooteco.prolog.member.domain.Member) MemberNotFoundException(wooteco.prolog.member.exception.MemberNotFoundException) Studylog(wooteco.prolog.studylog.domain.Studylog) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with StudylogScrap

use of wooteco.prolog.studylog.domain.StudylogScrap in project prolog by woowacourse.

the class StudylogScrapRepositoryTest method countMemberScrapTest.

@DisplayName("count기능을 테스트한다.")
@Test
void countMemberScrapTest() {
    // given
    StudylogScrap studylogScrap = new StudylogScrap(member, studylog);
    // when
    int countBefore = studylogScrapRepository.countByMemberIdAndScrapStudylogId(member.getId(), studylog.getId());
    studylogScrapRepository.save(studylogScrap);
    int countAfter = studylogScrapRepository.countByMemberIdAndScrapStudylogId(member.getId(), studylog.getId());
    // then
    assertThat(countBefore).isEqualTo(0);
    assertThat(countAfter).isEqualTo(1);
}
Also used : StudylogScrap(wooteco.prolog.studylog.domain.StudylogScrap) Test(org.junit.jupiter.api.Test) RepositoryTest(wooteco.support.utils.RepositoryTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with StudylogScrap

use of wooteco.prolog.studylog.domain.StudylogScrap in project prolog by woowacourse.

the class StudylogScrapService method unregisterScrap.

@Transactional
public void unregisterScrap(Long memberId, Long studylogId) {
    StudylogScrap scrap = studylogScrapRepository.findByMemberIdAndStudylogId(memberId, studylogId).orElseThrow(StudylogScrapNotExistException::new);
    studylogScrapRepository.delete(scrap);
}
Also used : StudylogScrapNotExistException(wooteco.prolog.studylog.exception.StudylogScrapNotExistException) StudylogScrap(wooteco.prolog.studylog.domain.StudylogScrap) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with StudylogScrap

use of wooteco.prolog.studylog.domain.StudylogScrap in project prolog by woowacourse.

the class StudylogService method checkScrapedOrRead.

private void checkScrapedOrRead(Long memberId, Long studylogId) {
    if (studylogScrapRepository.existsByMemberIdAndStudylogId(memberId, studylogId)) {
        StudylogScrap studylogScrap = studylogScrapRepository.findByMemberIdAndStudylogId(memberId, studylogId).orElseThrow(StudylogScrapNotExistException::new);
        studylogScrapRepository.delete(studylogScrap);
    }
    if (studylogReadRepository.existsByMemberIdAndStudylogId(memberId, studylogId)) {
        StudylogRead studylogRead = studylogReadRepository.findByMemberIdAndStudylogId(memberId, studylogId).orElseThrow(StudylogReadNotExistException::new);
        studylogReadRepository.delete(studylogRead);
    }
}
Also used : StudylogRead(wooteco.prolog.studylog.domain.StudylogRead) StudylogScrapNotExistException(wooteco.prolog.studylog.exception.StudylogScrapNotExistException) StudylogScrap(wooteco.prolog.studylog.domain.StudylogScrap) StudylogReadNotExistException(wooteco.prolog.studylog.exception.StudylogReadNotExistException)

Example 5 with StudylogScrap

use of wooteco.prolog.studylog.domain.StudylogScrap in project prolog by woowacourse.

the class StudylogScrapRepositoryTest method findByMemberIdAndStudylogIdTest.

@DisplayName("멤버 아이디와 스터디로그 아이디로 찾아오기 기능을 테스트한다.")
@Test
void findByMemberIdAndStudylogIdTest() {
    // given
    StudylogScrap expectedStudylogScrap = new StudylogScrap(member, studylog);
    // when
    studylogScrapRepository.save(expectedStudylogScrap);
    StudylogScrap studylogScrap = studylogScrapRepository.findByMemberIdAndStudylogId(member.getId(), studylog.getId()).get();
    // then
    assertThat(expectedStudylogScrap).isEqualTo(studylogScrap);
}
Also used : StudylogScrap(wooteco.prolog.studylog.domain.StudylogScrap) Test(org.junit.jupiter.api.Test) RepositoryTest(wooteco.support.utils.RepositoryTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

StudylogScrap (wooteco.prolog.studylog.domain.StudylogScrap)5 DisplayName (org.junit.jupiter.api.DisplayName)2 Test (org.junit.jupiter.api.Test)2 Transactional (org.springframework.transaction.annotation.Transactional)2 StudylogScrapNotExistException (wooteco.prolog.studylog.exception.StudylogScrapNotExistException)2 RepositoryTest (wooteco.support.utils.RepositoryTest)2 Member (wooteco.prolog.member.domain.Member)1 MemberNotFoundException (wooteco.prolog.member.exception.MemberNotFoundException)1 Studylog (wooteco.prolog.studylog.domain.Studylog)1 StudylogRead (wooteco.prolog.studylog.domain.StudylogRead)1 StudylogNotFoundException (wooteco.prolog.studylog.exception.StudylogNotFoundException)1 StudylogReadNotExistException (wooteco.prolog.studylog.exception.StudylogReadNotExistException)1 StudylogScrapAlreadyRegisteredException (wooteco.prolog.studylog.exception.StudylogScrapAlreadyRegisteredException)1