-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e61617
commit 0365f6c
Showing
3 changed files
with
112 additions
and
14 deletions.
There are no files selected for viewing
37 changes: 25 additions & 12 deletions
37
packages/supabase-edge-functions/db/models/save_palettes.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
import { pgTable, timestamp, uuid, serial, varchar } from "drizzle-orm/pg-core"; | ||
import { | ||
pgTable, | ||
primaryKey, | ||
timestamp, | ||
uuid, | ||
varchar, | ||
} from "drizzle-orm/pg-core"; | ||
import { authModel } from "./auth.model"; | ||
|
||
export const paletteModel = pgTable("saved-palettes", { | ||
id: serial("id").primaryKey(), | ||
createdAt: timestamp("created_at").defaultNow(), | ||
palette: varchar("palette").notNull().array(), | ||
userId: uuid("user_id") | ||
.notNull() | ||
.references(() => authModel.id, { | ||
onDelete: "no action", | ||
onUpdate: "no action", | ||
}), | ||
}); | ||
export const paletteModel = pgTable( | ||
"saved-palettes", | ||
{ | ||
createdAt: timestamp("created_at").defaultNow(), | ||
palette: varchar("palette").notNull(), | ||
userId: uuid("user_id") | ||
.notNull() | ||
.references(() => authModel.id, { | ||
onDelete: "no action", | ||
onUpdate: "no action", | ||
}), | ||
}, | ||
(table) => { | ||
return { | ||
pk: primaryKey({ name: "id", columns: [table.palette, table.userId] }), | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/supabase-edge-functions/supabase/functions/saved_palettes/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Database } from "../__shared/types/supabase.ts"; | ||
import verify from "../__shared/middlewares/verify.ts"; | ||
// @deno-types="npm:@types/express" | ||
import express from "npm:express"; | ||
import cors from "npm:cors-express"; | ||
import { PaletteHandler } from "../__shared/services/PaletteHandler.ts"; | ||
import HTTPResponse from "../__shared/constants/HTTPResponse.ts"; | ||
|
||
const app = express(); | ||
app.use(cors()); | ||
app.use(express.json()); | ||
const palette = new PaletteHandler(); | ||
const ROUTE = "/saved_palettes"; // name of the function | ||
const options = ["save", "unsave"]; | ||
|
||
app.get(`${ROUTE}`, verify, async (req, res) => { | ||
const user_id = req.currentUserId as string; | ||
const { data, status, code } = await palette.getSavedPalettes(user_id); | ||
res.status(code).json({ data, status }); | ||
}); | ||
|
||
app.put(`${ROUTE}`, verify, async (req, res) => { | ||
const query = req.query; | ||
const operation = query.q; | ||
const user_id = req.currentUserId as string; | ||
// @ts-ignore | ||
if (!options.includes(operation)) { | ||
return res | ||
.status(400) | ||
.json({ status: "ERROR", message: "q should be save or unsave" }); | ||
} | ||
|
||
const { savedPalette }: { savedPalette: string[] } = req.body; | ||
if (savedPalette instanceof Array === false || savedPalette.length === 0) { | ||
return res | ||
.status(400) | ||
.json({ | ||
status: "ERROR", | ||
message: | ||
"savedPalette should be an array in provided in the request body", | ||
}); | ||
} | ||
const action = | ||
operation === "save" | ||
? palette.savePalette(savedPalette, user_id) | ||
: palette.unSavePalette(savedPalette, user_id); | ||
const { code, status } = await action; | ||
return res.status(code).json({ status, message: `palette ${operation}d` }); | ||
}); | ||
app.listen(8000); |