correction de petites erreurs

This commit is contained in:
batche
2022-11-21 12:08:36 +01:00
parent f1df3964f9
commit b8d404ce13
8 changed files with 48 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Friendship } from 'src/friendship/entities/friendship.entity'; import { Friendship } from 'src/friendship/entities/friendship.entity';
import { FriendshipService } from 'src/friendship/friendship.service';
import { User } from 'src/users/entities/user.entity'; import { User } from 'src/users/entities/user.entity';
import { UsersModule } from 'src/users/users.module'; import { UsersModule } from 'src/users/users.module';
import { UsersService } from 'src/users/users.service'; import { UsersService } from 'src/users/users.service';
@@ -20,7 +21,7 @@ import { SessionSerializer } from './utils/serializer';
// } // }
// }) // })
], ],
providers: [AuthenticationService, FortyTwoStrategy, UsersService, SessionSerializer, providers: [AuthenticationService, FortyTwoStrategy, UsersService, SessionSerializer, FriendshipService
// JwtStrategy // JwtStrategy
], ],
exports: [AuthenticationService], exports: [AuthenticationService],

View File

@@ -26,7 +26,7 @@ export class AuthenticationService {
let createUsersDtoWithUsername : CreateUsersDto = createUsersDto; let createUsersDtoWithUsername : CreateUsersDto = createUsersDto;
while (check_name === true) while (check_name === true)
{ {
createUsersDtoWithUsername = { ...createUsersDto, username: createUsersDto.username + randomUUID() }; createUsersDtoWithUsername = { ...createUsersDto, username: createUsersDto.username + randomUUID() };
check_name = await this.userService.isUsernameExists(createUsersDtoWithUsername.username); check_name = await this.userService.isUsernameExists(createUsersDtoWithUsername.username);
} }
return this.userService.create(createUsersDtoWithUsername); return this.userService.create(createUsersDtoWithUsername);

View File

@@ -117,4 +117,16 @@ export class FriendshipService {
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND); throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
return this.friendshipRepository.remove(friendship); return this.friendshipRepository.remove(friendship);
} }
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToCheckId: string) {
const friendship = await this.friendshipRepository
.createQueryBuilder('friendship')
.where('friendship.requesterId = :requestee', { requestee: userConnectedId })
.orWhere('friendship.requesterId = :requesteeBis', { requesteeBis: userToCheckId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getOne();
if (friendship)
return true;
return false;
}
} }

View File

@@ -1,6 +1,6 @@
import { Exclude } from "class-transformer"; import { Exclude } from "class-transformer";
import { IsEmail, Length } from "class-validator"; import { IsEmail, Length } from "class-validator";
import { Column, Entity, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm"; import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne, PrimaryGeneratedColumn, Unique } from "typeorm";
import { Friendship } from "../../friendship/entities/friendship.entity"; import { Friendship } from "../../friendship/entities/friendship.entity";
import { UserStats } from "./userStat.entities"; import { UserStats } from "./userStat.entities";
@@ -51,7 +51,7 @@ export class User {
@OneToMany(type => Friendship , (friendship) => friendship.addresseeId) @OneToMany(type => Friendship , (friendship) => friendship.addresseeId)
addresseeId: Friendship[]; addresseeId: Friendship[];
@JoinTable() @JoinColumn()
@OneToOne(() => UserStats, { cascade: true }) @OneToOne(() => UserStats, { cascade: true })
stats: UserStats; stats: UserStats;
} }

View File

@@ -45,8 +45,9 @@ export class UsersController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Get('search') @Get('search')
findOneByUsername(@Query('username') username: string) { findOneByUsername(@Query('username') username: string, @Req() req) {
return this.usersService.findOneByUsername(username); const user : User = req.user;
return this.usersService.findOneByUsername(user.id.toString(),username);
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)

View File

@@ -4,13 +4,12 @@ import { UsersController } from './users.controller';
import { User } from './entities/user.entity'; import { User } from './entities/user.entity';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Friendship } from '../friendship/entities/friendship.entity'; import { Friendship } from '../friendship/entities/friendship.entity';
import { AuthenticationService } from 'src/auth/42/authentication.service';
import { AuthenticationModule } from 'src/auth/42/authentication.module';
import { UserStats } from './entities/userStat.entities'; import { UserStats } from './entities/userStat.entities';
import { FriendshipService } from 'src/friendship/friendship.service';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([User, Friendship, UserStats])], imports: [TypeOrmModule.forFeature([User, Friendship, UserStats])],
providers: [UsersService], providers: [UsersService, FriendshipService],
exports: [UsersService], exports: [UsersService],
controllers: [UsersController], controllers: [UsersController],
}) })

