ad.controller.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // apps/box-app-api/src/feature/ads/ad.controller.ts
  2. import { Controller, Get, Logger, Query } from '@nestjs/common';
  3. import { AdService } from './ad.service';
  4. import { GetAdPlacementQueryDto } from './dto/get-ad-placement.dto';
  5. import { AdDto } from './dto/ad.dto';
  6. @Controller('ads')
  7. export class AdController {
  8. private readonly logger = new Logger(AdController.name);
  9. constructor(private readonly adService: AdService) {}
  10. /**
  11. * GET /ads/placement
  12. *
  13. * Example:
  14. * /ads/placement?scene=home&slot=top&adType=BANNER
  15. *
  16. * Returns a single AdDto or null (if no suitable ad is found).
  17. * Your global response interceptor will wrap this into the standard envelope.
  18. */
  19. @Get('placement')
  20. async getAdForPlacement(
  21. @Query() query: GetAdPlacementQueryDto,
  22. ): Promise<AdDto | null> {
  23. const { scene, slot, adType, maxTries } = query;
  24. const maxTriesNum =
  25. maxTries != null ? Number.parseInt(maxTries, 10) || 3 : 3;
  26. const ad = await this.adService.getAdForPlacement({
  27. scene,
  28. slot,
  29. adType,
  30. maxTries: maxTriesNum,
  31. });
  32. // Optional logging for debugging / analytics:
  33. if (!ad) {
  34. this.logger.debug(
  35. `No ad returned for scene=${scene}, slot=${slot}, adType=${adType}`,
  36. );
  37. }
  38. // Let your global response interceptor wrap this into:
  39. // { status, code, data, error, timestamp, ... }
  40. return ad;
  41. }
  42. }