API Workshop
#1
Agenda
- 1. Dababase
- 2. Models
- 3. Operations
- 4. API
Database
- 1. Migrations
- 2. Schema
- 3. Seeds
- 4. Views
- 5. Functions
- 6. Triggers
Migrations
- Define the steps needed how to build the database schema
Schema
- Current state of database schema after applying all migrations
Seeds
- How to prefill the database
Views
- SQL SELECT Commands which can be used as tables
Functions
- Code snippets which can be executed manually or by triggers
Triggers
- Define events on which database functions should be called
Models
- 1. Relationships
- 2. Callbacks
- 3. Scopes
- 4. Validations
- 5. Small helpers
Relationships
- How is the relationship of current model to other models
- belongs_to (parent)
- has_one (1:1 relationship)
- has_many (1:n relationship)
Callbacks
- Defines what to do after certain events
- before/after
- validation
- save
- create/update/destroy
- commit/rollback
Scopes
- Define SQL queries and filters
- Read only
Validations
- Presence
- Uniqueness
- Min/Max
- Custom
Operations
- 1. Not part of rails
- 2. Initalize
- 3. Call
- 4. Helpers
Not part of rails
- Business logic
- Usually this is part of the Model, Controller or API
- Files got too huge and hard to mange so we separated the this
Initialize
- Defines which context and parameters are needed to execute
- Repositories* (soon Models)
- Context
- Helpers
Call
- Executes the operation
- Parameters
- Parameter Defauls
- Uses helpers if logic is complex
Helpers
- Helps to split logic into smaller easy to understand chunks
- Parameters
API
- 1. Helpers
- 2. Entry Point
- 3. Endpoint
- 4. Documentation
Helpers
- Small code snippets/definitions which are shared
and accessible for all Endpoints - Parameters
- Reusable logic
Entry point
- Global definitions of our API
- Which versions exist
- Which endpoints are present
- Global documentation
Endpoint
- 1. Parameters
- 2. Helpers
- 3. Resource
Helpers
- Small code snippets/definitions which are shared
and accessible for all Resources in SAME FILE - Reusable logic
Parameters
- Defines which parameters could be present when calling the endpoint
- Validates data type
- Ignores not mentioned parameters
Actions
- Defines which endpoints are available
- Path (/absences, ...)
- Method (get, post, ...)
Resource Workflow
- Authenticate
- Fetch context
- Check for errors
- Business logic
- Return data
Documentation
- 1. Introduction
- 2. Endpoint Description
- 3. Action Description
- 4. Entities
- 5. Parameters
Introduction
- Global description shown above all resources
Endpoint Description
- Description on top of an endpoint
Resource Description
- Implementation notes for each resource
Entities
- Use for sample responses (and/or parameters)
Parameters
- Add a description for each parameter
Links
Rails Links
API Docs Examples