diff --git a/backend/src/app/controllers/url/putUrlHandler.ts b/backend/src/app/controllers/url/putUrlHandler.ts index 5c9f26d..4d108b7 100644 --- a/backend/src/app/controllers/url/putUrlHandler.ts +++ b/backend/src/app/controllers/url/putUrlHandler.ts @@ -1,4 +1,5 @@ import { Request, Response } from "express"; +import shortid from "shortid"; import URLs from "../../db/url.schema"; const isUrlValid = (url: string) => { @@ -8,11 +9,33 @@ const isUrlValid = (url: string) => { export default async (req: Request, res: Response) => { try { if (!isUrlValid(req.body.url)) { - return res.status(422).send("INVALID URL"); + return res.status(422).send({ error: "URL Provided is Invalid" }); + } + + // Validate that there isn't a conflict + if (req.body.slug) { + const conflict = await URLs.findById(req.body.slug); + if (!!conflict) { + return res + .status(409) + .send({ error: `Slug '${req.body.slug}' is already registered.` }); + } + } else { + // Only return existing matches if the slug isn't custom + // Custom ones can continue to conflict :) + const existing = await URLs.findOne({ + url: req.body.url, + isSlugCustom: false, + }); + if (existing) { + return res.status(200).send(existing._id); + } } const url = new URLs(); url.url = req.body.url; + url.isSlugCustom = !!req.body.slug; + url._id = req.body.slug || shortid.generate(); await url.save(); return res.status(200).send(url._id); diff --git a/backend/src/app/db/url.schema.ts b/backend/src/app/db/url.schema.ts index 6c45693..ae41247 100644 --- a/backend/src/app/db/url.schema.ts +++ b/backend/src/app/db/url.schema.ts @@ -7,11 +7,16 @@ const UrlSchema: Schema = new Schema({ default: shortid.generate, }, url: String, + isSlugCustom: { + type: Boolean, + default: false, + }, }); export type UrlModel = Document & { _id: string; url: string; + isSlugCustom: boolean; }; export default mongoose.model("urls", UrlSchema, "urls"); diff --git a/frontend/.gitignore b/frontend/.gitignore index 4d29575..3711ab2 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -21,3 +21,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* +**/.DS_Store \ No newline at end of file diff --git a/frontend/public/app-icon.png b/frontend/public/app-icon.png index c5f6e68..643476c 100644 Binary files a/frontend/public/app-icon.png and b/frontend/public/app-icon.png differ diff --git a/frontend/public/index.html b/frontend/public/index.html index 613a583..d826726 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -5,13 +5,10 @@ - + - dv2ls - Utils for Devs! + dv2ls: 🛠 for 👨‍💻 diff --git a/frontend/public/robots.txt b/frontend/public/robots.txt index d849ff9..94d5ab5 100644 --- a/frontend/public/robots.txt +++ b/frontend/public/robots.txt @@ -1,3 +1,5 @@ # https://www.robotstxt.org/robotstxt.html User-agent: * Disallow: /paste?id=* +Disallow: /code?id=* +Disallow: /f \ No newline at end of file diff --git a/frontend/src/axios/putUrlHandler.js b/frontend/src/axios/putUrlHandler.js index 275287e..9c9e6b7 100644 --- a/frontend/src/axios/putUrlHandler.js +++ b/frontend/src/axios/putUrlHandler.js @@ -1,3 +1,3 @@ import axios from 'axios'; -export default (url) => axios.put('/api/url', { url }).then((resp) => resp.data); +export default (url, slug) => axios.put('/api/url', { url, slug }).then((resp) => resp.data); diff --git a/frontend/src/components/UrlShortener.jsx b/frontend/src/components/UrlShortener.jsx index 77bd20e..fce1849 100644 --- a/frontend/src/components/UrlShortener.jsx +++ b/frontend/src/components/UrlShortener.jsx @@ -8,6 +8,7 @@ import putUrlHandler from '../axios/putUrlHandler'; export default () => { const [state, setState] = useState({ url: null, + slug: null, loading: false, result: null, error: false, @@ -17,7 +18,9 @@ export default () => { evt.preventDefault(); if (state.url) { setState({ ...state, loading: true }); - putUrlHandler(state.url).then((data) => { + putUrlHandler( + state.url, state.slug, + ).then((data) => { setState({ ...state, result: `${window.location.origin}/${data}`, @@ -25,8 +28,13 @@ export default () => { error: false, }); }).catch((ex) => { - if (ex.response && ex.response.data === 'INVALID URL') { - setState({ ...state, error: true }); + if (ex.response?.data?.error) { + setState({ + ...state, + result: null, + loading: false, + error: ex.response.data.error, + }); } }); } @@ -35,8 +43,7 @@ export default () => { return ( -

URL Shortener

- +

Link Shortener

@@ -47,27 +54,37 @@ export default () => { onChange={(evt) => setState({ ...state, url: evt.target.value })} required /> + + setState({ ...state, slug: evt.target.value })} + /> + - {state.error && ( - - Please enter a valid URL - - )}
+ {state.error && ( +
+ {state.error} +
+ )} + {state.result && ( -
- - {state.result} - -
+
+ + {state.result} + +
)}
diff --git a/frontend/src/components/modules/NavBar.jsx b/frontend/src/components/modules/NavBar.jsx index e8c927c..078fe34 100644 --- a/frontend/src/components/modules/NavBar.jsx +++ b/frontend/src/components/modules/NavBar.jsx @@ -5,7 +5,7 @@ import NavLinkItem from './NavLinkItem'; export default () => ( - {window.location.origin.substr(window.location.origin.indexOf('//') + 2)} + dv2ls: Dev Tools