Skip to content

Commit

Permalink
FIX:Minor bugs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BIIJESH committed Dec 13, 2024
1 parent fd05fdf commit 533859b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 89 deletions.
72 changes: 35 additions & 37 deletions src/controller/user/userDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,40 @@ export class UserDelete extends OpenAPIRoute {
};

async handle(c: Context<AppBindings>) {
basicAuth({
username: c.env.USERNAME,
password: c.env.PASSWORD,
});
try {
console.log("test1");
const data = await this.getValidatedData<typeof this.schema>();
console.log("test3");
const userSlug = data.params.userSlug;
console.log(userSlug);
const userRes = await c.get("db").query.user.findFirst({
where: (u, { eq }) => eq(u.id, userSlug),
});
if (!userRes) {
throw new Error("User Doesn't exist");
}
if (userRes.role != "audience") {
throw new Error("you can only delete audience");
}
const res = await c
.get("db")
.delete(userTable)
.where(eq(user.id, userSlug));
if (!res.success) {
throw new Error("Could not delete from db");
}
return { user: userRes };
} catch (error) {
console.log("Error: ", error);
return c.json(
{
success: false,
error: String(error),
},
500,
);
}
// try {
// //TODO:https://github.com/PoskOfficial/Miti/blob/v2/apps/api/src/auth/oslo-auth.ts read this thingy
// basicAuth({
// username: c.env.USERNAME,
// password: c.env.PASSWORD,
// });
// const data = await this.getValidatedData<typeof this.schema>();
// const userSlug = data.params.userSlug;
// const userRes = await c.get("db").query.user.findFirst({
// where: (u, { eq }) => eq(u.id, userSlug),
// });
// if (!userRes) {
// throw new Error("User Doesn't exist");
// }
// if (userRes.role != "audience") {
// throw new Error("you can only delete audience");
// }
// const res = await c
// .get("db")
// .delete(userTable)
// .where(eq(user.id, userSlug));
// if (!res.success) {
// throw new Error("Could not delete from db");
// }
// return { user: userRes };
// } catch (error) {
// console.log("Error: ", error);
// return c.json(
// {
// success: false,
// error: String(error),
// },
// 500,
// );
// }
}
}
76 changes: 27 additions & 49 deletions src/controller/user/userList.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,47 @@
import { Bool, Num, OpenAPIRoute } from "chanfana";
import { Bool, OpenAPIRoute } from "chanfana";
import { z } from "zod";
import { Task } from "../../types";
import { AppBindings } from "bindings";
import { Context } from "hono";
import { selectUserSchema, userTable } from "db/user";
import { basicAuth } from "hono/basic-auth";

export class UserList extends OpenAPIRoute {
schema = {
tags: ["Tasks"],
summary: "List Tasks",
request: {
query: z.object({
page: Num({
description: "Page number",
default: 0,
}),
isCompleted: Bool({
description: "Filter by completed flag",
required: false,
}),
}),
},
tags: ["User"],
summary: "Fetch all Users",
request: {},
responses: {
"200": {
description: "Returns a list of tasks",
description: "Returns a list of all Users",
content: {
"application/json": {
schema: z.object({
series: z.object({
success: Bool(),
result: z.object({
tasks: Task.array(),
}),
}),
success: Bool(),
result: z.object({ selectUserSchema }),
}),
},
},
},
},
};

async handle(c) {
// Get validated data
const data = await this.getValidatedData<typeof this.schema>();

// Retrieve the validated parameters
const { page, isCompleted } = data.query;

// Implement your own object list here

return {
success: true,
tasks: [
{
name: "Clean my room",
slug: "clean-room",
description: null,
completed: false,
due_date: "2025-01-05",
},
async handle(c: Context<AppBindings>) {
basicAuth({
username: c.env.USERNAME,
password: c.env.PASSWORD,
});
try {
const res = await c.get("db").select().from(userTable);
return { res: res };
} catch (error) {
console.log("Error:", error);
return c.json(
{
name: "Build something awesome with Cloudflare Workers",
slug: "cloudflare-workers",
description: "Lorem Ipsum",
completed: true,
due_date: "2022-12-24",
success: false,
error: String(error),
},
],
};
500,
);
}
}
}
1 change: 0 additions & 1 deletion src/controller/user/userRead.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Bool, OpenAPIRoute, Str } from "chanfana";
import { z } from "zod";
import { Task } from "../../types";

export class UserRead extends OpenAPIRoute {
schema = {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { initalizeDB } from "./db/db";
import { admin, home } from "./routes/routes";
import { fromHono } from "chanfana";
import { UserCreate } from "controller/user/userCreate";
import { UserDelete } from "controller/user/userDelete";
import { UserList } from "controller/user/userList";
const app = new Hono<AppBindings>();
app
.use(logger())
Expand Down Expand Up @@ -39,7 +39,7 @@ const openapi = fromHono(app, {

// openapi.get("/user", UserList);
openapi.post("/user", UserCreate);
openapi.delete("/user/:userSlug", UserDelete);
openapi.get("/user", UserList);

// openapi.get("/user/:taskSlug", UserRead);
// openapi.delete("/user/:taskSlug", UserDelete);
Expand Down

0 comments on commit 533859b

Please sign in to comment.