-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: start user friends feature * chore: add uploads folder into gitignore * feat: create simple friend request functional * feat: add option to add,remove and list friends on front
- Loading branch information
Showing
15 changed files
with
247 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
backend/src/friends/dto/createFriend.dto.ts → backend/src/friends/dto/createFriendDto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { IsString } from 'class-validator'; | ||
export class CreateFriendDto { | ||
@IsString() | ||
userId: string; | ||
|
||
export class CreateFriendDto { | ||
@IsString() | ||
friendId: string; | ||
friend_id: string; | ||
} |
6 changes: 2 additions & 4 deletions
6
backend/src/friends/dto/deleteFriend.dto.ts → backend/src/friends/dto/deleteFriendDto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { IsString } from 'class-validator'; | ||
export class DeleteFriendDto { | ||
@IsString() | ||
userId: string; | ||
|
||
export class DeleteFriendDto { | ||
@IsString() | ||
friendId: string; | ||
friend_id: string; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,49 @@ | ||
import { Body, Controller, Delete, Get, Post, Put } from '@nestjs/common'; | ||
import { UpdateFriendDto } from './dto/updateFriend.dto'; | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Post, | ||
Put, | ||
Req, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { FriendsService } from './friends.service'; | ||
import { CreateFriendDto } from './dto/createFriend.dto'; | ||
import { DeleteFriendDto } from './dto/deleteFriend.dto'; | ||
import { AccessTokenGuard } from 'src/auth/jwt/jwt.guard'; | ||
import { User } from '@prisma/client'; | ||
import { create } from 'domain'; | ||
import { CreateFriendDto } from './dto/createFriendDto'; | ||
import { DeleteFriendDto } from './dto/deleteFriendDto'; | ||
|
||
@Controller('friends') | ||
@UseGuards(AccessTokenGuard) | ||
export class FriendsController { | ||
constructor(private friendsService: FriendsService) {} | ||
|
||
@Post() | ||
async createFriend(@Body() createFriendDto: CreateFriendDto) { | ||
return this.friendsService.createFriend(createFriendDto); | ||
async createFriend( | ||
@Req() request: Request & { user: User }, | ||
@Body() createFriendDto: CreateFriendDto, | ||
) { | ||
const { id } = request.user; | ||
|
||
return this.friendsService.createFriend(id, createFriendDto); | ||
} | ||
|
||
@Get() | ||
async getFriends(@Body('userId') userId) { | ||
return this.friendsService.getFriends(userId); | ||
} | ||
async getFriends(@Req() request: Request & { user: User }) { | ||
const { id: userId } = request.user; | ||
|
||
@Put() | ||
async updateFriendStatus(@Body() updateFriendDto: UpdateFriendDto) { | ||
return this.friendsService.updateFriendStatus(updateFriendDto); | ||
return this.friendsService.getFriends(userId); | ||
} | ||
|
||
@Delete() | ||
async deleteFriend(@Body() deleteFriendDto: DeleteFriendDto) { | ||
return this.friendsService.deleteFriend(deleteFriendDto); | ||
async deleteFriend( | ||
@Req() request: Request & { user: User }, | ||
@Body() deleteFriendDto: DeleteFriendDto, | ||
) { | ||
const { id: userId } = request.user; | ||
|
||
return this.friendsService.deleteFriend(userId, deleteFriendDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,81 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
Injectable, | ||
NotAcceptableException, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import { CreateFriendDto } from './dto/createFriend.dto'; | ||
import { UpdateFriendDto } from './dto/updateFriend.dto'; | ||
import { DeleteFriendDto } from './dto/deleteFriend.dto'; | ||
import { CreateFriendDto } from './dto/createFriendDto'; | ||
import { DeleteFriendDto } from './dto/deleteFriendDto'; | ||
|
||
@Injectable() | ||
export class FriendsService { | ||
constructor(private prismaService: PrismaService) {} | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async findRelationship(user1Id, user2Id) { | ||
return await this.prismaService.friend.findFirst({ | ||
where: { | ||
AND: [ | ||
{ users: { some: { id: user1Id } } }, | ||
{ users: { some: { id: user2Id } } }, | ||
], | ||
}, | ||
async createFriend(userId: string, createFriendDto: CreateFriendDto) { | ||
const friendship = await this.prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { friends: { where: { id: createFriendDto.friend_id } } }, | ||
}); | ||
} | ||
|
||
async createFriend(createFriendDto: CreateFriendDto) { | ||
const existingFriendship = await this.findRelationship( | ||
createFriendDto.userId, | ||
createFriendDto.friendId, | ||
); | ||
if (friendship.friends.length > 0) { | ||
throw new NotAcceptableException('Friendship already exists'); | ||
} | ||
|
||
const friendUser = await this.prisma.user.findUnique({ | ||
where: { id: createFriendDto.friend_id }, | ||
}); | ||
|
||
if (existingFriendship) { | ||
return existingFriendship; | ||
if (!friendUser) { | ||
throw new NotFoundException('Friend not found'); | ||
} | ||
const friendRequest = await this.prismaService.friend.create({ | ||
data: { | ||
status: 'PENDING', | ||
users: { | ||
connect: [ | ||
{ id: createFriendDto.userId }, | ||
{ id: createFriendDto.friendId }, | ||
], | ||
}, | ||
}, | ||
|
||
await this.prisma.user.update({ | ||
where: { id: userId }, | ||
data: { friends: { connect: [{ id: createFriendDto.friend_id }] } }, | ||
}); | ||
|
||
await this.prisma.user.update({ | ||
where: { id: createFriendDto.friend_id }, | ||
data: { friends: { connect: [{ id: userId }] } }, | ||
}); | ||
return friendRequest; | ||
|
||
return { | ||
message: 'Friend added', | ||
friend: friendUser, | ||
}; | ||
} | ||
|
||
async getFriends(userId: string) { | ||
const friendships = await this.prismaService.friend.findMany({ | ||
where: { | ||
users: { | ||
some: { | ||
id: userId, | ||
}, | ||
}, | ||
status: 'ACCEPTED', | ||
}, | ||
select: { | ||
users: { | ||
where: { | ||
id: { | ||
not: userId | ||
} | ||
} | ||
} | ||
}, | ||
const friends = await this.prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { friends: true }, | ||
}); | ||
|
||
return friendships; | ||
return friends; | ||
} | ||
|
||
async updateFriendStatus(updateFriendDto: UpdateFriendDto) { | ||
const relationShip = await this.findRelationship( | ||
updateFriendDto.friendId, | ||
updateFriendDto.userId, | ||
); | ||
return await this.prismaService.friend.update({ | ||
where: { | ||
id: relationShip.id, | ||
}, | ||
data: { | ||
status: updateFriendDto.friendshipStatus, | ||
}, | ||
async deleteFriend(userId: string, deleteFriendDto: DeleteFriendDto) { | ||
const friendship = await this.prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { friends: { where: { id: deleteFriendDto.friend_id } } }, | ||
}); | ||
} | ||
|
||
async deleteFriend(deleteFriendDto: DeleteFriendDto) { | ||
const relationShip = await this.findRelationship( | ||
deleteFriendDto.friendId, | ||
deleteFriendDto.userId, | ||
); | ||
if (!relationShip) { | ||
return relationShip; | ||
if (friendship.friends.length === 0) { | ||
throw new NotFoundException('Friendship not found'); | ||
} | ||
return await this.prismaService.friend.delete({ | ||
where: { | ||
id: relationShip.id, | ||
}, | ||
|
||
await this.prisma.user.update({ | ||
where: { id: userId }, | ||
data: { friends: { disconnect: [{ id: deleteFriendDto.friend_id }] } }, | ||
}); | ||
|
||
await this.prisma.user.update({ | ||
where: { id: deleteFriendDto.friend_id }, | ||
data: { friends: { disconnect: [{ id: userId }] } }, | ||
}); | ||
|
||
return { | ||
message: 'Friend deleted', | ||
}; | ||
} | ||
} |
Oops, something went wrong.