|
| 1 | +import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi'; |
| 2 | +import express, { Router } from 'express'; |
| 3 | + |
| 4 | +import { |
| 5 | + ChannelSchema, |
| 6 | + CreateChannelSchema, |
| 7 | + DeleteChannelSchema, |
| 8 | + GetChannelSchema, |
| 9 | + UpdateChannelSchema, |
| 10 | +} from '@/api/channels/channelModel'; |
| 11 | +import { createApiResponse } from '@/api-docs/openAPIResponseBuilders'; |
| 12 | +import { messageResponse } from '@/common/utils/commonResponses'; |
| 13 | +import { validateRequest } from '@/common/utils/httpHandlers'; |
| 14 | + |
| 15 | +import AuthController from '../auth/authController'; |
| 16 | +import MembersController from '../members/memberController'; |
| 17 | +import MessageController from '../messages/messageController'; |
| 18 | +import { Messages } from '../messages/messageModel'; |
| 19 | +import { Users } from '../user/userModel'; |
| 20 | +import ChannelController from './channelController'; |
| 21 | + |
| 22 | +export const channelRegistery = new OpenAPIRegistry(); |
| 23 | + |
| 24 | +const bearerAuth = channelRegistery.registerComponent('securitySchemes', 'bearerAuth', { |
| 25 | + type: 'http', |
| 26 | + scheme: 'bearer', |
| 27 | + bearerFormat: 'JWT', |
| 28 | +}); |
| 29 | + |
| 30 | +channelRegistery.register('Channel', ChannelSchema); |
| 31 | + |
| 32 | +export const channelRouter: Router = (() => { |
| 33 | + const router = express.Router(); |
| 34 | + |
| 35 | + channelRegistery.registerPath({ |
| 36 | + method: 'post', |
| 37 | + path: '/channels', |
| 38 | + tags: ['Channel'], |
| 39 | + security: [{ [bearerAuth.name]: [] }], |
| 40 | + request: { |
| 41 | + body: { |
| 42 | + content: { |
| 43 | + 'application/json': { |
| 44 | + schema: CreateChannelSchema.shape.body, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + responses: createApiResponse(ChannelSchema, 'Success'), |
| 50 | + }); |
| 51 | + |
| 52 | + router.post( |
| 53 | + '/', |
| 54 | + [AuthController.authenticate, validateRequest(CreateChannelSchema)], |
| 55 | + ChannelController.createChannel |
| 56 | + ); |
| 57 | + |
| 58 | + /***************************************************************************** */ |
| 59 | + channelRegistery.registerPath({ |
| 60 | + method: 'get', |
| 61 | + path: '/channels/{id}', |
| 62 | + tags: ['Channel'], |
| 63 | + security: [{ [bearerAuth.name]: [] }], |
| 64 | + request: { params: GetChannelSchema.shape.params }, |
| 65 | + responses: createApiResponse(ChannelSchema, 'Success'), |
| 66 | + }); |
| 67 | + |
| 68 | + router.get( |
| 69 | + '/:id', |
| 70 | + AuthController.authenticate, |
| 71 | + validateRequest(GetChannelSchema), |
| 72 | + ChannelController.getChannelById |
| 73 | + ); |
| 74 | + |
| 75 | + /***************************************************************************** */ |
| 76 | + channelRegistery.registerPath({ |
| 77 | + method: 'patch', |
| 78 | + path: '/channels/{id}', |
| 79 | + tags: ['Channel'], |
| 80 | + security: [{ [bearerAuth.name]: [] }], |
| 81 | + request: { |
| 82 | + params: UpdateChannelSchema.shape.params, |
| 83 | + body: { |
| 84 | + content: { |
| 85 | + 'application/json': { |
| 86 | + schema: UpdateChannelSchema.shape.body, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + responses: createApiResponse(ChannelSchema, 'Success'), |
| 92 | + }); |
| 93 | + |
| 94 | + router.patch( |
| 95 | + '/:id', |
| 96 | + [AuthController.authenticate, validateRequest(UpdateChannelSchema)], |
| 97 | + ChannelController.updateChannel |
| 98 | + ); |
| 99 | + |
| 100 | + /***************************************************************************** */ |
| 101 | + channelRegistery.registerPath({ |
| 102 | + method: 'delete', |
| 103 | + path: '/channels/{id}', |
| 104 | + tags: ['Channel'], |
| 105 | + security: [{ [bearerAuth.name]: [] }], |
| 106 | + request: { params: GetChannelSchema.shape.params }, |
| 107 | + responses: createApiResponse(messageResponse, 'Success'), |
| 108 | + }); |
| 109 | + |
| 110 | + router.delete( |
| 111 | + '/:id', |
| 112 | + [AuthController.authenticate, validateRequest(DeleteChannelSchema)], |
| 113 | + ChannelController.deleteChannel |
| 114 | + ); |
| 115 | + |
| 116 | + /***************************************************************************** */ |
| 117 | + channelRegistery.registerPath({ |
| 118 | + method: 'get', |
| 119 | + path: '/channels/{id}/users', |
| 120 | + tags: ['Channel'], |
| 121 | + security: [{ bearerAuth: [] }], |
| 122 | + request: { params: GetChannelSchema.shape.params }, |
| 123 | + responses: createApiResponse(Users, 'Success'), |
| 124 | + }); |
| 125 | + |
| 126 | + router.get( |
| 127 | + '/:id/users', |
| 128 | + [AuthController.authenticate, validateRequest(GetChannelSchema)], |
| 129 | + MembersController.getChannelUsers |
| 130 | + ); |
| 131 | + |
| 132 | + /***************************************************************************** */ |
| 133 | + channelRegistery.registerPath({ |
| 134 | + method: 'get', |
| 135 | + path: '/channels/{id}/messages', |
| 136 | + tags: ['Channel'], |
| 137 | + security: [{ bearerAuth: [] }], |
| 138 | + request: { params: GetChannelSchema.shape.params, query: GetChannelSchema.shape.query }, |
| 139 | + responses: createApiResponse(Messages, 'Success'), |
| 140 | + }); |
| 141 | + |
| 142 | + router.get( |
| 143 | + '/:id/messages', |
| 144 | + [AuthController.authenticate, validateRequest(GetChannelSchema)], |
| 145 | + MessageController.getChannelMessages |
| 146 | + ); |
| 147 | + |
| 148 | + return router; |
| 149 | +})(); |
0 commit comments