Skip to content

neeraj2710/BlogHub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“ BlogHub β€” Full-Stack Blogging Platform

A full-stack blogging platform with a Vanilla JS + HTML/CSS frontend and a Spring Boot REST API backend. Authors can register, write posts, manage categories (admin only), and search/browse content seamlessly.

Java Spring Boot MySQL JavaScript HTML5 CSS3 Maven


πŸ“š Table of Contents


πŸ›  Tech Stack

Frontend

Layer Technology
Markup HTML5
Styling CSS3 (Custom Properties, Flexbox, Grid)
Scripting Vanilla JavaScript (ES6+)
HTTP Client Fetch API
Routing Client-side (hash-based)
State Session Cookie (JSESSIONID)

Backend

Layer Technology
Framework Spring Boot 4.0.4
Language Java 25
Database MySQL 8
ORM Spring Data JPA / Hibernate
Auth Session-based (HttpSession)
Validation Jakarta Bean Validation
Build Tool Maven

✨ Features

  • πŸ” User registration & session-based login / logout
  • πŸ“ Full CRUD for blog posts with pagination, sorting, and full-text search
  • πŸ—‚οΈ Category management β€” read for all users, write for ADMIN only
  • πŸ‘€ Author profile management with role-based access control
  • 🌐 Interactive frontend β€” no framework, pure HTML/CSS/JS
  • ⚠️ Global exception handling with structured error responses

🧩 Use Case Diagram

graph TD
    Guest([Guest])
    User([Authenticated User])
    Admin([Admin])

    Guest --> UC1[Register]
    Guest --> UC2[Login]
    Guest --> UC3[Browse Posts]
    Guest --> UC4[Search Posts]
    Guest --> UC5[View Categories]

    User --> UC2
    User --> UC6[Logout]
    User --> UC7[View My Profile]
    User --> UC8[Update My Profile]
    User --> UC9[Create Post]
    User --> UC10[Update Own Post]
    User --> UC11[Delete Own Post]
    User --> UC12[View My Posts]
    User --> UC3
    User --> UC4
    User --> UC5

    Admin --> UC13[Create Category]
    Admin --> UC14[Update Category]
    Admin --> UC15[Delete Category]
    Admin --> UC16[Delete Any Post]
    Admin --> UC17[Delete Any User]
    Admin --> UC7
    Admin --> UC8
    Admin --> UC9
    Admin --> UC3
Loading

πŸ—ƒοΈ ER Diagram

