Autogen calendars
This commit is contained in:
104
lib/osuuspuutarha/season.ex
Normal file
104
lib/osuuspuutarha/season.ex
Normal file
@@ -0,0 +1,104 @@
|
||||
defmodule Osuuspuutarha.Season do
|
||||
@moduledoc """
|
||||
The Season context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Osuuspuutarha.Repo
|
||||
|
||||
alias Osuuspuutarha.Season.Calendar
|
||||
|
||||
@doc """
|
||||
Returns the list of calendars.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_calendars()
|
||||
[%Calendar{}, ...]
|
||||
|
||||
"""
|
||||
def list_calendars do
|
||||
Repo.all(Calendar)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single calendar.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Calendar does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_calendar!(123)
|
||||
%Calendar{}
|
||||
|
||||
iex> get_calendar!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_calendar!(id), do: Repo.get!(Calendar, id)
|
||||
|
||||
@doc """
|
||||
Creates a calendar.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_calendar(%{field: value})
|
||||
{:ok, %Calendar{}}
|
||||
|
||||
iex> create_calendar(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_calendar(attrs \\ %{}) do
|
||||
%Calendar{}
|
||||
|> Calendar.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a calendar.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_calendar(calendar, %{field: new_value})
|
||||
{:ok, %Calendar{}}
|
||||
|
||||
iex> update_calendar(calendar, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_calendar(%Calendar{} = calendar, attrs) do
|
||||
calendar
|
||||
|> Calendar.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a calendar.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_calendar(calendar)
|
||||
{:ok, %Calendar{}}
|
||||
|
||||
iex> delete_calendar(calendar)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_calendar(%Calendar{} = calendar) do
|
||||
Repo.delete(calendar)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking calendar changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_calendar(calendar)
|
||||
%Ecto.Changeset{data: %Calendar{}}
|
||||
|
||||
"""
|
||||
def change_calendar(%Calendar{} = calendar, attrs \\ %{}) do
|
||||
Calendar.changeset(calendar, attrs)
|
||||
end
|
||||
end
|
||||
19
lib/osuuspuutarha/season/calendar.ex
Normal file
19
lib/osuuspuutarha/season/calendar.ex
Normal file
@@ -0,0 +1,19 @@
|
||||
defmodule Osuuspuutarha.Season.Calendar do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "calendars" do
|
||||
field :is_picked_up, :boolean, default: false
|
||||
field :pickup_date, :date
|
||||
field :order_id, :id
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(calendar, attrs) do
|
||||
calendar
|
||||
|> cast(attrs, [:pickup_date, :is_picked_up])
|
||||
|> validate_required([:pickup_date, :is_picked_up])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user