- Description
- User Story
- Acceptance Criteria
- Video Demo
- Screenshots
- Database Models
- API Routes
- Installation
- Usage
- Contact Me
- My Development Environment
- Languages, Technologies and Packages used
MongoDB is a popular choice for many social networks due to its speed with large amounts of data and flexibility with unstructured data. I have been using several of the technologies that social networking platforms use in their full-stack applications while on my journey to become a full stack developer; And because the foundation of these applications is data, it’s important that i understand how to build and structure the API first.
I was tasked with building an API for a social network web application where users can share their thoughts, react to friends’ thoughts, and create a friend list. For this project i used Express.js for routing, a MongoDB database, and theMongoose ODM.
AS A social media startup
I WANT an API for my social network that uses a NoSQL database
SO THAT my website can handle large amounts of unstructured dataGIVEN a social network API
WHEN I enter the command to invoke the application
THEN my server is started and the Mongoose models are synced to the MongoDB database
WHEN I open API GET routes in Insomnia for users and thoughts
THEN the data for each of these routes is displayed in a formatted JSON
WHEN I test API POST, PUT, and DELETE routes in Insomnia
THEN I am able to successfully create, update, and delete users and thoughts in my database
WHEN I test API POST and DELETE routes in Insomnia
THEN I am able to successfully create and delete reactions to thoughts and add and remove friends to a user’s friend listThe following walkthrough videos show initial setup and the POST, PUT, and DELETE routes for Users, Thoughts, Reactions and Friends being tested in Postman:
01 - Start Up - Sourcing schema, seeding database, starting application
The following animations show examples of the application's API routes being tested in Postman.
The following animation shows GET routes to return all users:
and Here you can see all thoughts being returned in Postman:
The following animation shows GET routes to return a single user and a single thought being tested in Insomnia:
The database for this application contains the following four models (click to expand):
User:
- `username`-
String
-
Unique
-
Required
-
Trimmed
-
email- String
- Required
- Unique
- Must match a valid email address (look into Mongoose's matching validation)
-
thoughts- Array of
_idvalues referencing theThoughtmodel
- Array of
-
friends- Array of
_idvalues referencing theUsermodel (self-reference)
- Array of
Schema Settings:
Created a virtual called friendCount that retrieves the length of the user's friends array field on query.
Thought:
- `thoughtText`-
String
-
Required
-
Must be between 1 and 280 characters
-
createdAt- Date
- Set default value to the current timestamp
- Use a getter method to format the timestamp on query
-
username(The user that created this thought)- String
- Required
-
reactions(These are like replies)- Array of nested documents created with the
reactionSchema
- Array of nested documents created with the
Schema Settings:
Create a virtual called reactionCount that retrieves the length of the thought's reactions array field on query.
Reaction (SCHEMA ONLY):
- `reactionId`-
Use Mongoose's ObjectId data type
-
Default value is set to a new ObjectId
-
reactionBody- String
- Required
- 280 character maximum
-
username- String
- Required
-
createdAt- Date
- Set default value to the current timestamp
- Use a getter method to format the timestamp on query
Schema Settings:
This will not be a model, but rather will be used as the reaction field's subdocument schema in the Thought model.
/api/users
-
GETall users -
GETa single user by its_idand populated thought and friend data -
POSTa new user:
// example data
{
"username": "lernantino",
"email": "lernantino@gmail.com"
}-
PUTto update a user by its_id -
DELETEto remove user by its_id
/api/users/:userId/friends/:friendId
-
POSTto add a new friend to a user's friend list -
DELETEto remove a friend from a user's friend list
/api/thoughts
-
GETto get all thoughts -
GETto get a single thought by its_id -
POSTto create a new thought (don't forget to push the created thought's_idto the associated user'sthoughtsarray field)
// example data
{
"thoughtText": "Here's a cool thought...",
"username": "lernantino",
"userId": "5edff358a0fcb779aa7b118b"
}-
PUTto update a thought by its_id -
DELETEto remove a thought by its_id
/api/thoughts/:thoughtId/reactions
-
POSTto create a reaction stored in a single thought'sreactionsarray field -
DELETEto pull and remove a reaction by the reaction'sreactionIdvalue
The application will be invoked by entering the following 3 commands line by line into your terminal / CLI
git clone git@github.com:faisal244/social-network-api.git
cd social-network-api
npm install
Once all the dependancies have been installed, please provide the application with the source database schema by entering the following 2 commands in your terminal:
npm run seed
npm run start
-
Contact me by Email: m.faisal244@gmail.com
- VScode
- Terminal
- Nodemon
- MacOS Monterey
- Git
- Github
-
Javascript
-
Node.JS v18.7.0
-
[NPM] Mongoose
-
[NPM] Express
-
[NPM] Dotenv
-
[NPM] date-fns

