Skip to content

Commit

Permalink
KNU-HAEDAL#1 feat: 기프티콘 및 유저 기프티콘 서비스 제작
Browse files Browse the repository at this point in the history
  • Loading branch information
momnpa333 committed Apr 4, 2024
1 parent 1658d32 commit ee1166d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import org.springframework.data.jpa.repository.JpaRepository;
import team.haedal.gifticionfunding.entity.gifticon.UserGifticon;
import team.haedal.gifticionfunding.entity.user.User;

import java.util.List;

public interface UserGifticonJpaRepository extends JpaRepository<UserGifticon, Long> {
List<UserGifticon> findByOwner(User owner);

List<UserGifticon> findByBuyer(User buyer);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package team.haedal.gifticionfunding.service.gifticon;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.haedal.gifticionfunding.entity.gifticon.Gifticon;
import team.haedal.gifticionfunding.entity.gifticon.GifticonUpdate;
import team.haedal.gifticionfunding.repository.gifticon.GifticonJpaRepository;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class GifticonService {

private final GifticonJpaRepository gifticonJpaRepository;

//상품등록
@Transactional
public Long registerGifticon(Gifticon gifticon){
gifticonJpaRepository.save(gifticon);
return gifticon.getId();
}

//상품 전체 조회
public List<Gifticon> findGifticonAll(){
return gifticonJpaRepository.findAll();
}

//상품 상세 조회
public Gifticon findGifticon(Long gifticonId){
return gifticonJpaRepository.findById(gifticonId).orElse(null);
}

//상품 수정

@Transactional
public void updateGifticon(Long gifticonId, GifticonUpdate gifticonUpadate){
Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null);
if(findGifticon == null){
throw new IllegalArgumentException("해당 상품이 존재하지 않습니다.");
}
findGifticon.updateGifticon(gifticonUpadate);
}

//상품 삭제
@Transactional
public void deleteGifticon(Long gifticonId){
gifticonJpaRepository.deleteById(gifticonId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package team.haedal.gifticionfunding.service.gifticon;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.haedal.gifticionfunding.entity.gifticon.GifticonPurchase;
import team.haedal.gifticionfunding.entity.gifticon.UserGifticon;
import team.haedal.gifticionfunding.entity.user.User;
import team.haedal.gifticionfunding.repository.gifticon.UserGifticonJpaRepository;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class UserGifticonService {
private final UserGifticonJpaRepository userGifticonJpaRepository;

/**
* 유저기프티콘 전체 조회
*/
public List<UserGifticon> findUserGifticonAll()
{
return userGifticonJpaRepository.findAll();
}

/**
* 유저기프티콘 상세 조회
*/
public UserGifticon findUserGifticon(Long userGifticonId)
{
return userGifticonJpaRepository.findById(userGifticonId).orElse(null);
}

/**
* 유저기프티콘 소유자별 조회
*/
public List<UserGifticon> findUserGifticonByOwner(User owner)
{
return userGifticonJpaRepository.findByOwner(owner);
}

/**
* 유저기프티콘 구매자별 조회
*/
public List<UserGifticon> findUserGifticonByBuyer(User buyer)
{
return userGifticonJpaRepository.findByBuyer(buyer);
}

/**
* 유저기프티콘 삭제
*/
@Transactional
public void deleteUserGifticon(Long userGifticonId)
{
userGifticonJpaRepository.deleteById(userGifticonId);
}




/**
* 기프티콘 구매
*/
@Transactional
public Long purchaseGifticon(GifticonPurchase gifticonPurchase)
{
UserGifticon userGifticon = UserGifticon.purchaseGifticon(gifticonPurchase);
userGifticonJpaRepository.save(userGifticon);
return userGifticon.getId();
}

/**
* 기프티콘 사용
*/
@Transactional
public void useGifticon(Long userGifticonId)
{
UserGifticon userGifticon = userGifticonJpaRepository.findById(userGifticonId).orElse(null);
if(userGifticon == null)
{
throw new IllegalArgumentException("해당 기프티콘이 존재하지 않습니다.");
}
userGifticon.useGifticon();
}

/**
* 기프티콘 환불
*/
@Transactional
public void refundGifticon(Long userGifticonId)
{
UserGifticon userGifticon = userGifticonJpaRepository.findById(userGifticonId).orElse(null);
if(userGifticon == null)
{
throw new IllegalArgumentException("해당 기프티콘이 존재하지 않습니다.");
}
userGifticon.refundGifticon();
}

/**
* 기프티콘 소유자 변경
*/
@Transactional
public void changeOwner(Long userGifticonId, User owner)
{
UserGifticon userGifticon = userGifticonJpaRepository.findById(userGifticonId).orElse(null);
if(userGifticon == null)
{
throw new IllegalArgumentException("해당 기프티콘이 존재하지 않습니다.");
}
userGifticon.changeOwner(owner);
}


}

0 comments on commit ee1166d

Please sign in to comment.