Browse Source

changed routes

master
nicolas-arnaud 2 years ago
parent
commit
f3821a0b76
  1. 7
      back/volume/src/api/api.module.ts
  2. 10
      back/volume/src/app.controller.ts
  3. 6
      back/volume/src/app.module.ts
  4. 2
      back/volume/src/auth/42.decorator.ts
  5. 8
      back/volume/src/auth/requestWithUser.interface.ts
  6. 15
      back/volume/src/users/user.dto.ts
  7. 16
      back/volume/src/users/user.entity.ts
  8. 44
      back/volume/src/users/users.controller.ts
  9. 11
      back/volume/src/users/users.service.ts

7
back/volume/src/api/api.module.ts

@ -1,7 +0,0 @@
import { Module } from '@nestjs/common'
import { ApiController } from './api.controller'
@Module({
controllers: [ApiController]
})
export class ApiModule {}

10
back/volume/src/api/api.controller.ts → back/volume/src/app.controller.ts

@ -1,14 +1,14 @@
import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common' import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common'
import { User } from 'src/auth/42.decorator'
import { AuthenticatedGuard } from 'src/auth/42-auth.guard'
import { Profile } from 'passport-42'
import { Request } from 'express' import { Request } from 'express'
import { Profile } from 'passport-42'
import { FtUser } from './auth/42.decorator'
import { AuthenticatedGuard } from './auth/42-auth.guard'
@Controller() @Controller()
export class ApiController { export class AppController {
@Get('profile') @Get('profile')
@UseGuards(AuthenticatedGuard) @UseGuards(AuthenticatedGuard)
profile (@User() user: Profile) { profile (@FtUser() user: Profile) {
return { user } return { user }
} }

6
back/volume/src/app.module.ts

@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config' import { ConfigModule } from '@nestjs/config'
import * as Joi from 'joi' import * as Joi from 'joi'
import { ApiModule } from './api/api.module' import { AppController } from './app.controller'
import { AuthModule } from './auth/auth.module' import { AuthModule } from './auth/auth.module'
import { ChatModule } from './chat/chat.module' import { ChatModule } from './chat/chat.module'
import { DbModule } from './db/db.module' import { DbModule } from './db/db.module'
@ -23,12 +23,12 @@ import { UsersModule } from './users/users.module'
JWT_EXPIRATION_TIME: Joi.string().required() JWT_EXPIRATION_TIME: Joi.string().required()
}) })
}), }),
ApiModule,
AuthModule, AuthModule,
ChatModule, ChatModule,
DbModule, DbModule,
PongModule, PongModule,
UsersModule UsersModule
] ],
controllers: [AppController]
}) })
export class AppModule { } export class AppModule { }

2
back/volume/src/auth/42.decorator.ts