View File

@@ -1,16 +1,14 @@
import { HttpCode, HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common'; import { HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { NotFoundError, of } from 'rxjs';
import { User } from './entities/user.entity'; import { User } from './entities/user.entity';
import { ConnectionOptionsReader, Repository } from 'typeorm'; import { Repository } from 'typeorm';
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';
import { Friendship } from '../friendship/entities/friendship.entity'; import { Friendship } from '../friendship/entities/friendship.entity';
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';
import { join } from 'path';
import { UserStats } from './entities/userStat.entities'; import { UserStats } from './entities/userStat.entities';
import { FriendshipService } from 'src/friendship/friendship.service';
import { stringify } from 'querystring';
// 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.
@@ -18,10 +16,9 @@ import { UserStats } from './entities/userStat.entities';
export class UsersService { export class UsersService {
constructor( constructor(
private readonly friendshipService: FriendshipService,
@InjectRepository(User) @InjectRepository(User)
private readonly userRepository: Repository<User>, private readonly userRepository: Repository<User>,
@InjectRepository(Friendship)
private readonly friendshipRepository: Repository<Friendship>,
) {} ) {}
async findOneByFourtyTwoId(fortytwo_id: string) { async findOneByFourtyTwoId(fortytwo_id: string) {
@@ -36,12 +33,15 @@ export class UsersService {
} }
async findOne(id: string) { async findOne(id: string) {
console.log(`FIND ONE USER SERVICE Find user ${id}`);
const user = await this.userRepository.createQueryBuilder('user') const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats') .leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id }) .where('user.id = :id', { id: +id })
.getOne(); .getOne();
if (!user) if (!user)
throw new NotFoundException(`The requested user not found.`); throw new NotFoundException(`The requested user not found.`);
console.log(`FIND ONE USER SERVICE The requested user found.`
+ user.stats.id + user.stats.winGame + user.stats.loseGame + user.stats.drawGame + user.stats.totalGame);
const partialUser : Partial<User> = { const partialUser : Partial<User> = {
username: user.username, username: user.username,
image_url: user.image_url, image_url: user.image_url,
@@ -59,13 +59,15 @@ export class UsersService {
return true; return true;
} }
async findOneByUsername(username: string) { async findOneByUsername(userConnectedId : string, username: string) {
const user = await this.userRepository.createQueryBuilder('user') const user : User = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats') .leftJoinAndSelect('user.stats', 'stats')
.where('user.username = :username', { username: username }) .where('user.username = :username', { username: username })
.getOne(); .getOne();
if (!user) if (!user)
throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND); throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND);
if (this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()))
throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND);
const partialUser : Partial<User> = { const partialUser : Partial<User> = {
username: user.username, username: user.username,
image_url: user.image_url, image_url: user.image_url,

View File

@@ -3,12 +3,16 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
let user = {}; let user = {};
let stats = {};
onMount (async() => { onMount (async() => {
console.log("PROFIL SVELTE"); console.log("PROFIL SVELTE");
const {data} = await axios.get('http://transcendance:8080/api/v2/user'); const response = await fetch('http://transcendance:8080/api/v2/user',
console.log(data.username); {
console.log(data); method : 'GET',
user = data; }).then(response => response.json());
console.log(response);
user = response;
stats = user.stats;
}); });
</script> </script>
@@ -21,6 +25,11 @@
<h1>Profil</h1> <h1>Profil</h1>
<ul> <ul>
<li>username : {user.username}</li> <li>username : {user.username}</li>
<li>stats : Win games {stats.winGame}
Lose games {stats.loseGame}
Draw games {stats.drawGame}
Total games {stats.totalGame}
</li>
</ul> </ul>
</div> </div>
</div> </div>