ajout de routes basiques, à améliorer (certaines requêtes renvoient une erreur interne et pas un 404), ajout du service pour les relations amicales, mais pour l'instant c'est ultra pété
This commit is contained in:
@@ -4,13 +4,14 @@ import { AppService } from './app.service';
|
||||
import { UsersModule } from './users/users.module';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { FriendshipstatusesModule } from './friendship/friendshipstatuses/friendshipstatuses.module';
|
||||
import { FriendshipController } from './friendship/friendship/friendship.controller';
|
||||
import { Module } from './friendship/.module';
|
||||
|
||||
import { FriendshipsModule } from './friendship/friendships.module';
|
||||
import { RouterModule } from '@nestjs/core';
|
||||
import { routesForUsers } from './routes/routes';
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule,
|
||||
FriendshipsModule,
|
||||
RouterModule.register(routesForUsers),
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'postgres',
|
||||
@@ -24,10 +25,8 @@ import { Module } from './friendship/.module';
|
||||
//avec une classe pour le module
|
||||
synchronize: true,
|
||||
}),
|
||||
FriendshipstatusesModule,
|
||||
Module,
|
||||
],
|
||||
controllers: [AppController, FriendshipController],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { IsEnum, IsInt } from 'class-validator';
|
||||
import { IsEnum, IsInt, IsNumber, IsOptional, IsPositive, IsString } from 'class-validator';
|
||||
import { FriendshipStatus } from '../entities/friendship.entity';
|
||||
|
||||
export class CreateFriendshipDto {
|
||||
@IsInt()
|
||||
readonly requesterId: number;
|
||||
@IsInt()
|
||||
readonly addresseeId: number;
|
||||
@IsString()
|
||||
readonly requesterId: string;
|
||||
@IsString()
|
||||
readonly addresseeId: string;
|
||||
@IsEnum(FriendshipStatus)
|
||||
@IsOptional()
|
||||
readonly status: FriendshipStatus;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Column, CreateDateColumn, Entity, ManyToOne, OneToMany, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { IsOptional } from "class-validator";
|
||||
import { Column, CreateDateColumn, Entity, JoinTable, ManyToOne, OneToMany, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
|
||||
export enum FriendshipStatus {
|
||||
@@ -16,14 +17,16 @@ export class Friendship {
|
||||
@CreateDateColumn()
|
||||
date : Date;
|
||||
|
||||
|
||||
@Column()
|
||||
@ManyToOne(type => User, user => user.requesterId)
|
||||
requesterId: number;
|
||||
requesterId: string;
|
||||
|
||||
@Column()
|
||||
@ManyToOne(type => User, user => user.addresseeId)
|
||||
addresseeId: number;
|
||||
addresseeId: string;
|
||||
|
||||
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED })
|
||||
|
||||
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED})
|
||||
status: FriendshipStatus;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,51 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, HttpCode, HttpException, HttpStatus, Param, Patch, Post } from '@nestjs/common';
|
||||
import { CreateUsersDto } from 'src/users/dto/create-users.dto';
|
||||
import { CreateFriendshipDto } from './dto/create-friendship.dto';
|
||||
import { UpdateFriendshipDto } from './dto/update-friendship.dto';
|
||||
import { FriendshipStatus } from './entities/friendship.entity';
|
||||
import { FriendshipService } from './friendship.service';
|
||||
|
||||
@Controller('friendship')
|
||||
export class FriendshipController {}
|
||||
@Controller()
|
||||
export class FriendshipController {
|
||||
constructor(private readonly friendshipService: FriendshipService) {}
|
||||
|
||||
@Get()
|
||||
findEmpty(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllFriends(userId);
|
||||
}
|
||||
@Get('friends')
|
||||
findAllFriends(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllFriends(userId);
|
||||
}
|
||||
|
||||
@Get('blocked')
|
||||
findAllBlocked(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllBlockedFriends(userId);
|
||||
}
|
||||
|
||||
@Get('pendant')
|
||||
findAllPendant(@Param('userId') userId: string) {
|
||||
return this.friendshipService.findAllPendantRequestsForFriendship(userId);
|
||||
}
|
||||
|
||||
@Get('friends/:friendId')
|
||||
findOneFriend(@Param('friendId') friendId: string) {
|
||||
return this.friendshipService.findOneFriend(friendId);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
create(@Body() createFriendshipDto: CreateFriendshipDto) {
|
||||
return this.friendshipService.create(createFriendshipDto);
|
||||
}
|
||||
|
||||
@Patch(':friendId')
|
||||
update(@Param('friendId') friendId: string, @Body() friendshipUpdateDto: UpdateFriendshipDto) {
|
||||
return this.friendshipService.updateFriendship(friendId, friendshipUpdateDto);
|
||||
}
|
||||
|
||||
@Delete(':userId/:friendId')
|
||||
remove(@Param('friendId') friendId: string) {
|
||||
return this.friendshipService.removeFriendship(friendId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Any, Repository } from 'typeorm';
|
||||
import { CreateFriendshipDto } from './dto/create-friendship.dto';
|
||||
import { UpdateFriendshipDto } from './dto/update-friendship.dto';
|
||||
import { Friendship, FriendshipStatus } from './entities/friendship.entity';
|
||||
@@ -17,10 +17,17 @@ export class FriendshipService {
|
||||
) {}
|
||||
|
||||
|
||||
async findOne(id: string) {
|
||||
const friendship = await this.friendshipRepository.findOneBy({id: +id});
|
||||
async findOneFriend(id: string) {
|
||||
const friendship = await this.friendshipRepository.find({where: {id: +id, status: FriendshipStatus.ACCEPTED}});
|
||||
if (!friendship)
|
||||
throw new HttpException(`The requested friendship not found.`,HttpStatus.NOT_FOUND);
|
||||
throw new HttpException(`The requested friend not found.`,HttpStatus.NOT_FOUND);
|
||||
return friendship;
|
||||
}
|
||||
|
||||
async findOneBlocked(id: string) {
|
||||
const friendship = await this.friendshipRepository.find({where: {id: +id, status: FriendshipStatus.BLOCKED}});
|
||||
if (!friendship)
|
||||
throw new HttpException(`The requested friend not found.`,HttpStatus.NOT_FOUND);
|
||||
return friendship;
|
||||
}
|
||||
|
||||
@@ -28,7 +35,8 @@ 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 friends = await this.friendshipRepository.find({where: {requesterId: +id, status: FriendshipStatus.ACCEPTED}});
|
||||
const friends = await this.friendshipRepository.find(
|
||||
{where: {requesterId: id, status: FriendshipStatus.ACCEPTED}});
|
||||
return friends;
|
||||
}
|
||||
|
||||
@@ -36,29 +44,45 @@ 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.BLOCKED}});
|
||||
const blocked = await this.friendshipRepository.find(
|
||||
{where: {requesterId: id, status: FriendshipStatus.BLOCKED}});
|
||||
return blocked;
|
||||
}
|
||||
|
||||
async create({requesterId, addresseeId, status }: CreateFriendshipDto) {
|
||||
const requester = await this.userRepository.findOneBy({id : +requesterId});
|
||||
async findAllPendantRequestsForFriendship(id: string) {
|
||||
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;
|
||||
}
|
||||
|
||||
async create(createFriendshipDto: CreateFriendshipDto) {
|
||||
const requester = await this.userRepository.findOneBy({id : +createFriendshipDto.requesterId});
|
||||
if (!requester)
|
||||
throw new HttpException(`The user does not exist.`,HttpStatus.NOT_FOUND);
|
||||
const addressee = await this.userRepository.findOneBy({id: +addresseeId});
|
||||
const addressee = await this.userRepository.findOneBy({id: +createFriendshipDto.addresseeId});
|
||||
if (!addressee)
|
||||
throw new HttpException(`The user does not exist.`,HttpStatus.NOT_FOUND);
|
||||
if (requesterId == addresseeId)
|
||||
if (createFriendshipDto.requesterId == createFriendshipDto.addresseeId)
|
||||
throw new HttpException(`You can't add yourself.`,HttpStatus.NOT_FOUND);
|
||||
const friendship = this.friendshipRepository.findOneBy({requesterId: +requesterId, addresseeId: +addresseeId});
|
||||
if ((await friendship).status === 'R')
|
||||
throw new HttpException(`The friendship request has already been sent the ${(await friendship).date}.`,HttpStatus.OK);
|
||||
if ((await friendship).status === 'A')
|
||||
throw new HttpException(`The friendship request has already been accepted.`,HttpStatus.OK);
|
||||
if ((await friendship).status === 'D')
|
||||
throw new HttpException(``,HttpStatus.OK);
|
||||
if ((await friendship).status === 'B')
|
||||
throw new HttpException(`The user does not exist.`,HttpStatus.OK);
|
||||
const newFriendship = this.friendshipRepository.create({requesterId: +requesterId, addresseeId: +addresseeId, status: status});
|
||||
const friendship = await this.friendshipRepository.findOneBy({requesterId: createFriendshipDto.requesterId, addresseeId: createFriendshipDto.addresseeId});
|
||||
if (friendship) {
|
||||
if (friendship.status && friendship.status === FriendshipStatus.ACCEPTED)
|
||||
throw new HttpException(`The friendship request has already been accepted.`,HttpStatus.OK);
|
||||
else if (friendship.status && friendship.status === FriendshipStatus.REQUESTED)
|
||||
throw new HttpException(`The friendship request has already been sent the ${friendship.date}.`,HttpStatus.OK);
|
||||
else if (friendship.status && friendship.status === FriendshipStatus.BLOCKED)
|
||||
throw new HttpException(``,HttpStatus.OK);
|
||||
else if (friendship.status && friendship.status === FriendshipStatus.DECLINED)
|
||||
throw new HttpException(`The request has been declined.`,HttpStatus.OK);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -72,7 +96,7 @@ export class FriendshipService {
|
||||
}
|
||||
|
||||
async removeFriendship(id: string) {
|
||||
const friendship = await this.findOne(id);
|
||||
const friendship = await this.findOneFriend(id);
|
||||
if (!friendship)
|
||||
throw new HttpException(`Your friend could not be deleted.`,HttpStatus.NOT_FOUND);
|
||||
return this.friendshipRepository.remove(friendship);
|
||||
|
||||
16
srcs/requirements/nestjs/api_back/src/routes/routes.ts
Normal file
16
srcs/requirements/nestjs/api_back/src/routes/routes.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Routes } from "@nestjs/core";
|
||||
import { FriendshipsModule } from "src/friendship/friendships.module";
|
||||
import { UsersModule } from "src/users/users.module";
|
||||
|
||||
export const routesForUsers: Routes = [
|
||||
{
|
||||
path: '/users',
|
||||
module: UsersModule,
|
||||
children: [
|
||||
{
|
||||
path: '/:userId/mycontacts',
|
||||
module: FriendshipsModule,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
@@ -34,4 +34,6 @@ export class User {
|
||||
@JoinTable()
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.addresseeId)
|
||||
addresseeId: Friendship[];
|
||||
|
||||
friendships: Friendship[];
|
||||
}
|
||||
|
||||
@@ -8,11 +8,12 @@ import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
|
||||
@Controller('users')
|
||||
@Controller('/')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
// par exemple dans postamn ou insomnia http://localhost:3000/users?limit=10&offset=20
|
||||
@Get()
|
||||
|
||||
@Get('all')
|
||||
findAll(@Query() query) {
|
||||
//const { limit, offset } = query;
|
||||
return this.usersService.findAll();
|
||||
|
||||
@@ -2,12 +2,14 @@ import { HttpCode, HttpException, HttpStatus, Injectable, NotFoundException, }
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { NotFoundError } from 'rxjs';
|
||||
import { User } from './entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ConnectionOptionsReader, Repository } from 'typeorm';
|
||||
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';
|
||||
|
||||
|
||||
// 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.
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
|
||||
@@ -19,6 +21,8 @@ export class UsersService {
|
||||
) {}
|
||||
|
||||
async findOne(id: string) {
|
||||
if (!isNumberString(id))
|
||||
throw new HttpException(`The requested user not found.`,HttpStatus.NOT_FOUND);
|
||||
const user = await this.userRepository.findOneBy({id: +id});
|
||||
if (!user)
|
||||
throw new NotFoundException(`The requested user not found.`);
|
||||
@@ -29,7 +33,9 @@ export class UsersService {
|
||||
return this.userRepository.find();
|
||||
}
|
||||
|
||||
create(createUserDto: CreateUsersDto) {
|
||||
async create(createUserDto: CreateUsersDto) {
|
||||
if (await this.userRepository.findOneBy({email: createUserDto.email}))
|
||||
throw new HttpException(`The user already exists.`,HttpStatus.CONFLICT);
|
||||
const user = this.userRepository.create(createUserDto);
|
||||
if (!user)
|
||||
throw new HttpException(`The user could not be created.`,HttpStatus.NOT_FOUND);
|
||||
|
||||
Reference in New Issue
Block a user