CLI can add, list, delete colors

This commit is contained in:
Ryan Pandya 2024-11-22 12:38:03 -08:00
parent de9dca5274
commit 47e8371fb3
5 changed files with 51 additions and 17 deletions

View File

@ -21,14 +21,14 @@ colorsCmd
try {
const colors = (await api.colors.list.query()).colors;
colors.sort((a, b) => b.numCategories - a.numCategories);
// colors.sort((a, b) => b.numCategories - a.numCategories);
if (getGlobalOptions().json) {
printObject(colors);
} else {
const data: string[][] = [["Id", "Name", "Hexcode", "Num categories"]];
const data: string[][] = [["Id", "Name", "Hexcode", "user"]];
colors.forEach((color) => {
data.push([color.id, color.name, color.hexcode, color.numCategories.toString()]);
data.push([color.id, color.name, color.hexcode, color.userId]);
});
console.log(
data.length <= 1 ?
@ -61,6 +61,21 @@ colorsCmd
.catch(printError(`Failed to create the color "${name}"`));
});
colorsCmd
.command("delete")
.description("delete a color")
.argument("<name>", "the name of the color")
.action(async (name: string) => {
const api = getAPIClient();
await api.colors.delete
.mutate({
colorName: name,
})
.then(printSuccess(`Successfully deleted the color "${name}"`))
.catch(printError(`Failed to delete the color "${name}"`));
});
// colorsCmd
// .command("delete")
// .description("delete a color")

View File

@ -139,7 +139,11 @@ export const colors = sqliteTable(
.references(() => users.id, { onDelete: "cascade" }),
},
(c) => ({
uniq: unique().on(c.userId, c.name)
// Name should be unique per user
uniq: unique().on(c.userId, c.name),
// Hexcode should start with a pound sign, or have one prepended
// TODO. No internet, so no chatGPT cheating nor regular google looking up,
// and fuck if the in built documentation makes any sense
}),
);
export const categories = sqliteTable(

View File

@ -18,6 +18,7 @@
"drizzle-orm": "^0.33.0",
"superjson": "^2.2.1",
"tiny-invariant": "^1.3.3",
"title-case": "^4.3.2",
"zod": "^3.23.8"
},
"devDependencies": {

View File

@ -9,26 +9,23 @@ import {
} from "@lifetracker/shared/types/colors";
import type { Context } from "../index";
import { authedProcedure, router } from "../index";
import { titleCase } from "title-case";
function conditionFromInput(input: { colorId: string }, userId: string) {
return and(eq(colors.id, input.colorId), eq(colors.userId, userId));
function conditionFromInput(input: { colorName: string }, userId: string) {
return and(eq(titleCase(colors.name), titleCase(input.colorName)), eq(colors.userId, userId));
}
async function createColor(
input: ZColor,
ctx: Context,
) {
console.log("[TRPC Router] Begin Create Color");
return ctx.db.transaction(async (trx) => {
try {
const result = await trx
.insert(colors)
.values({
name: input.name,
name: titleCase(input.name),
hexcode: input.hexcode,
inverse: calcInverseColor(input.hexcode),
userId: ctx.user!.id,
@ -65,7 +62,7 @@ export const colorsAppRouter = router({
id: z.string(),
name: z.string(),
hexcode: z.string(),
numCategories: z.number(),
userId: z.string(),
}),
),
}))
@ -75,13 +72,13 @@ export const colorsAppRouter = router({
id: colors.id,
name: colors.name,
hexcode: colors.hexcode,
userId: colors.userId,
})
.from(colors);
return {
colors: dbColors.map(({ ...color }) => ({
...color,
numCategories: 0,
})),
};
}),
@ -97,4 +94,18 @@ export const colorsAppRouter = router({
.mutation(async ({ input, ctx }) => {
return createColor(input, ctx);
}),
});
delete: authedProcedure
.input(
z.object({
colorName: z.string(),
}),
)
.mutation(async ({ input, ctx }) => {
const res = await ctx.db
.delete(colors)
.where(conditionFromInput(input, ctx.user.id));
if (res.changes == 0) {
throw new TRPCError({ code: "NOT_FOUND" });
}
}),
});

9
pnpm-lock.yaml generated
View File

@ -568,6 +568,9 @@ importers:
tiny-invariant:
specifier: ^1.3.3
version: 1.3.3
title-case:
specifier: ^4.3.2
version: 4.3.2
zod:
specifier: ^3.23.8
version: 3.23.8
@ -19731,7 +19734,7 @@ snapshots:
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
eslint-plugin-jsx-a11y: 6.8.0(eslint@8.57.0)
eslint-plugin-react: 7.33.2(eslint@8.57.0)
eslint-plugin-react-hooks: 4.6.0(eslint@8.57.0)
@ -19785,7 +19788,7 @@ snapshots:
enhanced-resolve: 5.15.0
eslint: 8.57.0
eslint-module-utils: 2.8.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0)
fast-glob: 3.3.1
get-tsconfig: 4.7.2
is-core-module: 2.13.1
@ -19824,7 +19827,7 @@ snapshots:
eslint: 8.57.0
ignore: 5.3.1
eslint-plugin-import@2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-node@0.3.9)(eslint-plugin-import@2.29.0(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0):
eslint-plugin-import@2.29.0(@typescript-eslint/parser@7.1.0(eslint@8.57.0)(typescript@5.3.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0):
dependencies:
array-includes: 3.1.7
array.prototype.findlastindex: 1.2.3