11import { FastifyInstance } from "fastify" ;
22import { StatusCodes } from "http-status-codes" ;
33
4- const dynamicDbData : any = [ ] ;
4+ import { notes } from "./../models/persistence/note" ;
55
66interface Diary {
77 id ?: number ;
@@ -13,14 +13,14 @@ interface Diary {
1313// CRUD endpoints for diary app
1414export async function diaryRouter ( app : FastifyInstance ) {
1515 app . get ( "/" , async ( request , reply ) => {
16- reply . status ( StatusCodes . OK ) . send ( dynamicDbData ) ;
16+ reply . status ( StatusCodes . OK ) . send ( notes ) ;
1717 } ) ;
1818
1919 app . get ( "/:id" , async ( request , reply ) => {
2020 const { id } = request . params as { id : string } ;
2121 const idInteger = parseInt ( id , 10 ) ; // Cast to integer decimal number
2222
23- const diary = dynamicDbData . find ( ( item : Diary ) => item . id === idInteger ) ;
23+ const diary = notes . find ( ( item : Diary ) => item . id === idInteger ) ;
2424
2525 if ( ! diary ) {
2626 console . info ( "Note entry not found" ) ;
@@ -40,13 +40,13 @@ export async function diaryRouter(app: FastifyInstance) {
4040 return ;
4141 }
4242
43- body . id = dynamicDbData . length + 1 ;
43+ body . id = notes . length + 1 ;
4444
4545 if ( ! body . createdAt ) {
4646 body . createdAt = new Date ( ) ;
4747 }
4848
49- dynamicDbData . push ( body ) ;
49+ notes . push ( body ) ;
5050 reply . status ( StatusCodes . CREATED ) . send ( body ) ;
5151 } ) ;
5252
@@ -55,27 +55,23 @@ export async function diaryRouter(app: FastifyInstance) {
5555 const { id } = request . params as { id : string } ;
5656 const idInteger = parseInt ( id , 10 ) ; // Cast to integer decimal number
5757
58- const index = dynamicDbData . findIndex (
59- ( item : Diary ) => item . id === idInteger
60- ) ;
58+ const index = notes . findIndex ( ( item : Diary ) => item . id === idInteger ) ;
6159 if ( index === - 1 ) {
6260 reply . status ( StatusCodes . NOT_FOUND ) . send ( ) ;
6361 } else {
64- dynamicDbData [ index ] = { ...dynamicDbData [ index ] , ...( body as Diary ) } ;
65- reply . status ( StatusCodes . OK ) . send ( dynamicDbData [ index ] ) ;
62+ notes [ index ] = { ...notes [ index ] , ...( body as Diary ) } ;
63+ reply . status ( StatusCodes . OK ) . send ( notes [ index ] ) ;
6664 }
6765 } ) ;
6866
6967 app . delete ( "/:id" , async ( request , reply ) => {
7068 const { id } = request . params as { id : string } ;
71- const index = dynamicDbData . findIndex (
72- ( item : Diary ) => item . id === parseInt ( id )
73- ) ;
69+ const index = notes . findIndex ( ( item : Diary ) => item . id === parseInt ( id ) ) ;
7470
7571 if ( index === - 1 ) {
7672 reply . status ( StatusCodes . NOT_FOUND ) . send ( ) ;
7773 } else {
78- dynamicDbData . splice ( index , 1 ) ;
74+ notes . splice ( index , 1 ) ;
7975 reply . status ( StatusCodes . OK ) . send ( {
8076 message : "Note entry deleted" ,
8177 } ) ;
0 commit comments