Piotoru

friends controller

Jan 22nd, 2023 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. // CONTROLLER
  2. import {
  3. Body,
  4. CACHE_MANAGER,
  5. Controller,
  6. Delete,
  7. Get,
  8. HttpException,
  9. HttpStatus,
  10. Inject,
  11. NotAcceptableException,
  12. Param,
  13. Patch,
  14. Post,
  15. Query,
  16. } from '@nestjs/common';
  17. import { Friends, Prisma } from '@prisma/client';
  18. import { Cache } from 'cache-manager';
  19.  
  20. import { FriendsService } from './friends.service';
  21. import { stringToJsonForGet } from '../utilities/convertValues';
  22. import { allContent } from '../constants/allCustomsHttpMessages';
  23. import { FriendDto } from '../DTOs/friend.dto';
  24.  
  25. @Controller('friends')
  26. export class FriendsController {
  27. constructor(
  28. private readonly friendsService: FriendsService,
  29. @Inject(CACHE_MANAGER) private cacheManager: Cache,
  30. ) {}
  31.  
  32. @Get()
  33. async findALl(
  34. @Query('orderBy') orderBy?: string,
  35. @Query('limit') limit?: string,
  36. @Query('where') where?: string,
  37. @Query('cursor') cursor?: string,
  38. ): Promise<FriendDto[] | { message: string; statusCode: HttpStatus }> {
  39. const getCache: FriendDto[] = await this.cacheManager.get('friends');
  40.  
  41. if (!!getCache) {
  42. return getCache;
  43. } else {
  44. let order;
  45.  
  46. if (typeof orderBy === 'string') {
  47. try {
  48. const { orderArray } = await stringToJsonForGet(orderBy);
  49. order = orderArray;
  50. } catch (e) {
  51. console.error(e);
  52. }
  53. }
  54.  
  55. let whereElements;
  56.  
  57. if (typeof where === 'string') {
  58. try {
  59. const { whereObj } = await stringToJsonForGet(where);
  60. whereElements = whereObj;
  61. } catch (e) {
  62. console.error(e);
  63. }
  64. }
  65.  
  66. const firstResults = await this.friendsService.friends({
  67. take: parseInt(limit) || undefined,
  68. orderBy: order || undefined,
  69. where: whereElements || undefined,
  70. });
  71.  
  72. const firstNextData: FriendDto[] = [];
  73. const nextData: FriendDto[] = [];
  74.  
  75. if (!!cursor) {
  76. const nextResults = await this.friendsService.friends({
  77. take: parseInt(limit) || undefined,
  78. orderBy: order || undefined,
  79. skip: 1,
  80. cursor: {
  81. id: cursor,
  82. },
  83. where: whereElements || undefined,
  84. });
  85.  
  86. if (nextResults.length > 0) {
  87. if (firstNextData.length === 0) {
  88. firstNextData.concat(firstResults, nextResults);
  89. await this.cacheManager.set('friends', firstNextData, 0);
  90. return firstNextData;
  91. }
  92.  
  93. if (nextData.length === 0) {
  94. nextData.concat(firstNextData, nextResults);
  95. await this.cacheManager.set('friends', nextData, 0);
  96. return nextData;
  97. }
  98.  
  99. nextData.concat(nextResults);
  100. await this.cacheManager.set('friends', nextData, 0);
  101. return nextData;
  102. } else {
  103. return allContent;
  104. }
  105. }
  106.  
  107. await this.cacheManager.set('friends', firstResults, 0);
  108. return firstResults;
  109. }
  110. }
  111.  
  112. @Get(':id')
  113. async findOne(@Param('id') id: string): Promise<FriendDto> {
  114. const getCache: FriendDto = await this.cacheManager.get('friendsOne');
  115.  
  116. if (!!getCache) {
  117. return getCache;
  118. } else {
  119. await this.friendsService.findFriend({ id });
  120. }
  121. }
  122.  
  123. @Post()
  124. async createFriend(
  125. @Body()
  126. friendData: Prisma.FriendsUncheckedCreateInput,
  127. ): Promise<string | NotAcceptableException> {
  128. return this.friendsService.createFriend(friendData);
  129. }
  130.  
  131. @Patch()
  132. async updateFriend(
  133. @Param('id') id: string,
  134. @Param('data') data: Prisma.FriendsUpdateInput,
  135. ): Promise<Friends> {
  136. return this.friendsService.updateFriend({
  137. where: { id },
  138. data,
  139. });
  140. }
  141.  
  142. @Delete(':id')
  143. async deleteFiend(
  144. @Param('id') id: string,
  145. ): Promise<HttpException> {
  146. return await this.friendsService.deleteFriend({ id });
  147. }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment