@@ -2,7 +2,14 @@ import {
22 FastifyPluginAsyncTypebox ,
33 Type
44} from "@fastify/type-provider-typebox" ;
5- import { TaskSchema } from "../../../schemas/tasks.js" ;
5+ import {
6+ TaskSchema ,
7+ Task ,
8+ CreateTaskSchema ,
9+ UpdateTaskSchema ,
10+ TaskStatus
11+ } from "../../../schemas/tasks.js" ;
12+ import { FastifyReply } from "fastify" ;
613
714const plugin : FastifyPluginAsyncTypebox = async ( fastify ) => {
815 fastify . get (
@@ -16,9 +23,160 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
1623 }
1724 } ,
1825 async function ( ) {
19- return [ { id : 1 , name : "Do something..." } ] ;
26+ const tasks = await fastify . repository . findMany < Task > ( "tasks" ) ;
27+
28+ return tasks ;
29+ }
30+ ) ;
31+
32+ fastify . get (
33+ "/:id" ,
34+ {
35+ schema : {
36+ params : Type . Object ( {
37+ id : Type . Number ( )
38+ } ) ,
39+ response : {
40+ 200 : TaskSchema ,
41+ 404 : Type . Object ( { message : Type . String ( ) } )
42+ } ,
43+ tags : [ "Tasks" ]
44+ }
45+ } ,
46+ async function ( request , reply ) {
47+ const { id } = request . params ;
48+ const task = await fastify . repository . find < Task > ( "tasks" , { where : { id } } ) ;
49+
50+ if ( ! task ) {
51+ return notFound ( reply ) ;
52+ }
53+
54+ return task ;
55+ }
56+ ) ;
57+
58+ fastify . post (
59+ "/" ,
60+ {
61+ schema : {
62+ body : CreateTaskSchema ,
63+ response : {
64+ 201 : {
65+ id : Type . Number ( )
66+ }
67+ } ,
68+ tags : [ "Tasks" ]
69+ }
70+ } ,
71+ async function ( request , reply ) {
72+ const id = await fastify . repository . create ( "tasks" , { data : { ...request . body , status : TaskStatus . New } } ) ;
73+ reply . code ( 201 ) ;
74+
75+ return {
76+ id
77+ } ;
78+ }
79+ ) ;
80+
81+ fastify . patch (
82+ "/:id" ,
83+ {
84+ schema : {
85+ params : Type . Object ( {
86+ id : Type . Number ( )
87+ } ) ,
88+ body : UpdateTaskSchema ,
89+ response : {
90+ 200 : TaskSchema ,
91+ 404 : Type . Object ( { message : Type . String ( ) } )
92+ } ,
93+ tags : [ "Tasks" ]
94+ }
95+ } ,
96+ async function ( request , reply ) {
97+ const { id } = request . params ;
98+ const affectedRows = await fastify . repository . update ( "tasks" , {
99+ data : request . body ,
100+ where : { id }
101+ } ) ;
102+
103+ if ( affectedRows === 0 ) {
104+ return notFound ( reply )
105+ }
106+
107+ const task = await fastify . repository . find < Task > ( "tasks" , { where : { id } } ) ;
108+
109+ return task as Task ;
110+ }
111+ ) ;
112+
113+ fastify . delete (
114+ "/:id" ,
115+ {
116+ schema : {
117+ params : Type . Object ( {
118+ id : Type . Number ( )
119+ } ) ,
120+ response : {
121+ 204 : Type . Null ( ) ,
122+ 404 : Type . Object ( { message : Type . String ( ) } )
123+ } ,
124+ tags : [ "Tasks" ]
125+ }
126+ } ,
127+ async function ( request , reply ) {
128+ const { id } = request . params ;
129+ const affectedRows = await fastify . repository . delete ( "tasks" , { id } ) ;
130+
131+ if ( affectedRows === 0 ) {
132+ return notFound ( reply )
133+ }
134+
135+ reply . code ( 204 ) . send ( null ) ;
20136 }
21137 ) ;
138+
139+ fastify . post (
140+ "/:id/assign" ,
141+ {
142+ schema : {
143+ params : Type . Object ( {
144+ id : Type . Number ( )
145+ } ) ,
146+ body : Type . Object ( {
147+ userId : Type . Optional ( Type . Number ( ) )
148+ } ) ,
149+ response : {
150+ 200 : TaskSchema ,
151+ 404 : Type . Object ( { message : Type . String ( ) } )
152+ } ,
153+ tags : [ "Tasks" ]
154+ }
155+ } ,
156+ async function ( request , reply ) {
157+ const { id } = request . params ;
158+ const { userId } = request . body ;
159+
160+ const task = await fastify . repository . find < Task > ( "tasks" , { where : { id } } ) ;
161+ if ( ! task ) {
162+ return notFound ( reply ) ;
163+ }
164+
165+ await fastify . repository . update ( "tasks" , {
166+ data : { assigned_user_id : userId } ,
167+ where : { id }
168+ } ) ;
169+
170+ task . assigned_user_id = userId
171+
172+ return task ;
173+ }
174+ )
22175} ;
23176
177+ function notFound ( reply : FastifyReply ) {
178+ reply . code ( 404 )
179+ return { message : "Task not found" }
180+ }
181+
24182export default plugin ;
0 commit comments