-
Notifications
You must be signed in to change notification settings - Fork 0
feat: guilds & users get #8
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
Open
Khayal-Dev
wants to merge
2
commits into
rest-dev
Choose a base branch
from
feat-rest/users-guilds-get
base: rest-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| return await this.guildsService.getGuildsByOffset(offset) | ||
| } | ||
|
|
||
| @Get(":id") | ||
| async getGuild(@Param(`id`) id: bigint) { | ||
| return await this.guildsService.getGuild(id) | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type topType = "points" | "published challenges" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need OwnerOnly middleware