@ -1,7 +1,7 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common' import { createParamDecorator, type ExecutionContext } from '@nestjs/common'
import { type Profile } from 'passport-42' import { type Profile } from 'passport-42'
export const User = createParamDecorator( export const FtUser = createParamDecorator(
(data: unknown, ctx: ExecutionContext): Profile => { (data: unknown, ctx: ExecutionContext): Profile => {
const request = ctx.switchToHttp().getRequest() const request = ctx.switchToHttp().getRequest()
return request.user return request.user

8
back/volume/src/auth/requestWithUser.interface.ts

@ -1,8 +0,0 @@
import { type Request } from 'express'
import type User from 'src/users/user.entity'
interface RequestWithUser extends Request {
user: User
}
export default RequestWithUser

15
back/volume/src/users/user.dto.ts

@ -1,9 +1,6 @@
import { import {
IsString, IsString,
IsNumber,
IsNotEmpty, IsNotEmpty,
IsEmail,
Length,
IsPositive, IsPositive,
IsOptional IsOptional
} from 'class-validator' } from 'class-validator'
@ -11,17 +8,7 @@ import {
import { ApiProperty } from '@nestjs/swagger' import { ApiProperty } from '@nestjs/swagger'
import { Express } from 'express' import { Express } from 'express'
export class CreateUserDto { export class UserDto {
@IsPositive()
@IsNotEmpty()
readonly id_42: number
@IsString()
@IsNotEmpty()
readonly username: string
}
export class UpdateUserDto {
@IsPositive() @IsPositive()
@IsNotEmpty() @IsNotEmpty()
readonly id_42: number readonly id_42: number

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

@ -2,10 +2,9 @@ import {
Entity, Entity,
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
Column, Column,
OneToOne,
OneToMany, OneToMany,
ManyToMany, ManyToMany,
JoinColumn JoinTable
} from 'typeorm' } from 'typeorm'
import Message from 'src/chat/model/message.entity' import Message from 'src/chat/model/message.entity'
@ -13,6 +12,9 @@ import Channel from 'src/chat/model/channel.entity'
@Entity() @Entity()
export class User { export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true }) @Column({ unique: true })
id_42: number id_42: number
@ -32,9 +34,17 @@ export class User {
rooms: Channel[] rooms: Channel[]
@ManyToMany(() => User) @ManyToMany(() => User)
@JoinTable()
blocked: User[] blocked: User[]
@ManyToMany(() => User) @ManyToMany(() => User, (user) => user.following)
@JoinTable()
followers: User[]
@ManyToMany(() => User, (user) => user.followers)
following: User[]
@ManyToMany(() => User, (user) => user.friends)
friends: User[] friends: User[]
// @Column({ default: { wr: -1, place: -1 } }) // @Column({ default: { wr: -1, place: -1 } })

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

@ -19,13 +19,14 @@ import { diskStorage } from 'multer'
import { type User } from './user.entity' import { type User } from './user.entity'
import { UsersService } from './users.service' import { UsersService } from './users.service'
import { CreateUserDto, UpdateUserDto, AvatarUploadDto } from './user.dto' import { UserDto, AvatarUploadDto } from './user.dto'
import RequestWithUser from 'src/auth/requestWithUser.interface' import { AuthenticatedGuard } from 'src/auth/42-auth.guard'
import { FtOauthGuard } from 'src/auth/42-auth.guard' import { FtUser } from 'src/auth/42.decorator'
import { ApiBody, ApiConsumes } from '@nestjs/swagger' import { Profile } from 'passport-42'
import { Response } from 'express' import { ApiBody, ApiConsumes } from '@nestjs/swagger'
import { Request, Response } from 'express'
import { createReadStream } from 'fs' import { createReadStream } from 'fs'
import { join } from 'path' import { join } from 'path'
@ -39,16 +40,29 @@ export class UsersController {
} }
@Post() @Post()
async create (@Body() payload: CreateUserDto) { @UseGuards(AuthenticatedGuard)
async create(
@Body() payload: UserDto,
@FtUser() profile: Profile) {
const user = await this.usersService.getOneUser42(profile.id);
if (user) {
return await this.usersService.update(user.id, payload)
} else {
return await this.usersService.create(payload) return await this.usersService.create(payload)
} }
}
@Post(':id') @Post("follow/:target")
update (@Param('id', ParseIntPipe) id: number, @Body() user: UpdateUserDto) { @UseGuards(AuthenticatedGuard)
this.usersService.update(id, user) followUser(
@FtUser() profile: Profile,
@Param('target, ParseIntPipe') target: number,
) {
this.usersService.follow(profile.id, target);
} }
@Post(':id/avatar') @Post('avatar')
@UseGuards(AuthenticatedGuard)
@UseInterceptors( @UseInterceptors(
FileInterceptor('avatar', { FileInterceptor('avatar', {
storage: diskStorage({ storage: diskStorage({
@ -69,18 +83,18 @@ export class UsersController {
type: AvatarUploadDto type: AvatarUploadDto
}) })
async addAvatar( async addAvatar(
@Param('id', ParseIntPipe) id: number, @FtUser() profile: Profile,
@UploadedFile() file: Express.Multer.File @UploadedFile() file: Express.Multer.File
) { ) {
await this.usersService.addAvatar(id, file.filename) await this.usersService.addAvatar(profile.id, file.filename)
} }
@Get(':id/avatar') @Get('avatar')
async getAvatar( async getAvatar(
@Param('id', ParseIntPipe) id: number, @FtUser() profile: Profile,
@Res({ passthrough: true }) response: Response @Res({ passthrough: true }) response: Response
) { ) {
const user = await this.usersService.findOne(id) const user = await this.usersService.getOneUser42(profile.id)
const filename = user.avatar const filename = user.avatar
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename)) const stream = createReadStream(join(process.cwd(), 'avatars/' + filename))
response.set({ response.set({

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

@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm' import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm' import { Repository } from 'typeorm'
import { User } from './user.entity' import { User } from './user.entity'
import { type CreateUserDto, type UpdateUserDto } from './user.dto' import { UserDto } from './user.dto'
import { type Channel } from 'src/chat/model/channel.entity' import { type Channel } from 'src/chat/model/channel.entity'
@Injectable() @Injectable()
@ -23,7 +23,7 @@ export class UsersService {
return await this.usersRepository.findOneBy({ id_42 }) return await this.usersRepository.findOneBy({ id_42 })
} }
async create (userData: CreateUserDto) { async create (userData: UserDto) {
try { try {
const newUser = this.usersRepository.create(userData) const newUser = this.usersRepository.create(userData)
return await this.usersRepository.save(newUser) return await this.usersRepository.save(newUser)
@ -46,7 +46,7 @@ export class UsersService {
.getMany() .getMany()
} }
async update (id: number, changes: UpdateUserDto) { async update (id: number, changes: UserDto) {
const updatedUser = await this.findOne(id) const updatedUser = await this.findOne(id)
this.usersRepository.merge(updatedUser, changes) this.usersRepository.merge(updatedUser, changes)
return await this.usersRepository.save(updatedUser) return await this.usersRepository.save(updatedUser)
@ -57,4 +57,9 @@ export class UsersService {
avatar: filename avatar: filename
}) })
} }
async follow(userFtId: number, targetFtId: number) {
const user = await this.getOneUser42(userFtId);
const target = await this.getOneUser42(targetFtId);
}
} }

Loading…
Cancel
Save