Skip to content

Vahe-OPT/task-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Task API

A REST API built in Go for managing tasks with user authentication, search, filtering, pagination, and business rule enforcement.

Tech Stack

  • Go — core language
  • chi — HTTP router
  • golang-jwt/jwt — JWT authentication
  • google/uuid — unique task IDs
  • In-memory store — no database required

Project Structure

task-api/
├── main.go               # Entry point, route registration
├── models/
│   ├── task.go           # Task struct and Status constants
│   └── user.go           # User struct
├── handlers/
│   └── task.go           # HTTP handlers (Login, CRUD)
├── middleware/
│   └── auth.go           # JWT validation middleware
├── store/
│   ├── task_store.go     # In-memory data layer + business logic
│   └── task_store_test.go # Unit tests
└── go.mod

Getting Started

Install dependencies

go mod tidy

Run the server

go run main.go

Server runs at http://localhost:8000

Run tests

go test ./store/... -v

API Endpoints

Authentication

Method Endpoint Description Auth
POST /login Login and get JWT token No

Tasks

Method Endpoint Description Auth
POST /tasks Create a new task Yes
GET /tasks List all tasks Yes
GET /tasks/{id} Get a single task Yes
PUT /tasks/{id} Update a task Yes
DELETE /tasks/{id} Delete a task Yes

Task Model

{
  "ID": "uuid",
  "Title": "Fix login bug",
  "Description": "The login page crashes on mobile",
  "Status": "TODO",
  "UserId": "1",
  "CreatedAt": "2026-04-07T10:00:00Z",
  "UpdatedAt": "2026-04-07T11:00:00Z"
}

Status values: TODO | IN_PROGRESS | DONE

Usage Examples

1. Login

POST /login
Content-Type: application/json

{
  "username": "john",
  "password": "1234"
}

Response:

{
  "token": "eyJhbGciOiJIUzI1NiIs..."
}

2. Create a Task

POST /tasks
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "Fix login bug",
  "description": "Crashes on mobile",
  "status": "TODO"
}

3. List Tasks with Filter & Pagination

GET /tasks?status=TODO&q=fix&page=1&limit=5
Authorization: Bearer <token>
Query Param Description Example
status Filter by status ?status=TODO
q Search in title/description ?q=bug
page Page number (default: 1) ?page=2
limit Items per page (default: 10) ?limit=5

4. Update a Task

PUT /tasks/{id}
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "Fix login bug",
  "description": "Fixed on mobile",
  "status": "DONE"
}

5. Delete a Task

DELETE /tasks/{id}
Authorization: Bearer <token>

Business Rules

  • Max 3 IN_PROGRESS tasks per user — attempting to create or update a 4th task to IN_PROGRESS returns 400 Bad Request
  • Task ownership — users can only view, update, or delete their own tasks. Accessing another user's task returns 403 Forbidden
  • Token expiry — JWT tokens expire after 24 hours

Test Credentials

username: john
password: 1234

Unit Tests

Test What it covers
TestCreateTask Task is created with a valid ID
TestCreateTask_3InProgressLimit 4th IN_PROGRESS task is rejected
TestGetTask_NotFound Returns error for non-existent task
TestUpdateTask_StatusChange Blocks status change when limit is hit
TestDeleteTask Task is removed from the store
TestGetAllTasks_Filter Returns only tasks matching the filter
TestGetAllTasks_Pagination Returns correct page slice

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages