Skip to content

Commit

Permalink
feat(api): get and create address for user
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevanzak committed Nov 12, 2023
1 parent 7be0e3f commit 4c14366
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/api/src/root.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { authRouter } from "./router/auth";
import { categoryRouter } from "./router/categories";
import { categoryRouter } from "./router/category";
import { productRouter } from "./router/product";
import { userRouter } from "./router/user";
import { createTRPCRouter } from "./trpc";


export const appRouter = createTRPCRouter({
auth: authRouter,
product: productRouter,
category: categoryRouter,
user: userRouter,
});

// export type definition of API
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions packages/api/src/router/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { addresses, insertAddressSchema } from "@vivat/db/schema/addresses";
import { createTRPCRouter, protectedProcedure } from "../trpc";

export const userRouter = createTRPCRouter({
getAddresses: protectedProcedure
.query(({ ctx }) => {
return ctx.db.query.addresses.findMany({
where: (addresses, { eq }) => eq(addresses.userId, ctx.auth.userId),
});
}),
createAddress: protectedProcedure
.input(insertAddressSchema)
.query(({ input, ctx }) => {
return ctx.db.insert(addresses).values({
...input,
userId: ctx.auth.userId,
});
}),
});
3 changes: 2 additions & 1 deletion packages/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { drizzle } from "drizzle-orm/planetscale-serverless";
import * as products from "./schema/products";
import * as users from "./schema/users";
import * as categories from "./schema/categories";
import * as addresses from "./schema/addresses";

export const schema = { ...users, ...products, ...categories };
export const schema = { ...users, ...products, ...categories, ...addresses };

export { mySqlTable as tableCreator } from "./schema/_table";

Expand Down
28 changes: 28 additions & 0 deletions packages/db/schema/addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { relations, sql } from "drizzle-orm";
import { varchar } from "drizzle-orm/mysql-core";

import { mySqlTable } from "./_table";
import { users } from "./users";
import { createInsertSchema } from "drizzle-zod";

export const addresses = mySqlTable("addresses", {
id: varchar("id", { length: 36 })
.notNull()
.primaryKey()
.default(sql`(UUID())`),
title: varchar("title", { length: 255 }).notNull(),
address: varchar("address", { length: 255 }).notNull(),
zipCode: varchar("zip_code", { length: 255 }).notNull(),
recipient: varchar("recipient", { length: 255 }).notNull(),
phoneNumber: varchar("phone_number", { length: 255 }).notNull(),
userId: varchar("user_id", { length: 255 }).notNull(),
});

export const addressesRelations = relations(addresses, ({ one }) => ({
user: one(users, {
fields: [addresses.userId],
references: [users.id],
}),
}));

export const insertAddressSchema = createInsertSchema(addresses);
2 changes: 2 additions & 0 deletions packages/db/schema/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { primaryKey, timestamp, varchar } from "drizzle-orm/mysql-core";

import { mySqlTable } from "./_table";
import { products } from "./products";
import { addresses } from "./addresses";

export const users = mySqlTable("users", {
id: varchar("id", { length: 256 }).notNull().primaryKey(),
Expand All @@ -17,6 +18,7 @@ export const users = mySqlTable("users", {
export const usersRelations = relations(users, ({ many }) => ({
product: many(products),
usersToProducts: many(usersToProducts),
address: many(addresses),
}));

export const usersToProducts = mySqlTable(
Expand Down

0 comments on commit 4c14366

Please sign in to comment.