Skip to content

Commit 22c1bea

Browse files
Mikadowsangristan
authored andcommitted
feat: add benchmark creation
1 parent 24fdaa4 commit 22c1bea

File tree

8 files changed

+134
-0
lines changed

8 files changed

+134
-0
lines changed

src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LoggerModule } from 'nestjs-pino';
77
import { RedisModule } from 'nestjs-redis';
88
import { AppController } from './app.controller';
99
import { AuthModule } from './auth/auth.module';
10+
import { BenchmarkModule } from './benchmarks/benchmark.module';
1011
import { HealthController } from './health/health.controller';
1112
import ormconfig from './ormconfig';
1213
import redisconfig from './redisconfig';
@@ -17,6 +18,7 @@ import { UsersModule } from './users/users.module';
1718
imports: [
1819
ConfigModule.forRoot(),
1920
TypeOrmModule.forRoot(ormconfig),
21+
BenchmarkModule,
2022
AuthModule,
2123
UsersModule,
2224
SubmissionsModule,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Body, Controller, Post } from '@nestjs/common';
2+
import { CreateBenchmarkDto } from 'src/benchmarks/dto/create-benchmark.dto';
3+
import { BenchmarkService } from 'src/benchmarks/benchmark.service';
4+
import { Benchmark } from './benchmark.entity';
5+
6+
7+
@Controller('benchmarks')
8+
export class BenchmarkController {
9+
constructor(private readonly benchmarkService: BenchmarkService) {
10+
}
11+
12+
@Post()
13+
async createBenchmark(
14+
@Body()
15+
benchmark: CreateBenchmarkDto
16+
) : Promise<Benchmark>{
17+
console.log(benchmark);
18+
return this.benchmarkService.create(benchmark);
19+
}
20+
21+
}

src/benchmarks/benchmark.entity.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
import { BaseEntity, Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
3+
import { ApiProperty } from '@nestjs/swagger';
4+
import { User } from 'src/users/user.entity';
5+
6+
@Entity('benchmarks')
7+
export class Benchmark extends BaseEntity {
8+
constructor(partial: Partial<Benchmark>) {
9+
super();
10+
Object.assign(this, partial);
11+
}
12+
13+
@ApiProperty()
14+
@PrimaryGeneratedColumn('uuid')
15+
id: string;
16+
17+
@ApiProperty()
18+
@Column()
19+
title: string;
20+
21+
@ApiProperty()
22+
@Column()
23+
subject: string;
24+
25+
@ApiProperty()
26+
@Column()
27+
giturl: string;
28+
29+
@ApiProperty()
30+
@CreateDateColumn()
31+
createdAt: Date;
32+
33+
@ApiProperty()
34+
@Column()
35+
difficulty: string;
36+
37+
@ManyToOne((type) => User, (user) => user.benchmarks, {eager: true})
38+
@ApiProperty({type: () => User})
39+
creator: User;
40+
41+
}

src/benchmarks/benchmark.module.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { Benchmark } from './benchmark.entity';
4+
import { BenchmarkService } from './benchmark.service';
5+
import { BenchmarkController } from './benchmark.controller';
6+
7+
@Module({
8+
imports: [TypeOrmModule.forFeature([Benchmark])],
9+
providers: [BenchmarkService],
10+
controllers: [BenchmarkController],
11+
exports: [BenchmarkService],
12+
})
13+
export class BenchmarkModule {}

src/benchmarks/benchmark.service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { CreateBenchmarkDto } from './dto/create-benchmark.dto';
3+
import { Benchmark } from './benchmark.entity';
4+
5+
@Injectable()
6+
export class BenchmarkService {
7+
8+
async create(benchmarkData: CreateBenchmarkDto): Promise<Benchmark> {
9+
const benchmark = new Benchmark(benchmarkData);
10+
// TODO : Link benchmark to user
11+
return benchmark.save();
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { IsNotEmpty, IsString } from 'class-validator';
2+
import { ApiProperty } from '@nestjs/swagger';
3+
4+
export class CreateBenchmarkDto {
5+
@IsNotEmpty()
6+
@IsString()
7+
@ApiProperty()
8+
title: string;
9+
10+
@IsNotEmpty()
11+
@IsString()
12+
@ApiProperty()
13+
subject: string;
14+
15+
@IsNotEmpty()
16+
@IsString()
17+
@ApiProperty()
18+
giturl: string;
19+
20+
@IsNotEmpty()
21+
@IsString()
22+
@ApiProperty()
23+
difficulty: string;
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {MigrationInterface, QueryRunner} from "typeorm";
2+
3+
export class AddBenchmarks1621724651623 implements MigrationInterface {
4+
name = 'AddBenchmarks1621724651623'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`CREATE TABLE "benchmarks" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "title" character varying NOT NULL, "subject" character varying NOT NULL, "giturl" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "difficulty" character varying NOT NULL, "creatorId" uuid, CONSTRAINT "PK_7e1ca1c910680770da116e0b95c" PRIMARY KEY ("id"))`);
8+
await queryRunner.query(`ALTER TABLE "benchmarks" ADD CONSTRAINT "FK_f7dfba1f69546feced443cd097c" FOREIGN KEY ("creatorId") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
9+
}
10+
11+
public async down(queryRunner: QueryRunner): Promise<void> {
12+
await queryRunner.query(`ALTER TABLE "benchmarks" DROP CONSTRAINT "FK_f7dfba1f69546feced443cd097c"`);
13+
await queryRunner.query(`DROP TABLE "benchmarks"`);
14+
}
15+
16+
}

src/users/user.entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
import { ApiProperty } from '@nestjs/swagger';
33
import { Exclude } from 'class-transformer';
4+
import { Benchmark } from 'src/benchmarks/benchmark.entity';
45
import { Submission } from 'src/submissions/submission.entity';
56
import {
67
BaseEntity,
@@ -61,6 +62,9 @@ export class User extends BaseEntity {
6162
@OneToMany((type) => Submission, (submission) => submission.user)
6263
submissions: Submission[];
6364

65+
@OneToMany((type) => Benchmark, (benchmark) => benchmark.creator)
66+
benchmarks: Benchmark[];
67+
6468
@BeforeInsert()
6569
@BeforeUpdate()
6670
async hashPassword(): Promise<void> {

0 commit comments

Comments
 (0)