-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
370cf87
commit 1844da2
Showing
10 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
prisma/migrations/20241212130729_add_pinned_post/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- CreateTable | ||
CREATE TABLE "PinnedPost" ( | ||
"postId" TEXT NOT NULL, | ||
"pinnedById" TEXT NOT NULL, | ||
"pinnedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "PinnedPost_pkey" PRIMARY KEY ("postId") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "PinnedPost" ADD CONSTRAINT "PinnedPost_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "PinnedPost" ADD CONSTRAINT "PinnedPost_pinnedById_fkey" FOREIGN KEY ("pinnedById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Request, Response, Router } from 'express'; | ||
import { param } from 'express-validator'; | ||
import { customExpressValidatorResult, generateError } from '../../common/errorHandler'; | ||
import { authenticate } from '../../middleware/authenticate'; | ||
import { pinPost } from '../../services/Post'; | ||
|
||
export function postPin(Router: Router) { | ||
Router.post('/posts/:postId/pin', authenticate(), param('postId').isString().withMessage('postId must be a string!').isLength({ min: 1, max: 100 }).withMessage('postId length must be between 1 and 100 characters.'), route); | ||
} | ||
|
||
interface Param { | ||
postId: string; | ||
} | ||
|
||
async function route(req: Request, res: Response) { | ||
const params = req.params as unknown as Param; | ||
|
||
const validateError = customExpressValidatorResult(req); | ||
|
||
if (validateError) { | ||
return res.status(400).json(validateError); | ||
} | ||
|
||
const [status, error] = await pinPost(params.postId, req.userCache.id).catch((e) => { | ||
console.error(e); | ||
return [false, generateError('Something went wrong. Try again later.')]; | ||
}); | ||
|
||
if (error) { | ||
return res.status(400).json(error); | ||
} | ||
|
||
res.json({ success: status }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Request, Response, Router } from 'express'; | ||
import { param } from 'express-validator'; | ||
import { customExpressValidatorResult, generateError } from '../../common/errorHandler'; | ||
import { authenticate } from '../../middleware/authenticate'; | ||
import { unpinPost } from '../../services/Post'; | ||
|
||
export function postPinDelete(Router: Router) { | ||
Router.delete('/posts/:postId/pin', authenticate(), param('postId').isString().withMessage('postId must be a string!').isLength({ min: 1, max: 100 }).withMessage('postId length must be between 1 and 100 characters.'), route); | ||
} | ||
|
||
interface Param { | ||
postId: string; | ||
} | ||
|
||
async function route(req: Request, res: Response) { | ||
const params = req.params as unknown as Param; | ||
|
||
const validateError = customExpressValidatorResult(req); | ||
|
||
if (validateError) { | ||
return res.status(400).json(validateError); | ||
} | ||
|
||
const [status, error] = await unpinPost(params.postId, req.userCache.id).catch((e) => { | ||
console.error(e); | ||
return [false, generateError('Something went wrong. Try again later.')]; | ||
}); | ||
|
||
if (error) { | ||
return res.status(400).json(error); | ||
} | ||
|
||
res.json({ success: status }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters