Skip to content

Commit

Permalink
fix debugger and use
Browse files Browse the repository at this point in the history
  • Loading branch information
Dereje1 committed Feb 15, 2023
1 parent f2f9ca5 commit 4dceec1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,33 @@ These instructions will get you a copy of the project up and running on your loc
>Create a new directory and initialize git
```
$ mkdir Pinterest-Clone
$ cd Pinterest-Clone
$ git init
mkdir Pinterest-Clone
cd Pinterest-Clone
git init
```
>Pull from github and install packages
```
$ git pull https://github.com/Dereje1/Pinterest-Clone.git
$ npm install
git pull https://github.com/Dereje1/Pinterest-Clone.git
npm install
```

>To run mongoDB locally with docker :
In the root folder run
`docker-compose up mongodb` and then use `mongodb://root:123456@localhost:27017` for `MONGOLAB_URI`
in your `.env `file below, alternatively, you can get a connection string from mongodb Atlas after setting up your own db.

```
docker-compose up mongodb
```
Alternatively, you can get a connection string from mongodb Atlas after setting up your own db.

>create .env files
In the root of the project create a .env file with the following contents
```
SESSION_SECRET=<Secret for Express Session>
MONGOLAB_URI=<Mongo Connection URI>
MONGOLAB_URI=mongodb://root:123456@localhost:27017
TWITTER_CONSUMER_KEY=< Get from Twitter Developer API >
TWITTER_CONSUMER_SECRET=< Get from Twitter Developer API >
TWITTER_CALLBACK=http://localhost:8080/auth/twitter/redirect
Expand All @@ -60,10 +62,12 @@ AWS_ACCESS_KEY_ID=< Get from AWS >
AWS_SECRET_KEY=< Get from AWS >
S3_BUCKET_NAME=< s3 bucket name for uploaded pins>
NODE_ENV=<development|production>
DEBUG=Pinterest-Clone:server
DEBUG_COLORS=1
```
Run development environment
Run the development environment
```
$ npm run dev
npm run dev
```
You can now go to `http://localhost:8080/` and see the project running in dev mode.

Expand Down
14 changes: 5 additions & 9 deletions bin/www.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ require('dotenv').config();
import http from 'http';
import debugg from 'debug';

process.env.DEBUG = 'Pinterest-Clone:server';
const debug = debugg('Pinterest-Clone:server');
// const http = require('http');

const appPath = process.env.NODE_ENV === 'development' ? '../server/app' : '../app';
// eslint-disable-next-line import/no-dynamic-require
const app = require(appPath).default;

