diff --git a/src/actions/brandActions.ts b/src/actions/brandActions.ts index b10c83b..65c052c 100644 --- a/src/actions/brandActions.ts +++ b/src/actions/brandActions.ts @@ -7,15 +7,19 @@ export const getData = async () => { const data = await db.select().from(brand).orderBy(asc(brand.name)); return data; }; + export const addBrand = async ( name: string) => { await db.insert(brand).values({ name: name, }); }; + export const deleteBrand = async (id: number) => { + "use server"; await db.delete(brand).where(eq(brand.id, id)); - revalidatePath("/"); + revalidatePath("/Brands"); }; + export const editBrand = async (id: number, name: string) => { await db .update(brand) diff --git a/src/actions/manufacturerActions.ts b/src/actions/manufacturerActions.ts new file mode 100644 index 0000000..53f30dc --- /dev/null +++ b/src/actions/manufacturerActions.ts @@ -0,0 +1,10 @@ +"use server"; +import { eq, not , asc} from "drizzle-orm"; +import { revalidatePath } from "next/cache"; +import { db } from "../db"; +import { manufacturer } from "@/db/schema/Manufacturer"; +export const getData = async () => { + const data = await db.select().from(manufacturer).orderBy(asc(manufacturer.name)); + return data; +}; + diff --git a/src/app/Accounts/page.tsx b/src/app/Accounts/page.tsx index 0033302..bf623dc 100644 --- a/src/app/Accounts/page.tsx +++ b/src/app/Accounts/page.tsx @@ -1,6 +1,6 @@ import { Account } from "@/db/schema/Account"; import { getData } from "../../actions/accountActions"; -import Brands from "../../components/Brand/brands"; +import Brands from "../../components/Brand/BrandsList"; export default async function AccountsPage() { const data = await getData(); diff --git a/src/app/Brands/page.tsx b/src/app/Brands/page.tsx index e68f00e..f0983f4 100644 --- a/src/app/Brands/page.tsx +++ b/src/app/Brands/page.tsx @@ -1,11 +1,11 @@ import { getData } from "../../actions/brandActions"; -import Brands from "../../components/Brand/brands"; +import BrandsList from "../../components/Brand/BrandsList"; export default async function BrandsPage() { const data = await getData(); return (
- +
); } diff --git a/src/components/Brand/brands.tsx b/src/components/Brand/BrandsList.tsx similarity index 93% rename from src/components/Brand/brands.tsx rename to src/components/Brand/BrandsList.tsx index 4d9dd8c..5aea045 100644 --- a/src/components/Brand/brands.tsx +++ b/src/components/Brand/BrandsList.tsx @@ -7,15 +7,18 @@ import { addBrand, deleteBrand, editBrand } from "../../actions/brandActions"; interface Props { brands: brandType[]; } -const Brands: FC = ({ brands }) => { + +const BrandsList: FC = ({ brands }) => { // State to manage the list of brand items const [brandItems, setBrandItems] = useState(brands); + // Function to create a new brand item const createBrand = (name: string) => { const id = (brandItems.at(-1)?.id || 0) + 1; addBrand(name); setBrandItems((prev) => [...prev, { id: id, name: name }]); }; + // Function to change the text of a brand item const changeBrandName = (id: number, name: string) => { setBrandItems((prev) => @@ -25,10 +28,11 @@ const Brands: FC = ({ brands }) => { }; // Function to delete a brand item - const deleteTodoItem = (id: number) => { + const deleteBrandItem = (id: number) => { setBrandItems((prev) => prev.filter((brand) => brand.id !== id)); deleteBrand(id); }; + // Rendering the brand List component return (
@@ -49,4 +53,4 @@ const Brands: FC = ({ brands }) => {
); }; -export default Brands; \ No newline at end of file +export default BrandsList; \ No newline at end of file diff --git a/src/components/Brand/brand.tsx b/src/components/Brand/brand.tsx index 7064a5c..49f0e25 100644 --- a/src/components/Brand/brand.tsx +++ b/src/components/Brand/brand.tsx @@ -40,7 +40,7 @@ const Brand: FC = ({ deleteBrand(brand.id); } }; - // Rendering the Todo component + // Rendering the Brands component return (
{/* Checkbox for marking the todo as done */} diff --git a/src/db/schema/ComponentTypes.ts b/src/db/schema/ComponentTypes.ts new file mode 100644 index 0000000..d892482 --- /dev/null +++ b/src/db/schema/ComponentTypes.ts @@ -0,0 +1,9 @@ +import { pgTable, integer, varchar } from "drizzle-orm/pg-core"; +import { sql } from "drizzle-orm"; +import { timestamps } from "./helpers/columns.helpers"; + +export const ComponentType = pgTable("component_type", { + id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "component_type_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }), + name: varchar({length: 100}).notNull(), + ...timestamps +}) \ No newline at end of file diff --git a/src/db/schema/Manufacturer.ts b/src/db/schema/Manufacturer.ts new file mode 100644 index 0000000..ab27622 --- /dev/null +++ b/src/db/schema/Manufacturer.ts @@ -0,0 +1,9 @@ +import { pgTable, integer, varchar } from "drizzle-orm/pg-core"; +import { sql } from "drizzle-orm"; +import { timestamps } from "./helpers/columns.helpers"; + +export const manufacturer = pgTable("manufacturer", { + id: integer().primaryKey().generatedAlwaysAsIdentity({ name: "manufacturer_id_seq", startWith: 1, increment: 1, minValue: 1, maxValue: 2147483647, cache: 1 }), + name: varchar({length:100}).notNull(), + ...timestamps +}) \ No newline at end of file diff --git a/src/types/accountType.ts b/src/types/accountType.ts new file mode 100644 index 0000000..c0d7c7d --- /dev/null +++ b/src/types/accountType.ts @@ -0,0 +1,12 @@ +export type brandType = { + id: number; + first_name: string; + last_name: string; + username : string; + email: string; + password_hash: string; + updated_at: Date; + created_at: Date; + deleted_at: Date; + + }; \ No newline at end of file diff --git a/src/types/allTypes.ts b/src/types/allTypes.ts new file mode 100644 index 0000000..05559b0 --- /dev/null +++ b/src/types/allTypes.ts @@ -0,0 +1,9 @@ +export type brandType = { + id: number; + name: string; +}; + +export type manufacturerType = { + id: number; + name: string; +}; \ No newline at end of file diff --git a/src/types/manufacturerType.ts b/src/types/manufacturerType.ts new file mode 100644 index 0000000..dac3a46 --- /dev/null +++ b/src/types/manufacturerType.ts @@ -0,0 +1,4 @@ +export type manufacturerType = { + id: number; + name: string; + }; \ No newline at end of file