continue the journey

This commit is contained in:
batche
2022-10-21 16:55:39 +02:00
parent a175ef7162
commit b12db3e912
14 changed files with 67 additions and 38 deletions

View File

@@ -5,7 +5,7 @@ import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
@Module({
imports: [AuthModule, UsersModule],
imports: [UsersModule],
controllers: [AppController],
providers: [AppService],
})

View File

@@ -1,5 +1,5 @@
export class User {
userId: number;
userId: string;
username: string;
password: string;
}

View File

@@ -11,28 +11,28 @@ export class UsersController {
// par exemple dans postamn ou insomnia http://localhost:3000/users?limit=10&offset=20
@Get()
findAll(@Query() query) {
const { limit, offset } = query;
return `This action returns all users. The limits is ${limit} and the offset is ${offset}`;
//const { limit, offset } = query;
return this.usersService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string) {
return 'This action returns a single user whose id is ' + id;
return this.usersService.findOne(id);
}
@Post()
@HttpCode(HttpStatus.GONE)
create(@Body() body) {
return body;
return this.usersService.create(body);
}
@Patch(':id')
update(@Param('id') id: string, @Body() body) {
return 'The user whose id is ' + id + ' has been updated';
return this.usersService.update(id, body);
}
@Delete(':id')
remove(@Param('id') id: string) {
return 'The user whose id is ' + id + ' has been deleted';
return this.usersService.remove(id);
}
}

View File

@@ -1,36 +1,52 @@
import { Injectable } from '@nestjs/common';
import { HttpCode, HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common';
import { NotFoundError } from 'rxjs';
import { User } from './entities/user.entity';
@Injectable()
export class UsersService {
private users:User[] = [
{
userId: 1,
userId: '1',
username: 'john',
password: 'changeme',
},
{
userId: 2,
userId: '2',
username: 'maria',
password: 'guess',
},
];
findOne(id: number) {
return this.users.find(user => user.userId === id);
findOne(id: string) {
const user = this.users.find(user => user.userId === id);
if (!user)
throw new NotFoundException(`The requested user not found.`);
return user;
}
findAll() {
if (!this.users.length)
throw new HttpException(`No user exists.`,HttpStatus.NOT_FOUND);
return this.users;
}
create(createUserDto: any) {
this.users.push(createUserDto);
const user = this.users.push(createUserDto);
if (!user)
throw new HttpException(`The user could not be created.`,HttpStatus.NOT_FOUND);
return 'User has been created';
}
update(id: number, updateUserDto: any) {
update(id: string, updateUserDto: any) {
const index = this.users.findIndex(user => user.userId === id);
this.users[index] = updateUserDto;
return this.users[index].username + ' has been updated';
}
remove(id: string) {
const index = this.users.findIndex(user => user.userId === id);
this.users.splice(index, 1);
return 'User has been deleted';
}
}