Skip to content
Open
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
9 changes: 8 additions & 1 deletion apps/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"db:gen": "prisma generate",
"db:pull": "prisma db pull",
"db:push": "prisma db push"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-fastify": "9.3.12",
"@prisma/client": "4.10.1",
"fastify": "4.15.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
Expand All @@ -41,6 +47,7 @@
"eslint-plugin-prettier": "^4.0.0",
"jest": "29.3.1",
"prettier": "^2.3.2",
"prisma": "4.10.1",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "29.0.3",
Expand Down
83 changes: 83 additions & 0 deletions apps/rest/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Guild {
id BigInt @unique()
settings Json
@@map("guilds")
}

model User {
id BigInt @id @unique()
points Int
solved_challenges SolvedChallenge[]
published_challenges Challenge[]
Auth Auth?
@@map("users")
}

model Challenge {
id BigInt @id @unique()
title String
difficulty Difficulties
tags String[]
description String
status String
created_at DateTime
end_at DateTime
points Int
author User @relation(fields: [author_id], references: [id])
author_id BigInt @unique()
solvers SolvedChallenge[]
unit_tests UnitTest[]
@@map("challenges")
}

model Auth {
user User @relation(fields: [user_id], references: [id])
user_id BigInt @unique()
access_token String @unique()
refresh_token String @unique()
@@map("auths")
}

model SolvedChallenge {
solver User @relation(fields: [solver_id], references: [id])
solver_id BigInt @unique()
challenge Challenge @relation(fields: [challenge_id], references: [id])
challenge_id BigInt @unique()
@@map("solved_challenges")
}

model UnitTest {
id BigInt @id @unique()
name String
challenge Challenge @relation(fields: [challenge_id], references: [id])
challenge_id BigInt @unique()
languages Language[]
difficulty Difficulties
code String
@@map("unit_tests")
}

model Language {
name String @id @unique()
runtime_engine String
unit_tests UnitTest[]
@@map("languages")
}

enum Difficulties {
Easy
medium
Hard
}
22 changes: 0 additions & 22 deletions apps/rest/src/app.controller.spec.ts

This file was deleted.

21 changes: 14 additions & 7 deletions apps/rest/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { UsersModule } from './users/users.module'
import { GuildsModule } from './guilds/guilds.module'
import { AuthMiddleware } from 'middlewares/auth.middleware'

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [UsersModule, GuildsModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes({ path: '*', method: RequestMethod.ALL })
}
}
18 changes: 18 additions & 0 deletions apps/rest/src/guilds/guilds.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, Get, Param, Query } from '@nestjs/common'
import { GuildsService } from './guilds.service'

@Controller('guilds')
export class GuildsController {
constructor(private readonly guildsService: GuildsService) { }

@Get()
async getGuildsByOffset(@Query(`offset`) offset: number) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need OwnerOnly middleware

return await this.guildsService.getGuildsByOffset(offset)
}

@Get(":id")
async getGuild(@Param(`id`) id: bigint) {
return await this.guildsService.getGuild(id)
}

}
9 changes: 9 additions & 0 deletions apps/rest/src/guilds/guilds.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { GuildsController } from './guilds.controller';
import { GuildsService } from './guilds.service';

@Module({
controllers: [GuildsController],
providers: [GuildsService]
})
export class GuildsModule {}
45 changes: 45 additions & 0 deletions apps/rest/src/guilds/guilds.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'
import { Guild } from '@prisma/client'
import prismaClient from 'prisma'

const prisma = prismaClient

@Injectable()
export class GuildsService {

// Get all Guilds by offset
async getGuildsByOffset(offset: number): Promise<Guild[] | HttpException | undefined> {
// Find all Guilds by offset
const guilds = await prisma.guild.findMany({
where: {

},
take: offset * 50,
skip: offset === 1 ? 0 : offset * 50
})

// If aren't Guilds in DB then return HttpException
if (guilds.length === 0) return new HttpException("There aren't any guilds in DB", HttpStatus.NOT_FOUND)

// If there're Guilds in DB then return Guilds
return guilds

}

// Get Guild from DB
async getGuild(id: bigint): Promise<Guild | HttpException | undefined> {
// Find Guild by id
const guild = await prisma.guild.findUnique({
where: {
id: id
}
})

// If Guild in DB then return Guild
if (guild) return guild

// If Guild isn't in DB then return HttpException
if (!guild) return new HttpException(`Cannot find guild with id: ${id}`, HttpStatus.NOT_FOUND)
}

}
20 changes: 15 additions & 5 deletions apps/rest/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core'
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify'
import { AppModule } from './app.module'

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
// const app = await NestFactory.create<NestFastifyApplication>(
// AppModule,
// new FastifyAdapter(),
// )
const app = await NestFactory.create(
AppModule
)
await app.listen(3000, '0.0.0.0')
}
bootstrap();
bootstrap()
10 changes: 10 additions & 0 deletions apps/rest/src/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Injectable, NestMiddleware } from '@nestjs/common'
import { FastifyRequest, FastifyReply } from 'fastify'

@Injectable()
export class AuthMiddleware implements NestMiddleware {
use(req: FastifyRequest, res: FastifyReply, next: () => void) {
console.log(req.headers)
next()
}
}
5 changes: 5 additions & 0 deletions apps/rest/src/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {PrismaClient} from "@prisma/client"

const prismaClient = new PrismaClient()

export default prismaClient
1 change: 1 addition & 0 deletions apps/rest/src/users/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type topType = "points" | "published challenges"
26 changes: 26 additions & 0 deletions apps/rest/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Body, Controller, Get, HttpException, Param, Post, Query } from '@nestjs/common';
import { User } from '@prisma/client'
import { topType } from './types/types';
import { UsersService } from './users.service'

@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) { }

@Get()
async getUsersByOffset(@Query(`offset`) offset: number): Promise<User[] | HttpException | undefined> {
return await this.usersService.getUsersByOffset(offset)
}

@Get(":id")
async getUser(@Param(`id`) id: bigint): Promise<User | HttpException | undefined> {
return await this.usersService.getUser(id)
}

@Get()
async getTopUsers(@Query(`top_type`) top_type: topType, @Query(`offset`) offset: number): Promise<User[] | HttpException | undefined> {
return await this.usersService.getTopUsers(top_type,offset)
}


}
9 changes: 9 additions & 0 deletions apps/rest/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
controllers: [UsersController],
providers: [UsersService]
})
export class UsersModule {}
Loading