add notes
This commit is contained in:
		
							
								
								
									
										104
									
								
								lib/memex/notes.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								lib/memex/notes.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | ||||
| defmodule Memex.Notes do | ||||
|   @moduledoc """ | ||||
|   The Notes context. | ||||
|   """ | ||||
|  | ||||
|   import Ecto.Query, warn: false | ||||
|   alias Memex.Repo | ||||
|  | ||||
|   alias Memex.Notes.Note | ||||
|  | ||||
|   @doc """ | ||||
|   Returns the list of notes. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> list_notes() | ||||
|       [%Note{}, ...] | ||||
|  | ||||
|   """ | ||||
|   def list_notes do | ||||
|     Repo.all(Note) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Gets a single note. | ||||
|  | ||||
|   Raises `Ecto.NoResultsError` if the Note does not exist. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> get_note!(123) | ||||
|       %Note{} | ||||
|  | ||||
|       iex> get_note!(456) | ||||
|       ** (Ecto.NoResultsError) | ||||
|  | ||||
|   """ | ||||
|   def get_note!(id), do: Repo.get!(Note, id) | ||||
|  | ||||
|   @doc """ | ||||
|   Creates a note. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> create_note(%{field: value}) | ||||
|       {:ok, %Note{}} | ||||
|  | ||||
|       iex> create_note(%{field: bad_value}) | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   def create_note(attrs \\ %{}) do | ||||
|     %Note{} | ||||
|     |> Note.changeset(attrs) | ||||
|     |> Repo.insert() | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Updates a note. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> update_note(note, %{field: new_value}) | ||||
|       {:ok, %Note{}} | ||||
|  | ||||
|       iex> update_note(note, %{field: bad_value}) | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   def update_note(%Note{} = note, attrs) do | ||||
|     note | ||||
|     |> Note.changeset(attrs) | ||||
|     |> Repo.update() | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Deletes a note. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> delete_note(note) | ||||
|       {:ok, %Note{}} | ||||
|  | ||||
|       iex> delete_note(note) | ||||
|       {:error, %Ecto.Changeset{}} | ||||
|  | ||||
|   """ | ||||
|   def delete_note(%Note{} = note) do | ||||
|     Repo.delete(note) | ||||
|   end | ||||
|  | ||||
|   @doc """ | ||||
|   Returns an `%Ecto.Changeset{}` for tracking note changes. | ||||
|  | ||||
|   ## Examples | ||||
|  | ||||
|       iex> change_note(note) | ||||
|       %Ecto.Changeset{data: %Note{}} | ||||
|  | ||||
|   """ | ||||
|   def change_note(%Note{} = note, attrs \\ %{}) do | ||||
|     Note.changeset(note, attrs) | ||||
|   end | ||||
| end | ||||
							
								
								
									
										22
									
								
								lib/memex/notes/note.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lib/memex/notes/note.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| defmodule Memex.Notes.Note do | ||||
|   use Ecto.Schema | ||||
|   import Ecto.Changeset | ||||
|  | ||||
|   @primary_key {:id, :binary_id, autogenerate: true} | ||||
|   @foreign_key_type :binary_id | ||||
|   schema "notes" 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(note, attrs) do | ||||
|     note | ||||
|     |> cast(attrs, [:title, :content, :tag, :visibility]) | ||||
|     |> validate_required([:title, :content, :tag, :visibility]) | ||||
|   end | ||||
| end | ||||
		Reference in New Issue
	
	Block a user