Skip to content

Commit

Permalink
Dummy Data for /shop/get/ (#133)
Browse files Browse the repository at this point in the history
* Point Shop dummy data

---------

Co-authored-by: Aydan Pirani <aydanpirani@gmail.com>
  • Loading branch information
npunati27 and AydanPirani committed Dec 16, 2023
1 parent d420c1c commit 0d5f366
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import staffRouter from "./services/staff/staff-router.js";
import newsletterRouter from "./services/newsletter/newsletter-router.js";
import versionRouter from "./services/version/version-router.js";
import admissionRouter from "./services/admission/admission-router.js";
import shopRouter from "./services/shop/shop-router.js";
// import { InitializeConfigReader } from "./middleware/config-reader.js";
import { ErrorHandler } from "./middleware/error-handler.js";
import Models from "./database/models.js";
Expand Down Expand Up @@ -37,6 +38,7 @@ app.use("/staff/", staffRouter);
app.use("/user/", userRouter);
app.use("/admission/", admissionRouter);
app.use("/version/", versionRouter);
app.use("/shop/", shopRouter);

// Ensure that API is running
app.get("/", (_: Request, res: Response) => {
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const Config = {
/* Misc */
EVENT_ID_LENGTH: 32,
EVENT_BYTES_GEN: 16,
SHOP_BYTES_GEN: 16,
};

export default Config;
1 change: 1 addition & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export enum Database {
ATTENDEE = "attendee",
NEWSLETTER = "newsletter",
REGISTRATION = "registration",
SHOP = "shop",
}
11 changes: 11 additions & 0 deletions src/database/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AdmissionDecision } from "./admission-db.js";
import { EventAttendance, EventMetadata, PublicEvent, StaffEvent } from "./event-db.js";
import { NewsletterSubscription } from "./newsletter-db.js";
import { RegistrationApplication, RegistrationInfo } from "./registration-db.js";
import { ShopItem, ShopQuantity } from "./shop-db.js";
import { UserAttendance, UserInfo } from "./user-db.js";
import { AnyParamConstructor } from "@typegoose/typegoose/lib/types.js";

Expand All @@ -26,6 +27,11 @@ enum AdmissionCollection {
DECISION = "decision",
}

enum ShopCollection {
ITEMS = "items",
QUANTITIES = "quantities",
}

enum EventCollection {
METADATA = "metadata",
ATTENDANCE = "attendance",
Expand Down Expand Up @@ -72,6 +78,9 @@ export default class Models {
// Registration
static RegistrationInfo: mongoose.Model<RegistrationInfo> = undefined!;
static RegistrationApplication: mongoose.Model<RegistrationApplication> = undefined!;
//Shop
static ShopItem: mongoose.Model<ShopItem> = undefined!;
static ShopQuantity: mongoose.Model<ShopQuantity> = undefined!;
// User
static UserInfo: mongoose.Model<UserInfo> = undefined!;
static UserAttendance: mongoose.Model<UserAttendance> = undefined!;
Expand All @@ -92,6 +101,8 @@ export default class Models {
Database.REGISTRATION,
RegistrationCollection.APPLICATION,
);
this.ShopItem = getModel(ShopItem, Database.SHOP, ShopCollection.ITEMS);
this.ShopQuantity = getModel(ShopQuantity, Database.SHOP, ShopCollection.QUANTITIES);
this.UserInfo = getModel(UserInfo, Database.USER, UserCollection.INFO);
this.UserAttendance = getModel(UserAttendance, Database.USER, UserCollection.ATTENDANCE);
}
Expand Down
35 changes: 35 additions & 0 deletions src/database/shop-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { prop } from "@typegoose/typegoose";

export class ShopItem {
@prop({ required: true })
public itemId: string;

@prop({ required: true })
public name: string;

@prop({ required: true })
public price: number;

@prop({ required: true })
public isRaffle: boolean;

constructor(itemId: string, name: string, price: number, isRaffle: boolean) {
this.itemId = itemId;
this.name = name;
this.price = price;
this.isRaffle = isRaffle;
}
}

export class ShopQuantity {
@prop({ required: true })
public itemId: string;

@prop({ requireD: true })
public quantity: number;

constructor(itemId: string, quantity: number) {
this.itemId = itemId;
this.quantity = quantity;
}
}
19 changes: 19 additions & 0 deletions src/services/shop/shop-formats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface ItemFormat {
itemId: string;
name: string;
price: number;
isRaffle: boolean;
quantity: number;
}

export interface QuantityFormat {
itemId: string;
quantity: number;
}

export interface ShopItemFormat {
itemId: string;
name: string;
price: number;
isRaffle: boolean;
}
86 changes: 86 additions & 0 deletions src/services/shop/shop-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Request, Response, Router } from "express";
import { NextFunction } from "express-serve-static-core";
import { weakJwtVerification } from "../../middleware/verify-jwt.js";
import { StatusCode } from "status-code-enum";
// import { RouterError } from "../../middleware/error-handler.js";
// import { ItemFormat, QuantityFormat, ShopItemFormat } from "./shop-formats.js";
// import Models from "../../database/models.js";

const shopRouter: Router = Router();
// shopRouter.get("/", weakJwtVerification, async (_: Request, res: Response, next: NextFunction) => {
// try {
// const shopItems: ShopItemFormat[] = await Models.ShopItem.find();
// const shopQuantities: QuantityFormat[] = await Models.ShopQuantity.find();

// const itemsMap = new Map<string, ShopItemFormat>();
// shopItems.forEach((item) => {
// return itemsMap.set(item.itemId, item);
// });

// const itemsWithQuantity: ItemFormat[] = Array.from(itemsMap.values()).map((item) => {
// const quantity = shopQuantities.find((q) => {
// return q.itemId === item.itemId;
// });
// const itemQuantity = quantity ? quantity.quantity : 0;

// return {
// itemId: item.itemId,
// name: item.name || "",
// price: item.price || 0,
// isRaffle: item.isRaffle || false,
// quantity: itemQuantity,
// };
// });

// return res.status(StatusCode.SuccessOK).send(itemsWithQuantity);
// } catch (error) {
// console.error(error);
// return next(new RouterError(StatusCode.ServerErrorInternal, "InternalServerError"));
// }
// });

/**
* @api {get} /shop/ GET /shop/
* @apiGroup Shop
* @apiDescription Get item details for all items.
*
* @apiSuccess (200: Success) {Json} items The items details.
* @apiSuccessExample Example Success Response
* HTTP/1.1 200 OK
* {
* [{
* "name": "hoodie",
* "price": 15,
* "isRaffle": true,
* "quantity": 20
* }, {
* "name": "shoe",
* "price": 20,
* "isRaffle": false,
* "quantity": 1
* }]
* }
*
* @apiUse weakJwtVerification
* */
shopRouter.get("/", weakJwtVerification, async (_1: Request, res: Response, _2: NextFunction) => {
const data = [
{
itemId: "0x1218907123897312891",
name: "sticker",
price: 15,
isRaffle: true,
quantity: 20,
},
{
itemId: "0x1218907123897312892",
name: "shoes",
price: 20,
isRaffle: false,
quantity: 1,
},
];
return res.status(StatusCode.SuccessOK).send(data);
});

export default shopRouter;

0 comments on commit 0d5f366

Please sign in to comment.