95 lines
1.6 KiB
Elixir
95 lines
1.6 KiB
Elixir
defmodule Aggiedit.Uploads do
|
|
@moduledoc """
|
|
The Uploads context.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
alias Aggiedit.Repo
|
|
|
|
alias Aggiedit.Uploads.Upload
|
|
|
|
@doc """
|
|
Returns the list of uploads.
|
|
|
|
## Examples
|
|
|
|
iex> list_uploads()
|
|
[%Upload{}, ...]
|
|
|
|
"""
|
|
def list_uploads do
|
|
Repo.all(Upload)
|
|
end
|
|
|
|
@doc """
|
|
Gets a single upload.
|
|
|
|
Raises `Ecto.NoResultsError` if the Upload does not exist.
|
|
|
|
## Examples
|
|
|
|
iex> get_upload!(123)
|
|
%Upload{}
|
|
|
|
iex> get_upload!(456)
|
|
** (Ecto.NoResultsError)
|
|
|
|
"""
|
|
def get_upload!(id), do: Repo.get!(Upload, id)
|
|
|
|
def create_upload(attrs, user) do
|
|
%Upload{}
|
|
|> Repo.preload(:user)
|
|
|> Upload.changeset(attrs)
|
|
|> Ecto.Changeset.put_assoc(:user, user)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
@doc """
|
|
Updates a upload.
|
|
|
|
## Examples
|
|
|
|
iex> update_upload(upload, %{field: new_value})
|
|
{:ok, %Upload{}}
|
|
|
|
iex> update_upload(upload, %{field: bad_value})
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def update_upload(%Upload{} = upload, attrs) do
|
|
upload
|
|
|> Upload.changeset(attrs)
|
|
|> Repo.update()
|
|
end
|
|
|
|
@doc """
|
|
Deletes a upload.
|
|
|
|
## Examples
|
|
|
|
iex> delete_upload(upload)
|
|
{:ok, %Upload{}}
|
|
|
|
iex> delete_upload(upload)
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
"""
|
|
def delete_upload(%Upload{} = upload) do
|
|
Repo.delete(upload)
|
|
end
|
|
|
|
@doc """
|
|
Returns an `%Ecto.Changeset{}` for tracking upload changes.
|
|
|
|
## Examples
|
|
|
|
iex> change_upload(upload)
|
|
%Ecto.Changeset{data: %Upload{}}
|
|
|
|
"""
|
|
def change_upload(%Upload{} = upload, attrs \\ %{}) do
|
|
Upload.changeset(upload, attrs)
|
|
end
|
|
end
|