amélioration du user. Maintenant les requêtes fonctionnent correctement. Doit toujours être testé en profondeur, mais apriori on ne peut plus accepter sa propre requête.
This commit is contained in:
@@ -11,7 +11,7 @@ import { routesForUsers } from './routes/routes';
|
||||
@Module({
|
||||
imports: [UsersModule,
|
||||
FriendshipsModule,
|
||||
RouterModule.register(routesForUsers),
|
||||
// RouterModule.register(routesForUsers),
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'postgres',
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { IsOptional, IsPositive } from "class-validator";
|
||||
|
||||
export class PaginationQueryDto {
|
||||
|
||||
@IsOptional()
|
||||
@IsPositive()
|
||||
limit: number;
|
||||
|
||||
@IsPositive()
|
||||
@IsOptional()
|
||||
offset: number;
|
||||
}
|
||||
@@ -5,30 +5,31 @@ import { UpdateFriendshipDto } from './dto/update-friendship.dto';
|
||||
import { FriendshipStatus } from './entities/friendship.entity';
|
||||
import { FriendshipService } from './friendship.service';
|
||||
|
||||
@Controller()
|
||||
@Controller('network')
|
||||
export class FriendshipController {
|
||||
constructor(private readonly friendshipService: FriendshipService) {}
|
||||
|
||||
@Get()
|
||||
@Get(':userId/friends')
|
||||
findEmpty(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllFriends(userId);
|
||||
}
|
||||
@Get('friends')
|
||||
findAllFriends(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllFriends(userId);
|
||||
}
|
||||
|
||||
@Get('blocked')
|
||||
@Get(':userId/blocked')
|
||||
findAllBlocked(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllBlockedFriends(userId);
|
||||
}
|
||||
|
||||
@Get('pendant')
|
||||
findAllPendant(@Param('userId') userId: string) {
|
||||
@Get(':userId/pending')
|
||||
findAllPendantFriendshipRequested(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllPendantRequestsForFriendship(userId);
|
||||
}
|
||||
|
||||
@Get('friends/:friendId')
|
||||
@Get(':userId/received')
|
||||
findAllPendantFriendshipReceived(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllReceivedRequestsForFriendship(userId);
|
||||
}
|
||||
|
||||
@Get(':userId/myfriends/:friendId')
|
||||
findOneFriend(@Param('friendId') friendId: string) {
|
||||
return this.friendshipService.findOneFriend(friendId);
|
||||
}
|
||||
@@ -39,9 +40,9 @@ export class FriendshipController {
|
||||
return this.friendshipService.create(createFriendshipDto);
|
||||
}
|
||||
|
||||
@Patch(':friendId')
|
||||
update(@Param('friendId') friendId: string, @Body() friendshipUpdateDto: UpdateFriendshipDto) {
|
||||
return this.friendshipService.updateFriendship(friendId, friendshipUpdateDto);
|
||||
@Patch(':userId/received/:relationshipId')
|
||||
update(@Param('friendId') relationshipId: string, @Body() {status}: UpdateFriendshipDto) {
|
||||
return this.friendshipService.updateFriendship(relationshipId, {status});
|
||||
}
|
||||
|
||||
@Delete(':userId/:friendId')
|
||||
|
||||
@@ -53,9 +53,22 @@ export class FriendshipService {
|
||||
const user = await this.userRepository.findOneBy({id: +id});
|
||||
if (!user)
|
||||
throw new HttpException(`The requested user not found.`,HttpStatus.NOT_FOUND);
|
||||
const blocked = await this.friendshipRepository.find(
|
||||
{where: {requesterId: id, status: FriendshipStatus.REQUESTED}});
|
||||
return blocked;
|
||||
const requests = await this.friendshipRepository.find({
|
||||
where:
|
||||
{requesterId: id, status: FriendshipStatus.REQUESTED},
|
||||
relations: ['requesterId']});
|
||||
return requests;
|
||||
}
|
||||
|
||||
async findAllReceivedRequestsForFriendship(id: string) {
|
||||
const user = await this.userRepository.findOneBy({id: +id});
|
||||
if (!user)
|
||||
throw new HttpException(`The requested user not found.`,HttpStatus.NOT_FOUND);
|
||||
const requests = await this.friendshipRepository.find({
|
||||
where:
|
||||
{addresseeId: id, status: FriendshipStatus.REQUESTED},
|
||||
relations: ['addresseeId']});
|
||||
return requests;
|
||||
}
|
||||
|
||||
async create(createFriendshipDto: CreateFriendshipDto) {
|
||||
@@ -80,13 +93,11 @@ export class FriendshipService {
|
||||
}
|
||||
const newFriendship = this.friendshipRepository.create(
|
||||
{requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId, status: FriendshipStatus.REQUESTED});
|
||||
const sendrquest = this.friendshipRepository.create(
|
||||
{requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId, status: FriendshipStatus.REQUESTED});
|
||||
this.friendshipRepository.save(sendrquest);
|
||||
return this.friendshipRepository.save(newFriendship);
|
||||
}
|
||||
|
||||
async updateFriendship(id: string, updateFriendshipDto: UpdateFriendshipDto) {
|
||||
|
||||
const friendship = await this.friendshipRepository.preload(
|
||||
{id: +id,
|
||||
...updateFriendshipDto});
|
||||
|
||||
@@ -15,6 +15,9 @@ async function bootstrap() {
|
||||
forbidNonWhitelisted: true,
|
||||
//permet de transformer les données en fonction de leur type
|
||||
transform: true,
|
||||
transformOptions: {//permet de transformer les données en fonction de leur type
|
||||
enableImplicitConversion: true,
|
||||
},
|
||||
}),
|
||||
);
|
||||
await app.listen(3000);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Exclude } from 'class-transformer';
|
||||
import { IsEmail, IsString } from 'class-validator';
|
||||
|
||||
export class CreateUsersDto {
|
||||
@@ -6,6 +7,7 @@ export class CreateUsersDto {
|
||||
@IsString()
|
||||
readonly username: string;
|
||||
@IsString()
|
||||
@Exclude()
|
||||
readonly password: string;
|
||||
@IsEmail()
|
||||
readonly email: string;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type } from "os";
|
||||
import { Exclude } from "class-transformer";
|
||||
import { Column, Entity, JoinTable, ManyToMany, OneToMany, PrimaryGeneratedColumn, Unique } from "typeorm";
|
||||
import { Friendship } from "../../friendship/entities/friendship.entity";
|
||||
|
||||
@@ -22,6 +22,7 @@ export class User {
|
||||
//Dans le cadre d'un tableau on peut faire :
|
||||
// @Column('json', { nullable: true })
|
||||
@Column()
|
||||
@Exclude()
|
||||
password: string;
|
||||
|
||||
@Column('json', { nullable: true })
|
||||
@@ -35,5 +36,7 @@ export class User {
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.addresseeId)
|
||||
addresseeId: Friendship[];
|
||||
|
||||
friendships: Friendship[];
|
||||
constructor(partial: Partial<User>) {
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,21 +2,22 @@ import {
|
||||
Body, Controller, Delete, Get, HttpCode,
|
||||
HttpStatus, Param, Patch, Post, Query
|
||||
} from '@nestjs/common';
|
||||
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
||||
import { CreateUsersDto } from './dto/create-users.dto';
|
||||
import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
|
||||
@Controller('/')
|
||||
@Controller('user')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
// par exemple dans postamn ou insomnia http://localhost:3000/users?limit=10&offset=20
|
||||
|
||||
@Get('all')
|
||||
findAll(@Query() query) {
|
||||
findAll(@Query() paginationquery : PaginationQueryDto) {
|
||||
//const { limit, offset } = query;
|
||||
return this.usersService.findAll();
|
||||
return this.usersService.findAll(paginationquery);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
|
||||
@@ -7,6 +7,7 @@ import { CreateUsersDto } from './dto/create-users.dto';
|
||||
import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
import { Friendship } from '../friendship/entities/friendship.entity';
|
||||
import { isNumberString } from 'class-validator';
|
||||
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
||||
|
||||
// On va devoir sûrement trouver un moyen plus simple pour passer l'id, sûrement via des pipes
|
||||
// ou des interceptors, mais pour l'instant on va faire comme ça.
|
||||
@@ -29,8 +30,13 @@ export class UsersService {
|
||||
return user;
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.userRepository.find();
|
||||
// Example : http://localhost:3000/users?limit=10&offset=20
|
||||
findAll(paginationquery : PaginationQueryDto) {
|
||||
const { limit, offset } = paginationquery;
|
||||
return this.userRepository.find({
|
||||
skip: offset,
|
||||
take: limit,
|
||||
});
|
||||
}
|
||||
|
||||
async create(createUserDto: CreateUsersDto) {
|
||||
|
||||
Reference in New Issue
Block a user