Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created July 24, 2023 02:54
Show Gist options
  • Save christopherbauer/f111d5ef4e29ad2a2fa4e8cbe884741a to your computer and use it in GitHub Desktop.
Save christopherbauer/f111d5ef4e29ad2a2fa4e8cbe884741a to your computer and use it in GitHub Desktop.
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