add a partial user to be returned

This commit is contained in:
batche
2022-11-14 17:25:43 +01:00
parent aa3eb111c1
commit 79b3cbb3d3
8 changed files with 123 additions and 10 deletions

View File

@@ -1,5 +1,4 @@
import { IsBoolean, IsEmail, IsOptional, IsString } from 'class-validator'; import { IsBoolean, IsEmail, IsOptional, IsString } from 'class-validator';
import { Unique } from 'typeorm';
export class CreateUsersDto { export class CreateUsersDto {
@IsString() @IsString()

View File

@@ -0,0 +1,4 @@
import { OmitType, PartialType } from "@nestjs/mapped-types";
import { CreateUsersDto } from "./create-users.dto";
export class PartialUsersDto extends OmitType(CreateUsersDto, ['fortyTwoId'] as const){}

View File

@@ -2,7 +2,7 @@
// et de les mettre comme optionnelles. De plus on peut hériter // et de les mettre comme optionnelles. De plus on peut hériter
// des décorateurs de la classe parente (par exemple @IsString()). // des décorateurs de la classe parente (par exemple @IsString()).
import { PartialType } from "@nestjs/mapped-types"; import { OmitType, PartialType } from "@nestjs/mapped-types";
import { CreateUsersDto } from "./create-users.dto"; import { CreateUsersDto } from "./create-users.dto";
export class UpdateUsersDto extends PartialType(CreateUsersDto){} export class UpdateUsersDto extends OmitType(CreateUsersDto, ['fortyTwoId', 'email'] as const){}

View File

@@ -31,11 +31,11 @@ export class UsersController {
* car un utilisateur est crée à la première connexion avec l'Oauth de 42. * car un utilisateur est crée à la première connexion avec l'Oauth de 42.
*/ */
// @UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
// @Get() @Get()
// findOne(@Req() req) { findOne(@Req() req) {
// return this.usersService.findOne(req.user.id); return this.usersService.findOne(req.user.id);
// } }
// @UseGuards(AuthenticateGuard) // @UseGuards(AuthenticateGuard)
// @Post() // @Post()
@@ -47,6 +47,7 @@ export class UsersController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@Patch() @Patch()
update(@Req() req, @Body() usersUpdateDto: UpdateUsersDto) { update(@Req() req, @Body() usersUpdateDto: UpdateUsersDto) {
console.log("DANS PATCH USERS");
return this.usersService.update(req.user.id, usersUpdateDto); return this.usersService.update(req.user.id, usersUpdateDto);
} }

View File

@@ -8,6 +8,7 @@ import { UpdateUsersDto } from './dto/update-users.dto';
import { Friendship } from '../friendship/entities/friendship.entity'; import { Friendship } from '../friendship/entities/friendship.entity';
import { isNumberString } from 'class-validator'; import { isNumberString } from 'class-validator';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto'; import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
import { PartialUsersDto } from './dto/partial-users.dto';
// On va devoir sûrement trouver un moyen plus simple pour passer l'id, sûrement via des pipes // On va devoir sûrement trouver un moyen plus simple pour passer l'id, sûrement via des pipes
// ou des interceptors, mais pour l'instant on va faire comme ça. // ou des interceptors, mais pour l'instant on va faire comme ça.
@@ -36,7 +37,13 @@ export class UsersService {
const user = await this.userRepository.findOneBy({id: +id}); const user = await this.userRepository.findOneBy({id: +id});
if (!user) if (!user)
throw new NotFoundException(`The requested user not found.`); throw new NotFoundException(`The requested user not found.`);
return user; const partialUser : Partial<User> = {
username: user.username,
email: user.email,
image_url: user.image_url,
isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth,
};
return partialUser;
} }
// Example : http://localhost:3000/users?limit=10&offset=20 // Example : http://localhost:3000/users?limit=10&offset=20
@@ -68,7 +75,7 @@ export class UsersService {
} }
async remove(id: string) { async remove(id: string) {
const user = await this.findOne(id); const user = await this.userRepository.findOneBy({id: +id});
if (!user) if (!user)
throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND); throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND);
return this.userRepository.remove(user); return this.userRepository.remove(user);

View File

@@ -3,11 +3,13 @@
import Home from "./pages/home/home.svelte"; import Home from "./pages/home/home.svelte";
import Router, {link} from 'svelte-spa-router'; import Router, {link} from 'svelte-spa-router';
import Profil from "./pages/profil/profil.svelte"; import Profil from "./pages/profil/profil.svelte";
import UpdateProfil from "./pages/profil/updateProfil.svelte";
const routes = { const routes = {
"/": Home, "/": Home,
"/login": Login, "/login": Login,
"/profil": Profil, "/profil": Profil,
"/update-profil": UpdateProfil,
}; };
</script> </script>

View File

@@ -2,9 +2,11 @@
import Router, { link } from "svelte-spa-router"; import Router, { link } from "svelte-spa-router";
import Login from "../auth/login.svelte"; import Login from "../auth/login.svelte";
import Profil from "../profil/profil.svelte"; import Profil from "../profil/profil.svelte";
import UpdateProfil from "../profil/updateProfil.svelte";
const routes = { const routes = {
"/login": Login, "/login": Login,
"/profil": Profil, "/profil": Profil,
"/update-profil": UpdateProfil,
}; };
</script> </script>
@@ -31,6 +33,16 @@
Profil Profil
</a> </a>
</li> </li>
<li class="nav-item">
<a
href="/update-profil"
use:link
class="nav-link active"
aria-current="page"
>
Update Profile
</a>
</li>
</ul> </ul>
<hr /> <hr />
</div> </div>

View File

@@ -0,0 +1,88 @@
<script>
import { onMount } from "svelte";
let username;
let image_url;
let gAuth = false;
let errors = {username, image_url};
onMount(async () => {
await fetch("http://transcendance:8080/api/v2/user").
then(response => response.json()).
then(data => {
console.log(data);
username = data.username;
image_url = data.image_url;
gAuth = data.isEnabledTwoFactorAuth;
});
});
$: submit = async() => {
errors.username = "";
errors.image_url ="";
if (username === undefined || username.trim() === "") {
errors.username = "Username is required";
return;
}
if (image_url === undefined || image_url.trim() === "") {
errors.image_url = "image_url is required";
return;
}
await fetch("http://transcendance:8080/api/v2/user/",
{
method: "PATCH",
body: JSON.stringify({
"username": username,
"image_url": image_url,
"isEnabledTwoFactorAuth": gAuth
})});
};
</script>
<body>
<main class="form-signin w-100 m-auto">
<div class="p-20">
{#if errors.username}
<div class="alert alert-danger" role="alert">
{errors.username}
</div>
{/if}
{#if errors.image_url}
<div class="alert alert-danger" role="alert">
{errors.image_url}
</div>
{/if}
<form on:submit|preventDefault={submit}>
<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} />
<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" />
<div>
<input type="checkbox" bind:checked={gAuth} id="gAuth" name="gAuth">
<label for="gAuth">Enable google authenticator</label>
</div>
<button type="submit" class="p-2 bg-blue-500 text-white mt-4 px-6">
Change
</button>
</form>
</div>
</main>
</body>
<style>
body {
display: flex;
align-items: center;
padding-top: 40px;
padding-bottom: 40px;
}
.form-signin {
max-width: 330px;
padding: 15px;
}
</style>