Commit 18188772 authored by nanahira's avatar nanahira

support preview ad

parent f7ee1b3c
Pipeline #17964 passed with stages
in 3 minutes and 46 seconds
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
import {
Controller,
DefaultValuePipe,
Get,
Param,
ParseIntPipe,
Query,
} from '@nestjs/common';
import { AdService } from './ad.service';
import { ApiOkResponse, ApiOperation, ApiParam } from '@nestjs/swagger';
import {
ApiOkResponse,
ApiOperation,
ApiParam,
ApiQuery,
} from '@nestjs/swagger';
import { ReturnMessageDto, StringReturnMessageDto } from 'nicot';
import { Ad } from '../entities/ad.entity';
......@@ -12,9 +24,12 @@ export class AdController {
@Get('random')
@ApiOperation({ summary: 'Get ad' })
@ApiQuery({ name: 'id', type: Number, required: false })
@ApiOkResponse({ type: AdReturnMessageDto })
async getAd() {
return new AdReturnMessageDto(200, 'success', await this.ad.getAd());
async getAd(
@Query('id', new DefaultValuePipe(0), new ParseIntPipe()) id: number,
) {
return new AdReturnMessageDto(200, 'success', await this.ad.getAd(id));
}
@Get('click/:id')
......
......@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { BlankReturnMessageDto, CrudService } from 'nicot';
import { Ad } from '../entities/ad.entity';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import { DataSource, FindOptionsWhere } from 'typeorm';
@Injectable()
export class AdService extends CrudService(Ad) {
......@@ -10,11 +10,10 @@ export class AdService extends CrudService(Ad) {
super(db.getRepository(Ad));
}
async getAd() {
async getAd(id?: number) {
const cond: FindOptionsWhere<Ad> = id ? { id } : { enabled: true };
const activeAds = await this.repo.find({
where: {
enabled: true,
},
where: cond,
select: {
id: true,
content: true,
......@@ -35,7 +34,7 @@ export class AdService extends CrudService(Ad) {
async clickAd(id: number) {
const ad = await this.repo.findOne({
where: {
enabled: true,
// enabled: true,
id,
},
select: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment