Skip to content

Instantly share code, notes, and snippets.

@interstar
Created July 30, 2017 19:14
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 interstar/fbfd0391e9d33f5a2fb7334398f60ee8 to your computer and use it in GitHub Desktop.
Save interstar/fbfd0391e9d33f5a2fb7334398f60ee8 to your computer and use it in GitHub Desktop.
Versao services.clj no exemplo de grupo FP de Calango
(ns clojure-web-server.routes.services
(:require [ring.util.http-response :refer :all]
[compojure.api.sweet :refer :all]
[schema.core :as s])
)
(defonce ideias (atom {:id 0 :nome "root" :criancas
[{:id 1 :nome "hello" :criancas {}}]}))
(defn buscar [id ids]
(if (= id (:id ids))
ids
(some #(buscar id %) (:criancas ids) )
))
(defn add [pai-id nova-id nova-nome {:keys [id nome criancas]}]
(if (= pai-id id)
{:id id :nome nome
:criancas (cons {:id nova-id
:nome nova-nome
:criancas []} criancas) }
{:id id :nome nome
:criancas (map #(add pai-id nova-id nova-nome %) criancas)}
) )
(defapi service-routes
{:swagger {:ui "/swagger-ui"
:spec "/swagger.json"
:data {:info {:version "1.0.0"
:title "Sample API"
:description "Sample Services"}}}}
(context "/api" []
:tags ["Ideias"]
(GET "/idea/:id" []
:path-params [id :- String]
:summary "search by id"
(ok (buscar (Integer. id) @ideias)))
(POST "/new-idea/:id" []
:return String
:path-params [id :- Long]
:query-params [nid :- Long, nome :- String]
:summary "add a new node"
(ok (str (reset! ideias (add id nid nome @ideias))))
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment