项目作者: alae-touba

项目描述 :
question-and-answer website where users can create and follow topics, add questions in these topics, answer questions and like/dislike answers.
高级语言: JavaScript
项目地址: git://github.com/alae-touba/social-network.git
创建时间: 2020-07-02T14:01:54Z
项目社区:https://github.com/alae-touba/social-network

开源协议:

下载


Table of content

About

This is a question-and-answer social network website where users are able to:

  • Create and follow different topics
  • Ask questions in these topics
  • Answer questions and like/dislike answers.
  • Track other users in the website to see what questions someone asked, what answers he/she gave, which topics they follow, …etc

Entity Relationship Diagram

Here is the ERD for this app:

ERD

Modeling Data - Relationships Between The 4 Models

A user has many topics (create)\
A topic belongs to one user \
=> one to many

A user has many questions (ask) \
A question belongs to one user \
=> one to many

A user has many answers (answer) \
An answer belongs to one user \
=> one to many

A topic has many questions \
A question belongs to one topic \
=> one to many

A question has many answers \
An answer belongs to one question \
=> one to many

A user can follow many topics (follow) \
A topic has many followers \
=> many to many

An answer has many users who like it \
A user can like many answers \
=> many to many

Mongoose Schemas

I use two way (parent & child) referencing for all relationships:

  1. User{
  2. _id : ObjectId
  3. firstName : String
  4. lastName : String
  5. email : String
  6. password : String
  7. imageName : String
  8. registrationDate: Date
  9. topics : Topic[] //we store the topics ids
  10. questions : Question[] //same
  11. answers : Answer[]
  12. topicsFollowed : Topic[]
  13. answersLiked : Answer[]
  14. }
  1. Topic{
  2. _id : ObjectId
  3. name : String
  4. description : String
  5. imageName : String
  6. creationDate : Date
  7. user : User
  8. questions : Question[]
  9. usersFollowers : User[]
  10. }
  1. Question{
  2. _id : ObjectId
  3. content : String
  4. creationDate : Date
  5. topic : Topic
  6. user : User
  7. answers : Answer[]
  8. }
  1. Answers{
  2. _id : ObjectId
  3. content : String
  4. creationDate : Date
  5. user : User
  6. question : Question
  7. usersWhoLike : User[]
  8. }

API

  1. GET /users
  2. GET /users/:id
  3. GET /users/:id/:content (content == questions | answers | topics | topics-followed)
  4. GET /users/register
  5. GET /users/login
  6. POST /users/register
  7. POST /users/login
  8. DELETE /users/logout
  1. GET /topics
  2. GET /topics/:id
  3. GET /topics/create
  4. POST /topics/create
  5. GET /topics/:id/question
  6. POST /topics/:id/question
  7. DELETE /topics/:id //NIY
  8. PATCH /topics/:id //NIY
  9. POST /topics/follow-infollow //NIY
  1. GET /questions/:id
  2. GET /questions/:id/answer
  3. POST /questions/:id/answer
  4. DELETE /questions/:id //NIY
  5. PATH /questions/:id //NIY
  1. POST /answers/like-unlike
  2. GET /answers/:id //NIY
  3. DELETE /answers/:id //NIY
  4. PATH /answers/:id //NIY

(NIY <=> Not Implemented Yet )

Use it locally

Using docker

  • Clone the project & unzip it
  • Open the project in the terminal

  • Run

    1. docker compose up

    if you watch the logs you may see this error:

    ERD

    This is related to EOL (end of line) difference between Windows & Linux systems. If you are cloning the project in Windows Machine, change the EOL for the file mongodb-database/import-data.sh to be LF (\n).

    In VSCODE you can do this easily by looking at the bottom menu when the file is opened.

    ERD

  • Open a browser & head to http://localhost:8082 to see the content of the database (social_network_db)

    you should see 4 collections (users, topics, quetions, answers), each one with some data in it.

    If its not the case, refer to previous step (error may be related to EOL for the mongodb-database/import-data.sh).

  • Visit http://localhost:3001 to see the app running

  • You can log in to the website with a bunch of existing accounts:

Detailed steps on how to run the application locally in Linux

  • To install Nodejs, visit this link https://github.com/nodesource/distributions/blob/master/README.md

    Go to Debian and Ubuntu based distributions section and find the wanted Nodejs version and run the commands. For example, if you want to install Nodejs version 12, you will run:

    1. # Using Ubuntu
    2. curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
    3. sudo apt-get install -y nodejs
  • Install MongoDB database via

    1. sudo apt install mongodb
  • Start the MongoDB service via:

    1. sudo service mongodb start
  • You can check if MongoDB service is active using:

    1. sudo systemctl status mongodb
  • Now its time to create the database and its collections, to do this, open the mongo shell using:

    1. mongo
  • From the mongo shell, run these commands one after another:

    1. use social_network_db
    2. db.createCollection('users')
    3. db.createCollection('questions')
    4. db.createCollection('answers')
    5. db.createCollection('topics')
  • Download the project into the filesystem

  • Unzip it

  • Now we need to import the data into the mongodb database collections

  • Open a terminal & go to the downloaded project’s directory & run the following commands one after another

    1. mongoimport --db social_network_db --collection users --drop --file ./mongodb-database/users.json --jsonArray
    1. mongoimport --db social_network_db --collection questions --drop --file ./mongodb-database/questions.json --jsonArray
    1. mongoimport --db social_network_db --collection answers --drop --file ./mongodb-database/answers.json --jsonArray
    1. mongoimport --db social_network_db --collection topics --drop --file ./mongodb-database/topics.json --jsonArray
  • Always from the project’s directory, run

    1. npm install

    to install npm dependencies

  • ALways from the project’s directory, run:

    1. npm run startdev

    To run the app

  • go to http://locahost:3000

  • You can log in to the website with a bunch of existing accounts (email/password):

Detailed steps on how to run the application locally in Windows

  • MongoDB/Nodejs Download & install

    • To download Nodejs for windows, follow the link https://nodejs.org/en/download/ and use the installer and follow the installation steps

    • Download MongoDB for Windows from the link https://www.mongodb.com/try/download/community

    • Run the .msi file to run the installation wizard (leave everything as default.. default is BEST)

    • Create this directory structure C:\data\db

    • Go to MongoDB installation folder and somewhere there in the directory structure you will find a bin folder, add it to the PATH env variable .

    • Now we need to download The MongoDB Database Tools (colleciton of command-line utilities for working with MongoDB), to do this head to https://www.mongodb.com/try/download/database-tools?tck=docs_databasetools and download the zip file

    • Extract the zip under C:\ drive

    • under this zip, you will find a bin folder, add it to the PATH env variable

  • Running MongoDB server and importing the data into the database

    • Open cmd and run the MongoDB deamon using this command:

      1. .\mongod.exe
    • Now we need to create the database and its collections, open cmd run the MongoDB client

      1. .\mongo.exe
    • From the mongo shell that appears, run the following commands one after another

      1. use social_network_db
      2. db.createCollection('users')
      3. db.createCollection('questions')
      4. db.createCollection('answers')
      5. db.createCollection('topics')
    • The rest of the steps to import the data and run the app are the same as in Linux

How It Looks

ERD

ERD

ERD

ERD

ERD

ERD

ERD

ERD