/**
* Normalize a port into a number, string, or false.
*/
Expand Down Expand Up @@ -81,12 +79,10 @@ const onError = (error: {syscall: string, code: string}) => {

const onListening = () => {
const addr = server.address();
if (addr) {
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `port ${addr.port}`;
debug(`Listening on ${bind}`);
}
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `port ${addr?.port}`;
debug(`Listening on ${bind}`);
};

/**
Expand Down
7 changes: 6 additions & 1 deletion server/controllers/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request } from 'express';
import debugg from 'debug';
import { genericResponseType } from '../interfaces';
import {
getUserProfile,
Expand All @@ -7,21 +8,25 @@ import pins from '../models/pins';
import { UserType } from '../models/user';
import pinLinks from '../models/pinlinks';

const debug = debugg('Pinterest-Clone:server');

const deletePin = async (req: Request, res: genericResponseType) => {
const { userId, displayName, isAdmin } = getUserProfile(req.user as UserType);
const query = { _id: req.params._id };
const pinID = req.params._id;
debug(`Deleting pin -> ${pinID} by userId -> ${userId}`);
try {
const pin = await pins.findById(pinID).exec();
if (!pin) return res.end();
if (userId === pin.owner.toString() || isAdmin) {
const removedPin = await pins.findOneAndRemove(query).exec();
await pinLinks.findOneAndRemove({ pin_id: pinID }).exec();
console.log(`${displayName} deleted pin ${removedPin && removedPin.imgDescription}`);
debug(`${displayName} deleted pin ${removedPin && removedPin.imgDescription}`);
return res.json(removedPin);
}
throw new Error(`Pin ID: ${pinID} is not owned by user ID: ${userId} - delete operation cancelled!`);
} catch (error) {
debug(`Error deleting pin -> ${pinID} by userId -> ${userId}`);
return res.json(error);
}
};
Expand Down
11 changes: 11 additions & 0 deletions server/controllers/get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request } from 'express';
import mongoose from 'mongoose';
import debugg from 'debug';
import { genericResponseType } from '../interfaces';
import {
getUserProfile, filterPins,
Expand All @@ -8,21 +9,26 @@ import pins from '../models/pins';
import users, { UserType } from '../models/user';
import savedTags from '../models/tags';

const debug = debugg('Pinterest-Clone:server');

export const getPins = async (req: Request, res: genericResponseType) => {
const { userId, isAdmin } = getUserProfile(req.user as UserType);
debug(`Getting all pins for userId -> ${userId}`);
try {
const allPins = await pins.find({ isBroken: false })
.populate(['owner', 'savedBy', 'comments.user'])
.exec();
res.json(filterPins({ rawPins: allPins, userId, isAdmin }));
} catch (error) {
debug(`Error getting all pins for userId -> ${userId}`);
res.json(error);
}
};

export const getUserPins = async (req: Request, res: genericResponseType) => {
const { userId, isAdmin } = getUserProfile(req.user as UserType);
const mongooseUserId = mongoose.Types.ObjectId(userId);
debug(`Getting user pins for userId -> ${userId}`);
try {
if (isAdmin) {
const allPins = await pins.find({ isBroken: false }).populate(['owner', 'savedBy', 'comments.user']).exec();
Expand All @@ -35,6 +41,7 @@ export const getUserPins = async (req: Request, res: genericResponseType) => {
.exec();
return res.json({ profilePins: filterPins({ rawPins: profilePins, userId, isAdmin }) });
} catch (error) {
debug(`Error getting user pins for userId -> ${userId}`);
return res.json(error);
}
};
Expand All @@ -46,6 +53,7 @@ export const getProfilePins = async (
const userId = req.params.userid;
const mongooseUserId = mongoose.Types.ObjectId(userId);
const { userId: loggedInUserid } = getUserProfile(req.user as UserType);
debug(`Getting profile pins for profile -> ${userId} by user -> ${loggedInUserid}`);
try {
const user = await users.findById(userId).exec();
if (!user) {
Expand All @@ -69,16 +77,19 @@ export const getProfilePins = async (
},
});
} catch (error) {
debug(`Error etting profile pins for profile -> ${userId} by user -> ${loggedInUserid}`);
// mongoose errors out on invalid ObjectIds sent -> redirect also in that case
return res.json({ redirect: '/' });
}
};

export const getTags = async (req: Request, res: genericResponseType) => {
try {
debug('Getting all distinct tags');
const tags = await savedTags.find().distinct('tag').exec();
res.json(tags);
} catch (error) {
debug('Error getting all distinct tags');
res.json(error);
}
};
8 changes: 7 additions & 1 deletion server/controllers/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request } from 'express';
import mongoose from 'mongoose';
import debugg from 'debug';
import { genericResponseType } from '../interfaces';
import {
getUserProfile, uploadImageToS3,
Expand All @@ -8,9 +9,12 @@ import pins from '../models/pins';
import { UserType } from '../models/user';
import pinLinks from '../models/pinlinks';

const debug = debugg('Pinterest-Clone:server');

export const addPin = async (req: Request, res: genericResponseType) => {
const { displayName, userId, service } = getUserProfile(req.user as UserType);
const { imgLink: originalImgLink } = req.body;
debug(`Creating new pin for userId -> ${userId}`);
try {
const newImgLink = await uploadImageToS3({
originalImgLink, userId, displayName, service,
Expand All @@ -29,9 +33,10 @@ export const addPin = async (req: Request, res: genericResponseType) => {
originalImgLink: addedpin.originalImgLink,
cloudFrontLink: newImgLink ? `https://d1ttxrulihk8wq.cloudfront.net/${newImgLink.split('/')[4]}` : '',
});
console.log(`${displayName} added pin ${addedpin.imgDescription}`);
debug(`${displayName} added pin ${addedpin.imgDescription}`);
res.json(addedpin);
} catch (error) {
debug(`Error creating new pin for userId -> ${userId}`);
res.json(error);
}
};
Expand All @@ -48,6 +53,7 @@ export const getDuplicateError = async (req: Request, res: genericResponseType)
}).exec();
return res.json({ duplicateError: Boolean(duplicateFound) });
} catch (error) {
debug('Error looking for duplicates');
return res.json(error);
}
};
26 changes: 19 additions & 7 deletions server/controllers/put.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request } from 'express';
import mongoose from 'mongoose';
import debugg from 'debug';
import { genericResponseType } from '../interfaces';
import {
getUserProfile, filterPins,
Expand All @@ -8,12 +9,14 @@ import pins from '../models/pins';
import users, { UserType } from '../models/user';
import savedTags from '../models/tags';

const debug = debugg('Pinterest-Clone:server');

export const pinImage = async (req: Request, res: genericResponseType) => {
const pinID = req.params._id;
const {
userId, displayName, isAdmin,
} = getUserProfile(req.user as UserType);

debug(`Pinning image with id -> ${pinID} by userId -> ${userId}`);
try {
const pin = await pins.findById(pinID).exec();
if (!pin) return res.end();
Expand All @@ -27,19 +30,21 @@ export const pinImage = async (req: Request, res: genericResponseType) => {
.exec();
if (!updatedPin) return res.end();
const [filteredAndUpdatedPin] = filterPins({ rawPins: [updatedPin], userId, isAdmin });
console.log(`${displayName} pinned ${updatedPin.imgDescription}`);
debug(`${displayName} pinned ${updatedPin.imgDescription}`);
return res.json(filteredAndUpdatedPin);
}
console.log(`${displayName} has the pin - ${pin.imgDescription} already saved`);
debug(`${displayName} has the pin - ${pin.imgDescription} already saved`);
return res.end();
} catch (error) {
debug(`Error pinning image with id -> ${pinID} by userId -> ${userId}`);
return res.json(error);
}
};

export const unpin = async (req: Request, res: genericResponseType) => {
const { userId, displayName, isAdmin } = getUserProfile(req.user as UserType);
const pinID = req.params._id;
debug(`Unpinning image with id -> ${pinID} by userId -> ${userId}`);
try {
const pin = await pins.findById(pinID).exec();
if (!pin) return res.end();
Expand All @@ -51,9 +56,10 @@ export const unpin = async (req: Request, res: genericResponseType) => {
.exec();
if (!updatedPin) return res.end();
const [filteredAndUpdatedPin] = filterPins({ rawPins: [updatedPin], userId, isAdmin });
console.log(`${displayName} unpinned ${updatedPin.imgDescription}`);
debug(`${displayName} unpinned ${updatedPin.imgDescription}`);
return res.json(filteredAndUpdatedPin);
} catch (error) {
debug(`Error unpinning image with id -> ${pinID} by userId -> ${userId}`);
return res.json(error);
}
};
Expand All @@ -64,6 +70,7 @@ export const addComment = async (req: Request, res: genericResponseType) => {
} = getUserProfile(req.user as UserType);
const pinID = req.params._id;
const { comment } = req.body;
debug(`Adding comment for pin with id -> ${pinID} by userId -> ${userId}`);
try {
const update = { $push: { comments: { comment, user: mongoose.Types.ObjectId(userId) } } };
const modified = { new: true };
Expand All @@ -72,9 +79,10 @@ export const addComment = async (req: Request, res: genericResponseType) => {
.exec();
if (!updatedPin) return res.end();
const [filteredAndUpdatedPin] = filterPins({ rawPins: [updatedPin], userId, isAdmin });
console.log(`${displayName} commented on ${updatedPin.imgDescription}`);
debug(`${displayName} commented on ${updatedPin.imgDescription}`);
return res.json(filteredAndUpdatedPin);
} catch (error) {
debug(`Error adding comment for pin with id -> ${pinID} by userId -> ${userId}`);
return res.json(error);
}
};
Expand All @@ -84,6 +92,7 @@ export const updateTags = async (req: Request, res: genericResponseType) => {
userId, displayName, isAdmin,
} = getUserProfile(req.user as UserType);
const { pinID, tag, deleteId } = req.query;
debug(`Updating tags for pin with id -> ${pinID} by userId -> ${userId}`);
try {
const pin = await pins.findById(pinID).exec();
if (!pin) return res.end();
Expand All @@ -101,9 +110,10 @@ export const updateTags = async (req: Request, res: genericResponseType) => {
.exec();
if (!updatedPin) return res.end();
const [filteredAndUpdatedPin] = filterPins({ rawPins: [updatedPin], userId, isAdmin });
console.log(`${displayName} ${deleteId ? 'deleted' : `added ${tag}`} tag on ${updatedPin.imgDescription}`);
debug(`${displayName} ${deleteId ? 'deleted' : `added ${tag}`} tag on ${updatedPin.imgDescription}`);
return res.json(filteredAndUpdatedPin);
} catch (error) {
debug(`Error updating tags for pin with id -> ${pinID} by userId -> ${userId}`);
return res.json(error);
}
};
Expand All @@ -113,13 +123,15 @@ export const updateDisplayName = async (req: Request, res: genericResponseType)
userId, displayName,
} = getUserProfile(req.user as UserType);
const { newDisplayName } = req.body;
debug(`Updating Display name for userId -> ${userId}`);
try {
const user = await users.findById(userId).exec();
const update = { $set: { displayName: newDisplayName } };
await user?.updateOne(update);
console.log(`${displayName} changed to ${newDisplayName}`);
debug(`${displayName} changed to ${newDisplayName}`);
return res.end();
} catch (error) {
debug(`Error updating Display name for userId -> ${userId}`);
return res.json(error);
}
};

0 comments on commit 4dceec1

Please sign in to comment.