Piotoru

friends service

Jan 22nd, 2023 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. // SERVICE
  2. import {
  3. BadRequestException,
  4. CACHE_MANAGER,
  5. HttpException,
  6. Inject,
  7. Injectable,
  8. NotAcceptableException,
  9. } from '@nestjs/common';
  10. import { Cache } from 'cache-manager';
  11. import { PrismaService } from '../prisma/prisma.service';
  12. import { Friends, Prisma } from '@prisma/client';
  13.  
  14. import { deleted } from '../constants/allCustomsHttpMessages';
  15. import { FriendDto } from '../DTOs/friend.dto';
  16.  
  17. @Injectable()
  18. export class FriendsService {
  19. constructor(
  20. private prisma: PrismaService,
  21. @Inject(CACHE_MANAGER) private cacheManager: Cache,
  22. ) {}
  23. async findFriend(
  24. friendWhereUniqueInput: Prisma.FriendsWhereUniqueInput,
  25. ): Promise<FriendDto | null> {
  26. const _findOne = await this.prisma.friends.findUnique({
  27. where: friendWhereUniqueInput,
  28. });
  29.  
  30. const friendArray: FriendDto = {
  31. username: _findOne.usernameId,
  32. friend: _findOne.friendId,
  33. };
  34.  
  35. await this.cacheManager.set('friendOne', friendArray, 0);
  36. return friendArray;
  37. }
  38.  
  39. async friends(params: {
  40. skip?: number;
  41. take?: number;
  42. cursor?: Prisma.FriendsWhereUniqueInput;
  43. where?: Prisma.FriendsWhereInput;
  44. orderBy?: Prisma.FriendsOrderByWithRelationInput;
  45. }): Promise<FriendDto[]> {
  46. const { skip, take, cursor, where, orderBy } = params;
  47. const friendsArray: FriendDto[] = [];
  48.  
  49. const _friends = await this.prisma.friends.findMany({
  50. skip,
  51. take,
  52. cursor,
  53. where,
  54. orderBy,
  55. });
  56.  
  57. for (const _f of _friends) {
  58. friendsArray.push({
  59. username: _f.usernameId,
  60. friend: _f.friendId,
  61. });
  62. }
  63.  
  64. return friendsArray;
  65. }
  66.  
  67. async createFriend(
  68. data: Prisma.FriendsUncheckedCreateInput,
  69. ): Promise<string | NotAcceptableException> {
  70. try {
  71. const { usernameId, friendId } = data;
  72.  
  73. usernameId === friendId &&
  74. new BadRequestException('Your friend cannot be you.');
  75.  
  76. const addedFriend = await this.friends({
  77. where: { AND: [{ usernameId }, { friendId }] },
  78. });
  79.  
  80. addedFriend.length > 0 &&
  81. new NotAcceptableException(`We're already friends.`);
  82.  
  83. await this.prisma.friends.create({
  84. data,
  85. });
  86.  
  87. return `Success!!! We're friends.`;
  88. } catch (e) {
  89. console.error(e);
  90. }
  91. }
  92.  
  93. async updateFriend(params: {
  94. where: Prisma.FriendsWhereUniqueInput;
  95. data: Prisma.FriendsUpdateInput;
  96. }): Promise<Friends> {
  97. const { where, data } = params;
  98. return this.prisma.friends.update({ data, where });
  99. }
  100.  
  101. async deleteFriend(
  102. where: Prisma.FriendsWhereUniqueInput,
  103. ): Promise<HttpException> {
  104. const { id } = where;
  105. await this.prisma.friends.delete({ where });
  106. await this.cacheManager.del('friends');
  107. await this.cacheManager.del('friendOne');
  108. return deleted(id);
  109. }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment