Skip to content

shohel01716/task-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Task Management API

A production-ready RESTful API for task management built with Go, Gin, GORM, and PostgreSQL. Features JWT authentication, comprehensive testing, and interactive Swagger documentation.

πŸš€ Features

  • βœ… Complete CRUD operations for tasks
  • βœ… JWT Authentication with secure password hashing (bcrypt)
  • βœ… User Management (register, login, profile)
  • βœ… Advanced Querying (filtering, pagination, search)
  • βœ… Task Priorities (low, medium, high) & due dates
  • βœ… Unit Tests with comprehensive coverage
  • βœ… Swagger Documentation (interactive API explorer)
  • βœ… CORS Enabled for frontend integration
  • βœ… Standardized JSON responses
  • βœ… Soft Deletes with GORM

πŸ›  Tech Stack

Component Technology
Language Go 1.23+
Framework Gin Web Framework
ORM GORM
Database PostgreSQL 12+
Authentication JWT (golang-jwt/jwt)
Password bcrypt
Documentation Swagger/OpenAPI
Testing testify

πŸ“ Project Structure

task-app/
β”œβ”€β”€ main.go                    # Entry point with Swagger annotations
β”œβ”€β”€ config/
β”‚   └── database.go           # GORM database configuration
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ task.go              # Task model with GORM tags
β”‚   β”œβ”€β”€ user.go              # User model with bcrypt
β”‚   └── response.go          # Standardized API responses
β”œβ”€β”€ handlers/
β”‚   β”œβ”€β”€ task_handler.go      # Task CRUD + filtering/search
β”‚   └── auth_handler.go      # Register/Login/Profile
β”œβ”€β”€ middleware/
β”‚   └── auth.go              # JWT authentication middleware
β”œβ”€β”€ migrations/
β”‚   └── init.sql             # Initial database schema
β”œβ”€β”€ docs/                     # Auto-generated Swagger docs
β”œβ”€β”€ go.mod                    # Go dependencies
└── README.md

⚑ Quick Start

Prerequisites

  • Go 1.23+
  • PostgreSQL 12+
  • psql CLI tool

Installation

# 1. Clone/Navigate to project
cd /Users/shohel/goProjects/task-app

# 2. Install dependencies
go mod download

# 3. Create database
psql -U shohel -c "CREATE DATABASE taskdb;"

# 4. Run migrations (GORM auto-migrates on startup)
# Tables: tasks, users (created automatically)

# 5. Start server
go run main.go

Server runs on: http://localhost:8086
Swagger UI: http://localhost:8086/api/v1/swagger/index.html

πŸ“š API Endpoints

Authentication (Public)

Method Endpoint Description
POST /api/v1/register Create new user account
POST /api/v1/login Login and get JWT token

User (Protected)

Method Endpoint Description
GET /api/v1/profile Get current user profile

Tasks (Protected - Requires JWT)

Method Endpoint Description
POST /api/v1/tasks Create new task
GET /api/v1/tasks Get all tasks (with filters)
GET /api/v1/tasks/:id Get task by ID
PUT /api/v1/tasks/:id Update task
DELETE /api/v1/tasks/:id Delete task (soft delete)

Query Parameters

GET /api/v1/tasks supports:

  • ?status=pending - Filter by status (pending, in_progress, completed)
  • ?search=keyword - Search in title/description
  • ?page=1&limit=10 - Pagination (default: page=1, limit=10)

πŸ” Authentication

1. Register

curl -X POST http://localhost:8086/api/v1/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john",
    "email": "john@example.com",
    "password": "password123",
    "full_name": "John Doe"
  }'

Response:

{
  "status": "success",
  "message": "User registered successfully",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIs...",
    "user": {
      "ID": 1,
      "username": "john",
      "email": "john@example.com",
      "full_name": "John Doe"
    }
  }
}

2. Use Token

Add JWT token to Authorization header:

curl http://localhost:8086/api/v1/tasks \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

πŸ“ Task Examples

Create Task

curl -X POST http://localhost:8086/api/v1/tasks \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "title": "Complete API",
    "description": "Finish task management API",
    "status": "in_progress",
    "priority": "high",
    "due_date": "2025-12-31T23:59:59Z"
  }'

Filter & Search

# Get pending tasks
curl "http://localhost:8086/api/v1/tasks?status=pending" \
  -H "Authorization: Bearer YOUR_TOKEN"

# Search tasks
curl "http://localhost:8086/api/v1/tasks?search=API" \
  -H "Authorization: Bearer YOUR_TOKEN"

# Paginate (page 2, 20 items)
curl "http://localhost:8086/api/v1/tasks?page=2&limit=20" \
  -H "Authorization: Bearer YOUR_TOKEN"

πŸ§ͺ Testing

# Run all tests
go test ./handlers/... -v

# Run specific test
go test ./handlers/... -run TestRegister -v

# Test coverage
go test ./handlers/... -cover

Test Database: taskdb_test (auto-created)

πŸ“– Swagger Documentation

Interactive API Explorer: http://localhost:8086/api/v1/swagger/index.html

Features:

  • Try all endpoints directly from browser
  • Automatic request/response examples
  • JWT authentication support
  • Model schemas

Regenerate Swagger Docs

# Install swag CLI
go install github.com/swaggo/swag/cmd/swag@latest

# Generate docs
~/go/bin/swag init

πŸ”§ Configuration

Database

Edit main.go:

dbConfig := config.DatabaseConfig{
    Host:     "localhost",
    Port:     5432,
    User:     "shohel",
    Password: "",        // Your password
    DBName:   "taskdb",
}

JWT Secret

Edit middleware/auth.go:

var JWTSecret = []byte("your-secret-key-change-this-in-production")

Server Port

Edit main.go:

port := ":8086"  // Change to desired port

πŸ“Š Database Schema

Tasks Table

id          SERIAL PRIMARY KEY
title       VARCHAR(255) NOT NULL
description TEXT
status      VARCHAR(50) DEFAULT 'pending'
priority    VARCHAR(20) DEFAULT 'medium'
due_date    TIMESTAMP
created_at  TIMESTAMP
updated_at  TIMESTAMP
deleted_at  TIMESTAMP  -- Soft delete

Users Table

id          SERIAL PRIMARY KEY
username    VARCHAR(100) UNIQUE NOT NULL
email       VARCHAR(255) UNIQUE NOT NULL
password    VARCHAR(255) NOT NULL  -- bcrypt hashed
full_name   VARCHAR(255)
created_at  TIMESTAMP
updated_at  TIMESTAMP
deleted_at  TIMESTAMP

🎯 Task Status & Priority

Status: pending | in_progress | completed
Priority: low | medium | high

πŸ“„ License

MIT License - Free for educational and commercial use.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages