|
| 1 | +package org.devkor.apu.saerok_server.domain.ad.api; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation; |
| 4 | +import io.swagger.v3.oas.annotations.media.Content; |
| 5 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 6 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 7 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 8 | +import jakarta.validation.Valid; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import org.devkor.apu.saerok_server.domain.ad.api.dto.request.AdEventRequest; |
| 11 | +import org.devkor.apu.saerok_server.domain.ad.api.dto.response.AdEventStatusResponse; |
| 12 | +import org.devkor.apu.saerok_server.domain.ad.api.dto.response.GetAdSlotResponse; |
| 13 | +import org.devkor.apu.saerok_server.domain.ad.application.AdEventService; |
| 14 | +import org.devkor.apu.saerok_server.domain.ad.application.AdSelectionResult; |
| 15 | +import org.devkor.apu.saerok_server.domain.ad.application.AdSelectorService; |
| 16 | +import org.devkor.apu.saerok_server.domain.ad.core.entity.Ad; |
| 17 | +import org.devkor.apu.saerok_server.domain.ad.core.entity.AdEventType; |
| 18 | +import org.devkor.apu.saerok_server.global.shared.infra.ImageDomainService; |
| 19 | +import org.springframework.web.bind.annotation.GetMapping; |
| 20 | +import org.springframework.web.bind.annotation.PathVariable; |
| 21 | +import org.springframework.web.bind.annotation.PostMapping; |
| 22 | +import org.springframework.web.bind.annotation.RequestBody; |
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 24 | +import org.springframework.web.bind.annotation.RestController; |
| 25 | + |
| 26 | +@Tag(name = "Ad API", description = "클라이언트 광고 노출 및 이벤트 API") |
| 27 | +@RestController |
| 28 | +@RequiredArgsConstructor |
| 29 | +@RequestMapping("${api_prefix}/ad") |
| 30 | +public class AdController { |
| 31 | + |
| 32 | + private final AdSelectorService adSelectorService; |
| 33 | + private final AdEventService adEventService; |
| 34 | + private final ImageDomainService imageDomainService; |
| 35 | + |
| 36 | + @GetMapping("/slots/{slotName}") |
| 37 | + @Operation( |
| 38 | + summary = "슬롯 단위 광고 요청", |
| 39 | + description = """ |
| 40 | + 주어진 슬롯 이름에 대해 광고를 하나 선택해서 반환합니다.<br> |
| 41 | + - 슬롯의 fallbackRatio에 따라 FALLBACK 또는 AD를 응답합니다.<br> |
| 42 | + - AD인 경우 이미지 URL과 클릭 시 이동할 URL을 함께 내려줍니다. |
| 43 | + """, |
| 44 | + responses = { |
| 45 | + @ApiResponse(responseCode = "200", description = "응답 성공", |
| 46 | + content = @Content(schema = @Schema(implementation = GetAdSlotResponse.class))), |
| 47 | + @ApiResponse(responseCode = "404", description = "슬롯을 찾을 수 없음", content = @Content) |
| 48 | + } |
| 49 | + ) |
| 50 | + public GetAdSlotResponse getSlotAd( |
| 51 | + @PathVariable String slotName |
| 52 | + ) { |
| 53 | + AdSelectionResult result = adSelectorService.selectAdForSlot(slotName); |
| 54 | + |
| 55 | + if (result.isFallback()) { |
| 56 | + return new GetAdSlotResponse("FALLBACK", result.ttlSeconds(), null); |
| 57 | + } |
| 58 | + |
| 59 | + Ad ad = result.ad(); |
| 60 | + String imageUrl = imageDomainService.toUploadImageUrl(ad.getObjectKey()); |
| 61 | + |
| 62 | + GetAdSlotResponse.AdPayload payload = new GetAdSlotResponse.AdPayload( |
| 63 | + ad.getId(), |
| 64 | + imageUrl, |
| 65 | + ad.getTargetUrl() |
| 66 | + ); |
| 67 | + |
| 68 | + return new GetAdSlotResponse("AD", result.ttlSeconds(), payload); |
| 69 | + } |
| 70 | + |
| 71 | + @PostMapping("/event/impression") |
| 72 | + @Operation( |
| 73 | + summary = "광고 노출 이벤트 기록", |
| 74 | + description = """ |
| 75 | + 광고 이미지가 실제로 렌더링되었을 때 호출해 주세요.<br> |
| 76 | + deviceId는 서버에서 SHA-256 해싱 후 deviceHash로 저장합니다.<br> |
| 77 | + 동일 기기에서 10초 내에 같은 슬롯의 같은 광고가 노출된 기록은 중복 기록되지 않습니다. |
| 78 | + """, |
| 79 | + responses = { |
| 80 | + @ApiResponse(responseCode = "200", description = "기록 성공", |
| 81 | + content = @Content(schema = @Schema(implementation = AdEventStatusResponse.class))), |
| 82 | + @ApiResponse(responseCode = "400", description = "잘못된 요청", content = @Content), |
| 83 | + @ApiResponse(responseCode = "404", description = "광고를 찾을 수 없음", content = @Content) |
| 84 | + } |
| 85 | + ) |
| 86 | + public AdEventStatusResponse logImpression( |
| 87 | + @Valid @RequestBody AdEventRequest request |
| 88 | + ) { |
| 89 | + adEventService.logEvent( |
| 90 | + AdEventType.IMPRESSION, |
| 91 | + request.adId(), |
| 92 | + request.slotName(), |
| 93 | + request.deviceId() |
| 94 | + ); |
| 95 | + return new AdEventStatusResponse("ok"); |
| 96 | + } |
| 97 | + |
| 98 | + @PostMapping("/event/click") |
| 99 | + @Operation( |
| 100 | + summary = "광고 클릭 이벤트 기록", |
| 101 | + description = """ |
| 102 | + 광고 클릭 시 호출해 주세요.<br> |
| 103 | + deviceId는 서버에서 SHA-256 해싱 후 deviceHash로 저장합니다.<br> |
| 104 | + 동일 기기에서 10초 내에 같은 슬롯의 같은 광고를 클릭한 기록은 중복 기록되지 않습니다. |
| 105 | + """, |
| 106 | + responses = { |
| 107 | + @ApiResponse(responseCode = "200", description = "기록 성공", |
| 108 | + content = @Content(schema = @Schema(implementation = AdEventStatusResponse.class))), |
| 109 | + @ApiResponse(responseCode = "400", description = "잘못된 요청", content = @Content), |
| 110 | + @ApiResponse(responseCode = "404", description = "광고를 찾을 수 없음", content = @Content) |
| 111 | + } |
| 112 | + ) |
| 113 | + public AdEventStatusResponse logClick( |
| 114 | + @Valid @RequestBody AdEventRequest request |
| 115 | + ) { |
| 116 | + adEventService.logEvent( |
| 117 | + AdEventType.CLICK, |
| 118 | + request.adId(), |
| 119 | + request.slotName(), |
| 120 | + request.deviceId() |
| 121 | + ); |
| 122 | + return new AdEventStatusResponse("ok"); |
| 123 | + } |
| 124 | +} |
0 commit comments