'Designing an API for a Forum
So I am trying to figure out how to design my backend for a forum website I am building (similar to reddit) and I just want to be sure I am doing things right.
The home page will have a list of threads. Each thread listed on the home page will have the title of the thread, the number of comments, and the username of the author.
Then when you click on one of the threads it will open up that thread's page and display the title, description, author's username, number of comments, and comments.
Here is what I am thinking for a design:
models:
User
username: string
id: number
Comment
id: number
author: User
dateCreated: date
content: text
replies: [Comment]
numberOfReplies: number
Thread
id: number
author: User
dateCreated: date
title: text
description: text
comments: [Comment]
numberOfComments: number
I think I will have an endpoint for grabbing a list of the threads on the main page like so:
GET /home/threads?pageNumber={page number}&threadsPerPage={number of threads per page}
The endpoint will return a list of the most recent threads based on page number and number of threads per page. Each thread returned will not have the comments since that would take too long and it's not needed for the home page. So the value for the comments variable of each thread will just be an empty array.
Then when you click on one of the threads, we will an endpoint like so:
GET /thread/{threadID}/comments?startIndex={start index}&count={number of comments to return}
This endpoint will return up to count comments on the thread starting from index startIndex. If there are more than the number of comments returned, the frontend will display a clickable text: X more comments at the bottom. If they click, it will hit this same endpoint to grab more comments.
If a comment has replies, the frontend will have a clickable text: X more replies. If the user clicks, it will hit this endpoint:
GET /comment/{commentID}/replies?startIndex={start index}&count={number of replies to return}
It will return count amount of comments' replies at startindex.
Does this seem right? I've never really built an API before so I would appreciate the advice.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
