Tuto part 53-58

This commit is contained in:
Mathis H (Avnyr) 2024-02-23 16:08:59 +01:00
parent 134afde21c
commit 3f61a16324
Signed by: Mathis
GPG Key ID: 9B3849C18C153DDD
21 changed files with 205 additions and 51 deletions

View File

@ -1,2 +1,3 @@
# template-nestjs
53:58

31
docker-compose.yaml Normal file
View File

@ -0,0 +1,31 @@
version: '3'
services:
database:
image: 'postgres:latest'
ports:
- 15432:5432
env_file:
- .env
networks:
- postgres-network
volumes:
- ./db-data/:/var/lib/postgresql/data/
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
pgadmin:
image: dpage/pgadmin4
ports:
- 15433:80
env_file:
- .env
depends_on:
- database
networks:
- postgres-network
volumes:
- ./pgadmin-data/:/var/lib/pgadmin/
networks:
postgres-network:
driver: bridge

11
init.sql Normal file
View File

@ -0,0 +1,11 @@
-- create a table
CREATE TABLE test(
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name TEXT NOT NULL,
archived BOOLEAN NOT NULL DEFAULT FALSE
);
-- add test data
INSERT INTO test (name, archived)
VALUES ('test row 1', true),
('test row 2', false);

View File

@ -1,13 +1,13 @@
{
"name": "template",
"name": "bookmarks-back",
"version": "0.0.1",
"description": "",
"author": "",
"author": "Mathis Herriot",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "biome --apply src test",
"format": "biome format src test",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
@ -23,6 +23,7 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@prisma/client": "^5.10.2",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
@ -36,6 +37,7 @@
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"jest": "^29.5.0",
"prisma": "^5.10.2",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",

View File

@ -0,0 +1,24 @@
-- CreateTable
CREATE TABLE `User` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` DATETIME(3) NOT NULL,
`email` VARCHAR(191) NOT NULL,
`hash` VARCHAR(191) NOT NULL,
`firstName` VARCHAR(191) NULL,
`lastName` VARCHAR(191) NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateTable
CREATE TABLE `Bookmark` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` DATETIME(3) NOT NULL,
`title` VARCHAR(191) NOT NULL,
`description` VARCHAR(191) NULL,
`link` VARCHAR(191) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

View File

@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "mysql"

36
prisma/schema.prisma Normal file
View File

@ -0,0 +1,36 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String
hash String
firstName String?
lastName String?
}
model Bookmark {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
description String?
link String
}

View File

@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

View File

@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

View File

@ -1,10 +1,14 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { BookmarkModule } from './bookmark/bookmark.module';
import { AuthService } from './auth/auth.service';
import { AuthController } from './auth/auth.controller';
import { PrismaModule } from './prisma/prisma.module';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [AuthModule, PrismaModule, UserModule, BookmarkModule],
providers: [AuthService],
controllers: [AuthController]
})
export class AppModule {}

View File

@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

View File

@ -0,0 +1,20 @@
import { Body, Controller, Post } from "@nestjs/common";
import { AuthService } from "./auth.service";
import { AuthDto } from "./dto";
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
@Post('signup')
signup(@Body() dto: AuthDto) {
console.log({dto});
return this.authService.signup()
}
@Post('signin')
signin(@Body() dto: AuthDto) {
console.log({dto});
return this.authService.signin()
}
}

11
src/auth/auth.module.ts Normal file
View File

@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";
import { PrismaModule } from "src/prisma/prisma.module";
import { AuthController } from "./auth.controller";
import { AuthService } from "./auth.service";
@Module({
imports: [PrismaModule],
controllers: [AuthController],
providers: [AuthService]
})
export class AuthModule {}

17
src/auth/auth.service.ts Normal file
View File

@ -0,0 +1,17 @@
import { Injectable } from "@nestjs/common";
import { PrismaService } from "src/prisma/prisma.service";
@Injectable({})
export class AuthService {
constructor(private prisma: PrismaService) {
}
signin() {
return {response: "Sign IN"}
}
signup() {
return {response: "Sign UP"}
}
}

4
src/auth/dto/auth.dto.ts Normal file
View File

@ -0,0 +1,4 @@
export interface AuthDto {
email: string,
password: string
}

1
src/auth/dto/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './auth.dto';

View File

@ -0,0 +1,4 @@
import { Module } from '@nestjs/common';
@Module({})
export class BookmarkModule {}

View File

@ -3,6 +3,6 @@ import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
await app.listen(3333);
}
bootstrap();

View File

@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Module({
providers: [PrismaService],
exports: [PrismaService]
})
export class PrismaModule {}

View File

@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient {
constructor() {
super({
datasources: {
db: {
url: "mysql://avnyr:orpmocclealis8havele@127.0.0.1:3306/bookmarks"
}
}
})
}
}

4
src/user/user.module.ts Normal file
View File

@ -0,0 +1,4 @@
import { Module } from '@nestjs/common';
@Module({})
export class UserModule {}