ajout des méthodes basiques pour le user + méthodes basique pour les relations amicales. Doit être testé. Manque les routes pour les relations amicales.
This commit is contained in:
@@ -4,6 +4,9 @@ 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';
|
||||
|
||||
|
||||
@Module({
|
||||
@@ -21,8 +24,10 @@ import { ConfigModule } from '@nestjs/config';
|
||||
//avec une classe pour le module
|
||||
synchronize: true,
|
||||
}),
|
||||
FriendshipstatusesModule,
|
||||
Module,
|
||||
],
|
||||
controllers: [AppController],
|
||||
controllers: [AppController, FriendshipController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { IsEnum, IsInt } from 'class-validator';
|
||||
import { FriendshipStatus } from '../entities/friendship.entity';
|
||||
|
||||
export class CreateFriendshipDto {
|
||||
@IsInt()
|
||||
readonly requesterId: number;
|
||||
@IsInt()
|
||||
readonly addresseeId: number;
|
||||
@IsEnum(FriendshipStatus)
|
||||
readonly status: FriendshipStatus;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from "@nestjs/mapped-types";
|
||||
import { CreateFriendshipDto } from "./create-friendship.dto";
|
||||
|
||||
export class UpdateFriendshipDto extends PartialType(CreateFriendshipDto){}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Column, CreateDateColumn, Entity, ManyToOne, OneToMany, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
|
||||
export enum FriendshipStatus {
|
||||
REQUESTED = 'R',
|
||||
ACCEPTED = 'A',
|
||||
DECLINED = 'D',
|
||||
BLOCKED = 'B',
|
||||
}
|
||||
|
||||
@Entity('friendships')
|
||||
export class Friendship {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@CreateDateColumn()
|
||||
date : Date;
|
||||
|
||||
@Column()
|
||||
@ManyToOne(type => User, user => user.requesterId)
|
||||
requesterId: number;
|
||||
|
||||
@Column()
|
||||
@ManyToOne(type => User, user => user.addresseeId)
|
||||
addresseeId: number;
|
||||
|
||||
@Column({ type: 'enum', enum: FriendshipStatus, default: FriendshipStatus.REQUESTED })
|
||||
status: FriendshipStatus;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { FriendshipController } from './friendship.controller';
|
||||
|
||||
describe('FriendshipController', () => {
|
||||
let controller: FriendshipController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [FriendshipController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<FriendshipController>(FriendshipController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
|
||||
@Controller('friendship')
|
||||
export class FriendshipController {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { FriendshipService } from './friendship.service';
|
||||
|
||||
describe('FriendshipService', () => {
|
||||
let service: FriendshipService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [FriendshipService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<FriendshipService>(FriendshipService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
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 { CreateFriendshipDto } from './dto/create-friendship.dto';
|
||||
import { UpdateFriendshipDto } from './dto/update-friendship.dto';
|
||||
import { Friendship, FriendshipStatus } from './entities/friendship.entity';
|
||||
|
||||
@Injectable()
|
||||
export class FriendshipService {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Friendship)
|
||||
private readonly friendshipRepository: Repository<Friendship>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
|
||||
async findOne(id: string) {
|
||||
const friendship = await this.friendshipRepository.findOneBy({id: +id});
|
||||
if (!friendship)
|
||||
throw new HttpException(`The requested friendship not found.`,HttpStatus.NOT_FOUND);
|
||||
return friendship;
|
||||
}
|
||||
|
||||
async findAllFriends(id: string) {
|
||||
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}});
|
||||
return friends;
|
||||
}
|
||||
|
||||
async findAllBlockedFriends(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.BLOCKED}});
|
||||
return blocked;
|
||||
}
|
||||
|
||||
async create({requesterId, addresseeId, status }: CreateFriendshipDto) {
|
||||
const requester = await this.userRepository.findOneBy({id : +requesterId});
|
||||
if (!requester)
|
||||
throw new HttpException(`The user does not exist.`,HttpStatus.NOT_FOUND);
|
||||
const addressee = await this.userRepository.findOneBy({id: +addresseeId});
|
||||
if (!addressee)
|
||||
throw new HttpException(`The user does not exist.`,HttpStatus.NOT_FOUND);
|
||||
if (requesterId == 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});
|
||||
return this.friendshipRepository.save(newFriendship);
|
||||
}
|
||||
|
||||
async updateFriendship(id: string, updateFriendshipDto: UpdateFriendshipDto) {
|
||||
const friendship = await this.friendshipRepository.preload(
|
||||
{id: +id,
|
||||
...updateFriendshipDto});
|
||||
if (!friendship)
|
||||
throw new HttpException(`The friendship could not be updated.`,HttpStatus.NOT_FOUND);
|
||||
return this.friendshipRepository.save(friendship);
|
||||
}
|
||||
|
||||
async removeFriendship(id: string) {
|
||||
const friendship = await this.findOne(id);
|
||||
if (!friendship)
|
||||
throw new HttpException(`Your friend could not be deleted.`,HttpStatus.NOT_FOUND);
|
||||
return this.friendshipRepository.remove(friendship);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { FriendshipService } from './friendship.service';
|
||||
import { FriendshipController } from './friendship.controller';
|
||||
import { Friendship } from './entities/friendship.entity';
|
||||
import { User } from '../users/entities/user.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Friendship, User])],
|
||||
providers: [FriendshipService],
|
||||
controllers: [FriendshipController],
|
||||
exports: [FriendshipService],
|
||||
})
|
||||
export class FriendshipsModule {}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsString } from 'class-validator';
|
||||
import { IsEmail, IsString } from 'class-validator';
|
||||
|
||||
export class CreateUsersDto {
|
||||
@IsString()
|
||||
@@ -7,4 +7,6 @@ export class CreateUsersDto {
|
||||
readonly username: string;
|
||||
@IsString()
|
||||
readonly password: string;
|
||||
@IsEmail()
|
||||
readonly email: string;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { type } from "os";
|
||||
import { Column, Entity, JoinTable, ManyToMany, OneToMany, PrimaryGeneratedColumn, Unique } from "typeorm";
|
||||
import { Friendship } from "../../friendship/entities/friendship.entity";
|
||||
|
||||
|
||||
@Entity('users')
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
username: string;
|
||||
|
||||
|
||||
@Column()
|
||||
email: string;
|
||||
|
||||
//Dans le cadre d'un tableau on peut faire :
|
||||
// @Column('json', { nullable: true })
|
||||
@Column()
|
||||
@@ -16,4 +26,12 @@ export class User {
|
||||
|
||||
@Column('json', { nullable: true })
|
||||
status: [string];
|
||||
|
||||
@JoinTable()
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.requesterId)
|
||||
requesterId: Friendship[];
|
||||
|
||||
@JoinTable()
|
||||
@OneToMany(type => Friendship , (friendship) => friendship.addresseeId)
|
||||
addresseeId: Friendship[];
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
@@ -3,9 +3,10 @@ import { UsersService } from './users.service';
|
||||
import { UsersController } from './users.controller';
|
||||
import { User } from './entities/user.entity';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Friendship } from '../friendship/entities/friendship.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User])],
|
||||
imports: [TypeOrmModule.forFeature([User, Friendship,])],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
controllers: [UsersController],
|
||||
|
||||
@@ -5,6 +5,7 @@ import { User } from './entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateUsersDto } from './dto/create-users.dto';
|
||||
import { UpdateUsersDto } from './dto/update-users.dto';
|
||||
import { Friendship } from '../friendship/entities/friendship.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -13,6 +14,8 @@ export class UsersService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
@InjectRepository(Friendship)
|
||||
private readonly friendshipRepository: Repository<Friendship>,
|
||||
) {}
|
||||
|
||||
async findOne(id: string) {
|
||||
@@ -48,6 +51,4 @@ export class UsersService {
|
||||
throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND);
|
||||
return this.userRepository.remove(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user