Last active
May 30, 2021 04:15
-
-
Save achukka/1d5ff6b718302b7cfd9706295c2f3f48 to your computer and use it in GitHub Desktop.
Create data source for tdsvc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync, writeFileSync } from "fs"; | |
import { item, user } from "./models"; | |
const ITEMS_SOURCE = "data/items.json"; | |
const USERS_SOURCE = "data/users.json"; | |
const load_items = (): Array<item> => { | |
const data = readFileSync(ITEMS_SOURCE, "utf-8"); | |
if (!data) { | |
return []; | |
} | |
return JSON.parse(data); | |
}; | |
const load_users = (): Array<user> => { | |
const data = readFileSync(USERS_SOURCE, "utf-8"); | |
if (!data) { | |
return []; | |
} | |
return JSON.parse(data); | |
}; | |
const write_items = (items: Array<item>): void => { | |
const data = JSON.stringify(items); | |
writeFileSync(ITEMS_SOURCE, data); | |
}; | |
const write_users = (users: Array<user>): void => { | |
const data = JSON.stringify(users); | |
writeFileSync(USERS_SOURCE, data); | |
}; | |
export const get_item = (id: number): item => { | |
const items = load_items(); | |
return items.find((i) => i.id == id); | |
}; | |
export const add_item = (item: item): string => { | |
const items = load_items(); | |
const new_items = items.concat(item); | |
try { | |
write_items(new_items); | |
return "Successfullly added item"; | |
} catch (error) { | |
return `Cannot add due to ${error}`; | |
} | |
}; | |
export const get_user = (id: number): user => { | |
const users = load_users(); | |
return users.find((i) => i.id == id); | |
}; | |
export const add_users = (user: user): string => { | |
const users = load_users(); | |
const new_users = users.concat(user); | |
try { | |
write_users(new_users); | |
return "Successfullly added user"; | |
} catch (error) { | |
return `Cannot add due to ${error}`; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 1, | |
"task": "Prepare Coffee", | |
"priority": 1, | |
"date": "2021-05-01" | |
}, | |
{ | |
"id": 2, | |
"task": "Boil Eggs", | |
"priority": 2, | |
"date": "2021-05-01" | |
}, | |
{ | |
"id": 3, | |
"task": "Buy Milk", | |
"priority": 3, | |
"date": "2021-05-01" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface item { | |
id: number; | |
task: string; | |
priority: number; | |
date: Date; | |
} | |
export interface user { | |
id: number; | |
first_name: string; | |
last_name: string; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 1, | |
"first_name": "John", | |
"last_name": "Doe" | |
}, | |
{ | |
"id": 2, | |
"first_name": "Brad", | |
"last_name": "Gabson" | |
}, | |
{ | |
"id": 3, | |
"first_name": "Allen", | |
"last_name": "Ray" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment