Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
feat(rendu): add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicGendre committed Jan 27, 2022
1 parent 3a51f10 commit a493b5e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ lerna-debug.log*
**/docker/volumes

# Prisma
**/prisma/migrations
# **/prisma/migrations
1 change: 1 addition & 0 deletions prisma/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const seedAccounts = async (): Promise<void> => {
username: 'Test',
birthDate: Faker.date.between('1985-01-01', '2000-01-05'),
email: '[email protected]',
isEmailConfirmed: true,
city: { connect: random.arrayElement(cities.map((m) => ({ id: m.id }))) },
sex: 1,
latitude: 44.837789 + generateRandomFloatInRange(-0.5, 0.5),
Expand Down
103 changes: 103 additions & 0 deletions prisma/migrations/20220127102540_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
-- CreateTable
CREATE TABLE "Account" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"password" TEXT NOT NULL,
"username" TEXT NOT NULL,

CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "City" (
"name" TEXT NOT NULL,
"postalCode" TEXT NOT NULL,
"id" UUID NOT NULL DEFAULT gen_random_uuid(),

CONSTRAINT "City_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"firstName" TEXT NOT NULL,
"lastName" TEXT NOT NULL,
"username" TEXT NOT NULL,
"birthDate" TIMESTAMPTZ(6),
"email" TEXT NOT NULL,
"sex" INTEGER NOT NULL DEFAULT 0,
"latitude" DOUBLE PRECISION,
"isEmailConfirmed" BOOLEAN NOT NULL DEFAULT false,
"longitude" DOUBLE PRECISION,
"avatarId" UUID,
"cityId" UUID NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Breed" (
"name" TEXT NOT NULL,
"id" UUID NOT NULL DEFAULT gen_random_uuid(),

CONSTRAINT "Breed_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Animal" (
"name" TEXT,
"sex" INTEGER NOT NULL DEFAULT 0,
"height" INTEGER NOT NULL,
"weight" INTEGER,
"description" TEXT NOT NULL,
"castrated" BOOLEAN NOT NULL,
"birthYear" INTEGER NOT NULL,
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"userId" UUID NOT NULL,

CONSTRAINT "Animal_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "File" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"fileName" TEXT NOT NULL,
"data" BYTEA NOT NULL,

CONSTRAINT "File_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "_AnimalToBreed" (
"A" UUID NOT NULL,
"B" UUID NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "Account_username_key" ON "Account"("username");

-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "_AnimalToBreed_AB_unique" ON "_AnimalToBreed"("A", "B");

-- CreateIndex
CREATE INDEX "_AnimalToBreed_B_index" ON "_AnimalToBreed"("B");

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_avatarId_fkey" FOREIGN KEY ("avatarId") REFERENCES "File"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_cityId_fkey" FOREIGN KEY ("cityId") REFERENCES "City"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Animal" ADD CONSTRAINT "Animal_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_AnimalToBreed" ADD FOREIGN KEY ("A") REFERENCES "Animal"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_AnimalToBreed" ADD FOREIGN KEY ("B") REFERENCES "Breed"("id") ON DELETE CASCADE ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
1 change: 1 addition & 0 deletions prisma/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const seedUsers = async (): Promise<void> => {
username: Faker.name.findName(),
birthDate: Faker.date.between('1985-01-01', '2000-01-05'),
email: Faker.internet.email(),
isEmailConfirmed: false,
city: { connect: random.arrayElement(cities.map((m) => ({ id: m.id }))) },
sex: 1,
latitude: 44.837789 + generateRandomFloatInRange(-0.5, 0.5),
Expand Down
15 changes: 14 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { AccountDto, CreateUserDto, AuthTokenDto, UserDto } from '../dtos';
import { AuthService } from './auth.service';
import { UserService } from '../user/user.service';
import { EmailConfirmationService } from 'src/email/confirmation/emailConfirmation.service';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiCreatedResponse,
ApiOkResponse,
} from '@nestjs/swagger';

@Controller('/auth')
export class AuthController {
Expand All @@ -16,6 +22,11 @@ export class AuthController {

@Post('/register')
@HttpCode(HttpStatus.CREATED)
@ApiCreatedResponse({ description: 'The account has been created' })
@ApiConflictResponse({
description: 'The email already exists',
})
@ApiBadRequestResponse({ description: 'Validation error' })
public async register(
//FIXME
@Body() body: CreateUserDto & { password: string },
Expand All @@ -33,9 +44,11 @@ export class AuthController {
}

@Post('/login')
@ApiOkResponse({ description: 'Login successful' })
@ApiConflictResponse({ description: 'The email already exists' })
@ApiBadRequestResponse({ description: 'Validation error' })
public async login(@Body() body: AccountDto): Promise<AuthTokenDto> {
const { username, password } = body;

return this.authService.login(username, password);
}
}
11 changes: 8 additions & 3 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Req,
Res,
StreamableFile,
UseGuards,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { User } from '@prisma/client';
Expand All @@ -23,14 +24,17 @@ import { UserService } from './user.service';

import { createParamDecorator } from '@nestjs/common';
import { Readable } from 'stream';
import { AuthGuard } from 'src/guards/auth.guard';

export const AuthUser = createParamDecorator((data, req) => {
return req.user;
});

@Controller('/users')
export class UserController {
constructor(private readonly userService: UserService) {}

@UseGuards(AuthGuard)
@Get('/')
async list(@Query() query: FilterDto<User> | undefined): Promise<User[]> {
return await this.userService.list({
Expand All @@ -41,15 +45,16 @@ export class UserController {
});
}

// @UseGuards(AuthGuard)
@UseGuards(AuthGuard)
@Get('/proximity')
async getUsersInRadius(
@Query('radius') radius: string,
@Request() req: any,
): Promise<User[]> {
console.log(req);
const id = 'fa045751-2a94-4a70-8643-bd58f84d7fe1';
const currentUser = await this.userService.get({
username: req.user.username,
id: id,
});
return await this.userService.getUsersInRadius(radius, currentUser);
}
Expand All @@ -66,7 +71,7 @@ export class UserController {
@UploadedFile() file: Express.Multer.File,
) {
return this.userService.addAvatar(
'34d2d1f0-3028-4b89-98ff-6f8df08e9f9d',
'fa045751-2a94-4a70-8643-bd58f84d7fe1',
file.buffer,
file.originalname,
);
Expand Down

0 comments on commit a493b5e

Please sign in to comment.