Skip to content

Latest commit

 

History

History
215 lines (140 loc) · 5.6 KB

Api.md

File metadata and controls

215 lines (140 loc) · 5.6 KB

User Schema (userModel.js)

import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
  id: { type: String, unique: true },
  name: {
    type: String,
    required: [true, "Name is required"],
    minlength: [3, "Name should be at least 3 characters long"],
  },
  email: {
    type: String,
    required: [true, "Email is required"],
    unique: true,
    lowercase: true,
    validate: {
      validator: (email) => /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,}$/.test(email),
      message: "Invalid email",
    },
  },
  password: { type: String, required: [true, "Password is required"] },
});

const userModel = mongoose.model("user", userSchema);

export default userModel;

Blog Schema (blogModel.js)

import mongoose from "mongoose";

const blogSchema = new mongoose.Schema({
  title: { type: String, required: [true, "Blog title is required"] },
  description: String,
  picture: String,
  content: { type: String, required: [true, "Blog content is required"] },
  published: { type: Boolean, required: true, default: false },
  name: String,
  user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
  tags: [{ type: mongoose.Schema.Types.ObjectId, ref: "tag" }],
  likes: [{ type: mongoose.Schema.Types.ObjectId, ref: "like" }],
  comments: [{ type: mongoose.Schema.Types.ObjectId, ref: "comment" }],
});

blogSchema.set("timestamps", true);

const blogModel = mongoose.model("blog", blogSchema);

export default blogModel;

Tag Schema (tagModel.js)

import mongoose from "mongoose";

const tagSchema = new mongoose.Schema({
  name: { type: String, required: true, unique: true },
  followers: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }],
});

const tagModel = mongoose.model("tag", tagSchema);

export default tagModel;

Like Schema (likeModel.js)

import mongoose from "mongoose";

const likeSchema = new mongoose.Schema({
  user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
  blog: { type: mongoose.Schema.Types.ObjectId, ref: "blog" },
});

const likeModel = mongoose.model("like", likeSchema);

export default likeModel;

Comment Schema (commentModel.js)

import mongoose from "mongoose";

const commentSchema = new mongoose.Schema({
  user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
  blog: { type: mongoose.Schema.Types.ObjectId, ref: "blog" },
  content: { type: String, required: true },
});

const commentModel = mongoose.model("comment", commentSchema);

export default commentModel;

Followers Schema (followersModel.js)

import mongoose from "mongoose";

const followersSchema = new mongoose.Schema({
  user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
  followers: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }],
});

const Followers = mongoose.model("Followers", followersSchema);

export default Followers;

Following Schema (followingModel.js)

import mongoose from "mongoose";

const followingSchema = new mongoose.Schema({
  user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
  following: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }],
});

const Following = mongoose.model("Following", followingSchema);

export default Following;

TagFollowing Schema (tagFollowingModel.js)

import mongoose from "mongoose";

const tagFollowingSchema = new mongoose.Schema({
  user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
  tags: [{ type: mongoose.Schema.Types.ObjectId, ref: "tag" }],
});

const TagFollowing = mongoose.model("TagFollowing", tagFollowingSchema);

export default TagFollowing;

API Endpoints

  1. User System:

    • Register User: POST /api/users/register

    • Login User: POST /api/users/login (Requires Authentication)

    • Logout User: POST /api/users/logout

    • Refresh Token: GEt /api/users/refreshToken

  2. Blog System:

    • Create Blog: POST /api/blogs (Requires Authentication)

    • Get All Blogs: GET /api/blogs

    • Get Blog by ID: GET /api/blogs/:blogId

    • Get Blog by UserID: GET /api/blogs/user/:UserId

    • Update Blog: PUT /api/blogs/:blogId (Requires Authentication)

    • Delete Blog: DELETE /api/blogs/:blogId (Requires Authentication)

    • Like Blog: POST /api/blogs/:blogId/like (Requires Authentication)

    • Unlike Blog: DELETE /api/blogs/:blogId/unlike (Requires Authentication)

  3. Tag System:

    • Create Tag: POST /api/tags (Requires Authentication)

    • Get All Tags: GET /api/tags

    • Get Tag by ID: GET /api/tags/:tagId

    • Update Tag: PUT /api/tags/:tagId (Requires Authentication) x

    • Delete Tag: DELETE /api/tags/:tagId (Requires Authentication) x

    • Follow Tag: POST /api/tags/:userId/follow/:tagId (Requires Authentication)

    • Unfollow Tag: DELETE /api/tags/:userId/unfollow/:tagId (Requires Authentication)

    • Get Tags Followed by a User: GET /api/tags/tag-following/:userId

  4. Comments System:

    • Create Comment: POST /api/comments/:blogId (Requires Authentication)

    • Get Comments for a Blog: GET /api/comments/:blogId

    • Update Comment: PUT /api/comments/:commentId (Requires Authentication)

    • Delete Comment: DELETE /api/comments/:commentId (Requires Authentication)

  5. Follow System:

    • Follow User: POST /api/users/:userId/follow (Requires Authentication)

    • Unfollow User: DELETE /api/users/:userId/unfollow (Requires Authentication)

    • Get Followers of a User: GET /api/followers/:userId

    • Get Users Followed by a User: GET /api/following/:userId