Skip to content

Commit 9ed5f0f

Browse files
hNeutHugo Troonbeeckx
authored andcommitted
get put post pour la db , mysql a acces a nest
1 parent 22545f2 commit 9ed5f0f

10 files changed

Lines changed: 137 additions & 63 deletions

api/docker-compose.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '3'
2+
3+
services:
4+
mysql:
5+
image: mysql:latest
6+
container_name: iziplan_container
7+
command: --default-authentication-plugin=mysql_native_password
8+
environment:
9+
MYSQL_ROOT_PASSWORD: iziplan_root
10+
MYSQL_DATABASE: iziplan_db
11+
MYSQL_USER: iziplan_user
12+
MYSQL_PASSWORD: iziplan_psw
13+
ports:
14+
- "3333:3306"

api/package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"has-property-descriptors": "^1.0.2",
3434
"has-proto": "^1.0.3",
3535
"has-symbols": "^1.0.3",
36+
"mysql": "^2.18.1",
3637
"mysql2": "^3.9.2",
3738
"object-inspect": "^1.13.1",
3839
"qs": "^6.11.0",
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
1-
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
1+
import { Controller, Get, Post, Body, Patch, Param, Delete , Put } from '@nestjs/common';
22
import { AccueilService } from './accueil.service';
3-
import { CreateAccueilDto } from './dto/create-accueil.dto';
4-
import { UpdateAccueilDto } from './dto/update-accueil.dto';
3+
import { Accueil } from './entities/accueil.entity';
4+
import { AccueilDto } from './dto/accueil.dto';
55

66
@Controller('accueil')
77
export class AccueilController {
88
constructor(private readonly accueilService: AccueilService) {}
99

10-
@Post()
11-
create(@Body() createAccueilDto: CreateAccueilDto) {
12-
return this.accueilService.create(createAccueilDto);
13-
}
14-
1510
@Get()
16-
findAll() {
11+
getAll():Promise<Accueil[]>{
1712
return this.accueilService.findAll();
1813
}
1914

20-
@Get(':id')
21-
findOne(@Param('id') id: string) {
22-
return this.accueilService.findOne(+id);
23-
}
24-
25-
@Patch(':id')
26-
update(@Param('id') id: string, @Body() updateAccueilDto: UpdateAccueilDto) {
27-
return this.accueilService.update(+id, updateAccueilDto);
15+
@Post()
16+
create(@Body() accueilDto:AccueilDto):Promise<Accueil>{
17+
return this.accueilService.create(accueilDto);
2818
}
2919

30-
@Delete(':id')
31-
remove(@Param('id') id: string) {
32-
return this.accueilService.remove(+id);
20+
@Put('/:id')
21+
update(@Param('id') id : number , @Body()accueilDto:AccueilDto): Promise<Accueil>{
22+
return this.accueilService.update(id,accueilDto);
3323
}
3424
}

api/src/accueil/accueil.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { Module } from '@nestjs/common';
22
import { AccueilService } from './accueil.service';
33
import { AccueilController } from './accueil.controller';
4+
import { Accueil } from './entities/accueil.entity';
5+
import { TypeOrmModule } from '@nestjs/typeorm';
46

57
@Module({
8+
imports: [TypeOrmModule.forFeature([Accueil])],
69
controllers: [AccueilController],
710
providers: [AccueilService],
811
})

api/src/accueil/accueil.service.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import { Injectable } from '@nestjs/common';
2-
import { CreateAccueilDto } from './dto/create-accueil.dto';
3-
import { UpdateAccueilDto } from './dto/update-accueil.dto';
2+
import { InjectRepository } from '@nestjs/typeorm';
3+
import { Repository } from 'typeorm';
4+
import { Accueil } from './entities/accueil.entity';
5+
import { AccueilDto } from './dto/accueil.dto';
6+
47

58
@Injectable()
69
export class AccueilService {
7-
create(createAccueilDto: CreateAccueilDto) {
8-
return 'This action adds a new accueil';
9-
}
10-
11-
findAll() {
12-
return `This action returns all accueil`;
13-
}
10+
constructor(
11+
@InjectRepository(Accueil)
12+
private readonly accueilRepository: Repository<Accueil>,
13+
) {}
1414

15-
findOne(id: number) {
16-
return `This action returns a #${id} accueil`;
17-
}
18-
19-
update(id: number, updateAccueilDto: UpdateAccueilDto) {
20-
return `This action updates a #${id} accueil`;
21-
}
15+
async findAll(): Promise<Accueil[]> {
16+
return await this.accueilRepository.find();
17+
}
2218

23-
remove(id: number) {
24-
return `This action removes a #${id} accueil`;
19+
async create(accueilDto : AccueilDto) {
20+
const accueilEntities = new Accueil();
21+
accueilEntities.id = accueilDto.id;
22+
accueilEntities.nom = accueilDto.nom;
23+
accueilEntities.date= accueilDto.date;
24+
accueilEntities.lieu = accueilDto.lieu;
25+
const accueil = this.accueilRepository.create(accueilEntities);
26+
await this.accueilRepository.save(accueil);
27+
return accueil;
28+
29+
}
30+
async update(id : number , data : Partial<Accueil> ){
31+
await this.accueilRepository.update({id},data);
32+
const accueil= this.accueilRepository.findOne({where:{id}})
33+
return accueil
34+
}
2535
}
26-
}

api/src/accueil/dto/accueil.dto.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class AccueilDto {
2+
readonly id: number;
3+
4+
readonly nom: string ;
5+
6+
readonly lieu :string ;
7+
8+
readonly date : string;
9+
10+
}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
export class Accueil {}
1+
import {Entity, Column, PrimaryGeneratedColumn} from 'typeorm';
2+
3+
@Entity()
4+
export class Accueil {
5+
@PrimaryGeneratedColumn()
6+
id:number;
7+
8+
@Column()
9+
nom:string;
10+
11+
@Column()
12+
date:string;
13+
14+
@Column()
15+
lieu:string;
16+
}

api/src/app.module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { DataSource } from 'typeorm';
1010
TypeOrmModule.forRoot({
1111
type: 'mysql',
1212
host: 'localhost',
13-
port: 3306,
14-
username: 'root',
15-
password: 'root',
16-
database: 'iziplan_db.sql',
17-
entities: [],
13+
port: 3333,
14+
username: 'iziplan_user',
15+
password: 'iziplan_psw',
16+
database: 'iziplan_db',
17+
entities: ['././dist/accueil/entities/accueil.entity.js'],
1818
synchronize: true //attention à enlever lors de la production
1919
})
2020
],

docker-compose.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)