From 1ccbe33932165c89d397fa818e32ca29b2ca7890 Mon Sep 17 00:00:00 2001 From: Czekaj Tom Date: Fri, 5 Jan 2024 00:35:30 +0100 Subject: [PATCH] :sparkles: Add tip of the day --- src/app.module.ts | 3 ++- src/tips/models/entities/tip.entity.ts | 13 +++++++++++++ src/tips/tips.controller.ts | 20 ++++++++++++++++++++ src/tips/tips.module.ts | 12 ++++++++++++ src/tips/tips.service.ts | 19 +++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/tips/models/entities/tip.entity.ts create mode 100644 src/tips/tips.controller.ts create mode 100644 src/tips/tips.module.ts create mode 100644 src/tips/tips.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 0a13b6f..55bcf3b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,8 +5,9 @@ import {ConfigModule} from "@nestjs/config"; import {UsersModule} from "./users/users.module"; import {VerificationCodesModule} from "./verification-codes/verification-codes.module"; import {TodosModule} from "./todos/todos.module"; +import {TipsModule} from "./tips/tips.module"; @Module({ - imports: [AuthModule, VersionModule, ConfigModule.forRoot({isGlobal: true}), UsersModule, VerificationCodesModule, TodosModule], + imports: [AuthModule, VersionModule, ConfigModule.forRoot({isGlobal: true}), UsersModule, VerificationCodesModule, TodosModule, TipsModule], }) export class AppModule{} diff --git a/src/tips/models/entities/tip.entity.ts b/src/tips/models/entities/tip.entity.ts new file mode 100644 index 0000000..e705d38 --- /dev/null +++ b/src/tips/models/entities/tip.entity.ts @@ -0,0 +1,13 @@ +import {Tips} from "@prisma/client"; +import {ApiProperty} from "@nestjs/swagger"; + +export class TipEntity implements Tips{ + @ApiProperty() + id: number; + @ApiProperty() + tips: string; + @ApiProperty() + author: string; + @ApiProperty() + order: number; +} diff --git a/src/tips/tips.controller.ts b/src/tips/tips.controller.ts new file mode 100644 index 0000000..929fd60 --- /dev/null +++ b/src/tips/tips.controller.ts @@ -0,0 +1,20 @@ +import {Controller, Get, HttpStatus, UseGuards} from "@nestjs/common"; +import {ApiBearerAuth, ApiResponse, ApiTags} from "@nestjs/swagger"; +import {AtGuard} from "../auth/guards/at.guard"; +import {TipsService} from "./tips.service"; +import {TipEntity} from "./models/entities/tip.entity"; + +@Controller("tips") +@ApiTags("Tips") +export class TipsController{ + constructor(private readonly tipsService: TipsService){} + + @Get("tod") + @UseGuards(AtGuard) + @ApiBearerAuth() + @ApiResponse({status: HttpStatus.OK, description: "Returns the tip of the day", type: TipEntity}) + @ApiResponse({status: HttpStatus.NOT_FOUND, description: "No tip found for today"}) + async getTipOfTheDay(): Promise{ + return await this.tipsService.getTipOfTheDay(); + } +} diff --git a/src/tips/tips.module.ts b/src/tips/tips.module.ts new file mode 100644 index 0000000..3c40c40 --- /dev/null +++ b/src/tips/tips.module.ts @@ -0,0 +1,12 @@ +import {Module} from "@nestjs/common"; +import {TipsService} from "./tips.service"; +import {TipsController} from "./tips.controller"; +import {ServicesModule} from "../services/services.module"; +import {UsersModule} from "../users/users.module"; + +@Module({ + controllers: [TipsController], + providers: [TipsService], + imports: [ServicesModule, UsersModule] +}) +export class TipsModule{} diff --git a/src/tips/tips.service.ts b/src/tips/tips.service.ts new file mode 100644 index 0000000..41aed6c --- /dev/null +++ b/src/tips/tips.service.ts @@ -0,0 +1,19 @@ +import {Injectable} from "@nestjs/common"; +import {PrismaService} from "../services/prisma.service"; + +@Injectable() +export class TipsService{ + + constructor( + private readonly prismaService: PrismaService + ){} + + async getTipOfTheDay(){ + const day = new Date().getDay(); + return this.prismaService.tips.findFirst({ + where: { + order: day + } + }); + } +}