-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
package upgrades, refactoring, and fixes
- Loading branch information
1 parent
c8c08ab
commit a1800c5
Showing
16 changed files
with
195 additions
and
263 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
// @ts-check | ||
import eslint from "@eslint/js"; | ||
import tseslint from "typescript-eslint"; | ||
|
||
export default [ | ||
{ | ||
ignores: ["node_modules/"] | ||
}, | ||
...tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended, { | ||
languageOptions: { | ||
ecmaVersion: "latest", | ||
sourceType: "module" | ||
}, | ||
rules: { | ||
"@typescript-eslint/ban-ts-comment": "off", | ||
"@typescript-eslint/no-var-requires": "off" | ||
} | ||
}) | ||
]; |
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
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,135 +1,100 @@ | ||
import { Elysia } from "elysia"; | ||
|
||
import { errorResponse, projection } from "@helpers"; | ||
import { BadRequestError, buildBagResponse, NotFoundError, parseBody, projection } from "@helpers"; | ||
import { Bag, Disc } from "@models"; | ||
import { assertIsRequestAuthorized } from "@services"; | ||
|
||
export const initBagRoutes = (app: Elysia) => { | ||
/* Get all bags (optionally filter by user id) */ | ||
app.get("/bag", async ({ set, request, query }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { user_id } = query as Record<string, string>; | ||
const bags = await (user_id ? Bag.find({ user_id }, projection) : Bag.find({}, projection)); | ||
if (!bags || bags.length === 0) return []; | ||
return bags; | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.get("/bag", async ({ request, query }) => { | ||
assertIsRequestAuthorized(request); | ||
const { user_id } = query as Record<string, string>; | ||
const bags = await (user_id ? Bag.find({ user_id }, projection) : Bag.find({}, projection)); | ||
if (!bags || bags.length === 0) return []; | ||
return bags; | ||
}); | ||
|
||
/* Get bag by id */ | ||
app.get("/bag/:id", async ({ set, request, params }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { id } = params as Record<string, string>; | ||
const bag = await Bag.findOne({ id }, projection); | ||
if (!bag) throw { code: 404, data: "Bag not found." }; | ||
return bag; | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.get("/bag/:id", async ({ request, params }) => { | ||
assertIsRequestAuthorized(request); | ||
const { id } = params as Record<string, string>; | ||
const bag = await Bag.findOne({ id }, projection); | ||
if (!bag) throw new NotFoundError("Bag not found"); | ||
return bag; | ||
}); | ||
|
||
/* Create new bag (bearer auth secured) */ | ||
app.post("/bag/create", async ({ set, body, request }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { user_id, name } = JSON.parse(body as string) as { user_id: string; name: string }; | ||
if (!user_id) throw { code: 400, data: "Required body field missing: user_id" }; | ||
if (!name) throw { code: 400, data: "Required body field missing: name" }; | ||
|
||
if (await Bag.findOne({ user_id, name })) | ||
throw { code: 400, data: "You already have a bag with that name." }; | ||
|
||
if (name.length < 1) throw { code: 400, data: "Bag name must be at least 1 character." }; | ||
if (name.length > 32) throw { code: 400, data: "Bag name must be no more than 32 characters." }; | ||
|
||
return Bag.create({ user_id, name }); | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.post("/bag/create", async ({ request, body }) => { | ||
assertIsRequestAuthorized(request); | ||
const { user_id, name } = parseBody(body) as { | ||
user_id: string; | ||
name: string; | ||
}; | ||
if (!user_id) throw new BadRequestError("Required body field missing: user_id"); | ||
if (!name) throw new BadRequestError("Required body field missing: name"); | ||
if (await Bag.findOne({ user_id, name })) throw new BadRequestError("You already have a bag with that name"); | ||
if (name.length < 1) throw new BadRequestError("Bag name must be at least 1 character"); | ||
if (name.length > 32) throw new BadRequestError("Bag name must be no more than 32 characters"); | ||
const bag = await Bag.create({ user_id, name }); | ||
return buildBagResponse(bag); | ||
}); | ||
|
||
/* Add disc to bag (bearer auth secured) */ | ||
app.put("/bag/add-disc", async ({ set, body, request }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { id, disc_id } = JSON.parse(body as string) as { id: string; disc_id: string }; | ||
if (!id) throw { code: 400, data: "Required body field missing: id" }; | ||
if (!disc_id) throw { code: 400, data: "Required body field missing: disc_id" }; | ||
|
||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw { code: 404, data: "Bag not found." }; | ||
|
||
const disc = await Disc.findOne({ id: disc_id }); | ||
if (!disc) throw { code: 404, data: "Disc not found." }; | ||
if (bag.discs.includes(disc_id)) throw { code: 400, data: "Bag already contains this disc." }; | ||
|
||
bag.discs.push(disc_id); | ||
return Bag.updateOne({ id }, bag); | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.put("/bag/add-disc", async ({ request, body }) => { | ||
assertIsRequestAuthorized(request); | ||
const { id, disc_id } = parseBody(body) as { id: string; disc_id: string }; | ||
if (!id) throw new BadRequestError("Required body field missing: id"); | ||
if (!disc_id) throw new BadRequestError("Required body field missing: disc_id"); | ||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw new NotFoundError("Bag not found"); | ||
const disc = await Disc.findOne({ id: disc_id }); | ||
if (!disc) throw new NotFoundError("Disc not found"); | ||
if (bag.discs.includes(disc_id)) throw new BadRequestError("Bag already contains this disc"); | ||
bag.discs.push(disc_id); | ||
await Bag.updateOne({ id }, bag); | ||
return buildBagResponse(bag); | ||
}); | ||
|
||
/* Remove disc from bag (bearer auth secured) */ | ||
app.put("/bag/remove-disc", async ({ set, body, request }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { id, disc_id } = JSON.parse(body as string) as { id: string; disc_id: string }; | ||
if (!id) throw { code: 400, data: "Required body field missing: id" }; | ||
if (!disc_id) throw { code: 400, data: "Required body field missing: disc_id" }; | ||
|
||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw { code: 404, data: "Bag not found." }; | ||
|
||
const disc = await Disc.findOne({ id: disc_id }); | ||
if (!disc) throw { code: 404, data: "Disc not found." }; | ||
if (!bag.discs.includes(disc_id)) throw { code: 400, data: "Bag does not contain this disc." }; | ||
|
||
bag.discs = bag.discs.filter(discId => discId !== disc_id); | ||
return Bag.updateOne({ id }, bag); | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.put("/bag/remove-disc", async ({ request, body }) => { | ||
assertIsRequestAuthorized(request); | ||
const { id, disc_id } = parseBody(body) as { id: string; disc_id: string }; | ||
if (!id) throw new BadRequestError("Required body field missing: id"); | ||
if (!disc_id) throw new BadRequestError("Required body field missing: disc_id"); | ||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw new NotFoundError("Bag not found"); | ||
const disc = await Disc.findOne({ id: disc_id }); | ||
if (!disc) throw new NotFoundError("Disc not found"); | ||
if (!bag.discs.includes(disc_id)) throw new BadRequestError("Bag does not contain this disc"); | ||
bag.discs = bag.discs.filter(discId => discId !== disc_id); | ||
await Bag.updateOne({ id }, bag); | ||
return buildBagResponse(bag); | ||
}); | ||
|
||
/* Update name of bag (bearer auth secured) */ | ||
app.put("/bag/update-name", async ({ set, body, request }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { id, name } = JSON.parse(body as string) as { id: string; name: string }; | ||
if (!id) throw { code: 400, data: "Required body field missing: id" }; | ||
if (!name) throw { code: 400, data: "Required body field missing: name" }; | ||
|
||
if (name.length < 1) throw { code: 400, data: "Bag name must be at least 1 character." }; | ||
if (name.length > 32) throw { code: 400, data: "Bag name must be no more than 32 characters." }; | ||
|
||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw { code: 404, data: "Bag not found." }; | ||
|
||
if (await Bag.findOne({ user_id: bag.user_id, name })) | ||
throw { code: 400, data: "You already have a bag with that name." }; | ||
|
||
bag.name = name; | ||
return Bag.updateOne({ id }, bag); | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.put("/bag/update-name", async ({ request, body }) => { | ||
assertIsRequestAuthorized(request); | ||
const { id, name } = parseBody(body) as { id: string; name: string }; | ||
if (!id) throw new BadRequestError("Required body field missing: id"); | ||
if (!name) throw new BadRequestError("Required body field missing: name"); | ||
if (name.length < 1) throw new BadRequestError("Bag name must be at least 1 character"); | ||
if (name.length > 32) throw new BadRequestError("Bag name must be no more than 32 characters"); | ||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw new NotFoundError("Bag not found"); | ||
if (await Bag.findOne({ user_id: bag.user_id, name })) | ||
throw new BadRequestError("You already have a bag with that name"); | ||
bag.name = name; | ||
await Bag.updateOne({ id }, bag); | ||
return buildBagResponse(bag); | ||
}); | ||
|
||
/* Delete bag (bearer auth secured) */ | ||
app.delete("/bag/delete/:id", async ({ set, params, request }) => { | ||
try { | ||
assertIsRequestAuthorized(request); | ||
const { id } = params as Record<string, string>; | ||
if (!id) throw { code: 400, data: "Required path param missing: id" }; | ||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw { code: 404, data: "Bag not found." }; | ||
return Bag.deleteOne({ id }); | ||
} catch (error) { | ||
return errorResponse(set, error); | ||
} | ||
app.delete("/bag/delete/:id", async ({ request, params }) => { | ||
assertIsRequestAuthorized(request); | ||
const { id } = params as Record<string, string>; | ||
if (!id) throw new BadRequestError("Required path param missing: id"); | ||
const bag = await Bag.findOne({ id }); | ||
if (!bag) throw new NotFoundError("Bag not found"); | ||
return await Bag.deleteOne({ id }); | ||
}); | ||
}; |
Oops, something went wrong.