Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rates API #51

Merged
merged 18 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "otl-nest",
"version": "0.0.1",
"version": "3.3.1",
"description": "",
"author": "",
"private": true,
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FeedsModule } from './modules/feeds/feeds.module';
import { LecturesModule } from './modules/lectures/lectures.module';
import { NoticesModule } from './modules/notices/notices.module';
import { PlannersModule } from './modules/planners/planners.module';
import { RatesModule } from './modules/rates/rates.module';
import { ReviewsModule } from './modules/reviews/reviews.module';
import { SemestersModule } from './modules/semesters/semesters.module';
import { SessionModule } from './modules/session/session.module';
Expand All @@ -32,6 +33,7 @@ import { PrismaModule } from './prisma/prisma.module';
UserModule,
SemestersModule,
TimetablesModule,
RatesModule,
StatusModule,
FeedsModule,
WishlistModule,
Expand Down
20 changes: 20 additions & 0 deletions src/common/interfaces/IRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Type } from 'class-transformer';
import { IsNumber, Max, Min } from 'class-validator';

export namespace IRate {
export interface Basic {
id: number;
user_id: number;
score: number;
version: string;
created_datetime: Date | null;
}

export class CreateDto {
@IsNumber()
@Max(5)
@Min(1)
@Type(() => Number)
score!: number;
}
}
12 changes: 12 additions & 0 deletions src/common/interfaces/serializer/rate.serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { support_rate } from '@prisma/client';
import { IRate } from '../IRate';

export function toJsonRate(rate: support_rate): IRate.Basic {
return {
id: rate.id,
score: rate.score,
user_id: rate.user_id,
version: rate.version,
created_datetime: rate.created_datetime,
};
}
21 changes: 18 additions & 3 deletions src/modules/rates/rates.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { Controller } from '@nestjs/common';
import { Body, Controller, Post } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import { GetUser } from 'src/common/decorators/get-user.decorator';
import { IRate } from 'src/common/interfaces/IRate';
import { toJsonRate } from 'src/common/interfaces/serializer/rate.serializer';
import { RatesService } from './rates.service';

@Controller('rates')
export class RatesController {}
@Controller('api/rates')
export class RatesController {
constructor(private readonly ratesService: RatesService) {}
@Post()
async createRates(
@Body() body: IRate.CreateDto,
@GetUser() user: session_userprofile,
): Promise<IRate.Basic> {
const rate = await this.ratesService.createRate(body, user);
return toJsonRate(rate);
}
}
5 changes: 4 additions & 1 deletion src/modules/rates/rates.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from 'src/prisma/prisma.module';
import { RateRepository } from 'src/prisma/repositories/rates.repository';
import { RatesController } from './rates.controller';
import { RatesService } from './rates.service';

@Module({
imports: [PrismaModule],
controllers: [RatesController],
providers: [RatesService],
providers: [RatesService, RateRepository],
})
export class RatesModule {}
31 changes: 29 additions & 2 deletions src/modules/rates/rates.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { session_userprofile, support_rate } from '@prisma/client';
import { IRate } from 'src/common/interfaces/IRate';
import { RateRepository } from 'src/prisma/repositories/rates.repository';
import settings from 'src/settings';

@Injectable()
export class RatesService {}
export class RatesService {
constructor(private readonly rateRepository: RateRepository) {}
async createRate(
body: IRate.CreateDto,
user: session_userprofile,
): Promise<support_rate> {
const { score } = body;
const year = new Date().getFullYear();
const version = settings().getVersion();

const rate = await this.rateRepository.getRate(user.id, year, version);
if (rate) {
throw new BadRequestException('You already rated for current year');
}

const newRate = await this.rateRepository.createRate(
user.id,
score,
year,
version,
);
return newRate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Warnings:
- Made the column `created_datetime` on table `support_rate` required. This step will fail if there are existing NULL values in that column.
*/
-- AlterTable
ALTER TABLE `support_rate`
MODIFY `created_datetime` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0);
33 changes: 33 additions & 0 deletions src/prisma/repositories/rates.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma.service';

@Injectable()
export class RateRepository {
constructor(private readonly prisma: PrismaService) {}

async getRate(userId: number, year: number, version: string) {
return this.prisma.support_rate.findFirst({
where: {
user_id: userId,
year,
version,
},
});
}

async createRate(
userId: number,
score: number,
year: number,
version: string,
) {
return this.prisma.support_rate.create({
data: {
user_id: userId,
year,
score,
version,
},
});
}
}
2 changes: 1 addition & 1 deletion src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ model support_rate {
id Int @id @default(autoincrement())
score Int @db.SmallInt
year Int @db.SmallInt
created_datetime DateTime? @db.DateTime(0)
created_datetime DateTime @default(now()) @db.DateTime(0)
user_id Int
version String @db.VarChar(20)
session_userprofile session_userprofile @relation(fields: [user_id], references: [id], onUpdate: Restrict, map: "support_rate_user_id_6d69ec9d_fk_session_userprofile_id")
Expand Down
5 changes: 5 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default () => {
getJwtConfig: () => getJwtConfig(),
getSsoConfig: () => getSsoConfig(),
getCorsConfig: () => getCorsConfig(),
getVersion: () => getVersion(),
};
};

Expand Down Expand Up @@ -72,3 +73,7 @@ const getSsoConfig = (): any => {
ssoSecretKey: process.env.SSO_SECRET_KEY,
};
};

const getVersion = () => {
return String(process.env.npm_package_version);
};
Loading