Skip to content

A full-featured Blog API built with ASP.NET Core. It includes user authentication, CRUD operations for blog posts, a commenting system with nested replies, and the ability for users to like posts and comments.

License

Notifications You must be signed in to change notification settings

HoBaaMa/Blog-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📝 Blog API

A comprehensive ASP.NET Core Web API for a blog platform, featuring posts, comments, likes, and user authentication.


📋 Table of Contents


✨ Features

  • 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.

🧰 Technologies Used


🚀 Getting Started

🔧 Prerequisites

⚙️ Setup

  1. Clone the repository:
    git clone https://github.com/HoBaaMa/Blog-API.git
  2. Configure the database connection: Update the DefaultConnection string in appsettings.json with your SQL Server details.
  3. Apply migrations:
    dotnet ef database update
  4. Run the application:
    dotnet run
  5. Access Swagger UI: Navigate to https://localhost:{port}/swagger in your browser.

🌐 API Endpoints

Accounts

🔠 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

Blog Posts

🔠 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

Comments

🔠 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

🧱 Models

BlogPost

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; }
}

Comment

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; }
}

❗ Error Handling

  • Returns 400 Bad Request for invalid input.
  • Returns 401 Unauthorized for failed login attempts.
  • Returns 403 Forbidden for unauthorized actions.
  • Returns 404 Not Found if a resource does not exist.
  • Returns 500 Internal Server Error for unexpected server-side errors.

📦 Required NuGet Packages

📦 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

🪪 License

This project is licensed under the MIT License.

About

A full-featured Blog API built with ASP.NET Core. It includes user authentication, CRUD operations for blog posts, a commenting system with nested replies, and the ability for users to like posts and comments.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages