From 0365f6c8b0fb696f331ed7654ca4edcb0a096d5b Mon Sep 17 00:00:00 2001 From: arnabchatterjee Date: Mon, 22 Apr 2024 21:22:09 +0530 Subject: [PATCH] feat -- added palette saving --- .../db/models/save_palettes.model.ts | 37 +++++++++----- .../__shared/services/PaletteHandler.ts | 39 ++++++++++++++- .../functions/saved_palettes/index.ts | 50 +++++++++++++++++++ 3 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 packages/supabase-edge-functions/supabase/functions/saved_palettes/index.ts diff --git a/packages/supabase-edge-functions/db/models/save_palettes.model.ts b/packages/supabase-edge-functions/db/models/save_palettes.model.ts index b15e716..bf3f372 100644 --- a/packages/supabase-edge-functions/db/models/save_palettes.model.ts +++ b/packages/supabase-edge-functions/db/models/save_palettes.model.ts @@ -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", - }), -}); \ No newline at end of file +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] }), + }; + } +); diff --git a/packages/supabase-edge-functions/supabase/functions/__shared/services/PaletteHandler.ts b/packages/supabase-edge-functions/supabase/functions/__shared/services/PaletteHandler.ts index 40cc527..350c2ef 100755 --- a/packages/supabase-edge-functions/supabase/functions/__shared/services/PaletteHandler.ts +++ b/packages/supabase-edge-functions/supabase/functions/__shared/services/PaletteHandler.ts @@ -1,9 +1,12 @@ import HTTPResponse from "../constants/HTTPResponse.ts"; import { supabaseClient } from "../supabaseClient.ts"; import { Database } from "../types/supabase.ts"; -type PaletteTable = Database["public"]["Tables"]["palette"]; import DefaultColors from "../constants/DefaultColors.ts"; import ProjectHandler from "./ProjectHandler.ts"; + +type PaletteTable = Database["public"]["Tables"]["palette"]; +type SavedPalettes = Database["public"]["Tables"]["saved-palettes"]; + export class PaletteHandler { private async create(projectId: number) { const { data, status, error } = await supabaseClient @@ -59,4 +62,36 @@ export class PaletteHandler { if (error) return HTTPResponse("ERROR", status, data); return HTTPResponse("SUCCESS", status, data); } -} + public async savePalette(palette: string[], user_id: string) { + const paletteToString = palette.join("-"); + const { status, error } = await supabaseClient + .from("saved-palettes") + .upsert( + { palette: paletteToString, user_id: user_id }, + { + onConflict: "palette,user_id", + } + ); + if (error || ![201,200].includes(status) ) return HTTPResponse("ERROR", status); + return HTTPResponse("SUCCESS", status); + } + public async unSavePalette(palette: string[], user_id: string) { + const paletteToString = palette.join("-"); + const { error, status } = await supabaseClient + .from("saved-palettes") + .delete() + .filter("palette", "eq", paletteToString) + .filter("user_id", "eq", user_id); + if (error || status !== 204) return HTTPResponse("ERROR", status); + return HTTPResponse("SUCCESS", status); + } + + public async getSavedPalettes(user_id: string) { + const {data,error,status} = await supabaseClient + .from("saved-palettes") + .select("palette") + .filter("user_id", "eq", user_id); + if(error || status !== 200) return HTTPResponse("ERROR",status) + return HTTPResponse("SUCCESS",status,data) + } +} \ No newline at end of file diff --git a/packages/supabase-edge-functions/supabase/functions/saved_palettes/index.ts b/packages/supabase-edge-functions/supabase/functions/saved_palettes/index.ts new file mode 100644 index 0000000..06b109b --- /dev/null +++ b/packages/supabase-edge-functions/supabase/functions/saved_palettes/index.ts @@ -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);