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)
@JoinTable()
followers: User[]
followers: User[];
@ManyToMany(() => User, (user) => user.friends)
friends: User[]
@ManyToMany(() => User)
@JoinTable()
friends: User[];
// @Column({ default: { wr: -1, place: -1 } })
// rank: { wr: number; place: number };

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

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

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

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

13
docker-compose.yml

@ -30,6 +30,8 @@ services:
container_name: postgres
image: postgres
ports: [5432:5432]
volumes:
- /data/postgres:/data/postgres
networks: [transcendence]
restart: always
env_file: .env
@ -42,6 +44,13 @@ services:
- "8080:80"
volumes:
- /data/pgadmin:/root/.pgadmin
env_file:
- .env
environment:
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]
logging:
driver: none

Loading…
Cancel
Save