Created
July 24, 2023 02:54
-
-
Save christopherbauer/f111d5ef4e29ad2a2fa4e8cbe884741a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface DatabaseModel { | |
id: number; | |
name: string; | |
description: string; | |
createdAt: Date; | |
updatedAt: Date; | |
archived: boolean; | |
} | |
type PutAPIModel = Partial<DatabaseModel>; | |
type APIRequest = { body: PutAPIModel }; | |
type StatusResponse = { send: (o: any) => void }; | |
type APIResponse = { status: (code: number) => StatusResponse }; | |
const update: (id: number, model: Partial<DatabaseModel>) => Promise<boolean> = () => Promise.resolve(true); | |
const insert: (model: Partial<DatabaseModel>) => Promise<boolean> = () => Promise.resolve(true); | |
const put = async (request: APIRequest, response: APIResponse) => { | |
const { body } = request; | |
const { id } = body; | |
let success; | |
if (id) { | |
//update | |
success = await update(id, body); | |
} else { | |
//insert | |
success = await insert(body); | |
} | |
if (success) { | |
response.status(200).send(success); | |
} else { | |
response.status(400).send(success); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment