A comprehensive ASP.NET Core Web API for a blog platform, featuring posts, comments, likes, and user authentication.
- ✨ Features
- 🧰 Technologies Used
- 🚀 Getting Started
- 🌐 API Endpoints
- 🧱 Model
- ❗ Error Handling
- 📦 Required NuGet Packages
- 🪪 License
- User Authentication: Secure registration and login functionality for users.
- Blog Post Management: Full CRUD (Create, Read, Update, Delete) operations for blog posts.
- Comment System: Users can comment on blog posts, with support for nested replies.
- Like Functionality: Users can like both blog posts and comments.
- Partial Updates: Supports JSON Patch for efficiently updating comments.
- Structured Logging: Utilizes Serilog for detailed and structured application logging.
- Clone the repository:
git clone https://github.com/HoBaaMa/Blog-API.git
- Configure the database connection:
Update the
DefaultConnectionstring inappsettings.jsonwith your SQL Server details. - Apply migrations:
dotnet ef database update
- Run the application:
dotnet run
- Access Swagger UI:
Navigate to
https://localhost:{port}/swaggerin your browser.
| 🔠 Method | 🌐 Endpoint | 📝 Description |
|---|---|---|
| ➕ POST | /api/accounts/register |
Register a new user |
| ➡️ POST | /api/accounts/login |
Log in a user |
| ⬅️ POST | /api/accounts/logout |
Log out a user |
| 🔠 Method | 🌐 Endpoint | 📝 Description |
|---|---|---|
| 🟢 GET | /api/blogposts |
Get all blog posts |
| 🔍 GET | /api/blogposts/{id} |
Get a blog post by ID |
| ➕ POST | /api/blogposts |
Create a new blog post |
| ♻️ PUT | /api/blogposts/{id} |
Update an existing blog post |
| ❌ DELETE | /api/blogposts/{id} |
Delete a blog post |
| 🔠 Method | 🌐 Endpoint | 📝 Description |
|---|---|---|
| 🟢 GET | /api/comments/blogpost/{id} |
Get all comments for a blog post |
| 🔍 GET | /api/comments/{id} |
Get a comment by ID |
| ➕ POST | /api/comments |
Create a new comment |
| 🩹 PATCH | /api/comments/{id} |
Partially update a comment |
| ❌ DELETE | /api/comments/{id} |
Delete a comment |
public class BlogPost
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public ICollection<Comment> Comments { get; set; }
public ICollection<Like> Likes { get; set; }
}public class Comment
{
public Guid Id { get; set; }
public string Content { get; set; }
public DateTime CreatedAt { get; set; }
public Guid BlogPostId { get; set; }
public BlogPost BlogPost { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public ICollection<Like> Likes { get; set; }
public Guid? ParentCommentId { get; set; }
public Comment ParentComment { get; set; }
public ICollection<Comment> Replies { get; set; }
}- Returns
400 Bad Requestfor invalid input. - Returns
401 Unauthorizedfor failed login attempts. - Returns
403 Forbiddenfor unauthorized actions. - Returns
404 Not Foundif a resource does not exist. - Returns
500 Internal Server Errorfor unexpected server-side errors.
| 📦 Package Name | 📝 Description |
|---|---|
| 📂 Microsoft.AspNetCore.Identity.EntityFrameworkCore | ASP.NET Core Identity provider for EF Core |
| 🩹 Microsoft.AspNetCore.JsonPatch | JSON Patch support |
| 🧩 Microsoft.AspNetCore.Mvc.NewtonsoftJson | Newtonsoft.Json support |
| 📂 Microsoft.EntityFrameworkCore | Entity Framework Core ORM |
| 🛢️ Microsoft.EntityFrameworkCore.SqlServer | SQL Server provider for EF Core |
| 🛠️ Microsoft.EntityFrameworkCore.Tools | EF Core CLI tools |
| 🧾 Serilog.AspNetCore | Serilog logging |
| ⚙️ Serilog.Settings.Configuration | Serilog configuration from appsettings.json |
| 📁 Serilog.Sinks.File | Serilog file sink |
| 📖 Swashbuckle.AspNetCore | Swagger / OpenAPI support |
This project is licensed under the MIT License.