Quack Service & Controller
We will create our Quack service and controller
Having created the model we can now begin implementing the CRUD functionalities.
We want to list
, create
, read
, update
, and delete
the Quacks.
Quack Service#
We start with the service, so let's go over them one by one. We can start with the imports:
import { httpErrors } from "oak";
import { AppRouterContext } from "../../main.ts";
import { getClient } from "../utils.ts";
import { Quack, QuackKeys } from "./model.ts";
List Quacks#
export async function listQuack(ctx: AppRouterContext): Promise<Array<Quack>> {
const client = await getClient(ctx);
const result = await client.queryObject<Quack>(
`SELECT * FROM ${QuackKeys.TableName}`,
);
client.release();
return result.rows;
}
The list
method is straightforward. With larger applications you would want to
have some filtering, sorting and pagination, but that is a bit outside of the
scope of this course, and is usually handled by ORMs
Create Quack#
export async function createQuack(
ctx: AppRouterContext,
data: Partial<Quack>,
): Promise<Quack> {
if (!data.text || !data.createdBy) {
throw new httpErrors.BadRequest("Missing data");
}
const client = await getClient(ctx);
const result = await client.queryObject<Quack>({
text:
`INSERT INTO ${QuackKeys.TableName} (${QuackKeys.ColText}, ${QuackKeys.ColCreatedBy}) VALUES ($1, $2) RETURNING *`,
args: [
data.text,
data.createdBy,
],
});
client.release();
return result.rows[0];
}
With a failsafe for missing data, this method creates a Quack with created_by
and text
while using default values for the rest.
Read Quack#
export async function readQuack(
ctx: AppRouterContext,
id: Quack["id"],
): Promise<Quack | null> {
const client = await getClient(ctx);
const result = await client.queryObject<Quack>({
text:
`SELECT * FROM ${QuackKeys.TableName} WHERE ${QuackKeys.ColId} = $1 LIMIT 1`,
args: [id],
});
client.release();
if (result.rows?.[0]?.id !== id) {
return null;
}
return result.rows[0];
}
Here we simply pass in the id
and return either the Quack
or null
Update Quack#
export async function updateQuack(
ctx: AppRouterContext,
id: Quack["id"],
data: Partial<Quack>,
): Promise<Quack> {
if (!data.text) {
throw new httpErrors.BadRequest("Missing data");
}
const client = await getClient(ctx);
const result = await client.queryObject<Quack>({
text:
`UPDATE ${QuackKeys.TableName} SET ${QuackKeys.ColText} = $1 WHERE ${QuackKeys.ColId} = $2 RETURNING *`,
args: [
data.text,
id,
],
});
client.release();
return result.rows[0];
}
As only the creator can edit a Quack, we don't need to update anything else than the text.
Delete Quack#
export async function deleteQuack(
ctx: AppRouterContext,
id: Quack["id"],
): Promise<void> {
const client = await getClient(ctx);
await client.queryObject({
text: `DELETE FROM ${QuackKeys.TableName} WHERE ${QuackKeys.ColId} = $1`,
args: [id],
});
client.release();
}
Simply deleting the Quack based on the ID, we don't need to return anything and just assume that something was deleted if no error was thrown.
Quack controller#
This page is a preview of Build and deploy a REST API with Deno