better findAll() in user module, no pagination tho, could add but would be hella annoying

This commit is contained in:
Me
2022-12-22 03:34:53 +01:00
parent 670936e4a1
commit beeaf1fb27
3 changed files with 111 additions and 35 deletions

View File

@@ -22,9 +22,11 @@ export class UsersController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('all')
findAll(@Query() paginationquery : PaginationQueryDto) {
findAll(@Query() paginationquery : PaginationQueryDto, @Req() req) {
//const { limit, offset } = query;
return this.usersService.findAll(paginationquery);
// not convinced i want pagination... i mean maybe for loading and such, we shall see
const user : User = req.user;
return this.usersService.findAll(paginationquery, user.id.toString());
}
////////////////////////////////////////////////////////
@@ -36,14 +38,8 @@ export class UsersController {
* car un utilisateur est crée à la première connexion avec l'Oauth de 42.
*/
// @UseGuards(AuthenticateGuard)
// @UseGuards(TwoFactorGuard)
// @Get()
// findOne(@Req() req) {
// console.log("Backend Getting current user");
// return this.usersService.findOne(req.user.id);
// }
// similar to @Get('myfriends/:friendUsername') but doesn't return a friendship just returns the user profile
// GET http://transcendance:8080/user?username=NomDuUser
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get()
@@ -58,23 +54,25 @@ export class UsersController {
}
}
// 99% sure this is useless
// GET http://transcendance:8080/user?username=NomDuUser
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('search')
findOneByUsername(@Query('username') username: string, @Req() req) {
const user : User = req.user;
console.log('searching for user' + user.username);
return this.usersService.findOneByUsername(user.id.toString(),username);
}
// @UseGuards(AuthenticateGuard)
// @UseGuards(TwoFactorGuard)
// @Get('search')
// findOneByUsername(@Query('username') username: string, @Req() req) {
// const user : User = req.user;
// console.log('searching for user' + user.username);
// return this.usersService.findOneByUsername(user.id.toString(),username);
// }
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('stats')
getStats(@Req() request) {
return this.usersService.getStats(request.user.id);
}
// also seems useless...
// GET http://transcendance:8080/user/stats
// @UseGuards(AuthenticateGuard)
// @UseGuards(TwoFactorGuard)
// @Get('stats')
// getStats(@Req() request) {
// return this.usersService.getStats(request.user.id);
// }
@UseGuards(AuthenticateGuard)

View File

@@ -1,7 +1,7 @@
import { HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Repository } from 'typeorm';
import { Repository, Not } from 'typeorm';
import { CreateUsersDto } from './dto/create-users.dto';
import { UpdateUsersDto } from './dto/update-users.dto';
import { Friendship } from '../friendship/entities/friendship.entity';
@@ -84,15 +84,91 @@ export class UsersService {
return partialUser;
}
// Example : http://localhost:3000/users?limit=10&offset=20
findAll(paginationquery : PaginationQueryDto) {
const { limit, offset } = paginationquery;
return this.userRepository.find({
skip: offset,
take: limit,
});
// only useful if doing Pagination i think
// add return type
async createPartialUsersArray(users: User[], userConnectedId: string) {
let partialUsers : Partial<User>[] = [];
for (const user of users) {
// this will sort of work but i need more like findIfUserIsBlocked so you can unblock people... maybe ?
// or i have a tab for all the users you have blocked... yea idk yet
// there's already a backend func in frienships for that so yea lets do that!
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) {
partialUsers.push({username: user.username, image_url: user.image_url, status: user.status, stats: user.stats});
}
}
return partialUsers;
}
// OMG I HATE ADDING PAGINATION...
// Example : http://localhost:3000/users?limit=10&offset=20
// async findAll(paginationquery : PaginationQueryDto, userConnectedId: string) {
// // const { limit, offset } = paginationquery;
// let { limit, offset } = paginationquery;
// // ok pagination only sort of works, like if
// // const users = await this.userRepository.find({skip: offset, take: limit,});
// let users;
// let partialUsers : Partial<User>[] = [];
// // let partialUsers;
// // partialUsers = await this.createPartialUsersArray(users, userConnectedId);
// // ok this is the part where i would need to be like ok if you didn't get enough users cuz at least one is blocked, then you need to find that many more and try again and prolly put that in a loop and break when you've got enough, but like i hate that
// console.log("IN users.service");
// console.log("limit: " + limit);
// console.log("offset: " + offset);
// while (1) {
// users = await this.userRepository.find({skip: offset, take: limit,});
// console.log("users.service users");
// console.log({...users});
// if (users === undefined) // i'm just guessing what the result will be... in the case we run out of users
// break;
// partialUsers = await this.createPartialUsersArray(users, userConnectedId);
// if (limit && offset) {
// offset += limit;
// limit = limit - partialUsers.length;
// console.log(limit)
// if (limit === 0)
// break;
// } else {
// break;
// }
// }
// // for (const user of users) {
// // // this will sort of work but i need more like findIfUserIsBlocked so you can unblock people... maybe ?
// // // or i have a tab for all the users you have blocked... yea idk yet
// // // there's already a backend func in frienships for that so yea lets do that!
// // if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) {
// // partialUsers.push({username: user.username, image_url: user.image_url, status: user.status, stats: user.stats});
// // }
// // }
// return partialUsers;
// }
// The fuck Pagination version
async findAll(paginationquery : PaginationQueryDto, userConnectedId: string) {
const { limit, offset } = paginationquery;
const users = await this.userRepository.find({where: {id: Not(+userConnectedId)}, order: {username: "ASC"}, skip: offset, take: limit,});
let partialUsers : Partial<User>[] = [];
for (const user of users) {
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, user.id.toString()) === false) {
partialUsers.push({username: user.username, image_url: user.image_url, status: user.status, stats: user.stats});
}
}
return partialUsers;
}
async create(createUserDto: CreateUsersDto) {
if (await this.userRepository.findOneBy({fortyTwoId: createUserDto.fortyTwoId}))
throw new HttpException(`The user already exists.`,HttpStatus.CONFLICT);