From 3f1793a67d3756878ef4f4e9490beb13568a08e8 Mon Sep 17 00:00:00 2001 From: Ricardo Brito Date: Sun, 2 Jun 2024 03:15:32 -0300 Subject: [PATCH 1/2] feat: Added enhancement branch and test implementations. --- bun.lockb | Bin 7355 -> 7007 bytes .../20240602030715_init/migration.sql | 11 +++ prisma/migrations/migration_lock.toml | 3 + prisma/schema.prisma | 10 +- prisma/table.sql | Bin 0 -> 24576 bytes src/entities/index.ts | 17 +++- src/routes/index.ts | 34 ++----- src/services/index.ts | 91 ++++++++++++++---- 8 files changed, 118 insertions(+), 48 deletions(-) create mode 100644 prisma/migrations/20240602030715_init/migration.sql create mode 100644 prisma/migrations/migration_lock.toml create mode 100644 prisma/table.sql diff --git a/bun.lockb b/bun.lockb index f058f2d67b0e20dde748b694965fc1e0dd5f4f61..64132c0ac913cfb079462377262b5911b3dca5b7 100755 GIT binary patch delta 1192 zcmc&zO-NKx6u$S(JZIj_ym6dA=cmAglu4XsrktGo8A-@$5DK!%$UqZvB2i03k<>+0 z^g~W3LTu3{s#)kFDKd+sq?D4ZTpG5`f^1bT($0Cl2W?t+;NI_k=iGblx#yfa5nGH7 z1_M2vLpK*14i(*axnb(T_~P*K<@29bnvaGT%;TpjfBHAS^D#woAe`k$pS0L(bJi7% zb+$FP6*so0(t~-M44koS#s;1u<|1|>IuYv-WyGfR``plojxhC8v~OEY%{`do91eMh{Ek&VB>lMuX&U#LJlx2O++j;d3b&iV%`siXkiTNlch4ac0i z#b$?5du5Oq6^#<_z(Iw>rlf|YW>pGNMMjh%I#M8!7c!fm){(o5dXOB@K`Ka=%1Nhj za)jSS=?1Dib;GpkRPTbjjx8|n$Qb#ta3mNGhTyi-j~SbAdU+jGxV*dxzT>(JI$U1A z_T^%u)zvoP$~A#s!g53~4eNW?XJv2n-+Q&c+57Fp>_z+6e1V7Y<`~Actb<9HC#rqg z^wuL+3m#U)1l|`31u?!-?HfyN<*$|w3{Rk?Bp8VXx3d);Slu4eezj=_U08%F>}F_p ihkVb~-+!*O1EUi;vQdk^IK{bTjHP2wMD<<)=y delta 1448 zcmcgsYiLwQ6rQ=+>pt%$`--`)d6=b%qPhFfU9%dqtAXmeZ53(RCK8Ko)T|~ZQKOQ8 z31X{1YU6r-8705^NQDG3+9p0wuvA;BMi3!T(Sj9g17Z-GDjLt(y;uC9e?2hs%{g;s z&YAO=yP>SGf_v-gz5C)nw++uMDf4=4qv z0|$D^Tq>WBNQBsxQNS#{ruXX*S|$(Q@F?pLj7{4aO+Pv?a`x})fuh9j%*t;E=um zKGw&14jk1dV1+?~7s!baGbCUVxdB6*>*0Uo`i&CQ8{^yvKN=J8xk&=a6z68BH6_4c zmf!+%5*W=17)Gwy9Ot<(f?T6TfjDod{sjs)bsF4GnfJjphs;mGDyQt$ewy%LO--Kgb&tS*#Rmm2 z&{^H=oY4)vTUYykI>$F(J81v6RN#KauY}Rnc{=EG7Pz&KDebL0RCZ=bSm1jDKE)qU zDm&n&Qx<|c7dh&}T3X8YGOHdI2pX?cW;o2a+0-J#x9RL6T diff --git a/prisma/migrations/20240602030715_init/migration.sql b/prisma/migrations/20240602030715_init/migration.sql new file mode 100644 index 0000000..eda07a6 --- /dev/null +++ b/prisma/migrations/20240602030715_init/migration.sql @@ -0,0 +1,11 @@ +-- CreateTable +CREATE TABLE "todos" ( + "id" TEXT NOT NULL PRIMARY KEY, + "title" TEXT NOT NULL, + "owner" TEXT NOT NULL DEFAULT 'Anonymous', + "avatar" TEXT DEFAULT 'https://abre.ai/jUTD', + "done" BOOLEAN NOT NULL DEFAULT false +); + +-- CreateIndex +CREATE UNIQUE INDEX "todos_title_key" ON "todos"("title"); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..e5e5c47 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "sqlite" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a086d26..a99ceef 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,4 +7,12 @@ datasource db { url = env("DATABASE_URL") } -// Model definitions +model Todo { + id String @id + title String @unique + owner String @default("Anonymous") + avatar String? @default("https://abre.ai/jUTD") + done Boolean @default(false) + + @@map("todos") +} diff --git a/prisma/table.sql b/prisma/table.sql new file mode 100644 index 0000000000000000000000000000000000000000..ebb8a55167c983dfe9c2181f40d1e4566bdd4c70 GIT binary patch literal 24576 zcmeI(O^@3|7zc2>*`(d5k&!@(xQ!yMc2|&HJFi|OB+`w$4WvmoO`zF}8NVd!ZenNc z39#aTRux?NARIVw>4}d~@fE0w)FVeGjvcZ|i$Ji4qW>sP{PN5*p5Hi;{E~;qO^4ZJ z;Q3?9NKtr85Jll5LIgqB;!}!GaY^u*jd+27ic9mao81!j2G29n??Uo+SI9h*e!Bf* z<_A8D0s#m>00Izz00bZa0SNq01p3KT_Pwn5jpJJOSIo1#KxYmc+4`A%5iX_7l1IyO-i^Nfj zE|YlZ%&G6&F5}1l*yfeTlc+g5nT#BsC}8#^&`odZhCOYxy4sP};a`l0jiZ)UUr#8f zcMor*vY#ohI}X+dPFL2~&Cca-&3ri-M5cq=Q1GD66HsQ81KzsaiGis%jPUWm!=vrGw&uRZuFbQ9T$`Y)df= z`#>=cR70&Alon*uG^nhW%a+L_OG>$HlnRBitkOcovPw#|Tr`R$@%v}bKfhOy3njTC z7v!Q`RmwWQzQV!{M`_+KQTj#TFBAws00Izz00bZa0SG_<0uX=z1l}-#?L_ZyU0#^q z|9>h-Po=Nlur6YEApijgKmY;|fB*y_009U<00P%8ApI;H=HG4H>Q_tW-btl4;g9)S zwohvAa5|#C;X90Z@qa#s4m+J1d!{$upKloKP23^h$8TpTE?Vljanf0&=-AX`kAuHX zaaT-j%_`sfwB4VW519LLHTY*4yl^Mx_y1oC(o5;rYez!^5P$##AOHafKmY;|fB*y_ z009VGyFiv-X^;|Io6FY>=I8$}1nC9-0dVaDfCeA{0SG_<0uX=z1Rwwb2tWV=|3X0e bO?YQ~9~fv4M9tc#u~csr5bTwV7E`E0a~ literal 0 HcmV?d00001 diff --git a/src/entities/index.ts b/src/entities/index.ts index 4986cce..470249c 100644 --- a/src/entities/index.ts +++ b/src/entities/index.ts @@ -1,7 +1,14 @@ +import type { Todo } from "@prisma/client"; + +export interface Response { + status: number; + data?: Todo[] | Todo | { error: string }; +} + export abstract class Services { - abstract create(resource: unknown): Promise; - abstract read(): Promise; - abstract readOne(id: string): Promise; - abstract update(id: string): Promise; - abstract delete(id: string): Promise; + abstract create(resource: unknown): Promise; + abstract read(): Promise; + abstract readOne(id: string): Promise; + abstract update(id: string, resource: unknown): Promise; + abstract delete(id: string): Promise; } diff --git a/src/routes/index.ts b/src/routes/index.ts index 42968ec..3869651 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,57 +1,41 @@ -import { isEmpty } from "radash"; import type { Hono } from "hono"; +import type { StatusCode } from "hono/utils/http-status"; import services from "services/index"; export const BrowserRouter = (Router: Hono) => { Router.post("/", async (context) => { const data = await context.req.json(); - const response = await services.create(data); + const todo = await services.create(data); - if (response.status !== 201) - return context.json({ error: "Failed to create resource" }, 500); - - return context.body(response.data, 201); + return context.json(todo.data, todo.status as StatusCode); }); Router.get("/", async (context) => { const response = await services.read(); - if (isEmpty(response.data)) - return context.json({ message: "No resources registered" }, 200); - - return context.json(response.data, 200); + return context.json(response.data, response.status as StatusCode); }); Router.get("/:id", async (context) => { const id = context.req.param("id"); - const response = await services.readOne(id); - if (isEmpty(response.data)) - return context.json({ error: "Resource not found" }, 404); - - return context.json(response.data, 200); + return context.json(response.data, response.status as StatusCode); }); Router.put("/:id", async (context) => { const id = context.req.param("id"); - const response = await services.update(id); - - if (isEmpty(response.data)) - return context.json({ error: "Resource not found" }, 200); + const { title, done } = await context.req.json(); + const response = await services.update(id, { title, done }); - return context.json(response.data, 200); + return context.json(response.data, response.status as StatusCode); }); Router.delete("/:id", async (context) => { const id = context.req.param("id"); const response = await services.delete(id); - if (response.status !== 200) { - return context.json({ error: "Failed to delete resource" }, 500); - } - - return context.body(response.data, 200); + return context.json(response.data, response.status as StatusCode); }); return Router; diff --git a/src/services/index.ts b/src/services/index.ts index 5e3b641..58d9d14 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,31 +1,88 @@ -import { uid } from "radash"; +import type { Todo } from "@prisma/client"; +import { uid, isEmpty } from "radash"; import { db } from "config/orm/client"; -import type { Services } from "entities/index"; +import type { Services, Response } from "entities/index"; class Service implements Services { - public async create(resource: unknown) { - // Implement the create method here - return { status: 201, data: null }; + public async create({ + title, + done, + owner, + avatar + }: Todo): Promise { + try { + await db.todo.create({ + data: { + id: uid(10), + title, + done, + owner, + avatar + } + }); + + return { status: 201 }; + } catch (error) { + return { status: 409, data: { error: "Todo already exists" } }; + } } - public async read() { - // Implement the read method here - return { status: 200, data: [] }; + public async read(): Promise { + try { + const todos = await db.todo.findMany(); + + if (isEmpty(todos)) return { status: 204 }; + + return { status: 200, data: todos }; + } catch (error) { + return { status: 500, data: { error: "Internal server error" } }; + } } - public async readOne(id: string) { - // Implement the readOne method here - return { status: 200, data: {} }; + public async readOne(id: string): Promise { + try { + const todo = await db.todo.findUnique({ + where: { id } + }); + + if (isEmpty(todo)) { + return { status: 404, data: { error: "Todo not found" } }; + } + + return { status: 200, data: todo as Todo }; + } catch (error) { + return { status: 500, data: { error: "Internal server error" } }; + } } - public async update(id: string) { - // Implement the update method here - return { status: 200, data: {} }; + public async update( + id: string, + data: { title: string; done: boolean } + ): Promise { + try { + const todo = await db.todo.update({ + where: { id }, + data: { + ...data + } + }); + + return { status: 200, data: todo }; + } catch (error) { + return { status: 404, data: { error: "Todo not found" } }; + } } - public async delete(id: string) { - // Implement the delete method here - return { status: 200, data: null }; + public async delete(id: string): Promise { + try { + await db.todo.delete({ + where: { id } + }); + + return { status: 200 }; + } catch (error) { + return { status: 404, data: { error: "Todo not found" } }; + } } } From 62d901192bde5bf08371d5dff03e592d786c5dd5 Mon Sep 17 00:00:00 2001 From: Ricardo Brito Date: Sun, 2 Jun 2024 18:45:43 -0300 Subject: [PATCH 2/2] refactor: Improvement in writing services --- .../migration.sql | 4 +- prisma/schema.prisma | 12 ++- prisma/table.sql | Bin 24576 -> 24576 bytes src/app.ts | 2 +- src/entities/index.ts | 14 --- src/interfaces/index.ts | 4 + src/main.ts | 2 +- src/routes/index.ts | 21 ++-- src/services/create/index.ts | 28 ++++++ src/services/delete/index.ts | 15 +++ src/services/index.ts | 94 ++---------------- src/services/read/findMany.ts | 9 ++ src/services/read/findOne.ts | 18 ++++ src/services/update/index.ts | 24 +++++ src/{config => utils}/logger.ts | 2 +- tsconfig.json | 6 +- 16 files changed, 132 insertions(+), 123 deletions(-) rename prisma/migrations/{20240602030715_init => 20240602214206_init}/migration.sql (66%) delete mode 100644 src/entities/index.ts create mode 100644 src/interfaces/index.ts create mode 100644 src/services/create/index.ts create mode 100644 src/services/delete/index.ts create mode 100644 src/services/read/findMany.ts create mode 100644 src/services/read/findOne.ts create mode 100644 src/services/update/index.ts rename src/{config => utils}/logger.ts (87%) diff --git a/prisma/migrations/20240602030715_init/migration.sql b/prisma/migrations/20240602214206_init/migration.sql similarity index 66% rename from prisma/migrations/20240602030715_init/migration.sql rename to prisma/migrations/20240602214206_init/migration.sql index eda07a6..8dc6741 100644 --- a/prisma/migrations/20240602030715_init/migration.sql +++ b/prisma/migrations/20240602214206_init/migration.sql @@ -2,9 +2,11 @@ CREATE TABLE "todos" ( "id" TEXT NOT NULL PRIMARY KEY, "title" TEXT NOT NULL, + "done" BOOLEAN NOT NULL DEFAULT false, "owner" TEXT NOT NULL DEFAULT 'Anonymous', "avatar" TEXT DEFAULT 'https://abre.ai/jUTD', - "done" BOOLEAN NOT NULL DEFAULT false + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL ); -- CreateIndex diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a99ceef..081b841 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -8,11 +8,13 @@ datasource db { } model Todo { - id String @id - title String @unique - owner String @default("Anonymous") - avatar String? @default("https://abre.ai/jUTD") - done Boolean @default(false) + id String @id + title String @unique + done Boolean @default(false) + owner String @default("Anonymous") + avatar String? @default("https://abre.ai/jUTD") + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt @@map("todos") } diff --git a/prisma/table.sql b/prisma/table.sql index ebb8a55167c983dfe9c2181f40d1e4566bdd4c70..a7eeb6ea7e64739dfe2d51b116a62d22e40064c5 100644 GIT binary patch delta 523 zcmZoTz}Rqrae_1>+e8^>aW)3Mv{$_RKNwiKzcBD# z>gE{g6QbZ88WiN}7ZMMW4GwYi4bb6I00O1bf)t#ZH?QQqDp+r1YLaG}YNl(MY?-QS zVw#qwn`n||p_`IuZe*HhkZ53Nlwx3RYG7cQVwh}^n3`sqWMOG)k(!odmSmimm}UwT zHBU-1O*2ljOtCOaOf)k{HcT-{OtCaFPBTwVG&D{&VC=v3oZrRBz{teF%)rRV(8S2V zEIuHw!Av;ZKPT=9DyLWvy^C4tF*&$}@0v zEGWuPE6qzuEKO>i5aa9Jrv{$_RKNwhfLKt|j@o(l?&%2r@WV4{aJ?_b0xTCom zE1B8F#l;z$11CrFWKVw2lQ{VZkILqiyjKP5(~QiF4J=G_lZ=uKbxq98lXNXCQjB#? z4Gb+36BE;nEmMpP%`B44Ez`_WQw)=mQY{UWEG?2O%##umjSP~LlM)RqOifdgfhtW5 zO-++bjEqbTEE0{(Qc_F|%}tGyj7=E(uf0m}FfuSQF)%YQGB7qUH#CjU%*!kRN-)1l zaNneGmtUU?9^vsfs(j>j){9JvAM4DbfUOLdsR*(^ueinsEQ9*Dc zf>INcOR|db8WCFJ#>l1$((MuEA6bwbP?G0mUQCh^CHY`iaDlz{fr0-6|I5vS4iESh y#DNatXIAAzHW{Mo4Fmrh{u`SG6?X93@V{jcf!hc5HP9#xxERzM!pypy=wblktF|)$ diff --git a/src/app.ts b/src/app.ts index 3aec509..87ebc00 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,7 +2,7 @@ import { Hono } from "hono"; import { cors } from "hono/cors"; import { BrowserRouter } from "routes/index"; -export const App = new Hono(); +export const App = new Hono({ strict: true }); App.use(cors({ origin: "*" })); App.route("/", BrowserRouter(App)); diff --git a/src/entities/index.ts b/src/entities/index.ts deleted file mode 100644 index 470249c..0000000 --- a/src/entities/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { Todo } from "@prisma/client"; - -export interface Response { - status: number; - data?: Todo[] | Todo | { error: string }; -} - -export abstract class Services { - abstract create(resource: unknown): Promise; - abstract read(): Promise; - abstract readOne(id: string): Promise; - abstract update(id: string, resource: unknown): Promise; - abstract delete(id: string): Promise; -} diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts new file mode 100644 index 0000000..b99516d --- /dev/null +++ b/src/interfaces/index.ts @@ -0,0 +1,4 @@ +import type { Todo } from "@prisma/client"; + +export type HttpError = { message: string }; +export type HttpResponse = Todo | Todo[]; diff --git a/src/main.ts b/src/main.ts index abe4c1d..b1e367a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { log } from "config/logger"; +import { log } from "utils/logger"; import { App } from "./app"; Bun.serve({ diff --git a/src/routes/index.ts b/src/routes/index.ts index 3869651..95c365d 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,26 +1,25 @@ import type { Hono } from "hono"; -import type { StatusCode } from "hono/utils/http-status"; -import services from "services/index"; +import * as services from "services/index"; export const BrowserRouter = (Router: Hono) => { Router.post("/", async (context) => { const data = await context.req.json(); - const todo = await services.create(data); + const response = await services.create(data); - return context.json(todo.data, todo.status as StatusCode); + return context.json(response); }); Router.get("/", async (context) => { - const response = await services.read(); + const response = await services.findMany(); - return context.json(response.data, response.status as StatusCode); + return context.json(response, 200); }); Router.get("/:id", async (context) => { const id = context.req.param("id"); - const response = await services.readOne(id); + const response = await services.findOne(id); - return context.json(response.data, response.status as StatusCode); + return context.json(response); }); Router.put("/:id", async (context) => { @@ -28,14 +27,14 @@ export const BrowserRouter = (Router: Hono) => { const { title, done } = await context.req.json(); const response = await services.update(id, { title, done }); - return context.json(response.data, response.status as StatusCode); + return context.json(response); }); Router.delete("/:id", async (context) => { const id = context.req.param("id"); - const response = await services.delete(id); + const response = await services.destroy(id); - return context.json(response.data, response.status as StatusCode); + return context.json(response); }); return Router; diff --git a/src/services/create/index.ts b/src/services/create/index.ts new file mode 100644 index 0000000..4eee346 --- /dev/null +++ b/src/services/create/index.ts @@ -0,0 +1,28 @@ +import { tryit, uid } from "radash"; +import { HTTPException } from "hono/http-exception"; +import type { Todo } from "@prisma/client"; +import { db } from "config/orm/client"; +import type { HttpError, HttpResponse } from "src/interfaces/index"; + +export const create = async ({ + title, + done, + owner, + avatar +}: Todo): Promise => { + const [error, todo] = await tryit(() => + db.todo.create({ + data: { + id: uid(10), + title, + done, + owner, + avatar + } + }) + )(); + + if (error) throw new HTTPException(409, { message: "Todo Already Exists" }); + + return todo; +}; diff --git a/src/services/delete/index.ts b/src/services/delete/index.ts new file mode 100644 index 0000000..a1b6cff --- /dev/null +++ b/src/services/delete/index.ts @@ -0,0 +1,15 @@ +import { HTTPException } from "hono/http-exception"; +import { tryit } from "radash"; +import { db } from "config/orm/client"; +import type { HttpResponse, HttpError } from "src/interfaces/index"; +import { findMany } from "services/read/findMany"; + +export const destroy = async ( + id: string +): Promise => { + const [error] = await tryit(() => db.todo.delete({ where: { id } }))(); + + if (error) throw new HTTPException(404, { message: "Todo Not Found" }); + + return findMany(); +}; diff --git a/src/services/index.ts b/src/services/index.ts index 58d9d14..e3cf851 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,89 +1,7 @@ -import type { Todo } from "@prisma/client"; -import { uid, isEmpty } from "radash"; -import { db } from "config/orm/client"; -import type { Services, Response } from "entities/index"; +import { findMany } from "./read/findMany"; +import { findOne } from "./read/findOne"; +import { update } from "./update"; +import { create } from "./create"; +import { destroy } from "./delete"; -class Service implements Services { - public async create({ - title, - done, - owner, - avatar - }: Todo): Promise { - try { - await db.todo.create({ - data: { - id: uid(10), - title, - done, - owner, - avatar - } - }); - - return { status: 201 }; - } catch (error) { - return { status: 409, data: { error: "Todo already exists" } }; - } - } - - public async read(): Promise { - try { - const todos = await db.todo.findMany(); - - if (isEmpty(todos)) return { status: 204 }; - - return { status: 200, data: todos }; - } catch (error) { - return { status: 500, data: { error: "Internal server error" } }; - } - } - - public async readOne(id: string): Promise { - try { - const todo = await db.todo.findUnique({ - where: { id } - }); - - if (isEmpty(todo)) { - return { status: 404, data: { error: "Todo not found" } }; - } - - return { status: 200, data: todo as Todo }; - } catch (error) { - return { status: 500, data: { error: "Internal server error" } }; - } - } - - public async update( - id: string, - data: { title: string; done: boolean } - ): Promise { - try { - const todo = await db.todo.update({ - where: { id }, - data: { - ...data - } - }); - - return { status: 200, data: todo }; - } catch (error) { - return { status: 404, data: { error: "Todo not found" } }; - } - } - - public async delete(id: string): Promise { - try { - await db.todo.delete({ - where: { id } - }); - - return { status: 200 }; - } catch (error) { - return { status: 404, data: { error: "Todo not found" } }; - } - } -} - -export default new Service(); +export { findMany, findOne, update, create, destroy }; diff --git a/src/services/read/findMany.ts b/src/services/read/findMany.ts new file mode 100644 index 0000000..da367f0 --- /dev/null +++ b/src/services/read/findMany.ts @@ -0,0 +1,9 @@ +import { tryit } from "radash"; +import { db } from "config/orm/client"; +import type { HttpResponse } from "src/interfaces/index"; + +export const findMany = async (): Promise => { + const [_, todos] = await tryit(db.todo.findMany)(); + + return todos ?? []; +}; diff --git a/src/services/read/findOne.ts b/src/services/read/findOne.ts new file mode 100644 index 0000000..57ab98f --- /dev/null +++ b/src/services/read/findOne.ts @@ -0,0 +1,18 @@ +import { tryit } from "radash"; +import { HTTPException } from "hono/http-exception"; +import type { HttpResponse, HttpError } from "src/interfaces/index"; +import { db } from "config/orm/client"; + +export const findOne = async ( + id: string +): Promise => { + const [error, todo] = await tryit(() => + db.todo.findUniqueOrThrow({ + where: { id } + }) + )(); + + if (error) throw new HTTPException(404, { message: "Todo Not Found" }); + + return todo; +}; diff --git a/src/services/update/index.ts b/src/services/update/index.ts new file mode 100644 index 0000000..29bcc82 --- /dev/null +++ b/src/services/update/index.ts @@ -0,0 +1,24 @@ +import { tryit } from "radash"; +import { HTTPException } from "hono/http-exception"; +import type { HttpResponse, HttpError } from "src/interfaces/index"; +import { db } from "config/orm/client"; + +type Fields = { title: string; done: boolean }; + +export const update = async ( + id: string, + data: Fields +): Promise => { + const [error, todo] = await tryit(() => + db.todo.update({ + where: { id }, + data: { + ...data + } + }) + )(); + + if (error) throw new HTTPException(404, { message: "Todo Not Found" }); + + return todo; +}; diff --git a/src/config/logger.ts b/src/utils/logger.ts similarity index 87% rename from src/config/logger.ts rename to src/utils/logger.ts index fde8d70..2d79086 100644 --- a/src/config/logger.ts +++ b/src/utils/logger.ts @@ -16,6 +16,6 @@ export const log = { chalk.underline("http://localhost:3000"), repository: chalk.hex("#0ba95a").bold("Repository: ") + - chalk.underline("https://github.com/username/repo") + chalk.underline("https://github.com/brittof/galxe") } } satisfies Logger; diff --git a/tsconfig.json b/tsconfig.json index 3b7e3f4..810bc58 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,7 @@ "forceConsistentCasingInFileNames": true, "skipLibCheck": true, "allowImportingTsExtensions": true, + "useUnknownInCatchVariables": true, "noEmit": true, "baseUrl": ".", "paths": { @@ -22,7 +23,10 @@ "src/config/*" ], "entities/*": [ - "src/entities/*" + "src/interfaces/*" + ], + "utils/*": [ + "src/utils/*" ] } }