|
| 1 | +import {Controller, Get, Param, ParseIntPipe, Put, Query} from '@nestjs/common'; |
| 2 | +import {ApiTags} from '@nestjs/swagger'; |
| 3 | +import {Api} from '@/common/decorators/api.decorator'; |
| 4 | +import {MessagesService} from './messages.service'; |
| 5 | +import {RedditService} from '../reddit.service'; |
| 6 | +import {PaginationArgs} from '../../common/pagination/pagination.args'; |
| 7 | + |
| 8 | +@ApiTags('reddit') |
| 9 | +@Controller('reddit/messages') |
| 10 | +export class MessagesController { |
| 11 | + constructor( |
| 12 | + private readonly messagesService: MessagesService, |
| 13 | + private readonly redditService: RedditService |
| 14 | + ) {} |
| 15 | + |
| 16 | + /** |
| 17 | + * Returns a paginated table of all Reddit messages stored in the database. |
| 18 | + */ |
| 19 | + @Get() |
| 20 | + @Api({ |
| 21 | + summary: 'List Reddit messages', |
| 22 | + description: 'Returns a paginated list of Reddit messages (DMs, comments) stored in the database.', |
| 23 | + queriesFrom: PaginationArgs, |
| 24 | + envelope: true, |
| 25 | + responses: [ |
| 26 | + {status: 200, description: 'Messages retrieved successfully.'}, |
| 27 | + ], |
| 28 | + }) |
| 29 | + async findAll( |
| 30 | + @Query() paginationArgs: PaginationArgs, |
| 31 | + @Query('isRead') isRead?: string, |
| 32 | + @Query('type') type?: string, |
| 33 | + @Query('subreddit') subreddit?: string |
| 34 | + ) { |
| 35 | + const filter: {isRead?: boolean; type?: string; subreddit?: string} = {}; |
| 36 | + if (isRead !== undefined) filter.isRead = isRead === 'true'; |
| 37 | + if (type) filter.type = type; |
| 38 | + if (subreddit) filter.subreddit = subreddit; |
| 39 | + return this.messagesService.findAll(paginationArgs, filter); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Triggers a Reddit inbox check and stores any new messages in the database. |
| 44 | + */ |
| 45 | + @Get('check') |
| 46 | + @Api({ |
| 47 | + summary: 'Check Reddit inbox for new messages', |
| 48 | + description: 'Connects to Reddit, fetches inbox messages and stores any new ones in the database.', |
| 49 | + envelope: true, |
| 50 | + responses: [ |
| 51 | + {status: 200, description: 'Inbox checked and new messages stored.'}, |
| 52 | + ], |
| 53 | + }) |
| 54 | + async checkInbox() { |
| 55 | + const newMessages = await this.redditService.checkRedditMessages(); |
| 56 | + return {count: newMessages.length, messages: newMessages}; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns a single Reddit message by ID. |
| 61 | + */ |
| 62 | + @Get(':id') |
| 63 | + @Api({ |
| 64 | + summary: 'Get Reddit message by ID', |
| 65 | + description: 'Returns a single Reddit message stored in the database.', |
| 66 | + responses: [ |
| 67 | + {status: 200, description: 'Message retrieved successfully.'}, |
| 68 | + {status: 404, description: 'Message not found.'}, |
| 69 | + ], |
| 70 | + }) |
| 71 | + async findOne(@Param('id', ParseIntPipe) id: number) { |
| 72 | + return this.messagesService.findOne(id); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Marks a Reddit message as read. |
| 77 | + */ |
| 78 | + @Put(':id/read') |
| 79 | + @Api({ |
| 80 | + summary: 'Mark message as read', |
| 81 | + description: 'Marks a Reddit message as read in the database.', |
| 82 | + responses: [ |
| 83 | + {status: 200, description: 'Message marked as read.'}, |
| 84 | + {status: 404, description: 'Message not found.'}, |
| 85 | + ], |
| 86 | + }) |
| 87 | + async markAsRead(@Param('id', ParseIntPipe) id: number) { |
| 88 | + return this.messagesService.markAsRead(id); |
| 89 | + } |
| 90 | +} |
0 commit comments