|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Get, |
| 4 | + Param, |
| 5 | + Query, |
| 6 | + NotFoundException, |
| 7 | + ParseIntPipe, |
| 8 | + UseGuards, |
| 9 | +} from "@nestjs/common"; |
| 10 | +import { InjectRepository } from "@nestjs/typeorm"; |
| 11 | +import { Repository } from "typeorm"; |
| 12 | +import { CacheService } from "../../cache/cache.service"; |
| 13 | +import { RaffleEntity } from "../../database/entities/raffle.entity"; |
| 14 | +import { TicketEntity } from "../../database/entities/ticket.entity"; |
| 15 | +import { ApiKeyGuard } from "../api-key.guard"; |
| 16 | + |
| 17 | +export interface RaffleListQuery { |
| 18 | + status?: string; |
| 19 | + creator?: string; |
| 20 | + asset?: string; |
| 21 | + category?: string; |
| 22 | + limit?: string; |
| 23 | + offset?: string; |
| 24 | +} |
| 25 | + |
| 26 | +@UseGuards(ApiKeyGuard) |
| 27 | +@Controller("raffles") |
| 28 | +export class RafflesController { |
| 29 | + constructor( |
| 30 | + @InjectRepository(RaffleEntity) |
| 31 | + private readonly raffleRepo: Repository<RaffleEntity>, |
| 32 | + @InjectRepository(TicketEntity) |
| 33 | + private readonly ticketRepo: Repository<TicketEntity>, |
| 34 | + private readonly cacheService: CacheService, |
| 35 | + ) {} |
| 36 | + |
| 37 | + /** |
| 38 | + * GET /raffles |
| 39 | + * List raffles with optional filters and pagination. |
| 40 | + * Uses cache for the active-raffle list; falls back to PostgreSQL. |
| 41 | + */ |
| 42 | + @Get() |
| 43 | + async list(@Query() query: RaffleListQuery) { |
| 44 | + const limit = Math.min(parseInt(query.limit ?? "20", 10), 100); |
| 45 | + const offset = parseInt(query.offset ?? "0", 10); |
| 46 | + |
| 47 | + // Serve from cache when querying active raffles with no other filters |
| 48 | + const isActiveOnlyQuery = |
| 49 | + (!query.status || query.status === "open") && |
| 50 | + !query.creator && |
| 51 | + !query.asset && |
| 52 | + !query.category && |
| 53 | + offset === 0 && |
| 54 | + limit === 20; |
| 55 | + |
| 56 | + if (isActiveOnlyQuery) { |
| 57 | + const cached = await this.cacheService.getActiveRaffles(); |
| 58 | + if (cached) return cached; |
| 59 | + } |
| 60 | + |
| 61 | + const qb = this.raffleRepo |
| 62 | + .createQueryBuilder("r") |
| 63 | + .orderBy("r.createdAt", "DESC") |
| 64 | + .limit(limit) |
| 65 | + .offset(offset); |
| 66 | + |
| 67 | + if (query.status) qb.andWhere("r.status = :status", { status: query.status }); |
| 68 | + if (query.creator) qb.andWhere("r.creator = :creator", { creator: query.creator }); |
| 69 | + if (query.asset) qb.andWhere("r.asset = :asset", { asset: query.asset }); |
| 70 | + |
| 71 | + const [items, total] = await qb.getManyAndCount(); |
| 72 | + |
| 73 | + const result = { |
| 74 | + data: items.map(this.formatRaffle), |
| 75 | + total, |
| 76 | + limit, |
| 77 | + offset, |
| 78 | + }; |
| 79 | + |
| 80 | + if (isActiveOnlyQuery) { |
| 81 | + await this.cacheService.setActiveRaffles(result); |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * GET /raffles/:id |
| 89 | + * Raffle detail — cache-first, PostgreSQL fallback. |
| 90 | + */ |
| 91 | + @Get(":id") |
| 92 | + async detail(@Param("id", ParseIntPipe) id: number) { |
| 93 | + const cached = await this.cacheService.getRaffleDetail(String(id)); |
| 94 | + if (cached) return cached; |
| 95 | + |
| 96 | + const raffle = await this.raffleRepo.findOne({ where: { id } }); |
| 97 | + if (!raffle) throw new NotFoundException(`Raffle ${id} not found`); |
| 98 | + |
| 99 | + const ticketCount = await this.ticketRepo.count({ where: { raffleId: id } }); |
| 100 | + |
| 101 | + const result = { |
| 102 | + ...this.formatRaffle(raffle), |
| 103 | + participant_count: ticketCount, |
| 104 | + }; |
| 105 | + |
| 106 | + await this.cacheService.setRaffleDetail(String(id), result); |
| 107 | + return result; |
| 108 | + } |
| 109 | + |
| 110 | + private formatRaffle(r: RaffleEntity) { |
| 111 | + return { |
| 112 | + id: r.id, |
| 113 | + creator: r.creator, |
| 114 | + status: r.status, |
| 115 | + ticket_price: r.ticketPrice, |
| 116 | + asset: r.asset, |
| 117 | + max_tickets: r.maxTickets, |
| 118 | + tickets_sold: r.ticketsSold, |
| 119 | + end_time: r.endTime, |
| 120 | + winner: r.winner, |
| 121 | + prize_amount: r.prizeAmount, |
| 122 | + created_ledger: r.createdLedger, |
| 123 | + finalized_ledger: r.finalizedLedger, |
| 124 | + metadata_cid: r.metadataCid, |
| 125 | + created_at: r.createdAt, |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
0 commit comments