ok have changed the entities and DTOs but not as much as expected, changed Friendship module, now working on User module

This commit is contained in:
Me
2022-12-24 23:09:44 +01:00
parent 4aa186fb48
commit 5cd5ffc24c
6 changed files with 119 additions and 111 deletions

View File

@@ -2,10 +2,12 @@ import { IsEnum, IsNotEmpty, IsString, IsPositive } from 'class-validator';
import { FriendshipStatus } from '../entities/friendship.entity'; import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto { export class CreateFriendshipDto {
// @IsNotEmpty()
@IsPositive() @IsPositive()
// @Max(1000) ? // @Max(1000) ?
readonly receiverId: number; readonly receiverId: number;
@IsNotEmpty()
@IsString()
readonly receiverUsername: string;
@IsEnum(FriendshipStatus) @IsEnum(FriendshipStatus)
readonly status: FriendshipStatus; readonly status: FriendshipStatus;
} }

View File

@@ -20,15 +20,20 @@ export class Friendship {
@ManyToOne(type => User, user => user.username) @ManyToOne(type => User, user => user.username)
sender: User; sender: User;
// hold on do i want this instead?
// @ManyToOne(type => User, user => user.id)
@ManyToOne(type => User, user => user.username) @ManyToOne(type => User, user => user.username)
receiver: User; receiver: User;
// ok so the username is still here because i think it might be useful for the frontend to have access to it, but really all i need is the ID's
// since i have the ID's do i even need the User ?
@Column() @Column()
senderUsername : string; senderUsername : string;
@Column() @Column()
senderId : number; senderId : number;
@Column()
receiverUsername : string;
@Column() @Column()
receiverId : number; receiverId : number;

View File

@@ -25,7 +25,7 @@ export class FriendshipController {
@Get('findfriends') @Get('findfriends')
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
findOne(@Query('username') otherUsername: string, @Query('id') id: string, @Req() req) { findOne(@Query('username') otherUsername: string, @Query('id') id: number, @Req() req) {
console.log('GET myfriend') console.log('GET myfriend')
const user = req.user; const user = req.user;
if (id !== undefined) { if (id !== undefined) {
@@ -105,7 +105,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
findAllPendantFriendshipRequested(@Req() req) { findAllPendantFriendshipRequested(@Req() req) {
const user = req.user; const user = req.user;
return this.friendshipService.findAllPendantRequestsForFriendship(user.username); return this.friendshipService.findAllPendantRequestsForFriendship(user.id.toString());
} }
// GET http://transcendance:8080/api/v2/network/received // GET http://transcendance:8080/api/v2/network/received
@@ -114,14 +114,14 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
findAllPendantFriendshipReceived(@Req() req) { findAllPendantFriendshipReceived(@Req() req) {
const user = req.user; const user = req.user;
return this.friendshipService.findAllReceivedRequestsForFriendship(user.username); return this.friendshipService.findAllReceivedRequestsForFriendship(user.id);
} }
// GET http://transcendance:8080/api/v2/network/blocked // GET http://transcendance:8080/api/v2/network/blocked
@Get('blocked') @Get('blocked')
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
findBlocked(@Query('relationshipId') relationshipId: string, @Req() req) { findBlocked(@Query('relationshipId') relationshipId: number, @Req() req) {
const user = req.user; const user = req.user;
if (relationshipId === undefined) if (relationshipId === undefined)
return this.friendshipService.findAllBlockedFriends(user.id); return this.friendshipService.findAllBlockedFriends(user.id);

View File

@@ -16,23 +16,21 @@ export class FriendshipService {
) { } ) { }
async findOneRelationshipById(friendId : string, userId : string) { async findOneRelationshipById(friendId : number, userId : number) {
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where( .where(
new Brackets((qb) => { new Brackets((qb) => {
qb.where( qb.where(
new Brackets((subAQb) => { new Brackets((subAQb) => {
subAQb.where('sender.id = :userId', { userId : userId}) subAQb.where('friendship.senderId = :userId', { userId : userId})
.andWhere('receiver.id = :friendId', {friendId : friendId}) .andWhere('friendship.receiverId = :friendId', {friendId : friendId})
}) })
) )
.orWhere( .orWhere(
new Brackets((subBQb) => { new Brackets((subBQb) => {
subBQb.where('sender.id = :friendId', {friendId : friendId}) subBQb.where('friendship.senderId = :friendId', {friendId : friendId})
.andWhere('receiver.id = :userId', {userId : userId}) .andWhere('friendship.receiverId = :userId', {userId : userId})
}) })
) )
}), }),
@@ -84,12 +82,12 @@ export class FriendshipService {
return friendship; return friendship;
} }
async findAllFriends(username: string) { async findAllFriends(userId: number) {
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .where('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
.andWhere('friendship.receiverUsername = :addressee', { addressee: username }) .andWhere('friendship.receiverId = :addressee', { addressee: userId })
.orWhere('friendship.senderUsername = :requester', { requester: username }) .orWhere('friendship.senderId = :requester', { requester: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.ACCEPTED })
.getMany(); .getMany();
// for (const friend of friendship) // for (const friend of friendship)
@@ -97,17 +95,17 @@ export class FriendshipService {
return friendship; return friendship;
} }
async findOneBlocked(friendshipId: string, username: string) { async findOneBlocked(friendshipId: number, userId: number) {
const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderUsername: username, status: FriendshipStatus.BLOCKED } }); const friendship = await this.friendshipRepository.find({ where: { id: +friendshipId, senderId: userId, status: FriendshipStatus.BLOCKED } });
if (!friendship) if (!friendship)
throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested blocked not found.`, HttpStatus.NOT_FOUND);
return friendship; return friendship;
} }
async findOneBlockedByUsername(blockedUsername : string, username : string) { async findOneBlockedByUsername(blockedUsername : string, userId : number) {
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.senderUsername = :username', {username : username}) .where('friendship.senderId = :senderId', {senderId : userId})
.andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : blockedUsername}) .andWhere('friendship.receiverUsername = :friendUsername', {friendUsername : blockedUsername})
.andWhere('friendship.status = :status ', {status : FriendshipStatus.BLOCKED}) .andWhere('friendship.status = :status ', {status : FriendshipStatus.BLOCKED})
.getOne() .getOne()
@@ -117,49 +115,56 @@ export class FriendshipService {
return friendship; return friendship;
} }
async findAllBlockedFriends(username: string) { async findAllBlockedFriends(userId: number) {
const friendships : Friendship[] = await this.friendshipRepository const friendships : Friendship[] = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.senderUsername = :requestee', { requestee: username }) .where('friendship.senderId = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.BLOCKED })
.getMany(); .getMany();
let partialFriendship : Partial<Friendship>[] = []; // let partialFriendship : Partial<Friendship>[] = [];
for (const friendship of friendships) { // for (const friendship of friendships) {
partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status}); // partialFriendship.push({id: friendship.id, date: friendship.date, senderUsername: friendship.senderUsername, receiverUsername: friendship.receiverUsername, status: friendship.status});
} // }
console.log('friendship.service findAllBlockedFriends, friendships:') console.log('friendship.service findAllBlockedFriends, friendships:')
console.log({...friendships}) console.log({...friendships})
console.log('friendship.service findAllBlockedFriends, partial friendship:') console.log('friendship.service findAllBlockedFriends, partial friendship:')
console.log({...partialFriendship}) // console.log({...partialFriendship})
return partialFriendship; // return partialFriendship;
return friendships;
} }
async findAllPendantRequestsForFriendship(username: string) { async findAllPendantRequestsForFriendship(userId: number) {
const friendship = await this.friendshipRepository const friendships = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.senderUsername = :requestee', { requestee: username }) .where('friendship.senderId = :requestee', { requestee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany(); .getMany();
let partialFriendship : Partial<Friendship>[] = []; // let partialFriendship : Partial<Friendship>[] = [];
for (const friend of friendship) { // for (const friend of friendships) {
console.log("FRIENDSHIP : " + friend); // console.log("FRIENDSHIP : " + friend);
partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); // partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status});
} // }
console.log("Pendant requests : " + partialFriendship); // console.log("Pendant requests : " + partialFriendship);
return partialFriendship; // return partialFriendship;
console.log('friendships services pendant friendships:')
console.log({...friendships})
return friendships;
} }
async findAllReceivedRequestsForFriendship(username: string) { async findAllReceivedRequestsForFriendship(userId: number) {
const friendship = await this.friendshipRepository const friendships = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.where('friendship.receiverUsername = :addressee', { addressee: username }) .where('friendship.receiverId = :addressee', { addressee: userId })
.andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED }) .andWhere('friendship.status = :status', { status: FriendshipStatus.REQUESTED })
.getMany(); .getMany();
let partialFriendship : Partial<Friendship>[] = []; // let partialFriendship : Partial<Friendship>[] = [];
for (const friend of friendship) { // for (const friend of friendship) {
partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status}); // partialFriendship.push({id: friend.id, senderUsername: friend.senderUsername, receiverUsername: friend.receiverUsername, status: friend.status});
} // }
return partialFriendship; // return partialFriendship;
console.log('friendship service received requests')
console.log({...friendships})
return friendships;
} }
async create(createFriendshipDto: CreateFriendshipDto, creator : User) : Promise <Partial<Friendship>> { async create(createFriendshipDto: CreateFriendshipDto, creator : User) : Promise <Partial<Friendship>> {
@@ -189,15 +194,17 @@ export class FriendshipService {
const newFriendship = new Friendship(); const newFriendship = new Friendship();
newFriendship.sender = creator; newFriendship.sender = creator;
newFriendship.senderUsername = creator.username; newFriendship.senderUsername = creator.username;
newFriendship.senderId = creator.id;
newFriendship.receiver = receiver; newFriendship.receiver = receiver;
newFriendship.receiverUsername = receiver.username; newFriendship.receiverUsername = receiver.username;
newFriendship.receiverId = receiver.id;
newFriendship.status = createFriendshipDto.status; newFriendship.status = createFriendshipDto.status;
const savedFriendship = this.friendshipRepository.save(newFriendship); const savedFriendship = this.friendshipRepository.save(newFriendship);
const partialFriendship : Partial<Friendship> = { const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id, id : (await savedFriendship).id,
date : (await savedFriendship).date, date : (await savedFriendship).date,
receiverUsername: (await savedFriendship).receiverUsername, receiverUsername: (await savedFriendship).receiverUsername,
// receiverId: (await savedFriendship).receiver.id, receiverId: (await savedFriendship).receiverId,
status : (await savedFriendship).status status : (await savedFriendship).status
} }
console.log('friendship.service create friendship, partial friendship') console.log('friendship.service create friendship, partial friendship')
@@ -208,20 +215,21 @@ export class FriendshipService {
} }
async acceptFriendship(relationshipId: string, user: User) { async acceptFriendship(relationshipId: string, user: User) {
const relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); const relation = await this.friendshipRepository.findOneBy({id: +relationshipId });
// console.log('.service accept friendship') // console.log('.service accept friendship')
// console.log({...relation}) // console.log({...relation})
if (!relation[0]) if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation[0].sender.id === user.id) { if (relation.senderId === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND); throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
} }
relation[0].status = FriendshipStatus.ACCEPTED; relation.status = FriendshipStatus.ACCEPTED;
const savedFriendship = this.friendshipRepository.save(relation[0]); const savedFriendship = this.friendshipRepository.save(relation);
const partialFriendship : Partial<Friendship> = { const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id, id : (await savedFriendship).id,
date : (await savedFriendship).date, date : (await savedFriendship).date,
receiverUsername: (await savedFriendship).receiverUsername, receiverUsername: (await savedFriendship).receiverUsername,
receiverId: (await savedFriendship).receiverId,
status : (await savedFriendship).status status : (await savedFriendship).status
} }
console.log('.service accept friendship END') console.log('.service accept friendship END')
@@ -251,20 +259,20 @@ export class FriendshipService {
} }
async blockFriendship(relationshipId: string, user: User) { async blockFriendship(relationshipId: string, user: User) {
let relation = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); let relation = await this.friendshipRepository.find({id: +relationshipId });
if (!relation[0]) if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND); throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
// do i need to check if they've already been blocked? // do i need to check if they've already been blocked?
if (relation[0].receiver.id === user.id) { if (relation.receiverId === user.id) {
// throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND); // throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND);
console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block') console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block')
console.log({...relation}) console.log({...relation})
// const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED) // const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED)
// const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"}) // const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"})
// we create a new one where you are the sender // we create a new one where you are the sender
const newFriendshipDto = {"receiverUsername": relation[0].sender.username, "status": FriendshipStatus.BLOCKED}; const newFriendshipDto = {"receiverUsername": relation.senderUsername, "receiverId": relation.senderId, "status": FriendshipStatus.BLOCKED};
// can't do it this way cuz READONLY // can't do it this way cuz READONLY
// const newFriendshipDto = new CreateFriendshipDto(); // const newFriendshipDto = new CreateFriendshipDto();
// newFriendshipDto.receiverUsername = relation[0].sender.username; // newFriendshipDto.receiverUsername = relation[0].sender.username;
@@ -274,12 +282,13 @@ export class FriendshipService {
// we set it to blocked like we do below... // we set it to blocked like we do below...
return await this.create(newFriendshipDto, user); return await this.create(newFriendshipDto, user);
} else { } else {
relation[0].status = FriendshipStatus.BLOCKED; relation.status = FriendshipStatus.BLOCKED;
const savedFriendship = this.friendshipRepository.save(relation[0]); const savedFriendship = this.friendshipRepository.save(relation);
const partialFriendship : Partial<Friendship> = { const partialFriendship : Partial<Friendship> = {
id : (await savedFriendship).id, id : (await savedFriendship).id,
date : (await savedFriendship).date, date : (await savedFriendship).date,
receiverUsername: (await savedFriendship).receiverUsername, receiverUsername: (await savedFriendship).receiverUsername,
receiverId: (await savedFriendship).receiverId,
status : (await savedFriendship).status status : (await savedFriendship).status
} }
return partialFriendship return partialFriendship
@@ -289,45 +298,43 @@ export class FriendshipService {
async removeFriendship(relationshipId: string, user : User) { async removeFriendship(relationshipId: string, user : User) {
// const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId }); // const friendship = await this.friendshipRepository.findOneBy({ id: +relationshipId });
// once again we need find() not findOneBy() so once again have to grab first elem of an array // once again we need find() not findOneBy() so once again have to grab first elem of an array
const friendship = await this.friendshipRepository.find({where: {id: +relationshipId }, relations: ['sender', 'receiver']} ); const friendship = await this.friendshipRepository.find({id: +relationshipId });
console.log('deleting a friendship .service') console.log('deleting a friendship .service')
console.log({...friendship}) console.log({...friendship})
console.log('who is the user') console.log('who is the user')
console.log({...user}) console.log({...user})
if (!friendship[0]) if (!friendship)
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND); throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
if (friendship[0].sender.id !== user.id && friendship[0].receiver.id !== user.id) { if (friendship.senderId !== user.id && friendship.receiverId !== user.id) {
throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN); throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN);
} }
console.log('.service deleted a friendship') console.log('.service deleted a friendship')
return this.friendshipRepository.remove(friendship[0]); return this.friendshipRepository.remove(friendship);
} }
//i think maybe i should be using ID rather than username... //i think maybe i should be using ID rather than username...
// async findIfUserIsBlockedOrHasBlocked(userConnectedUsername: string, userToFindUsername: string) { // async findIfUserIsBlockedOrHasBlocked(userConnectedUsername: string, userToFindUsername: string) {
async findIfUserIsBlockedOrHasBlocked(userConnectedId: string, userToFindId: string) { async findIfUserIsBlockedOrHasBlocked(userConnectedId: number, userToFindId: number) {
console.log("finding if user is blocked") console.log("finding if user is blocked")
console.log('user connected: ' + userConnectedId) console.log('user connected: ' + userConnectedId)
console.log('user to find: ' + userToFindId) console.log('user to find: ' + userToFindId)
const friendship = await this.friendshipRepository const friendship = await this.friendshipRepository
.createQueryBuilder('friendship') .createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
.where( .where(
new Brackets((qb) => { new Brackets((qb) => {
qb.where( qb.where(
new Brackets((subAQb) => { new Brackets((subAQb) => {
subAQb.where('sender.id = :senderId', { senderId: userConnectedId}) subAQb.where('friendship.senderId = :senderId', { senderId: userConnectedId})
.andWhere('receiver.id = :receiverId', { receiverId: userToFindId}) .andWhere('friendship.receiverId = :receiverId', { receiverId: userToFindId})
}) })
) )
.orWhere( .orWhere(
new Brackets((subBQb) => { new Brackets((subBQb) => {
subBQb.where('sender.id = :senderId', {senderId : userToFindId}) subBQb.where('friendship.senderId = :senderId', {senderId : userToFindId})
.andWhere('receiver.id = :receiverId', {receiverUsername : userConnectedId}) .andWhere('friendship.receiverId = :receiverId', {receiverUsername : userConnectedId})
}) })
) )
}), }),
@@ -347,7 +354,3 @@ export class FriendshipService {
return false; return false;
} }
} }
// SELECT "friendship"."id" AS "friendship_id", "friendship"."date" AS "friendship_date", "friendship"."senderUsername" AS "friendship_senderUsername", "friendship"."receiverUsername" AS "friendship_receiverUsername", "friendship"."status" AS "friendship_status", "friendship"."senderId" AS "friendship_senderId", "friendship"."receiverId" AS "friendship_receiverId", "sender"."id" AS "sender_id", "sender"."fortyTwoId" AS "sender_fortyTwoId", "sender"."username" AS "sender_username", "sender"."email" AS "sender_email", "sender"."image_url" AS "sender_image_url", "sender"."phone" AS "sender_phone", "sender"."status" AS "sender_status", "sender"."isEnabledTwoFactorAuth" AS "sender_isEnabledTwoFactorAuth", "sender"."isTwoFactorAuthenticated" AS "sender_isTwoFactorAuthenticated", "sender"."secretTwoFactorAuth" AS "sender_secretTwoFactorAuth", "sender"."statsId" AS "sender_statsId", "receiver"."id" AS "receiver_id", "receiver"."fortyTwoId" AS "receiver_fortyTwoId", "receiver"."username" AS "receiver_username", "receiver"."email" AS "receiver_email", "receiver"."image_url" AS "receiver_image_url", "receiver"."phone" AS "receiver_phone", "receiver"."status" AS "receiver_status", "receiver"."isEnabledTwoFactorAuth" AS "receiver_isEnabledTwoFactorAuth", "receiver"."isTwoFactorAuthenticated" AS "receiver_isTwoFactorAuthenticated", "receiver"."secretTwoFactorAuth" AS "receiver_secretTwoFactorAuth", "receiver"."statsId" AS "receiver_statsId" FROM "friendships" "friendship" LEFT JOIN "user" "sender" ON "sender"."id"="friendship"."senderId" LEFT JOIN "user" "receiver" ON "receiver"."id"="friendship"."receiverId" WHERE (("sender"."id" = $1 AND "receiver"."id" = :receiverId) OR ("sender"."id" = $2 AND "receiver"."id" = :receiverId)) AND "friendship"."status" = $3

View File

@@ -45,27 +45,24 @@ export class UsersController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Get() @Get()
findOne(@Query('username') username: string, @Req() req) { findOne(@Query('id') toFindId: number, @Req() req) {
if (username === undefined) { if (toFindId === undefined) {
console.log("Backend Getting current user"); console.log("Backend Getting current user");
return this.usersService.findOne(req.user.id); return this.usersService.findOne(req.user.id);
} else { } else {
const user : User = req.user; const user : User = req.user;
console.log('we have a query: ' + username) console.log('we have a query: ' + toFindId)
return this.usersService.findOneByUsername(user.id.toString(),username); return this.usersService.findOneById(user.id, toFindId);
} }
} }
// 99% sure this is useless
// GET http://transcendance:8080/user?username=NomDuUser // GET http://transcendance:8080/user?username=NomDuUser
// @UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
// @UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
// @Get('search') @Get()
// findOneByUsername(@Query('username') username: string, @Req() req) { findMe(@Req() req) {
// const user : User = req.user; return this.usersService.findOne(req.user.id);
// console.log('searching for user' + user.username); }
// return this.usersService.findOneByUsername(user.id.toString(),username);
// }
// also seems useless... // also seems useless...
// GET http://transcendance:8080/user/stats // GET http://transcendance:8080/user/stats

View File

@@ -60,29 +60,30 @@ export class UsersService {
return true; return true;
} }
async findOneByUsername(userConnectedId : string, usernameToFind: string) { // ok yea this shit makes no fucking sense any more since we're always gonna have the id, this is meant to return someone by username, fuck that
const userToFind : User = await this.userRepository.createQueryBuilder('user') // async findOneById(userId : number, toFindId: number) {
.leftJoinAndSelect('user.stats', 'stats') // const userToFind : User = await this.userRepository.createQueryBuilder('user')
.where('user.username = :username', { username: usernameToFind }) // .leftJoinAndSelect('user.stats', 'stats')
.getOne(); // .where('user.id = :toFindId', { toFindId: toFindId })
console.log('USERNAME OF FOUND USER : ' + userToFind.username); // .getOne();
// console.log({...user}) // console.log('USERNAME OF FOUND USER : ' + userToFind.username);
// you can't do that, you need to do user === undefined // // console.log({...user})
// if (user === undefined) // // you can't do that, you need to do user === undefined
if (!userToFind) // // if (user === undefined)
throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND); // if (!userToFind)
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id.toString())) { // throw new HttpException(`The user could not be found 1.`,HttpStatus.NOT_FOUND);
console.log('we are blocked in user.service') // if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(userConnectedId, userToFind.id)) {
throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND); // console.log('we are blocked in user.service')
} // throw new HttpException(`The user could not be found 2.`,HttpStatus.NOT_FOUND);
const partialUser : Partial<User> = { // }
username: userToFind.username, // const partialUser : Partial<User> = {
image_url: userToFind.image_url, // username: userToFind.username,
status: userToFind.status, // image_url: userToFind.image_url,
stats: userToFind.stats, // status: userToFind.status,
}; // stats: userToFind.stats,
return partialUser; // };
} // return partialUser;
// }
// fuck pagination Query // fuck pagination Query
// async findAll(paginationquery : PaginationQueryDto, currentUser: User) { // async findAll(paginationquery : PaginationQueryDto, currentUser: User) {
@@ -100,7 +101,7 @@ export class UsersService {
// console.log('user.services findIF Blocked... : ') // console.log('user.services findIF Blocked... : ')
// console.log(tmp) // console.log(tmp)
// if (tmp === false) { // if (tmp === false) {
if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id.toString(), otherUser.id.toString()) === false) { if (await this.friendshipService.findIfUserIsBlockedOrHasBlocked(currentUser.id, otherUser.id) === false) {
partialUsers.push({id: otherUser.id, username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats}); partialUsers.push({id: otherUser.id, username: otherUser.username, image_url: otherUser.image_url, status: otherUser.status, stats: otherUser.stats});
} }
} }