Browse Source

working friends

.
master
nicolas-arnaud 2 years ago
parent
commit
e729cf1561
  1. 7
      back/volume/src/users/user.entity.ts
  2. 7
      back/volume/src/users/users.controller.ts
  3. 27
      back/volume/src/users/users.service.ts
  4. 13
      docker-compose.yml

7
back/volume/src/users/user.entity.ts

@ -39,10 +39,11 @@ export class User {
@ManyToMany(() => User) @ManyToMany(() => User)
@JoinTable() @JoinTable()
followers: User[] followers: User[];
@ManyToMany(() => User, (user) => user.friends) @ManyToMany(() => User)
friends: User[] @JoinTable()
friends: User[];
// @Column({ default: { wr: -1, place: -1 } }) // @Column({ default: { wr: -1, place: -1 } })
// rank: { wr: number; place: number }; // rank: { wr: number; place: number };

7
back/volume/src/users/users.controller.ts

@ -8,7 +8,6 @@ import {
UploadedFile, UploadedFile,
UseGuards, UseGuards,
UseInterceptors, UseInterceptors,
Req,
Res, Res,
StreamableFile, StreamableFile,
BadRequestException BadRequestException
@ -56,9 +55,9 @@ export class UsersController {
@UseGuards(AuthenticatedGuard) @UseGuards(AuthenticatedGuard)
followUser( followUser(
@FtUser() profile: Profile, @FtUser() profile: Profile,
@Param('id, ParseIntPipe') id: number, @Param('id', ParseIntPipe) id: number,
) { ) {
this.usersService.invit(profile.id, id); return this.usersService.invit(profile.id, id);
} }
@Post('avatar') @Post('avatar')
@ -86,7 +85,7 @@ export class UsersController {
@FtUser() profile: Profile, @FtUser() profile: Profile,
@UploadedFile() file: Express.Multer.File @UploadedFile() file: Express.Multer.File
) { ) {
await this.usersService.addAvatar(profile.id, file.filename) return await this.usersService.addAvatar(profile.id, file.filename)
} }
@Get('avatar') @Get('avatar')

27
back/volume/src/users/users.service.ts

@ -20,7 +20,13 @@ export class UsersService {
} }
async findUser(ftId: number): Promise<User | null> { async findUser(ftId: number): Promise<User | null> {
return await this.usersRepository.findOneBy({ ftId }) return await this.usersRepository.findOne({
where: { ftId },
relations: {
friends: true,
followers: true,
}
});
} }
async create(userData: UserDto) { async create(userData: UserDto) {
@ -47,27 +53,26 @@ export class UsersService {
} }
async addAvatar(ftId: number, filename: string) { async addAvatar(ftId: number, filename: string) {
await this.usersRepository.update(ftId, { return await this.usersRepository.update(ftId, {
avatar: filename avatar: filename
}) })
} }
async invit(ftId: number, targetFtId: number) { async invit(ftId: number, targetFtId: number) {
const user = await this.usersRepository.findOne({ const user = await this.findUser(ftId);
where: { ftId }, const target = await this.findUser(targetFtId);
relations: { friends: true, followers: true }, if (!target) {
}); return new NotFoundException(`Error: user id ${targetFtId} isn't in our db.`)
const target = await this.usersRepository.findOne({ }
where: { ftId: targetFtId }, const id = user.followers.findIndex((follower) => follower.ftId === targetFtId)
relations: { friends: true, followers: true },
});
const id = user.followers.findIndex((follower) => follower.ftId == ftId)
if (id != -1) { if (id != -1) {
console.log(`Friend relation complete between ${user.username} and ${target.username}`);
user.friends.push(target); user.friends.push(target);
target.friends.push(user); target.friends.push(user);
user.followers.slice(id, 1); user.followers.slice(id, 1);
this.usersRepository.save(user); this.usersRepository.save(user);
} else { } else {
console.log(`You asked ${target.username} to be your friend.`);
target.followers.push(user); target.followers.push(user);
} }
this.usersRepository.save(target); this.usersRepository.save(target);

13
docker-compose.yml

@ -30,6 +30,8 @@ services:
container_name: postgres container_name: postgres
image: postgres image: postgres
ports: [5432:5432] ports: [5432:5432]
volumes:
- /data/postgres:/data/postgres
networks: [transcendence] networks: [transcendence]
restart: always restart: always
env_file: .env env_file: .env
@ -42,6 +44,13 @@ services:
- "8080:80" - "8080:80"
volumes: volumes:
- /data/pgadmin:/root/.pgadmin - /data/pgadmin:/root/.pgadmin
env_file: environment:
- .env PGADMIN_DEFAULT_EMAIL: 'usr@usr.com'
PGADMIN_DEFAULT_PASSWORD: 'pw'
GUNICORN_ACCESS_LOGFILE: '/dev/null'
PGADMIN_CONFIG_UPGRADE_CHECK_ENABLED: 'False'
depends_on:
- postgres
networks: [transcendence] networks: [transcendence]
logging:
driver: none

Loading…
Cancel
Save