share.tsx 766 B

12345678910111213141516171819202122232425262728293031
  1. import { Handlers } from "$fresh/server.ts";
  2. import {
  3. checkToken,
  4. makeErrorResponse,
  5. makeSuccessResponse,
  6. } from "utils/server.ts";
  7. import { find, update } from "utils/db.ts";
  8. export const handler: Handlers = {
  9. async POST(req: Request) {
  10. const reqJson = await req.json();
  11. const id = reqJson.id;
  12. const shared = reqJson.shared;
  13. const tokenUserId = checkToken(req);
  14. if (tokenUserId && id) {
  15. const post = find("Post", {
  16. id,
  17. user_id: tokenUserId,
  18. });
  19. if (post.length > 0) {
  20. const newPost = update("Post", id, {
  21. shared: Boolean(shared),
  22. });
  23. if (newPost.length > 0) {
  24. return makeSuccessResponse(true);
  25. }
  26. }
  27. }
  28. return makeErrorResponse();
  29. },
  30. };