add contexts
This commit is contained in:
		
							
								
								
									
										104
									
								
								lib/memex/contexts.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								lib/memex/contexts.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | ||||
| defmodule Memex.Contexts do | ||||
|   @moduledoc """ | ||||
|   The Contexts context. | ||||
|   """ | ||||
|  | ||||
|   import Ecto.Query, warn: false | ||||
|   alias Memex.Repo | ||||
|  | ||||
|   alias Memex.Contexts.Context | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of contexts. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> list_contexts() | ||||
|       [%Context{}, ...] | ||||
|  | ||||
|   """ | ||||
|   def list_contexts do | ||||
|     Repo.all(Context) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Gets a single context. | ||||
|  | ||||
|   Raises `Ecto.NoResultsError` if the Context does not exist. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_context!(123) | ||||
|       %Context{} | ||||
|  | ||||
|       iex> get_context!(456) | ||||
|       ** (Ecto.NoResultsError) | ||||
|  | ||||
|   """ | ||||
|   def get_context!(id), do: Repo.get!(Context, id) | ||||
|  | ||||
|   @doc """ | ||||
|   Creates a context. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> create_context(%{field: value}) | ||||
|       {:ok, %Context{}} | ||||
|  | ||||
|       iex> create_context(%{field: bad_value}) | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   def create_context(attrs \\ %{}) do | ||||
|     %Context{} | ||||
|     |> Context.changeset(attrs) | ||||
|     |> Repo.insert() | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Updates a context. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> update_context(context, %{field: new_value}) | ||||
|       {:ok, %Context{}} | ||||
|  | ||||
|       iex> update_context(context, %{field: bad_value}) | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   def update_context(%Context{} = context, attrs) do | ||||
|     context | ||||
|     |> Context.changeset(attrs) | ||||
|     |> Repo.update() | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Deletes a context. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> delete_context(context) | ||||
|       {:ok, %Context{}} | ||||
|  | ||||
|       iex> delete_context(context) | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   def delete_context(%Context{} = context) do | ||||
|     Repo.delete(context) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns an `%Ecto.Changeset{}` for tracking context changes. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> change_context(context) | ||||
|       %Ecto.Changeset{data: %Context{}} | ||||
|  | ||||
|   """ | ||||
|   def change_context(%Context{} = context, attrs \\ %{}) do | ||||
|     Context.changeset(context, attrs) | ||||
|   end | ||||
| end | ||||
							
								
								
									
										22
									
								
								lib/memex/contexts/context.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lib/memex/contexts/context.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| defmodule Memex.Contexts.Context do | ||||
|   use Ecto.Schema | ||||
|   import Ecto.Changeset | ||||
|  | ||||
|   @primary_key {:id, :binary_id, autogenerate: true} | ||||
|   @foreign_key_type :binary_id | ||||
|   schema "contexts" do | ||||
|     field :content, :string | ||||
|     field :tag, {:array, :string} | ||||
|     field :title, :string | ||||
|     field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] | ||||
|  | ||||
|     timestamps() | ||||
|   end | ||||
|  | ||||
|   @doc false | ||||
|   def changeset(context, attrs) do | ||||
|     context | ||||
|     |> cast(attrs, [:title, :content, :tag, :visibility]) | ||||
|     |> validate_required([:title, :content, :tag, :visibility]) | ||||
|   end | ||||
| end | ||||
		Reference in New Issue
	
	Block a user