eletrotupi / tcc/ commit / 1ae4e10

api: allow to fetch a mood entry

Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago 1ae4e1096335a981b91bb09c19f830b4eb417903 Browse files
Parents 94a9e94
Changed files
3
Additions
+28
Deletions
-1
api/src/controllers/moods.ts
1 1
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 {
9 10
  CreateMoodSchema
10 11
} from '@app/schemas'
42 43
      )
43 44
    } 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
1 1
import { Router } from "express";
2 2
import { MoodsController } from '@app/controllers/moods'
3 3
import { requireAuth } from '@app/middleware/auth';
4 4

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
1 1
import { prisma } from '@app/lib/prisma';
2 2
import { Mood } from '@prisma/client';
3 3

4 4
import { CreateMoodInput } from '@app/schemas';
5 5

59 59
    total,
60 60
    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 +
}