statistique de l'utilisateur

This commit is contained in:
batche
2022-11-19 12:05:30 +01:00
parent 686067375a
commit 40ce956a47
5 changed files with 100 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { OmitType, 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 PartialUsersDto extends OmitType(CreateUsersDto, ['fortyTwoId'] as const){} export class PartialUsersDto extends OmitType(CreateUsersDto, ['fortyTwoId', 'email'] as const){}

View File

@@ -1,7 +1,8 @@
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, PrimaryGeneratedColumn, Unique } from "typeorm"; import { Column, Entity, 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";
@Entity('user') @Entity('user')
@@ -49,4 +50,8 @@ export class User {
@JoinTable() @JoinTable()
@OneToMany(type => Friendship , (friendship) => friendship.addresseeId) @OneToMany(type => Friendship , (friendship) => friendship.addresseeId)
addresseeId: Friendship[]; addresseeId: Friendship[];
@JoinTable()
@OneToOne(() => UserStats, { cascade: true })
stats: UserStats;
} }

View File

@@ -0,0 +1,19 @@
import { Column, Entity, OneToOne, PrimaryGeneratedColumn } from "typeorm";
@Entity('stats')
export class UserStats {
@PrimaryGeneratedColumn()
id: number;
@Column({ default: 0 })
winGame: number;
@Column({ default: 0 })
loseGame: number;
@Column({ default: 0 })
drawGame: number;
@Column({ default: 0 })
totalGame: number;
}

View File

@@ -1,5 +1,5 @@
import { import {
Body, Controller, Delete, Get, NotFoundException, Patch, Post, Query, Req, Res, UploadedFile, UseGuards, UseInterceptors Body, Controller, Delete, Get, NotFoundException, Param, Patch, Post, Query, Req, Res, UploadedFile, UseGuards, UseInterceptors
} from '@nestjs/common'; } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express'; import { FileInterceptor } from '@nestjs/platform-express';
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards'; import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
@@ -41,12 +41,21 @@ export class UsersController {
return this.usersService.findOne(req.user.id); return this.usersService.findOne(req.user.id);
} }
// @UseGuards(AuthenticateGuard) // GET http://transcendance:8080/user?username=NomDuUser
// @Post() @UseGuards(AuthenticateGuard)
// @HttpCode(HttpStatus.CREATED) @UseGuards(TwoFactorGuard)
// create(@Body() createUsersDto : CreateUsersDto ) { @Get('search')
// return this.usersService.create(createUsersDto); findOneByUsername(@Query('username') username: string) {
// } return this.usersService.findOneByUsername(username);
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('stats')
getStats(@Req() request) {
return this.usersService.getStats(request.user.id);
}
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)

View File

@@ -10,6 +10,7 @@ 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 { PartialUsersDto } from './dto/partial-users.dto';
import { join } from 'path'; import { join } from 'path';
import { UserStats } from './entities/userStat.entities';
// 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.
@@ -35,7 +36,10 @@ export class UsersService {
} }
async findOne(id: string) { async findOne(id: string) {
const user = await this.userRepository.findOneBy({id: +id}); const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id })
.getOne();
if (!user) if (!user)
throw new NotFoundException(`The requested user not found.`); throw new NotFoundException(`The requested user not found.`);
const partialUser : Partial<User> = { const partialUser : Partial<User> = {
@@ -43,6 +47,7 @@ export class UsersService {
image_url: user.image_url, image_url: user.image_url,
isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth, isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth,
status: user.status, status: user.status,
stats: user.stats,
}; };
return partialUser; return partialUser;
} }
@@ -55,13 +60,17 @@ export class UsersService {
} }
async findOneByUsername(username: string) { async findOneByUsername(username: string) {
const user = await this.userRepository.findOneBy({username : username}); const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.username = :username', { username: username })
.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);
const partialUser : Partial<User> = { const partialUser : Partial<User> = {
username: user.username, username: user.username,
image_url: user.image_url, image_url: user.image_url,
status: user.status, status: user.status,
stats: user.stats,
}; };
return partialUser; return partialUser;
} }
@@ -81,6 +90,7 @@ export class UsersService {
const user = this.userRepository.create(createUserDto); const user = this.userRepository.create(createUserDto);
if (!user) if (!user)
throw new HttpException(`The user could not be created.`,HttpStatus.NOT_FOUND); throw new HttpException(`The user could not be created.`,HttpStatus.NOT_FOUND);
user.stats = new UserStats();
return this.userRepository.save(user); return this.userRepository.save(user);
} }
@@ -130,4 +140,50 @@ export class UsersService {
console.log(user.image_url); console.log(user.image_url);
return user.image_url; return user.image_url;
} }
async getStats(id: number) {
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id })
.getOne();
if (!user.stats || !user)
throw new HttpException(`The user's stats could not be found.`,HttpStatus.NOT_FOUND);
return user.stats;
}
async incrementVictories(id: number) {
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id })
.getOne();
if (!user.stats || !user)
throw new HttpException(`The user's stats could not be found.`,HttpStatus.NOT_FOUND);
user.stats.winGame++;
user.stats.totalGame++;
this.userRepository.save(user);
}
async incrementDefeats(id: number) {
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id })
.getOne();
if (!user.stats || !user)
throw new HttpException(`The user's stats could not be found.`,HttpStatus.NOT_FOUND);
user.stats.loseGame++;
user.stats.totalGame++;
this.userRepository.save(user);
}
async incrementDraws(id: number) {
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id })
.getOne();
if (!user.stats || !user)
throw new HttpException(`The user's stats could not be found.`,HttpStatus.NOT_FOUND);
user.stats.winGame++;
user.stats.totalGame++;
this.userRepository.save(user);
}
} }