api: allow to fetch a mood entry

Pedro Lucas Porcellis porcellis@eletrotupi.com 1 month ago 1ae4e1096335a981b91bb09c19f830b4eb417903
Parents: 94a9e94
3 file(s) changed
  • api/src/controllers/moods.ts +18 -1
  • api/src/routes/moods.ts +1 -0
  • api/src/services/mood.service.ts +9 -0
api/src/controllers/moods.ts
@@ -2,7 +2,8 @@ import { Request, Response, NextFunction } from 'express';
2 2 import { AuthenticatedRequest } from '@app/middleware/auth';
3 3 import {
4 4 createMood,
5 - getMoodsByUserId
5 + getMoodsByUserId,
6 + getMoodById
6 7 } from '@app/services/mood.service';
7 8
8 9 import {
@@ -44,4 +45,20 @@ } catch (err) {
44 45 next(err);
45 46 }
46 47 },
48 +
49 + show: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
50 + try {
51 + const mood = await getMoodById(Number(req.params.id))
52 +
53 + if (!mood) {
54 + return res.status(404).json({ errors: "not found" })
55 + }
56 +
57 + return res.status(200).json(
58 + mood
59 + )
60 + } catch (err) {
61 + next(err);
62 + }
63 + }
47 64 };
api/src/routes/moods.ts
@@ -5,6 +5,7 @@
5 5 const router = Router();
6 6
7 7 router.get('/', requireAuth, MoodsController.index);
8 + router.get('/:id', requireAuth, MoodsController.show);
8 9 router.post('/', requireAuth, MoodsController.create);
9 10
10 11 export default router;
api/src/services/mood.service.ts
@@ -61,3 +61,12 @@ page,
61 61 nextPage: page < totalPages ? page + 1 : null,
62 62 };
63 63 };
64 +
65 + export const getMoodById = async (id: number): Promise<Mood | null> => {
66 + return await prisma.mood.findUnique({
67 + where: {
68 + id
69 + },
70 + include: { moodComponents: true }
71 + });
72 + }