Skip to content

A RESTful API for a social network web application where users can share their thoughts, react to friends' thoughts, and create a friend list.

Notifications You must be signed in to change notification settings

ReplicantCoder9000/Module-17-Challenge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Social Network API

A RESTful API for a social network web application where users can share their thoughts, react to friends' thoughts, and create a friend list.

🚀 Features

  • User Management: Create, update, delete, and retrieve users
  • Thought Management: Create, update, delete, and retrieve thoughts
  • Friend List Management: Add and remove friends
  • Reaction Management: React to thoughts
  • MongoDB Database: Utilizes MongoDB for flexible data storage
  • Express.js Server: Fast, unopinionated web framework
  • Mongoose ODM: Elegant MongoDB object modeling

🛠️ Installation

  1. Clone the repository:

    git clone [repository-url]
  2. Install dependencies:

    cd social-network-api
    npm install
  3. Make sure MongoDB is installed and running on your system

  4. Configure environment variables:

    cp .env.example .env

    Then edit .env with your specific configuration:

    # Required: MongoDB connection string
    MONGODB_URI=mongodb://127.0.0.1:27017/your_database_name
    
    # Required: Server port number (e.g., 3000, 3001, 3002)
    PORT=your_preferred_port
    
    # Required: Node environment (development, production, or test)
    NODE_ENV=your_environment

    Important: Never commit your .env file to version control. The .env.example file serves as a template.

  5. Start the server:

    npm start

For development with auto-restart:

npm run dev

⚙️ Environment Variables

Variable Description Required Example
MONGODB_URI MongoDB connection string Yes mongodb://127.0.0.1:27017/your_database_name
PORT Server port number No Any valid port (e.g., 3000, 3001, 3002)
NODE_ENV Node environment No development, production, or test

Security Note:

  • Always use environment variables for sensitive information
  • Never commit real credentials to version control
  • Keep your .env file secure and local to your development environment
  • In production, use your hosting platform's secure environment variable system

📚 API Documentation

Users

Get all users

GET /api/users

Get user by ID

GET /api/users/:userId

Create a user

POST /api/users

Body:

{
  "username": "john_doe",
  "email": "[email protected]"
}

Update a user

PUT /api/users/:userId

Body:

{
  "username": "john_updated",
  "email": "[email protected]"
}

Delete a user

DELETE /api/users/:userId

Friends

Add a friend

POST /api/users/:userId/friends/:friendId

Remove a friend

DELETE /api/users/:userId/friends/:friendId

Thoughts

Get all thoughts

GET /api/thoughts

Get thought by ID

GET /api/thoughts/:thoughtId

Create a thought

POST /api/thoughts

Body:

{
  "thoughtText": "Here's a cool thought...",
  "username": "john_doe"
}

Update a thought

PUT /api/thoughts/:thoughtId

Body:

{
  "thoughtText": "Updated thought content"
}

Delete a thought

DELETE /api/thoughts/:thoughtId

Reactions

Add a reaction

POST /api/thoughts/:thoughtId/reactions

Body:

{
  "reactionBody": "Love this!",
  "username": "jane_doe"
}

Remove a reaction

DELETE /api/thoughts/:thoughtId/reactions/:reactionId

🏗️ Models

User

  • username: String (Unique, Required)
  • email: String (Unique, Required, Valid email format)
  • thoughts: Array of _id values referencing Thought model
  • friends: Array of _id values referencing User model
  • Virtual: friendCount - retrieves length of user's friends array

Thought

  • thoughtText: String (Required, 1-280 characters)
  • createdAt: Date (Default: current timestamp)
  • username: String (Required)
  • reactions: Array of nested Reaction documents
  • Virtual: reactionCount - retrieves length of thought's reactions array

Reaction (Schema Only)

  • reactionId: ObjectId (Default: new ObjectId)
  • reactionBody: String (Required, 280 character maximum)
  • username: String (Required)
  • createdAt: Date (Default: current timestamp)

🛠️ Technologies Used

  • Express.js
  • MongoDB
  • Mongoose ODM
  • Node.js
  • Moment.js (for timestamp formatting)

🧪 Testing

Using the Test Script

Use the included test script to run automated tests:

./test.sh

The test script will:

  1. Create test users
  2. Create thoughts
  3. Add reactions
  4. Test friend functionality
  5. Verify all CRUD operations

Testing with Insomnia

  1. Setup Insomnia

    • Download and install Insomnia
    • Create a new Collection named "Social Network API"
  2. Environment Setup in Insomnia

    • Create a new Environment
    • Add base URL variable:
      {
        "baseUrl": "http://localhost:your_port"
      }
  3. Testing User Routes

    a. Create a User

    • Method: POST
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users
    • Body (JSON):
      {
        "username": "testuser",
        "email": "[email protected]"
      }

    b. Get All Users

    • Method: GET
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users

    c. Get User by ID

    • Method: GET
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId
    • Replace :userId with the ID from create user response

    d. Update User

    • Method: PUT
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId
    • Body (JSON):
      {
        "username": "updateduser",
        "email": "[email protected]"
      }

    e. Delete User

    • Method: DELETE
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId
  4. Testing Friend Routes

    a. Add Friend

    • Method: POST
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId/friends/:friendId

    b. Remove Friend

    • Method: DELETE
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/users/:userId/friends/:friendId
  5. Testing Thought Routes

    a. Create Thought

    • Method: POST
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts
    • Body (JSON):
      {
        "thoughtText": "Here's a test thought",
        "username": "testuser"
      }

    b. Get All Thoughts

    • Method: GET
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts

    c. Get Thought by ID

    • Method: GET
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId

    d. Update Thought

    • Method: PUT
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId
    • Body (JSON):
      {
        "thoughtText": "Updated thought content"
      }

    e. Delete Thought

    • Method: DELETE
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId
  6. Testing Reaction Routes

    a. Add Reaction

    • Method: POST
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId/reactions
    • Body (JSON):
      {
        "reactionBody": "This is a reaction",
        "username": "testuser"
      }

    b. Remove Reaction

    • Method: DELETE
    • URL: {% raw %}{{baseUrl}}{% endraw %}/api/thoughts/:thoughtId/reactions/:reactionId

Testing Tips

  1. Save the user IDs and thought IDs from creation responses to use in other requests
  2. Test error cases by providing invalid data or IDs
  3. Verify that virtual fields (friendCount, reactionCount) are being populated
  4. Check that related data is being properly populated in GET responses
  5. Verify cascade deletion (e.g., deleting a user should delete their thoughts)

📝 License

This project is MIT licensed.

👥 Contributing

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a pull request

About

A RESTful API for a social network web application where users can share their thoughts, react to friends' thoughts, and create a friend list.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published