This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
137 lines (131 loc) · 4.06 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Habit } from '@prisma/client'
import type { NextApiRequest, NextApiResponse } from 'next'
import { unstable_getServerSession } from 'next-auth/next'
import { authOptions } from 'pages/api/auth/[...nextauth]'
import { prisma } from 'prisma/client'
/**
* @swagger
* /api/habits:
* get:
* description: Retrieves the the calling user's habits
* tags:
* - habits
* responses:
* 200:
* description: JSON representation of the calling user's habits
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/Habit'
* 401:
* description: No authorization details sent in request
* post:
* description: Creates a habit for the calling user
* tags:
* - habits
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* description: The name of the habit to create
* example: Exercise
* responses:
* 200:
* description: JSON representation of the created habit
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Habit'
* 401:
* description: No authorization details sent in request
*
* components:
* schemas:
* Habit:
* properties:
* habitId:
* type: integer
* description: the habit's id
* example: 17
* userId:
* type: string
* description: the habit owner's user id
* example: abc123
* name:
* type: string
* description: the habit's name
* example: Exercise
* streak:
* type: integer
* description: the habit's current streak
* example: 0
* streakContinuedAt:
* type: string
* nullable: true
* description: the date time in UTC the habit's streak was last continued
* example: null
* createdAt:
* type: string
* description: the date time in UTC the habit was created
* example: 2021-10-27T00:13:47.985Z
* updatedAt:
* type: string
* description: the date time in UTC the habit was updated
* example: 2021-11-27T20:32:42.402Z
*/
export default async function handler(req: NextApiRequest, res: NextApiResponse<Habit[] | Habit>) {
const session = await unstable_getServerSession(req, res, authOptions)
if (session && session.user && session.user.id) {
switch (req.method) {
case 'GET':
// get habits for a user
const habits = await getHabitsForUser(session.user.id)
res.status(200).json(habits)
return
case 'POST':
// create a new habit for a user
const newHabit = await createHabitForUser(session.user.id, req.body.name)
res.status(200).json(newHabit)
return
default:
res.setHeader('Allow', ['GET', 'POST'])
res.status(405).end(`Method ${req.method} Not Allowed`)
return
}
} else {
res.status(401).end()
return
}
}
export async function getHabitsForUser(userId: string): Promise<Habit[]> {
return await prisma.habit.findMany({
where: {
userId: userId,
},
orderBy: {
habitId: 'asc',
},
})
}
export async function createHabitForUser(userId: string, habitName: string): Promise<Habit> {
const createdHabit = await prisma.habit.create({
data: {
userId: userId,
name: habitName,
},
})
await prisma.habitActivityLog.create({
data: {
habitId: createdHabit.habitId,
activity: 'created',
},
})
return createdHabit
}