Skip to content

Instantly share code, notes, and snippets.

@shahryarjb
Created January 12, 2021 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahryarjb/ae238a3062c7ef7ae3c61299520e9a94 to your computer and use it in GitHub Desktop.
Save shahryarjb/ae238a3062c7ef7ae3c61299520e9a94 to your computer and use it in GitHub Desktop.
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