This is my first spring project. I have developed a REST API for posting blogs. For now, users can only post blogs containing text, but soon I will add feature to add images to the blogs. Following services are provided by the REST API:
- Users can be added (Authentication facility is not added)
- New Blogs can be added.
- Existing Blogs can be updated.
- Delete existing Blogs.
- Get All Blogs.
- Get specific Blog by given ID.
- Post Comment on existing Blog.
- Get all Comments for a Blog.
- Add Comment on a Comment.
- Get all Comments under the given Comment.
I have handled major exceptions and errors, but still due to time constraint I have not implemented many checks, I will update the new version soon.
- POST
/usercreate new user with email, name and password (No validation is done here, this is for demo purpose only). - GET
/user/{email}get specific user with given email. - PUT
/user/{email}update user with given email. - DELETE
/user/{email}Delete user with given email. - GET
/blogsGet all blogs. - GET
/blog/{post_id}Get blog with given ID. - POST
/blogsCreate new blog. - PUT
/blogs/{post_id}Update existing blog with given ID. - DELETE
/blogs/{post_id}Delete blog with given ID. - GET
/blogs/{post_id}/commentsGet all comments for given blogs with id = post_id. - POST
/blogs/{post_id}/commentsAdd new comment under the blog with id = post_id. - GET
/blogs/{post_id}/comments/{comment_id}Get all comments under a comment(id=comment_id) of a given blog(id=post_id). - POST
/blogs/{post_id}/comments/{parent_id}Create a comment under comment(id=comment_id) in blog (id=post_id). - PUT
/blogs/{post_id}/comments/{comment_id}Update root comment with id=comment_id under blog with id=post_id. - PUT
/blogs/{post_id}/comments/{parent_id}/{comment_id}Update nested comment(id=comment_id) under a comment(id=parent_id) with blog(id=post_id).
I choose Relational database, because the data was more like in relational fashion. One of the place where I could have used NoSQL database is for storing comments. But I have managed that using Relational database and retrieving using recursive queries. Main reason to choose Relational database is because the schema was fixed and have a proper structure. Relational databases also provide better consistency and integrity. This is my schema:
-
USERS
- email VARCHAR(50) PK
- password VARCHAR(20)
- full_name VARCHAR (250)
-
BLOG
- post_id VARCHAR(10) PK
- content LONGTEXT
- author VARCHAR(50) FK references USERS(email)
- created_on TIMESTAMP
- last_modified TIMESTAMP
-
COMMENTS
- comment_id VARCHAR(10) PK
- post_id VARCHAR(10) PK FK references BLOG(post_id)
- parent_id VARCHAR(10) PK
- content LONGTEXT
- author VARCHAR(50) FK references USERS(email)
- created_on TIMESTAMP
- last_modified TIMESTAMP
Note: All the id's are generated randomly and have length=10.
To retrieve all the comments under a given comment, I have used RECURSIVE query which is most suitable for hierarchical data.
I tried to handle following exceptions and validations:
- When Particular endpoint is not implemented it will give appropriate error message.
- Internal server errors. User will get approriate message.
- User already exists with given email.
- Blog Already exists.
- SQL exceptions (contraint violations).
For every error response, user will get appropriate message along with its HTTP status code.
This API can be improved further, but due to time constraint I have added only limited functionalities. For further API information I am adding POSTMAN collection in the repository.