Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AppDrop Backend Assignment API (Gin + PostgreSQL + GORM)

REST API for managing mobile app pages and widgets.

Tech stack

  • Go
  • Gin
  • PostgreSQL
  • GORM

Project structure

.
|-- .env.example
|-- .gitignore
|-- README.md
|-- go.mod
|-- cmd
|   |-- api
|   |   `-- main.go
|   `-- migrate
|       `-- main.go
`-- internal
    |-- config
    |   `-- config.go
    |-- database
    |   `-- database.go
    |-- handlers
    |   |-- helpers.go
    |   |-- page_handler.go
    |   `-- widget_handler.go
    |-- middleware
    |   |-- logging.go
    |   `-- recovery.go
    |-- migrations
    |   |-- migrate.go
    |   `-- sql
    |       `-- 001_init.sql
    |-- models
    |   |-- page.go
    |   `-- widget.go
    |-- response
    |   `-- response.go
    |-- router
    |   `-- router.go
    `-- validation
        `-- validation.go

Environment variables

Copy .env.example and update values:

PORT=8080
DATABASE_URL=postgres://postgres:postgres@localhost:5432/appdrop?sslmode=disable
AUTO_MIGRATE=true
GIN_MODE=debug

Database setup

  1. Create database:
CREATE DATABASE appdrop;
  1. Run migrations:
go run ./cmd/migrate

001_init.sql includes:

  • pages table with unique route
  • partial unique index enforcing single home page (is_home = true)
  • widgets table with config JSONB
  • PostgreSQL enum type for widgets (widget_type: banner, product_grid, text, image, spacer)
  • foreign key cascade delete (widgets.page_id -> pages.id ON DELETE CASCADE)

Run API server

go run ./cmd/api

Server starts on http://localhost:8080.

Error format

All errors are returned as:

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Page route already exists"
  }
}

Validation rules implemented

  • Page route is unique
  • Page name is required and non-empty
  • Page route is required and non-empty
  • Only one page can have is_home = true
  • Home page cannot be deleted
  • Widget type must be one of:
    • banner
    • product_grid
    • text
    • image
    • spacer
  • Widget config is optional, but if provided it must be valid JSON object

API endpoints

Pages

  • GET /pages
  • GET /pages/:id
  • POST /pages
  • PUT /pages/:id
  • DELETE /pages/:id

Widgets

  • POST /pages/:id/widgets
  • PUT /widgets/:id
  • DELETE /widgets/:id
  • POST /pages/:id/widgets/reorder

Curl examples

1) Create page

curl -X POST http://localhost:8080/pages \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Home",
    "route": "/home",
    "is_home": true
  }'

2) List pages

curl http://localhost:8080/pages

3) Get one page with widgets

curl http://localhost:8080/pages/<PAGE_ID>

4) Update page

curl -X PUT http://localhost:8080/pages/<PAGE_ID> \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Homepage",
    "route": "/home",
    "is_home": true
  }'

5) Add widget to a page

curl -X POST http://localhost:8080/pages/<PAGE_ID>/widgets \
  -H "Content-Type: application/json" \
  -d '{
    "type": "banner",
    "position": 1,
    "config": {
      "title": "Sale Banner",
      "image_url": "https://example.com/banner.jpg"
    }
  }'

6) Update widget

curl -X PUT http://localhost:8080/widgets/<WIDGET_ID> \
  -H "Content-Type: application/json" \
  -d '{
    "type": "image",
    "position": 2,
    "config": {
      "url": "https://example.com/image.png"
    }
  }'

7) Reorder widgets (array of widget IDs in new order)

curl -X POST http://localhost:8080/pages/<PAGE_ID>/widgets/reorder \
  -H "Content-Type: application/json" \
  -d '[
    "11111111-1111-1111-1111-111111111111",
    "22222222-2222-2222-2222-222222222222",
    "33333333-3333-3333-3333-333333333333"
  ]'

8) Delete widget

curl -X DELETE http://localhost:8080/widgets/<WIDGET_ID>

9) Delete page

curl -X DELETE http://localhost:8080/pages/<PAGE_ID>

Status codes used

  • 200 OK
  • 201 Created
  • 400 Bad Request
  • 404 Not Found
  • 409 Conflict
  • 500 Internal Server Error

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages