added advertising
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.krylov.refound.service.advertising;
|
||||
|
||||
import com.krylov.refound.dto.advertising.CampaignCreateRequestDto;
|
||||
import com.krylov.refound.dto.advertising.CampaignResponseDto;
|
||||
import com.krylov.refound.dto.advertising.CampaignStatusUpdateRequestDto;
|
||||
import com.krylov.refound.dto.advertising.CampaignUpdateRequestDto;
|
||||
import com.krylov.refound.entity.advertising.Campaign;
|
||||
import com.krylov.refound.enums.advertising.CampaignStatus;
|
||||
import com.krylov.refound.exception.InvalidCampaignStatusTransitionException;
|
||||
import com.krylov.refound.repository.advertising.CampaignRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CampaignService {
|
||||
|
||||
private final CampaignRepository campaignRepository;
|
||||
|
||||
public List<CampaignResponseDto> getCampaign() {
|
||||
return campaignRepository.findAllByOrderByCreatedAtDesc()
|
||||
.stream()
|
||||
.map(this::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Optional<CampaignResponseDto> getCampaignById(Long campaignId) {
|
||||
return campaignRepository.findById(campaignId).map(this::toDto);
|
||||
}
|
||||
|
||||
public CampaignResponseDto createCampaign(CampaignCreateRequestDto request){
|
||||
Campaign campaign = new Campaign();
|
||||
campaign.setName(request.name());
|
||||
campaign.setStatus(CampaignStatus.DRAFT);
|
||||
campaign.setStartDate(request.startDate());
|
||||
campaign.setEndDate(request.endDate());
|
||||
campaign.setDailyImpressionCap(request.dailyImpressionCap());
|
||||
campaignRepository.save(campaign);
|
||||
log.info("campaign {} created", campaign);
|
||||
return toDto(campaign);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CampaignResponseDto updateCampaign(Long campaignId, CampaignUpdateRequestDto request) {
|
||||
Campaign campaign = campaignRepository.findById(campaignId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Campaign not found: " + campaignId));
|
||||
|
||||
if (!request.startDate().isBefore(request.endDate())) {
|
||||
throw new IllegalArgumentException("startDate must be before endDate");
|
||||
}
|
||||
if (request.dailyImpressionCap() <= 0) {
|
||||
throw new IllegalArgumentException("dailyImpressionCap must be positive");
|
||||
}
|
||||
|
||||
campaign.setName(request.name());
|
||||
campaign.setStartDate(request.startDate());
|
||||
campaign.setEndDate(request.endDate());
|
||||
campaign.setDailyImpressionCap(request.dailyImpressionCap());
|
||||
|
||||
log.info("campaign {} updated", campaignId);
|
||||
return toDto(campaign);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CampaignResponseDto updateCampaignStatus(Long campaignId, CampaignStatusUpdateRequestDto request) {
|
||||
Campaign campaign = campaignRepository.findById(campaignId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Campaign not found: " + campaignId));
|
||||
|
||||
if (!isValidTransition(campaign.getStatus(), request.status())) {
|
||||
throw new InvalidCampaignStatusTransitionException(
|
||||
"Invalid status transition: "
|
||||
+ campaign.getStatus()
|
||||
+ " -> "
|
||||
+ request.status()
|
||||
);
|
||||
}
|
||||
|
||||
campaignRepository.updateStatus(campaignId, request.status());
|
||||
return toDto(campaign);
|
||||
}
|
||||
|
||||
/**
|
||||
* Разрешённые переходы статуса кампании. Не даёт, например, воскресить
|
||||
* FINISHED обратно в ACTIVE или перепрыгнуть из DRAFT сразу в FINISHED.
|
||||
*/
|
||||
private boolean isValidTransition(CampaignStatus from, CampaignStatus to) {
|
||||
if (from == to) {
|
||||
return false;
|
||||
}
|
||||
return switch (from) {
|
||||
case DRAFT -> to == CampaignStatus.ACTIVE;
|
||||
case ACTIVE -> to == CampaignStatus.PAUSED || to == CampaignStatus.FINISHED;
|
||||
case PAUSED -> to == CampaignStatus.ACTIVE || to == CampaignStatus.FINISHED;
|
||||
case FINISHED -> false;
|
||||
};
|
||||
}
|
||||
|
||||
private CampaignResponseDto toDto(Campaign campaign) {
|
||||
return new CampaignResponseDto(
|
||||
campaign.getId(),
|
||||
campaign.getName(),
|
||||
campaign.getStatus(),
|
||||
campaign.getStartDate(),
|
||||
campaign.getEndDate(),
|
||||
campaign.getDailyImpressionCap());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user