Skip to content

Commit

Permalink
feat -- added palette saving
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabChatterjee20k committed Apr 22, 2024
1 parent 8e61617 commit 0365f6c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 14 deletions.
37 changes: 25 additions & 12 deletions packages/supabase-edge-functions/db/models/save_palettes.model.ts
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] }),
};
}
);
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
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);

0 comments on commit 0365f6c

Please sign in to comment.