Last active
February 1, 2016 19:20
-
-
Save mndvns/94adbcceee57c8061bb1 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 Api.Resource.Cases do | |
defmacro __using__([type: type]) do | |
[read, create] = Utils.resources(type, [Read, Create]) | |
service = Utils.service(type) | |
quote do | |
use PoeApi.Resource | |
let cases = unquote(service).get_all(Input.get) | |
hyper do | |
action do | |
%{ | |
"count" => length(cases), | |
"collection" => for item <- cases do | |
link_to(unquote(read), item: item["sys_id"]) | |
|> ^Map.merge(item) | |
end, | |
"create" => link_to(unquote(create)) | |
} | |
end | |
end | |
end | |
end | |
end | |
defmodule Api.Resource.ChangeRequests, do: use Api.Resource.Cases, type: "change_requests" | |
defmodule Api.Resource.ServiceRequests, do: use Api.Resource.Cases, type: "service_requests" | |
defmodule Api.Resource.Incidents, do: use Api.Resource.Cases, type: "incidents" |
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 Case do | |
defmacro __using__([type: typename]) do | |
case_schemaname = String.rstrip(typename, ?s) | |
case_typename = Mix.Utils.camelize(String.rstrip(typename, ?s)) | |
case_service = Module.concat([Api.Service.Now, case_typename]) | |
quote do | |
import unquote(__MODULE__) | |
use Api.Model | |
schema unquote(case_schemaname) do | |
field :description, :string | |
field :number, :string | |
end | |
def get(id, input) do | |
unquote(case_service).get_one(id, input) | |
end | |
def get_all(input) do | |
unquote(case_service).get_all(input) | |
end | |
def create(body) do | |
unquote(case_service).create(body) | |
end | |
def remove(id) do | |
unquote(case_service).remove(id) | |
end | |
@create_inputs ~w( | |
description | |
update | |
) | |
end | |
end | |
end | |
defmodule Api.Service.ChangeRequest, do: use Case, type: "change_requests" | |
defmodule Api.Service.ServiceRequest, do: use Case, type: "service_requests" | |
defmodule Api.Service.Incident, do: use Case, type: "incidents" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment