-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Built service for adding messages - worked on unit tests
- Loading branch information
1 parent
a73a8ec
commit 326fc7b
Showing
28 changed files
with
587 additions
and
327 deletions.
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
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
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
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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatsService } from './chats.service'; | ||
import { ChatsResolver } from './chats.resolver'; | ||
|
||
import { PrismaModule } from '../prisma/prisma.module'; | ||
@Module({ | ||
imports: [PrismaModule], | ||
providers: [ChatsResolver, ChatsService], | ||
}) | ||
export class ChatsModule {} |
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
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,26 +1,71 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateChatInput } from './dto/create-chat.input'; | ||
import { UpdateChatInput } from './dto/update-chat.input'; | ||
|
||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { GraphQLError } from 'graphql'; | ||
import { Prisma } from '@prisma/client'; | ||
@Injectable() | ||
export class ChatsService { | ||
create(createChatInput: CreateChatInput) { | ||
return 'This action adds a new chat'; | ||
constructor(private prisma: PrismaService) {} | ||
|
||
// Method to create a new chat | ||
async create(createChatInput: CreateChatInput) { | ||
return this.prisma.message | ||
.create({ data: createChatInput }) | ||
.catch((error) => { | ||
// Handling unique constraint violation error | ||
if (error instanceof Prisma.PrismaClientKnownRequestError) { | ||
throw new GraphQLError( | ||
'Chat creation failed: Unique constraint violated.', | ||
); | ||
} | ||
// Handling other errors | ||
throw new GraphQLError('Chat creation failed.'); | ||
}); | ||
} | ||
|
||
findAll() { | ||
return `This action returns all chats`; | ||
// Method to find chats sent from a specific user | ||
async findFrom(id: string) { | ||
return this.prisma.message | ||
.findMany({ where: { senderID: id } }) | ||
.catch((error) => { | ||
// Handling errors while fetching chats from sender | ||
throw new GraphQLError( | ||
'Error occurred while fetching chats from sender.', | ||
); | ||
}); | ||
} | ||
|
||
findOne(id: number) { | ||
return `This action returns a #${id} chat`; | ||
// Method to find chats sent to a specific user | ||
async findTo(id: string) { | ||
return this.prisma.message | ||
.findMany({ where: { recipientID: id } }) | ||
.catch((error) => { | ||
// Handling errors while fetching chats to recipient | ||
throw new GraphQLError( | ||
'Error occurred while fetching chats to recipient.', | ||
); | ||
}); | ||
} | ||
|
||
update(id: number, updateChatInput: UpdateChatInput) { | ||
return `This action updates a #${id} chat`; | ||
// Method to update a chat | ||
async update(id: string, updateChatInput: UpdateChatInput) { | ||
return this.prisma.message | ||
.update({ | ||
where: { id: id }, | ||
data: { messageContents: updateChatInput.messageContents }, | ||
}) | ||
.catch((error) => { | ||
// Handling errors while updating a chat | ||
throw new GraphQLError('Error occurred while updating chat.'); | ||
}); | ||
} | ||
|
||
remove(id: number) { | ||
return `This action removes a #${id} chat`; | ||
// Method to remove a chat | ||
async remove(id: string) { | ||
return this.prisma.message.delete({ where: { id: id } }).catch((error) => { | ||
// Handling errors while deleting a chat | ||
throw new GraphQLError('Error occurred while deleting chat.'); | ||
}); | ||
} | ||
} |
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,7 +1,11 @@ | ||
import { InputType, Int, Field } from '@nestjs/graphql'; | ||
import { InputType, Field } from '@nestjs/graphql'; | ||
|
||
@InputType() | ||
export class CreateChatInput { | ||
@Field(() => Int, { description: 'Example field (placeholder)' }) | ||
exampleField: number; | ||
@Field({ description: 'ID of message sender' }) | ||
senderID: string; | ||
@Field({ description: 'ID of message recipient' }) | ||
recipientID: string; | ||
@Field({ description: 'Contents of message' }) | ||
messageContents: string; | ||
} |
Oops, something went wrong.