added functionality for user subscriptions

This commit is contained in:
Mikayla Dobson
2022-11-26 12:52:59 -06:00
parent 99c48d2a6e
commit 03ec2bf38c
18 changed files with 279 additions and 40 deletions

View File

@@ -17,7 +17,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
router.get('/', restrictAccess, (req, res, next) => {
// @ts-ignore: does not recognize structure of req.user
const user = req.user?.user;
const userData: IUser = {
const userData = {
id: user.id,
firstname: user.firstname,
lastname: user.lastname,
@@ -41,9 +41,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
if (error) throw error;
console.log('login successful');
})
// const { id, email, handle, firstname, lastname } = response.user;
// await UserControl.updateOne(response.user.id, { ...response.user, datemodified: now })
// res.status(200).send({ id: id, handle: handle, firstname: firstname, lastname: lastname });
res.cookie('userid', response.user.id, { maxAge: 1000 * 60 * 60 * 24 });
res.send(response);
res.end();
@@ -70,8 +68,6 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
router.post('/register', async (req, res, next) => {
try {
const data: IUser = req.body;
const now = new Intl.DateTimeFormat('en-US', {})
const response = await AuthInstance.register(data);
res.status(200).send(response);
} catch(e) {

View File

@@ -1,4 +1,5 @@
import { Express, Router } from "express";
import { restrictAccess } from "../auth/middlewares";
import CollectionCtl from "../controllers/CollectionCtl";
const CollectionInstance = new CollectionCtl();
@@ -7,7 +8,7 @@ const router = Router();
export const collectionRoute = (app: Express) => {
app.use('/collection', router);
router.get('/:id', async (req, res, next) => {
router.get('/:id', restrictAccess, async (req, res, next) => {
const { id } = req.params;
try {
const result = await CollectionInstance.getOne(id);
@@ -17,7 +18,8 @@ export const collectionRoute = (app: Express) => {
}
})
router.get('/', async (req, res, next) => {
// implement is admin on this route
router.get('/', restrictAccess, async (req, res, next) => {
try {
const result = await CollectionInstance.getAll();
res.status(200).send(result);
@@ -26,7 +28,7 @@ export const collectionRoute = (app: Express) => {
}
})
router.post('/', async (req, res, next) => {
router.post('/', restrictAccess, async (req, res, next) => {
const data = req.body;
try {
const result = await CollectionInstance.post(data);
@@ -35,4 +37,31 @@ export const collectionRoute = (app: Express) => {
next(e);
}
})
// router.get('/subscriptions', restrictAccess, async (req, res, next) => {
// res.send('sanity check');
// // // @ts-ignore
// // const { user } = req.user;
// // if (!user) return;
// // try {
// // const result = await CollectionInstance.getSubscriptions("9");
// // res.status(200).send(result);
// // } catch(e) {
// // next(e);
// // }
// })
// router.post('/subscribe', restrictAccess, async (req, res, next) => {
// // @ts-ignore
// const { user } = req.user;
// const { collection } = req.query;
// try {
// const result = await CollectionInstance.postSubscription(collection as string, user.id as string);
// res.status(201).send(result);
// } catch(e) {
// next(e);
// }
// })
}

View File

@@ -6,6 +6,7 @@ import { collectionRoute } from "./collection";
import { ingredientRoute } from "./ingredient";
import { groceryListRoute } from "./groceryList";
import { authRoute } from "./auth";
import { subscriptionRoute } from "./subscriptions";
export const routes = async (app: Express, passport: PassportStatic) => {
console.log('routes called');
@@ -14,6 +15,7 @@ export const routes = async (app: Express, passport: PassportStatic) => {
userRoute(app);
recipeRoute(app);
collectionRoute(app);
subscriptionRoute(app);
ingredientRoute(app);
groceryListRoute(app);
}

View File

@@ -0,0 +1,39 @@
import { Express, Router } from "express"
import { restrictAccess } from "../auth/middlewares";
import { CollectionCtl } from "../controllers";
const CollectionInstance = new CollectionCtl();
const router = Router();
export const subscriptionRoute = (app: Express) => {
app.use('/subscription', router);
router.get('/', async (req, res, next) => {
// @ts-ignore
const { user } = req.user;
if (!user) return;
try {
const result = await CollectionInstance.getSubscriptions(user.id as string);
res.status(200).send(result);
} catch(e) {
next(e);
}
})
router.post('/', restrictAccess, async (req, res, next) => {
// @ts-ignore
const { user } = req.user;
const { collection } = req.query;
try {
const result = await CollectionInstance.postSubscription(collection as string, user.id as string);
res.status(201).send(result);
} catch(e) {
next(e);
}
})
router.put('/', async (req, res, next) => {
})
}