Skip to content

Commit

Permalink
http route update
Browse files Browse the repository at this point in the history
  • Loading branch information
pinocchio-life-like committed Jul 27, 2024
1 parent 3c81c9b commit f56ebc3
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 21 deletions.
1 change: 1 addition & 0 deletions server/src/controllers/item/addItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const addItem = async (c) => {
packId,
type,
ownerId,
c.ctx.executionCtx,
);
return c.json({ result }, 200);
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions server/src/controllers/item/addItemGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const addItemGlobal = async (c: Context) => {
unit,
type,
ownerId,
c.ctx.executionCtx,
);
return c.json({ item }, 200);
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion server/src/controllers/item/deleteGlobalItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { z } from 'zod';
export const deleteGlobalItem = async (c) => {
try {
const { itemId } = await c.req.json();
const itemDeleted = await deleteGlobalItemService(itemId);
const itemDeleted = await deleteGlobalItemService(
itemId,
c.ctx.executionCtx,
);
return c.json({ itemDeleted }, 200);
} catch (error) {
return c.json(
Expand Down
6 changes: 5 additions & 1 deletion server/src/controllers/item/deleteItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { protectedProcedure } from '../../trpc';
export const deleteItem = async (c) => {
try {
const { itemId, packId } = await c.req.parseBody();
const itemDeleted = await deleteItemService(itemId, packId);
const itemDeleted = await deleteItemService(
itemId,
c.ctx.executionCtx,
packId,
);
return c.json({ itemDeleted }, 200);
} catch (error) {
return c.json({ error: `Failed to delete item: ${error.message}` }, 500);
Expand Down
1 change: 1 addition & 0 deletions server/src/controllers/item/editGlobalItemAsDuplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const editGlobalItemAsDuplicate = async (c) => {
quantity,
unit,
type,
c.ctx.executionCtx,
);
return c.json({ item }, 200);
} catch (error) {
Expand Down
10 changes: 9 additions & 1 deletion server/src/controllers/item/editItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ export const editItem = async (c) => {
try {
const { id, name, weight, unit, quantity, type } = await c.req.json();

const item = await editItemService(id, name, weight, unit, quantity, type);
const item = await editItemService(
c.ctx.executionCtx,
id,
name,
weight,
unit,
quantity,
type,
);
return c.json({ item }, 200);
} catch (error) {
return c.json({ error: `Failed to edit item: ${error.message}` }, 500);
Expand Down
18 changes: 18 additions & 0 deletions server/src/controllers/item/getSimilarItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import { getSimilarItemsService } from '../../services/item/getSimilarItemsServi
import { protectedProcedure } from '../../trpc';
import * as validator from '@packrat/validations';

export const getSimilarItems = async (c) => {
try {
const { id, limit, visibility } = await c.req.json();

if (limit < 1) {
throw new Error('limit must be greater than 0');
}

const items = await getSimilarItemsService(id, limit, visibility);
return c.json({ items }, 200);
} catch (error) {
return c.json(
{ error: `Failed to get similar items: ${error.message}` },
500,
);
}
};

export function getSimilarItemsRoute() {
return protectedProcedure
.input(validator.getSimilarItems)
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/item/importItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const importItems = async (c) => {
packId,
item.Category,
ownerId,
c.executionCtx,
c.ctx.executionCtx,
);
}
resolve('result');
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/item/importItemsGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const importItemsGlobal = async (c: Context) => {
item.Unit,
item.Category,
ownerId,
c.executionCtx,
c.ctx.executionCtx,
);
}
resolve('items');
Expand Down
7 changes: 6 additions & 1 deletion server/src/controllers/pack/addPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { protectedProcedure } from '../../trpc';
export const addPack = async (c) => {
try {
const { name, owner_id, is_public } = await c.req.json();
const pack = await addPackService(name, owner_id, is_public);
const pack = await addPackService(
name,
owner_id,
is_public,
c.ctx.executionCtx,
);
return c.json({ pack }, 200);
} catch (error) {
return c.json({ error: `Failed to add pack: ${error.message}` }, 500);
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/pack/deletePack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { protectedProcedure } from '../../trpc';
export const deletePack = async (c) => {
try {
const { packId } = await c.req.json();
await deletePackService(packId);
await deletePackService(packId, c.ctx.executionCtx);
return c.json({ msg: 'pack was deleted successfully' }, 200);
} catch (error) {
return c.json({ error: `Failed to delete pack: ${error.message}` }, 500);
Expand Down
7 changes: 6 additions & 1 deletion server/src/controllers/pack/duplicatePublicPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { protectedProcedure } from '../../trpc';
export const duplicatePublicPack = async (c) => {
try {
const { packId, ownerId, items } = await c.req.json();
const result = await duplicatePublicPackService(packId, ownerId, items);
const result = await duplicatePublicPackService(
packId,
ownerId,
items,
c.ctx.executionCtx,
);
return c.json({ result }, 200);
} catch (error) {
return c.json(
Expand Down
2 changes: 1 addition & 1 deletion server/src/controllers/pack/editPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as validator from '@packrat/validations';
export const editPack = async (c) => {
try {
const packData = await c.req.json();
const pack = await editPackService(packData);
const pack = await editPackService(packData, c.ctx.executionCtx);
return c.json({ pack }, 200);
} catch (error) {
return c.json({ error: `Failed to edit pack: ${error.message}` }, 500);
Expand Down
32 changes: 26 additions & 6 deletions server/src/controllers/pack/getSimilarPacks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import { protectedProcedure } from '../../trpc';
import { getSimilarPacksService } from '../../services/pack/pack.service';
import { getSimilarPacks } from '@packrat/validations';
import * as validator from '@packrat/validations';

export function getSimilarPacksRoute() {
return protectedProcedure.input(getSimilarPacks).query(async (opts) => {
const { id, limit, visibility } = opts.input;
export const getSimilarPacks = async (c) => {
try {
const { id, limit, visibility } = await c.req.json();

if (limit < 1) {
throw new Error('limit must be greater than 0');
}

const packs = await getSimilarPacksService(id, limit, visibility);
return packs;
});
return c.json({ packs }, 200);
} catch (error) {
return c.json(
{ error: `Failed to get similar packs: ${error.message}` },
500,
);
}
};

export function getSimilarPacksRoute() {
return protectedProcedure
.input(validator.getSimilarPacks)
.query(async (opts) => {
const { id, limit, visibility } = opts.input;

if (limit < 1) {
throw new Error('limit must be greater than 0');
}

const packs = await getSimilarPacksService(id, limit, visibility);
return packs;
});
}
6 changes: 0 additions & 6 deletions server/src/controllers/user/getUserById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ import { type Context } from 'hono';

export const getUserById = async (ctx: Context) => {
try {
// Correctly accessing route parameters in Hono
const { userId } = await ctx.req.param();
const user = await getUserByIdService(userId);

// if (!c.locals) c.locals = {};
// c.locals.data = user;
// return responseHandler(c);

ctx.set('data', user);
return await responseHandler(ctx);
} catch (error) {
ctx.set('error', error.message);
return await responseHandler(ctx);
// return c.json({ error: `Failed to get user: ${error.message}` }, 500);
}
};

Expand Down
8 changes: 8 additions & 0 deletions server/src/routes/itemRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
deleteGlobalItem,
importItems,
importItemsGlobal,
getSimilarItems,
} from '../controllers/item/index';
import * as validator from '@packrat/validations';
import { tryCatchWrapper } from '../helpers/tryCatchWrapper';
Expand All @@ -29,6 +30,13 @@ router.get(
tryCatchWrapper(getItems),
);

router.get(
'/similarItems',
authTokenMiddleware,
zodParser(validator.getSimilarItems, 'body'),
tryCatchWrapper(getSimilarItems),
);

router.get(
'/i/:id',
authTokenMiddleware,
Expand Down
8 changes: 8 additions & 0 deletions server/src/routes/packRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getPublicPacks,
scorePack,
duplicatePublicPack,
getSimilarPacks,
} from '../controllers/pack/index';
import * as validator from '@packrat/validations';
import { tryCatchWrapper } from '../helpers/tryCatchWrapper';
Expand All @@ -24,6 +25,13 @@ router.get(
tryCatchWrapper(getPublicPacks),
);

router.get(
'/similarPacks',
authTokenMiddleware,
zodParser(validator.getSimilarPacks, 'body'),
tryCatchWrapper(getSimilarPacks),
);

router.get(
'/:ownerId',
authTokenMiddleware,
Expand Down

0 comments on commit f56ebc3

Please sign in to comment.