erDiagram
    AUTHORS {
        BIGINT id PK
        VARCHAR name
        VARCHAR email UK
        VARCHAR password
        VARCHAR role
        VARCHAR about
    }

    CATEGORIES {
        BIGINT id PK
        VARCHAR cat_name UK
        VARCHAR description
    }

    POSTS {
        BIGINT id PK
        VARCHAR title
        LONGTEXT content
        DATETIME created_at
        BIGINT author_id FK
        BIGINT category_id FK
    }

    AUTHORS ||--o{ POSTS : "writes"
    CATEGORIES ||--o{ POSTS : "contains"
Loading

πŸ”„ Data Flow Diagram

Level 0 β€” Context Diagram

graph LR
    Client([Client / Browser]) -- HTTP Request --> BlogHub[BlogHub REST API]
    BlogHub -- HTTP Response --> Client
    BlogHub -- CRUD --> DB[(MySQL Database)]
Loading

Level 1 β€” Internal Data Flow

flowchart TD
    Client([Client])

    subgraph Frontend
        FE[HTML / CSS / JS Pages]
        FA[Fetch API Calls]
    end

    subgraph API Layer
        AC[AuthController\n/api/auth]
        UC[AuthorController\n/api/users]
        CC[CategoryController\n/api/categories]
        PC[PostController\n/api/posts]
    end

    subgraph Middleware
        SI[SessionAuthInterceptor\nChecks session & role]
    end

    subgraph Service Layer
        AS[AuthService]
        US[AuthorService]
        CS[CategoryService]
        PS[PostService]
    end

    subgraph Repository Layer
        AR[AuthorRepository]
        CR[CategoryRepository]
        PR[PostRepository]
    end

    DB[(MySQL\nbloghub_db)]

    Client --> FE --> FA
    FA -- "POST /register\nPOST /login" --> AC
    FA -- "All other /api/**" --> SI
    SI -- "401 if no session\n403 if not ADMIN" --> Client
    SI --> UC & CC & PC

    AC --> AS
    UC --> US
    CC --> CS
    PC --> PS

    AS --> AR
    US --> AR
    CS --> CR
    PS --> PR & AR & CR

    AR & CR & PR --> DB
Loading

Request-Response Flow (Login Example)

sequenceDiagram
    participant B as Browser (JS)
    participant AC as AuthController
    participant AS as AuthService
    participant AR as AuthorRepository
    participant DB as MySQL

    B->>AC: POST /api/auth/login {email, password}
    AC->>AS: login(LoginRequestDto, HttpSession)
    AS->>AR: findByEmail(email)
    AR->>DB: SELECT * FROM authors WHERE email=?
    DB-->>AR: Author row
    AR-->>AS: Optional<Author>
    AS-->>AS: Validate password match
    AS-->>AS: session.setAttribute(userId, role, ...)
    AS-->>AC: AuthResponseDto
    AC-->>B: 200 OK + Set-Cookie: JSESSIONID
Loading

πŸ“ Directory Structure

BlogHub/
β”œβ”€β”€ pom.xml
β”œβ”€β”€ .mvn/
β”‚   └── wrapper/
β”‚       └── maven-wrapper.properties
└── src/
    β”œβ”€β”€ main/
    β”‚   β”œβ”€β”€ java/com/mardox/bloghub/
    β”‚   β”‚   β”œβ”€β”€ BlogHubApplication.java             # Entry point
    β”‚   β”‚   β”œβ”€β”€ config/
    β”‚   β”‚   β”‚   └── WebConfig.java                  # Registers interceptor
    β”‚   β”‚   β”œβ”€β”€ controller/
    β”‚   β”‚   β”‚   β”œβ”€β”€ AuthController.java              # /api/auth
    β”‚   β”‚   β”‚   β”œβ”€β”€ AuthorController.java            # /api/users
    β”‚   β”‚   β”‚   β”œβ”€β”€ CategoryController.java          # /api/categories
    β”‚   β”‚   β”‚   └── PostController.java              # /api/posts
    β”‚   β”‚   β”œβ”€β”€ dto/
    β”‚   β”‚   β”‚   β”œβ”€β”€ AuthResponseDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ AuthorResponseDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ AuthorUpdateDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ CategoryRequestDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ CategoryResponseDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ CategoryUpdateDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ LoginRequestDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ PostRequestDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ PostResponseDto.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ PostUpdateDto.java
    β”‚   β”‚   β”‚   └── RegisterRequestDto.java
    β”‚   β”‚   β”œβ”€β”€ entity/
    β”‚   β”‚   β”‚   β”œβ”€β”€ Author.java                     # authors table
    β”‚   β”‚   β”‚   β”œβ”€β”€ Category.java                   # categories table
    β”‚   β”‚   β”‚   └── Post.java                       # posts table
    β”‚   β”‚   β”œβ”€β”€ exception/
    β”‚   β”‚   β”‚   β”œβ”€β”€ ErrorResponse.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ GlobalExceptionHandler.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ ResouceAlreadyExistsException.java
    β”‚   β”‚   β”‚   └── ResourceNotFoundException.java
    β”‚   β”‚   β”œβ”€β”€ interceptor/
    β”‚   β”‚   β”‚   └── SessionAuthInterceptor.java     # Auth guard
    β”‚   β”‚   β”œβ”€β”€ repository/
    β”‚   β”‚   β”‚   β”œβ”€β”€ AuthorRepository.java
    β”‚   β”‚   β”‚   β”œβ”€β”€ CategoryRepository.java
    β”‚   β”‚   β”‚   └── PostRepository.java
    β”‚   β”‚   └── service/
    β”‚   β”‚       β”œβ”€β”€ AuthService.java
    β”‚   β”‚       β”œβ”€β”€ AuthorService.java
    β”‚   β”‚       β”œβ”€β”€ CategoryService.java
    β”‚   β”‚       └── PostService.java
    β”‚   └── resources/
    β”‚       β”œβ”€β”€ static/                             # Frontend (HTML/CSS/JS)
    β”‚       β”‚   β”œβ”€β”€ index.html
    β”‚       β”‚   β”œβ”€β”€ css/
    β”‚       β”‚   └── js/
    β”‚       └── application.properties
    └── test/
        └── java/com/mardox/bloghub/
            └── BlogHubApplicationTests.java

πŸ“‘ API Reference

Base URL: http://localhost:8082

All endpoints except /api/auth/** require an active session (cookie JSESSIONID).


πŸ”‘ Auth β€” /api/auth

Method Endpoint Auth Required Description
POST /api/auth/register ❌ Register a new author
POST /api/auth/login ❌ Login, starts session
POST /api/auth/logout βœ… Invalidate session
GET /api/auth/me βœ… Get current logged-in user

πŸ‘€ Authors β€” /api/users

Method Endpoint Auth Required Role Description
GET /api/users βœ… Any List all authors
GET /api/users/{id} βœ… Any Get author by ID
PUT /api/users/{id} βœ… Self/Admin Update author profile
DELETE /api/users/{id} βœ… Self/Admin Delete author

πŸ—‚οΈ Categories β€” /api/categories

Method Endpoint Auth Required Role Description
GET /api/categories βœ… Any List all categories
GET /api/categories/{id} βœ… Any Get category by ID
POST /api/categories βœ… ADMIN Create category
PUT /api/categories/{id} βœ… ADMIN Update category
DELETE /api/categories/{id} βœ… ADMIN Delete category

πŸ“ Posts β€” /api/posts

Method Endpoint Auth Required Role Description
GET /api/posts βœ… Any Paginated posts (page, size, sortBy, sortDir)
GET /api/posts/getAll βœ… Any All posts or search (?term=keyword)
GET /api/posts/{id} βœ… Any Get post by ID
GET /api/posts/my-post βœ… Any Get current user's posts
POST /api/posts βœ… Any Create post
PUT /api/posts/{id} βœ… Self/Admin Update post
DELETE /api/posts/{id} βœ… Self/Admin Delete post

⚠️ Error Responses

HTTP Status Scenario
400 Validation failure
401 No active session
403 Insufficient role (non-admin)
404 Resource not found / already exists
500 Unhandled server error
{
  "statusCode": 404,
  "errorMessage": "Author not found with id: 5"
}

πŸš€ Getting Started

Prerequisites

  • Java 21+
  • Maven 3.9+
  • MySQL 8 running locally

Setup

1. Clone the repository

git clone https://github.com/neeraj2710/BlogHub.git
cd BlogHub

2. Configure the database

Edit src/main/resources/application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/bloghub_db?createDatabaseIfNotExist=true
spring.datasource.username=your_mysql_username
spring.datasource.password=your_mysql_password

3. Run the application

./mvnw spring-boot:run

4. Open in browser

http://localhost:8082

The database schema is auto-created by Hibernate on first run (ddl-auto=update).


πŸ§ͺ Quick Test (cURL)

# Register
curl -c cookies.txt -X POST http://localhost:8082/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice","email":"alice@example.com","password":"pass123","about":"Developer"}'

# Login
curl -c cookies.txt -X POST http://localhost:8082/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"alice@example.com","password":"pass123"}'

# Create a post (requires a category to exist first)
curl -b cookies.txt -X POST http://localhost:8082/api/posts \
  -H "Content-Type: application/json" \
  -d '{"title":"Hello World","content":"My first post.","categoryId":1}'

πŸ”’ Session Management

Attribute Value
Session timeout 30 minutes
Cookie name JSESSIONID
HTTP-only Yes
Same-site Lax

Session attributes set on login: userId, userName, userEmail, userRole


πŸ‘₯ Roles & Permissions

Role Permissions
USER CRUD own posts Β· Read-only on categories Β· Manage own profile
ADMIN All USER permissions + manage all categories Β· Delete any post or user

Made with ❀️ by Neeraj

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors