/
drobash
/
AdBoard_service
Обзор
Документация
Войти
/
drobash
/
AdBoard_service
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/org/example/service/TransactionService.java
72 строки
3 KB
dushesp
Создан сервис
14 апр 2025, 03:58
14 апр 2025, 03:58
568c930
Код
Авторство
О чём код?
package org.example.service; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.example.dto.request.TransactionRequestDto; import org.example.dto.response.TransactionResponseDto; import org.example.exception.AdvertisementNotFoundException; import org.example.exception.TransactionNotFoundException; import org.example.mapper.TransactionMapper; import org.example.model.Advertisement; import org.example.model.Transaction; import org.example.model.TransactionStatus; import org.example.repository.AdvertisementRepository; import org.example.repository.TransactionRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @Slf4j public class TransactionService { private final TransactionRepository transactionRepository; private final AdvertisementRepository advertisementRepository; private final TransactionMapper transactionMapper; @Transactional public TransactionResponseDto createTransaction(TransactionRequestDto dto) { Advertisement advertisement = advertisementRepository.findById(dto.getAdvertisementId()) .orElseThrow(() -> new AdvertisementNotFoundException(dto.getAdvertisementId())); Transaction transaction = transactionMapper.toEntity(dto); transaction.setSellerId(advertisement.getSellerId()); transaction.setAmount(dto.getAmount()); Transaction savedTransaction = transactionRepository.save(transaction); log.info("Created transaction with id: {}", savedTransaction.getId()); return transactionMapper.toDto(savedTransaction); } @Transactional public TransactionResponseDto completeTransaction(Long transactionId) { Transaction transaction = transactionRepository.findById(transactionId) .orElseThrow(() -> new TransactionNotFoundException(transactionId)); if (transaction.getStatus() != TransactionStatus.PENDING) { throw new IllegalStateException("Only pending transactions can be completed"); } transaction.setStatus(TransactionStatus.COMPLETED); Transaction updated = transactionRepository.save(transaction); log.info("Completed transaction with id: {}", transactionId); return transactionMapper.toDto(updated); } @Transactional public TransactionResponseDto cancelTransaction(Long transactionId) { Transaction transaction = transactionRepository.findById(transactionId) .orElseThrow(() -> new TransactionNotFoundException(transactionId)); if (transaction.getStatus() != TransactionStatus.PENDING) { throw new IllegalStateException("Only pending transactions can be cancelled"); } transaction.setStatus(TransactionStatus.CANCELLED); Transaction updated = transactionRepository.save(transaction); log.info("Cancelled transaction with id: {}", transactionId); return transactionMapper.toDto(updated); } }