Skip to content

Commit

Permalink
Fix all the backend organization issues (#269)
Browse files Browse the repository at this point in the history
* Fix all the backend organization issues

* Fix linting
  • Loading branch information
TheRickyZhang authored Mar 4, 2025
1 parent 4fa65f4 commit c7cb02a
Show file tree
Hide file tree
Showing 30 changed files with 602 additions and 515 deletions.
31 changes: 31 additions & 0 deletions drizzle/0001_premium_betty_brant.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ALTER TABLE `user` RENAME COLUMN "password_hash" TO "password";--> statement-breakpoint
CREATE TABLE `email_subscriber` (
`id` text PRIMARY KEY NOT NULL,
`email` text NOT NULL,
`name` text,
`subscribed_at` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `email_subscriber_email_unique` ON `email_subscriber` (`email`);--> statement-breakpoint
ALTER TABLE `user` ADD `email` text NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX `user_email_unique` ON `user` (`email`);--> statement-breakpoint
DROP INDEX IF EXISTS `personal_info_email_unique`;--> statement-breakpoint
DROP INDEX IF EXISTS "blog_tag_name_unique";--> statement-breakpoint
DROP INDEX IF EXISTS "blog_title_unique";--> statement-breakpoint
DROP INDEX IF EXISTS "email_subscriber_email_unique";--> statement-breakpoint
DROP INDEX IF EXISTS "event_name_unique";--> statement-breakpoint
DROP INDEX IF EXISTS "personal_info_phone_unique";--> statement-breakpoint
DROP INDEX IF EXISTS "user_username_unique";--> statement-breakpoint
DROP INDEX IF EXISTS "user_email_unique";--> statement-breakpoint
ALTER TABLE `personal_info` ALTER COLUMN "phone" TO "phone" text;--> statement-breakpoint
CREATE UNIQUE INDEX `blog_tag_name_unique` ON `blog_tag` (`name`);--> statement-breakpoint
CREATE UNIQUE INDEX `blog_title_unique` ON `blog` (`title`);--> statement-breakpoint
CREATE UNIQUE INDEX `event_name_unique` ON `event` (`name`);--> statement-breakpoint
CREATE UNIQUE INDEX `personal_info_phone_unique` ON `personal_info` (`phone`);--> statement-breakpoint
CREATE UNIQUE INDEX `user_username_unique` ON `user` (`username`);--> statement-breakpoint
ALTER TABLE `personal_info` DROP COLUMN `email`;--> statement-breakpoint
ALTER TABLE `event` ADD `slides_url` text;--> statement-breakpoint
ALTER TABLE `blog` DROP COLUMN `last_update_date`;--> statement-breakpoint
ALTER TABLE `blog` DROP COLUMN `tags`;--> statement-breakpoint
ALTER TABLE `sase_info` DROP COLUMN `mentors`;--> statement-breakpoint
ALTER TABLE `sase_info` DROP COLUMN `mentees`;
72 changes: 0 additions & 72 deletions src/client/AuthContext.tsx

This file was deleted.

26 changes: 16 additions & 10 deletions src/client/api/blogs.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
// libapi/blogs.ts
import type { Blog, BlogSearchResponse, CreateBlog, UpdateBlog } from "@shared/schema/blogSchema";
import { blogsApiResponseSchema, blogSearchResponseSchema, blogTitleSchema, singleBlogApiResponseSchema } from "@shared/schema/blogSchema";
import type { Blog, CreateBlog, UpdateBlog } from "@shared/schema/blogSchema";
import { blogSchema, blogSearchResponseSchema, blogTitleSchema, updateBlogSchema } from "@shared/schema/blogSchema";
import { apiFetch } from "@shared/utils";
import { z } from "zod";

// Fetch ALL Blogs
export const fetchBlogs = async (): Promise<Array<Blog>> => {
return apiFetch<Array<Blog>>("/api/blogs/all", { method: "GET" }, blogsApiResponseSchema);
const response = await apiFetch("/api/blogs/all", { method: "GET" }, z.array(blogSchema));
return response.data;
};

// Fetch Blog by ID
export const fetchBlogById = async (blogId: string): Promise<Blog> => {
if (!blogId) {
throw new Error("Blog ID is required");
}
return apiFetch<Blog>(`/api/blogs/${blogId}`, { method: "GET" }, singleBlogApiResponseSchema);
const response = await apiFetch(`/api/blogs/${blogId}`, { method: "GET" }, blogSchema);
return response.data;
};

// Search Blogs by Title
export const searchBlogsByTitle = async (title: string): Promise<BlogSearchResponse> => {
export const searchBlogsByTitle = async (title: string): Promise<Blog> => {
if (!title) {
throw new Error("Title is required");
}
blogTitleSchema.parse({ title });
return apiFetch<BlogSearchResponse>(
const response = await apiFetch(
"/api/blogs/search",
{
method: "GET",
Expand All @@ -31,28 +34,31 @@ export const searchBlogsByTitle = async (title: string): Promise<BlogSearchRespo
},
blogSearchResponseSchema,
);
return response.data;
};

export const createBlog = async (newBlog: CreateBlog): Promise<Blog> => {
return apiFetch<Blog>(
const response = await apiFetch(
"/api/blogs/add",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newBlog),
},
singleBlogApiResponseSchema,
blogSchema,
);
return response.data;
};

export const updateBlog = async (blog: UpdateBlog): Promise<Blog> => {
return apiFetch<Blog>(
const response = await apiFetch(
"/api/blogs/update",
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(blog),
},
singleBlogApiResponseSchema,
updateBlogSchema,
);
return response.data;
};
98 changes: 38 additions & 60 deletions src/client/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,62 @@
import type { ApiResponse } from "@shared/schema/responseSchema";
import type { DeleteUser, InsertUser, SelectUser, UpdateUser } from "@shared/schema/userSchema";
import { deleteUserSchema, insertUserSchema, selectUserSchema } from "@shared/schema/userSchema";
import { apiFetch } from "@shared/utils";

// Fetch ALL Users
export const fetchUsers = async (): Promise<Array<SelectUser>> => {
const response = await fetch("/api/users");
const json = (await response.json()) as ApiResponse<Array<SelectUser>>;

if (!response.ok) {
throw new Error(json.error?.message || "Unknown error occured");
}

return selectUserSchema.array().parse(json);
const response = await apiFetch("/api/users", { method: "GET" }, selectUserSchema.array());
return response.data;
};

export const fetchUser = async (id: string): Promise<SelectUser> => {
const response = await fetch(`/api/users/${id}`, {
method: "GET",
credentials: "include", // Include session credentials (cookies)
headers: {
"Content-Type": "application/json",
const response = await apiFetch(
`/api/users/${id}`,
{
method: "GET",
credentials: "include",
headers: { "Content-Type": "application/json" },
},
});

const json = (await response.json()) as ApiResponse<SelectUser>;

if (!response.ok) {
throw new Error(json.error?.message || "Unknown error occurred");
}

return selectUserSchema.parse(json.data);
selectUserSchema,
);
return response.data;
};

export const createUser = async (newUser: InsertUser): Promise<SelectUser> => {
insertUserSchema.parse(newUser);
const response = await fetch("/api/users", {
method: "POST",
body: JSON.stringify(newUser),
headers: {
"Content-Type": "application/json",
const response = await apiFetch(
"/api/users",
{
method: "POST",
body: JSON.stringify(newUser),
headers: { "Content-Type": "application/json" },
},
});
const json = (await response.json()) as ApiResponse<SelectUser>;

if (!response.ok) {
throw new Error(json.error?.message || "Unknown error occured");
}

return selectUserSchema.parse(json);
selectUserSchema,
);
return response.data;
};

export const updateUser = async (updatedUser: UpdateUser): Promise<SelectUser> => {
insertUserSchema.parse(updatedUser);
const response = await fetch(`/api/users/${updatedUser.id}`, {
method: "PATCH",
body: JSON.stringify(updatedUser),
headers: {
"Content-Type": "application/json",
const response = await apiFetch(
`/api/users/${updatedUser.id}`,
{
method: "PATCH",
body: JSON.stringify(updatedUser),
headers: { "Content-Type": "application/json" },
},
});
const json = (await response.json()) as ApiResponse<SelectUser>;

if (!response.ok) {
throw new Error(json.error?.message || "Unknown error occured");
}

return selectUserSchema.parse(json);
selectUserSchema,
);
return response.data;
};

export const deleteUser = async (userId: number): Promise<DeleteUser> => {
const response = await fetch(`/api/users/${userId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
const response = await apiFetch(
`/api/users/${userId}`,
{
method: "DELETE",
headers: { "Content-Type": "application/json" },
},
});
const json = (await response.json()) as ApiResponse<DeleteUser>;

if (!response.ok) {
throw new Error(json.error?.message || "Unknown error occured");
}
return deleteUserSchema.parse(json);
deleteUserSchema,
);
return response.data;
};
2 changes: 1 addition & 1 deletion src/client/components/navigation/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAuth } from "@/client/AuthContext";
import { useAuth } from "@/client/hooks/AuthContext";
import { cn } from "@/shared/utils";
import { DesktopMenu } from "@navigation/DesktopMenu";
import { Logo } from "@navigation/Logo";
Expand Down
Loading

0 comments on commit c7cb02a

Please sign in to comment.