Skip to content

Commit

Permalink
feat: list all urls and edit url using hash
Browse files Browse the repository at this point in the history
  • Loading branch information
xditya committed Feb 17, 2024
1 parent 67c56e4 commit a96b88e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
16 changes: 15 additions & 1 deletion database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ async function deleteURL(hash: string) {
return result;
}

export { createShortURL, getURL, deleteURL };
async function getAllURLs() {
const result = await URLStorage.find({}).toArray();
return result;
}

async function editURL(hash: string, url: string) {
hash = hash.toLowerCase();
if (!(await doesHashExist(hash))) {
return false;
}
await URLStorage.updateOne({ hash }, { $set: { url: url } });
return true;
}

export { createShortURL, getURL, deleteURL, getAllURLs, editURL };
65 changes: 64 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ GDSC MBCET
*/

import { Application, Router } from "oak";
import { createShortURL, getURL, deleteURL } from "./database.ts";
import {
createShortURL,
getURL,
deleteURL,
getAllURLs,
editURL,
} from "./database.ts";

import config from "$env";

Expand Down Expand Up @@ -89,6 +95,63 @@ router.post("/api/deleteURL", async (ctx) => {
ctx.response.body = { error: null };
});

router.post("/api/getAll", async (ctx) => {
const { userName, userPass } = await ctx.request.body().value;
if (!userName || !userPass) {
ctx.response.status = 400;
ctx.response.body = { error: "Invalid request" };
return;
}

// authorise
if (
userName !== config.SHORTENER_USERNAME ||
userPass !== config.SHORTENER_PASSWORD
) {
ctx.response.status = 401;
ctx.response.body = { error: "Unauthorized" };
return;
}

// get all hashes
const all = await getAllURLs();
if (!all) ctx.response.body = { error: "None Added." };
ctx.response.body = { error: null, data: all };
});

router.post("/api/editURL", async (ctx) => {
const { hash, userName, userPass, url } = await ctx.request.body().value;
if (!hash || !userName || !userPass || !url) {
ctx.response.status = 400;
ctx.response.body = { error: "Invalid request" };
return;
}

// authorise
if (
userName !== config.SHORTENER_USERNAME ||
userPass !== config.SHORTENER_PASSWORD
) {
ctx.response.status = 401;
ctx.response.body = { error: "Unauthorized" };
return;
}
try {
if (await editURL(hash, url))
return (ctx.response.body = { error: null, message: "Updated" });
else
return (ctx.response.body = {
error: "Not found",
message: "Hash not found",
});
} catch (e) {
ctx.response.status = 400;
ctx.response.body = { error: "Something went wrong.", message: e.message };
return;
}
ctx.response.body = { error: null };
});

const app = new Application();
app.use(async (ctx, next) => {
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
Expand Down

0 comments on commit a96b88e

Please sign in to comment.