Created
October 5, 2023 10:57
-
-
Save herawais/760b8b0e4cf47267a8a130d3d01e9fc1 to your computer and use it in GitHub Desktop.
src/api /index.ts
This file contains hidden or 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
//start of APIs for brand------------------- | |
//GET ALL BRANDS | |
app.get("/store/brands", cors(storeCorsOptions), async (req, res) => { | |
const brandService = req.scope.resolve("brandService"); | |
brandService.getBrands().then((brands) => { | |
return res.json(brands); | |
}); | |
}); | |
// GET A SINGLE BRAND BY NAME OR ID OR HANDLE | |
app.get("/store/brand/:param", cors(storeCorsOptions), async (req, res) => { | |
const brandService = req.scope.resolve("brandService"); | |
const param = req.params.param; | |
// Try to get the brand by name | |
brandService.getBrandByName(param).then((brand) => { | |
if (brand) { | |
return res.json(brand); | |
} else { | |
// If brand not found by name, try to get it by ID | |
brandService.getBrandById(param).then((brandById) => { | |
if (brandById) { | |
return res.json(brandById); | |
} else { | |
// If brand not found by ID, try to get it by handle | |
brandService.getBrandByHandle(param).then((brandByHandle) => { | |
if (brandByHandle) { | |
return res.json(brandByHandle); | |
} else { | |
// If still not found, return "no record found" | |
return res.json({ message: "No record found" }); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
}); | |
// GET ALL BRANDS | |
app.options("/admin/brands", cors(adminCorsOptions), bodyParser.json()); | |
app.get("/admin/brands", cors(adminCorsOptions), async (req, res) => { | |
const brandService = req.scope.resolve("brandService"); | |
brandService.getBrands().then((brands) => { | |
return res.json({ brands }); | |
}); | |
}); | |
// GET A SINGLE BRAND BY ID OR HANDLE ON ADMIN | |
app.get("/admin/brand/:id", cors(adminCorsOptions), async (req, res) => { | |
const brandService = req.scope.resolve("brandService"); | |
brandService.getBrandById(req.params.id).then((brand) => { | |
if (brand) { | |
return res.json({ brand }); | |
} else { | |
brandService.getBrandByHandle(req.params.id).then((brandByHandle) => { | |
return res.json({ brandByHandle }); | |
}); | |
} | |
}); | |
}); | |
// ADD A BRAND | |
app.options("/admin/brand", cors(adminCorsOptions), bodyParser.json()); | |
app.post( | |
"/admin/brand", | |
cors(adminCorsOptions), | |
bodyParser.json(), | |
async (req, res) => { | |
const schema = z.object({ | |
name: z.string().min(1), | |
handle: z.string().min(1), | |
desc: z.string().optional(), | |
img: z.string().optional(), | |
}); | |
/* @ts-ignore */ | |
const { success, error, data } = schema.safeParse(req.body); | |
if (!success) { | |
throw new MedusaError(MedusaError.Types.INVALID_DATA, error); | |
} | |
const brandService = req.scope.resolve("brandService"); | |
brandService.addBrand(data).then((brand) => { | |
return res.json(brand); | |
}); | |
} | |
); | |
// UPDATE A BRAND | |
app.options("/admin/brand/:id", cors(adminCorsOptions)); | |
app.post( | |
"/admin/brand/:id", | |
cors(adminCorsOptions), | |
bodyParser.json(), | |
async (req, res) => { | |
const schema = z.object({ | |
name: z.string().min(1), | |
handle: z.string().min(1), | |
desc: z.string().optional(), | |
img: z.string().optional(), | |
}); | |
/* @ts-ignore */ | |
const { success, error, data } = schema.safeParse(req.body); | |
if (!success) { | |
throw new MedusaError(MedusaError.Types.INVALID_DATA, error); | |
} | |
const brandService = req.scope.resolve("brandService"); | |
brandService.updateBrand(req.params.id, data).then((brand) => { | |
return res.json({ brand }); | |
}); | |
} | |
); | |
// DELETE A BRAND | |
app.delete("/admin/brand/:id", cors(adminCorsOptions), async (req, res) => { | |
const brandService = req.scope.resolve("brandService"); | |
brandService.deleteBrand(req.params.id).then(() => { | |
return res.sendStatus(200); | |
}); | |
}); | |
//DELETE A BRAND BY POST METHOD --- because useAdminCustomDelete did not get dynamic id on run time | |
app.options("/admin/delete-brand/", cors(adminCorsOptions)); | |
app.post( | |
"/admin/delete-brand/", | |
cors(adminCorsOptions), | |
bodyParser.json(), | |
async (req, res) => { | |
const schema = z.object({ | |
id: z.string().optional(), | |
}); | |
/* @ts-ignore */ | |
const { success, error, data } = schema.safeParse(req.body); | |
if (!success) { | |
throw new MedusaError(MedusaError.Types.INVALID_DATA, error); | |
} | |
const brandService = req.scope.resolve("brandService"); | |
brandService.deleteBrand(data.id) | |
.then(() => { | |
res.sendStatus(200); // Send a success status code | |
}) | |
.catch((error) => { | |
if (error.response && error.response.status === 400) { | |
res.status(400).json({ error: error.response.data.message }); // Send a custom error response | |
} else { | |
res.status(500).json({ error: "An error occurred" }); // Send a generic error response | |
} | |
}); | |
}) | |
//end of APIs for brand--------------------- | |
//start of APIs for product----------------- | |
// UPDATE A BRAND OF PRODUCT | |
app.options("/admin/brand/product/:id", cors(adminCorsOptions)); | |
app.post( | |
"/admin/brand/product/:id", | |
cors(adminCorsOptions), | |
bodyParser.json(), | |
async (req, res) => { | |
const schema = z.object({ | |
brandId: z.string().optional(), | |
}); | |
/* @ts-ignore */ | |
const { success, error, data } = schema.safeParse(req.body); | |
if (!success) { | |
throw new MedusaError(MedusaError.Types.INVALID_DATA, error); | |
} | |
const productService = req.scope.resolve("productService"); | |
const manager = req.scope.resolve("manager"); | |
await manager.transaction(async (transactionManager) => { | |
const productServiceTx = | |
productService.withTransaction(transactionManager); | |
const product = await productServiceTx.update(req.params.id, { | |
brand: data.brandId, | |
}); | |
return res.json({ product }); | |
}); | |
} | |
); | |
//end of APIs for product------------------- |
Author
herawais
commented
Oct 5, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment