io.fruitful.ecomerce.service.wishlist.MagentoWishlistServiceImpl Maven / Gradle / Ivy
package io.fruitful.ecomerce.service.wishlist;
import io.fruitful.ecomerce.commons.MagentoErrorInfo;
import io.fruitful.ecomerce.commons.MagentoException;
import io.fruitful.ecomerce.dto.MagentoProductResponse;
import io.fruitful.ecomerce.dto.MagentoWishlistRequest;
import io.fruitful.ecomerce.dto.MagentoWishlistResponse;
import io.fruitful.ecomerce.utils.MagentoInitService;
import io.fruitful.ecomerce.utils.RetrofitService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import retrofit2.Response;
@RequiredArgsConstructor
@Log4j2
@Service
public class MagentoWishlistServiceImpl implements MagentoWishlistService {
private final MagentoInitService magentoInitSv;
@Override
public boolean addFromCart(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().addFromCart(request.getBearerToken(), request).execute();
} catch (Exception ex) {
log.error("Error was occurred when add product to from cart to wish list.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
return RetrofitService.getData(response);
}
@Override
public boolean addFromShop(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().addFromShop(request.getBearerToken(), request).execute();
} catch (Exception ex) {
log.error("Error was occurred when add product from shop page to wish list.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
return RetrofitService.getData(response);
}
@Override
public boolean remove(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().remove(request.getBearerToken(), request.getSku()).execute();
} catch (Exception ex) {
log.error("Error was occurred when remove product from wish list.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
return RetrofitService.getData(response);
}
@Override
public MagentoWishlistResponse list(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().list(request.getBearerToken()).execute();
} catch (Exception ex) {
log.error("Error was occurred when displaying wish list.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
MagentoProductResponse data = RetrofitService.getData(response);
MagentoWishlistResponse wishlistResponse = new MagentoWishlistResponse();
wishlistResponse.setItems(data.getItems());
wishlistResponse.setTotalCount(data.getTotalCount());
return wishlistResponse;
}
@Override
public boolean moveAllToCart(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().moveAllToCart(request.getBearerToken()).execute();
} catch (Exception ex) {
log.error("Error was occurred when move all wishlist items to cart.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
return RetrofitService.getData(response);
}
@Override
public boolean removeAll(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().removeAll(request.getBearerToken()).execute();
} catch (Exception ex) {
log.error("Error was occurred when remove all wishlist items.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
return RetrofitService.getData(response);
}
@Override
public boolean addToCartFromWishlist(MagentoWishlistRequest request) throws MagentoException {
Response response;
try {
response = magentoInitSv.getMagentoWishlistApi().addToCartFromWishlist(request.getBearerToken(), request).execute();
} catch (Exception ex) {
log.error("Error was occurred while add product to cart from wishlist.", ex);
throw new MagentoException(MagentoErrorInfo.MAGENTO_CUSTOM_ERROR_CODE, ex.getMessage());
}
return RetrofitService.getData(response);
}
}