Skip to content

Commit

Permalink
Added the share list feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-elhaddad committed Oct 12, 2023
1 parent 6b1247c commit e5e1682
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/controllers/helpers/generateListURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import jwt from 'jsonwebtoken';
import { config } from '../../configuration/config';

const shareList = (l_id: number): string => {
const token = jwt.sign(String(l_id), config.jwtSecretKey);
const url = `http://localhost:3000/lists/${token}`;
return url;
}

export default shareList;
28 changes: 28 additions & 0 deletions src/controllers/lists.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import Lists from '../models/lists.model';
import List from '../types/List.type';
import generateListURL from './helpers/generateListURL';
import jwt from 'jsonwebtoken';

export const createList = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand Down Expand Up @@ -40,6 +42,32 @@ export const deleteList = async (req: Request, res: Response, next: NextFunction
}
};

export const shareList = async (req: Request, res: Response, next: NextFunction) => {
try {
const { l_id } = req.params;

const url = generateListURL(Number(l_id));
res.json({ message: 'URL generated successfully.', url });
} catch (err) {
res.locals.err = err;
next();
}
}

export const viewsharedList = async (req: Request, res: Response, next: NextFunction) => {
try {
const { token } = req.params;
const l_id = Number(jwt.decode(String(token)));

const list = await Lists.getList(l_id);

res.json({ message: 'List fetched successfully', list });
} catch (err) {
res.locals.err = err;
next();
}
}

export const addTask = async (req: Request, res: Response, next: NextFunction) => {
try {
const { l_id, t_id } = req.body;
Expand Down
4 changes: 3 additions & 1 deletion src/routes/api/listRouter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Application } from 'express';
import authenticate from '../../middlewares/authenticate.middleware';
import { addTask, createList, deleteList, getLists, removeTask } from '../../controllers/lists.controller';
import { addTask, createList, deleteList, getLists, removeTask, shareList, viewsharedList } from '../../controllers/lists.controller';
import errorHandler from '../../middlewares/errorHandler.middleware';

const listRouter = (app: Application) => {
app.post('/lists', authenticate, createList, errorHandler);
app.get('/lists', authenticate, getLists, errorHandler);
app.delete('/lists', authenticate, deleteList, errorHandler);
app.get('/lists/share/:l_id', authenticate, shareList, errorHandler);
app.get('/lists/:token', viewsharedList, errorHandler);
app.post('/lists/add-task', authenticate, addTask, errorHandler);
app.delete('/lists/remove-task', authenticate, removeTask, errorHandler);
};
Expand Down

0 comments on commit e5e1682

Please sign in to comment.