notifications.controller.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const router_1 = __importDefault(require("@koa/router"));
  7. const notifications_service_1 = require("./notifications.service");
  8. const router = new router_1.default({ prefix: '/api/notifications' });
  9. // 测试用户ID
  10. const TEST_USER_ID = 'test-user';
  11. /**
  12. * 获取通知列表
  13. * GET /api/notifications
  14. */
  15. router.get('/', async (ctx) => {
  16. try {
  17. const notifications = await notifications_service_1.notificationsService.getNotifications(TEST_USER_ID);
  18. ctx.body = {
  19. code: 0,
  20. message: 'success',
  21. data: notifications,
  22. };
  23. }
  24. catch (error) {
  25. ctx.body = {
  26. code: 400,
  27. message: error.message || '获取通知失败',
  28. };
  29. }
  30. });
  31. /**
  32. * 标记通知为已读
  33. * POST /api/notifications/read
  34. */
  35. router.post('/read', async (ctx) => {
  36. try {
  37. const { id } = ctx.request.body;
  38. await notifications_service_1.notificationsService.markAsRead(id);
  39. ctx.body = {
  40. code: 0,
  41. message: 'success',
  42. };
  43. }
  44. catch (error) {
  45. ctx.body = {
  46. code: 400,
  47. message: error.message || '标记失败',
  48. };
  49. }
  50. });
  51. exports.default = router;