Ajout de l'update utilisateur avec seulement certaines string + un exemple avec svelte
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';
|
||||||
|
import { validate } from 'class-validator';
|
||||||
|
import { plainToInstance } from 'class-transformer';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ValidationPipe implements PipeTransform<any> {
|
||||||
|
async transform(value: any, { metatype }: ArgumentMetadata) {
|
||||||
|
if (!metatype || !this.toValidate(metatype)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
const object = plainToInstance(metatype, value);
|
||||||
|
const errors = await validate(object);
|
||||||
|
if (errors.length > 0) {
|
||||||
|
throw new BadRequestException('Validation failed');
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private toValidate(metatype: Function): boolean {
|
||||||
|
const types: Function[] = [String, Boolean, Number, Array, Object];
|
||||||
|
return !types.includes(metatype);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,5 @@ export class CreateUsersDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
readonly image_url: string;
|
readonly image_url: string;
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
@IsOptional()
|
|
||||||
readonly isEnabledTwoFactorAuth: boolean;
|
readonly isEnabledTwoFactorAuth: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AuthenticateGuard } from 'src/auth/42/guards/42guards';
|
import { AuthenticateGuard } from 'src/auth/42/guards/42guards';
|
||||||
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
||||||
|
import { ValidationPipe } from 'src/common/validation/validation.pipe';
|
||||||
import { CreateUsersDto } from './dto/create-users.dto';
|
import { CreateUsersDto } from './dto/create-users.dto';
|
||||||
import { UpdateUsersDto } from './dto/update-users.dto';
|
import { UpdateUsersDto } from './dto/update-users.dto';
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ export class UsersController {
|
|||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@Patch()
|
@Patch()
|
||||||
update(@Req() req, @Body() usersUpdateDto: UpdateUsersDto) {
|
update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto) {
|
||||||
console.log("DANS PATCH USERS");
|
console.log("DANS PATCH USERS");
|
||||||
return this.usersService.update(req.user.id, usersUpdateDto);
|
return this.usersService.update(req.user.id, usersUpdateDto);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ export class UsersService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, updateUserDto: UpdateUsersDto) {
|
async update(id: string, updateUserDto: UpdateUsersDto) {
|
||||||
console.log(`Update user ${id} with ${updateUserDto}`);
|
console.log(`Update user ${id} with ${updateUserDto.image_url} + ${updateUserDto.isEnabledTwoFactorAuth}`);
|
||||||
const user = await this.userRepository.preload(
|
const user = await this.userRepository.preload(
|
||||||
{id: +id,
|
{id: +id,
|
||||||
...updateUserDto});
|
...updateUserDto});
|
||||||
|
|||||||
@@ -2,40 +2,44 @@
|
|||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
|
|
||||||
let username;
|
let usernameSv = "";
|
||||||
let image_url;
|
let image_urlSv = "";
|
||||||
let gAuth = false;
|
let gAuth = false;
|
||||||
let errors = {username, image_url};
|
let errors = {usernameSv, image_urlSv};
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await fetch("http://transcendance:8080/api/v2/user").
|
await fetch("http://transcendance:8080/api/v2/user").
|
||||||
then(response => response.json()).
|
then(response => response.json()).
|
||||||
then(data => {
|
then(data => {
|
||||||
console.log(data);
|
usernameSv = data.username;
|
||||||
username = data.username;
|
image_urlSv = data.image_url;
|
||||||
image_url = data.image_url;
|
|
||||||
gAuth = data.isEnabledTwoFactorAuth;
|
gAuth = data.isEnabledTwoFactorAuth;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$: submit = async() => {
|
$: submit = async() => {
|
||||||
errors.username = "";
|
errors.usernameSv = "";
|
||||||
errors.image_url ="";
|
errors.image_urlSv ="";
|
||||||
if (username === undefined || username.trim() === "") {
|
if (usernameSv === undefined || usernameSv.trim() === "") {
|
||||||
errors.username = "Username is required";
|
errors.usernameSv = "Username is required";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (image_url === undefined || image_url.trim() === "") {
|
if (image_urlSv === undefined || image_urlSv.trim() === "") {
|
||||||
errors.image_url = "image_url is required";
|
errors.image_urlSv = "image_url is required";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
console.log(usernameSv);
|
||||||
await fetch("http://transcendance:8080/api/v2/user/",
|
await fetch("http://transcendance:8080/api/v2/user/",
|
||||||
{
|
{
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
"username": username,
|
"username" : usernameSv,
|
||||||
"image_url": image_url,
|
"image_url" : image_urlSv,
|
||||||
"isEnabledTwoFactorAuth" : gAuth
|
"isEnabledTwoFactorAuth" : gAuth
|
||||||
})});
|
})
|
||||||
|
})
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -54,10 +58,10 @@
|
|||||||
{/if}
|
{/if}
|
||||||
<form on:submit|preventDefault={submit}>
|
<form on:submit|preventDefault={submit}>
|
||||||
<label for="username" class="block text-sm text-gray-600">Username</label>
|
<label for="username" class="block text-sm text-gray-600">Username</label>
|
||||||
<input id="username" type="text" placeholder="username" class="block px-1 py-2 mt-2 border-2 border-gray-100 text-gray-800" bind:value={username} />
|
<input id="username" type="text" placeholder=${usernameSv} class="block px-1 py-2 mt-2 border-2 border-gray-100 text-gray-800" bind:value={usernameSv} />
|
||||||
|
|
||||||
<label for="image_url" class="block text-sm text-gray-600 mt-4">image_url</label>
|
<label for="image_url" class="block text-sm text-gray-600 mt-4">image_url</label>
|
||||||
<input id="image_url" type="image_url" bind:value={image_url} placeholder="image_url" class="block px-1 py-2 mt-2 border-2 border-gray-100 text-gray-800" />
|
<input id="image_url" type="image_url" bind:value={image_urlSv} placeholder=${image_urlSv} class="block px-1 py-2 mt-2 border-2 border-gray-100 text-gray-800" />
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<input type="checkbox" bind:checked={gAuth} id="gAuth" name="gAuth">
|
<input type="checkbox" bind:checked={gAuth} id="gAuth" name="gAuth">
|
||||||
|
|||||||
Reference in New Issue
Block a user