create gists

This commit is contained in:
zack 2024-10-26 15:01:33 -04:00
parent 79a17290d5
commit 43a8412f06
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
90 changed files with 1777 additions and 2107 deletions

104
lib/zoeyscomputer/gists.ex Normal file
View file

@ -0,0 +1,104 @@
defmodule Zoeyscomputer.Gists do
@moduledoc """
The Gists context.
"""
import Ecto.Query, warn: false
alias Zoeyscomputer.Repo
alias Zoeyscomputer.Gists.Gist
@doc """
Returns the list of gists.
## Examples
iex> list_gists()
[%Gist{}, ...]
"""
def list_gists do
Repo.all(Gist)
end
@doc """
Gets a single gist.
Raises `Ecto.NoResultsError` if the Gist does not exist.
## Examples
iex> get_gist!(123)
%Gist{}
iex> get_gist!(456)
** (Ecto.NoResultsError)
"""
def get_gist!(id), do: Repo.get!(Gist, id)
@doc """
Creates a gist.
## Examples
iex> create_gist(%{field: value})
{:ok, %Gist{}}
iex> create_gist(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_gist(attrs \\ %{}) do
%Gist{}
|> Gist.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a gist.
## Examples
iex> update_gist(gist, %{field: new_value})
{:ok, %Gist{}}
iex> update_gist(gist, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_gist(%Gist{} = gist, attrs) do
gist
|> Gist.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a gist.
## Examples
iex> delete_gist(gist)
{:ok, %Gist{}}
iex> delete_gist(gist)
{:error, %Ecto.Changeset{}}
"""
def delete_gist(%Gist{} = gist) do
Repo.delete(gist)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking gist changes.
## Examples
iex> change_gist(gist)
%Ecto.Changeset{data: %Gist{}}
"""
def change_gist(%Gist{} = gist, attrs \\ %{}) do
Gist.changeset(gist, attrs)
end
end

View file

@ -0,0 +1,18 @@
defmodule Zoeyscomputer.Gists.Gist do
use Ecto.Schema
import Ecto.Changeset
schema "gists" do
field :code, :string
field :lang, :string
timestamps(type: :utc_datetime)
end
@doc false
def changeset(gist, attrs) do
gist
|> cast(attrs, [:code, :lang])
|> validate_required([:code, :lang])
end
end