Simple Todo App - Phoenix Typing CST Test
Loading…
Simple Todo App — Phoenix Code
Demonstrates a Phoenix controller and view for managing Todo items with a simple JSON API.
# lib/my_app_web/controllers/todo_controller.ex
defmodule MyAppWeb.TodoController do
use MyAppWeb, :controller
alias MyApp.Todos
def index(conn, _params) do
todos = Todos.list_todos()
json(conn, todos)
end
def create(conn, %{"todo" => todo_params}) do
{:ok, todo} = Todos.create_todo(todo_params)
json(conn, todo)
end
end
# lib/my_app/todos/todos.ex
defmodule MyApp.Todos do
alias MyApp.Repo
alias MyApp.Todos.Todo
def list_todos do
Repo.all(Todo)
end
def create_todo(attrs) do
%Todo{}
|> Todo.changeset(attrs)
|> Repo.insert()
end
end
# lib/my_app/todos/todo.ex
defmodule MyApp.Todos.Todo do
use Ecto.Schema
import Ecto.Changeset
schema "todos" do
field :title, :string
field :completed, :boolean, default: false
timestamps()
end
def changeset(todo, attrs) do
todo
|> cast(attrs, [:title, :completed])
|> validate_required([:title])
end
endPhoenix Language Guide
Phoenix is a high-performance, functional web framework written in Elixir, designed for building scalable and maintainable web applications and real-time systems.
Primary Use Cases
- ▸Real-time web applications (chat, notifications, dashboards)
- ▸RESTful APIs and JSON backends
- ▸Fault-tolerant, scalable systems
- ▸High-concurrency microservices
- ▸Rapid development with functional paradigms
Notable Features
- ▸Real-time communication via Channels
- ▸Ecto for database access and migrations
- ▸MVC architecture with functional patterns
- ▸Hot code reloading for rapid development
- ▸PubSub system for scalable messaging
Origin & Creator
Created by Chris McCord and released in 2014, maintained by the Phoenix Core Team and Elixir community.
Industrial Note
Phoenix is widely used for high-concurrency web applications, real-time dashboards, APIs, chat systems, and fault-tolerant systems where low-latency and scalability are critical.