added advertising
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
package com.krylov.refound.controller.advertising;
|
||||
|
||||
import com.krylov.refound.dto.advertising.AdImageUploadResponseDto;
|
||||
import com.krylov.refound.dto.advertising.AdvertisingAdminResponseDto;
|
||||
import com.krylov.refound.dto.advertising.AdvertisingCreateRequestDto;
|
||||
import com.krylov.refound.dto.advertising.AdvertisingUpdateRequestDto;
|
||||
import com.krylov.refound.dto.advertising.ConfirmUploadRequestDto;
|
||||
import com.krylov.refound.dto.advertising.PresignUploadRequestDto;
|
||||
import com.krylov.refound.dto.advertising.PresignUploadResponseDto;
|
||||
import com.krylov.refound.dto.advertising.TranscodingStatusDto;
|
||||
import com.krylov.refound.dto.advertising.VideoUploadConfirmedEvent;
|
||||
import com.krylov.refound.entity.advertising.Advertising;
|
||||
import com.krylov.refound.entity.advertising.Campaign;
|
||||
import com.krylov.refound.enums.advertising.AdvertisingType;
|
||||
import com.krylov.refound.repository.advertising.AdvertisingRepository;
|
||||
import com.krylov.refound.repository.advertising.CampaignRepository;
|
||||
import com.krylov.refound.service.advertising.AdvertisingAdminService;
|
||||
import com.krylov.refound.service.advertising.AdvertisingMediaService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/admin/ads")
|
||||
@RequiredArgsConstructor
|
||||
public class AdvertisingAdminController {
|
||||
|
||||
private static final int PRESIGN_URL_EXPIRY_MINUTES = 30;
|
||||
|
||||
private final AdvertisingRepository advertisingRepository;
|
||||
private final AdvertisingMediaService mediaService;
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
private final CampaignRepository campaignRepository;
|
||||
private final AdvertisingAdminService advertisingAdminService;
|
||||
|
||||
/**
|
||||
* Шаг 1: фронтенд запрашивает ссылку для прямой загрузки в MinIO.
|
||||
* Файл ещё не передан — только резервируем ключ и отдаём подписанный URL.
|
||||
*/
|
||||
@PostMapping("/{advertisingId}/video/presign-upload")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<PresignUploadResponseDto> presignUpload(@PathVariable Long advertisingId, @RequestBody PresignUploadRequestDto request) {
|
||||
log.info("presignUpload advertisingId {}", advertisingId);
|
||||
if (!advertisingRepository.existsById(advertisingId)) {
|
||||
log.warn("Advertising not found: {}", advertisingId);
|
||||
throw new EntityNotFoundException("Advertising not found: " + advertisingId);
|
||||
}
|
||||
boolean allowedContentType = advertisingAdminService.isAllowedContentType(request.contentType());
|
||||
if (!allowedContentType) {
|
||||
log.warn("allowedContentType for advertisingId {}", advertisingId);
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
String sourceKey = advertisingAdminService.getSourceKey(advertisingId, request);
|
||||
String uploadUrl = mediaService.getPresignedUploadUrl(sourceKey, request.contentType());
|
||||
return ResponseEntity.ok(new PresignUploadResponseDto(uploadUrl, sourceKey, PRESIGN_URL_EXPIRY_MINUTES));
|
||||
}
|
||||
|
||||
/**
|
||||
* Шаг 2: фронтенд сам сделал PUT на presigned URL, теперь сообщает,
|
||||
* что файл загружен — только после этого бэкенд узнаёт про файл и
|
||||
* запускает транскодинг.
|
||||
*/
|
||||
@PostMapping("/{advertisingId}/video/confirm-upload")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@Transactional
|
||||
public ResponseEntity<Void> confirmUpload(@PathVariable Long advertisingId, @RequestBody ConfirmUploadRequestDto request) {
|
||||
log.info("confirmUpload for advertisingId {}", advertisingId);
|
||||
Advertising advertising = advertisingRepository.findById(advertisingId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Advertising not found: " + advertisingId));
|
||||
|
||||
advertisingAdminService.updateTranscodingStatus(advertising.getId());
|
||||
eventPublisher.publishEvent(new VideoUploadConfirmedEvent(advertisingId, request.sourceKey()));
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<List<AdvertisingAdminResponseDto>> getAdvertisingsByCampaign(@RequestParam Long campaignId) {
|
||||
log.info("getAdvertisingsByCampaign {}", campaignId);
|
||||
List<AdvertisingAdminResponseDto> advertisings = advertisingAdminService.getAdvertisingsByCampaign(campaignId);
|
||||
return ResponseEntity.ok(advertisings);
|
||||
}
|
||||
|
||||
@GetMapping("/{advertisingId}/status")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<TranscodingStatusDto> getStatus(@PathVariable Long advertisingId) {
|
||||
log.info("getStatus for advertising {}", advertisingId);
|
||||
Advertising advertising = advertisingRepository.findById(advertisingId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Advertising not found: " + advertisingId));
|
||||
|
||||
return ResponseEntity.ok(new TranscodingStatusDto(advertising.getId(), advertising.getTranscodingStatus()));
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT /api/v1/admin/ads/{advertisingId} — обновление полей объявления
|
||||
* (targetUrl, weight). Тип/медиа/статус транскодинга не меняются.
|
||||
* Ответ 200: AdvertisingAdminResponseDto. 400 — невалидные данные.
|
||||
*/
|
||||
@PutMapping("/{advertisingId}")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<AdvertisingAdminResponseDto> updateAdvertising(
|
||||
@PathVariable Long advertisingId,
|
||||
@Valid @RequestBody AdvertisingUpdateRequestDto request) {
|
||||
log.info("updateAdvertising {}", advertisingId);
|
||||
if (request.weight() <= 0) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
AdvertisingAdminResponseDto dto = advertisingAdminService.updateAdvertising(advertisingId, request);
|
||||
return ResponseEntity.ok(dto);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<AdvertisingAdminResponseDto> createAdvertising(@Valid @RequestBody AdvertisingCreateRequestDto request) {
|
||||
log.info("createAdvertising {}", request);
|
||||
Campaign campaign = campaignRepository.findById(request.campaignId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Campaign not found: " + request.campaignId()));
|
||||
|
||||
if (request.weight() <= 0) {
|
||||
log.warn("weight AdvertisingCreateRequestDto {} <= 0", request);
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
Advertising advertising = advertisingAdminService.createAdvertising(campaign, request);
|
||||
if (request.type() == AdvertisingType.IMAGE && (request.mediaKey() == null || request.mediaKey().isBlank())) {
|
||||
log.warn("advertising image has bad mediaKey() or mediaKey isBlank");
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
AdvertisingAdminResponseDto adminDto = advertisingAdminService.toAdminDto(advertising);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(adminDto);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public ResponseEntity<AdImageUploadResponseDto> uploadAdImage(@RequestParam("file") MultipartFile file) {
|
||||
String mediaKey = mediaService.uploadImage(file);
|
||||
return ResponseEntity.ok(new AdImageUploadResponseDto(mediaKey));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user