|
| 1 | +import { getModelFromCollectionBase } from '../utils'; |
| 2 | +import { |
| 3 | + Route, |
| 4 | + Tags, |
| 5 | + Post, |
| 6 | + Body, |
| 7 | + Path, |
| 8 | + Put, |
| 9 | + Delete, |
| 10 | + Patch, |
| 11 | + Get, |
| 12 | +} from 'tsoa'; |
| 13 | +import { |
| 14 | + IPostPayload, |
| 15 | + IResponse, |
| 16 | +} from '../interfaces'; |
| 17 | +import { Post as PostModel, PostRate } from '../sqlz/models'; |
| 18 | + |
| 19 | +@Route('api/v1/post') |
| 20 | +@Tags('Post') |
| 21 | +export default class UserAuthController { |
| 22 | + |
| 23 | + @Get('/:id') |
| 24 | + public async fetch(@Path() id: number) { |
| 25 | + try { |
| 26 | + let post = await PostModel.findOne({ where: { id }, include: [{ model: PostRate, as: 'rates', required: false }] }); |
| 27 | + if (post) { |
| 28 | + post = getModelFromCollectionBase(post); |
| 29 | + const p: any = {...post}; |
| 30 | + const {rates} = p; |
| 31 | + let avgRate = 0; |
| 32 | + if (rates && rates.length > 0) { |
| 33 | + const rateLen = rates.length; |
| 34 | + rates.map((r: any) => { |
| 35 | + avgRate = avgRate + (r.rate / rateLen); |
| 36 | + }); |
| 37 | + } |
| 38 | + return { status: 200, data: {...post, avgRate}, error: null, message: 'Success to fetch post!' }; |
| 39 | + } |
| 40 | + return { status: 400, data: null, error: null, message: 'Success to fetch post!' }; |
| 41 | + } catch (error) { |
| 42 | + const response: IResponse = { status: 400, data: null, error, message: 'Failed to fetch post!' }; |
| 43 | + return response |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Post('/') |
| 48 | + public async create(@Body() body: IPostPayload) { |
| 49 | + try { |
| 50 | + const post = await PostModel.create({ ...body }); |
| 51 | + if (post) { |
| 52 | + return { status: 200, data: post, error: null, message: 'Success to create post!' }; |
| 53 | + } |
| 54 | + const response: IResponse = { status: 400, data: null, error: null, message: 'Failed to create post!' }; |
| 55 | + return response; |
| 56 | + } catch (error) { |
| 57 | + const response: IResponse = { status: 400, data: null, error, message: 'Failed to create post!' }; |
| 58 | + return response; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Patch('/:id') |
| 63 | + public async update(@Body() body: IPostPayload, @Path() id: number) { |
| 64 | + try { |
| 65 | + const exPost = await PostModel.findByPk(id); |
| 66 | + if (exPost) { |
| 67 | + const post = await PostModel.update({ ...body }, { where: { id }, returning: true }); |
| 68 | + return { status: 200, data: post[1][0], error: null, message: 'Success to update post!' }; |
| 69 | + } |
| 70 | + const response: IResponse = { status: 400, data: null, error: null, message: 'Failed to update post!' }; |
| 71 | + return response |
| 72 | + } catch (error) { |
| 73 | + const response: IResponse = { status: 400, data: null, error, message: 'Failed o update post!' }; |
| 74 | + return response; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Put('/:postId/:userId/:rate') |
| 79 | + public async rate(@Path() postId: number, @Path() userId: number, @Path() rate: number) { |
| 80 | + try { |
| 81 | + const exPostRate = await PostRate.findOne({ where: { postId, userId } }); |
| 82 | + if (exPostRate) { |
| 83 | + await PostRate.destroy({ where: { postId, userId } }); |
| 84 | + return { status: 200, data: null, error: null, message: 'Success to remove ost\'s rate!' }; |
| 85 | + } |
| 86 | + const postRate = await PostRate.create({ postId, userId, rate }); |
| 87 | + if (postRate) { |
| 88 | + return { status: 200, data: postRate, error: null, message: 'Success o rate post!' }; |
| 89 | + } |
| 90 | + const response: IResponse = { status: 400, data: null, error: null, message: 'Faile to rate post!' }; |
| 91 | + return response |
| 92 | + }catch (error) { |
| 93 | + const response: IResponse = { status: 400, data: null, error, message: 'Failed to create post!' }; |
| 94 | + return response |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Delete('/:postId') |
| 99 | + public async delete(@Path() postId: number) { |
| 100 | + try { |
| 101 | + await PostRate.destroy({ where: { postId } }); |
| 102 | + await PostModel.destroy({ where: { id: postId } }); |
| 103 | + const response: IResponse = { status: 200, data: null, error: null, message: 'Success to remove posts!' }; |
| 104 | + return response; |
| 105 | + } catch (error) { |
| 106 | + const response: IResponse = { status: 400, data: null, error, message: 'Failed to remo;ve post!' }; |
| 107 | + return response |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments