-
-
Save shahryarjb/ae238a3062c7ef7ae3c61299520e9a94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MishkaDatabase.CRUD do | |
defmacro __using__(opts) do | |
quote(bind_quoted: [opts: opts]) do | |
@interface_module opts | |
end | |
end | |
defmacro crud_add(attrs) do | |
quote do | |
module_selected = Keyword.get(@interface_module, :module) | |
error_atom = Keyword.get(@interface_module, :error_atom) | |
repo = Keyword.get(@interface_module, :repo) | |
add = module_selected.__struct__ | |
|> module_selected.changeset(unquote(attrs)) | |
|> repo.insert() | |
case add do | |
{:ok, data} -> {:ok, error_atom, data} | |
{:error, changeset} -> {:error, error_atom, changeset} | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment