add a partial user to be returned
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { IsBoolean, IsEmail, IsOptional, IsString } from 'class-validator';
|
||||
import { Unique } from 'typeorm';
|
||||
|
||||
export class CreateUsersDto {
|
||||
@IsString()
|
||||
|
||||
@@ -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){}
|
||||
@@ -2,7 +2,7 @@
|
||||
// et de les mettre comme optionnelles. De plus on peut hériter
|
||||
// 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";
|
||||
|
||||
export class UpdateUsersDto extends PartialType(CreateUsersDto){}
|
||||
export class UpdateUsersDto extends OmitType(CreateUsersDto, ['fortyTwoId', 'email'] as const){}
|
||||
|
||||
@@ -31,11 +31,11 @@ export class UsersController {
|
||||
* car un utilisateur est crée à la première connexion avec l'Oauth de 42.
|
||||
*/
|
||||
|
||||
// @UseGuards(AuthenticateGuard)
|
||||
// @Get()
|
||||
// findOne(@Req() req) {
|
||||
// return this.usersService.findOne(req.user.id);
|
||||
// }
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@Get()
|
||||
findOne(@Req() req) {
|
||||
return this.usersService.findOne(req.user.id);
|
||||
}
|
||||
|
||||
// @UseGuards(AuthenticateGuard)
|
||||
// @Post()
|
||||
@@ -47,6 +47,7 @@ export class UsersController {
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@Patch()
|
||||
update(@Req() req, @Body() usersUpdateDto: UpdateUsersDto) {
|
||||
console.log("DANS PATCH USERS");
|
||||
return this.usersService.update(req.user.id, usersUpdateDto);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
import { Friendship } from '../friendship/entities/friendship.entity';
|
||||
import { isNumberString } from 'class-validator';
|
||||
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
|
||||
// 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});
|
||||
if (!user)
|
||||
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
|
||||
@@ -68,7 +75,7 @@ export class UsersService {
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const user = await this.findOne(id);
|
||||
const user = await this.userRepository.findOneBy({id: +id});
|
||||
if (!user)
|
||||
throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND);
|
||||
return this.userRepository.remove(user);
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
import Home from "./pages/home/home.svelte";
|
||||
import Router, {link} from 'svelte-spa-router';
|
||||
import Profil from "./pages/profil/profil.svelte";
|
||||
import UpdateProfil from "./pages/profil/updateProfil.svelte";
|
||||
|
||||
const routes = {
|
||||
"/": Home,
|
||||
"/login": Login,
|
||||
"/profil": Profil,
|
||||
"/update-profil": UpdateProfil,
|
||||
};
|
||||
|
||||
</script>
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
import Router, { link } from "svelte-spa-router";
|
||||
import Login from "../auth/login.svelte";
|
||||
import Profil from "../profil/profil.svelte";
|
||||
import UpdateProfil from "../profil/updateProfil.svelte";
|
||||
const routes = {
|
||||
"/login": Login,
|
||||
"/profil": Profil,
|
||||
"/update-profil": UpdateProfil,
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -31,6 +33,16 @@
|
||||
Profil
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a
|
||||
href="/update-profil"
|
||||
use:link
|
||||
class="nav-link active"
|
||||
aria-current="page"
|
||||
>
|
||||
Update Profile
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
<hr />
|
||||
</div>
|
||||
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user