Skip to content

Commit

Permalink
chore: basic implementation with healthz
Browse files Browse the repository at this point in the history
  • Loading branch information
tonitienda committed May 6, 2024
1 parent 7c83611 commit 74eacf5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
2 changes: 1 addition & 1 deletion backend-rest-clojure/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
compojure/compojure {:mvn/version "1.6.2"}
org.clojure/data.json {:mvn/version "2.5.0"}
com.datomic/datomic-free {:mvn/version "0.9.5697"}
metosin/malli {:mvn/version "0.9.0"}}}
metosin/malli {:mvn/version "0.16.1"}}}

51 changes: 25 additions & 26 deletions backend-rest-clojure/src/kadai/in_memory.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,40 @@

(defrecord DatomicTasksDataSource [db]
tasks/TasksDataSource
(get-tasks [this owner-id]
(let [query '[:find ?title ?description ?status ?deleted-at ?deleted-by
:where
[?task :task/owner ?owner]
[?task :task/title ?title]
[?task :task/description ?description]
[?task :task/status ?status]
[?task :task/deleted-at ?deleted-at]
[?task :task/deleted-by ?deleted-by]
[(= ?owner owner-id)]]
results (d/q query db)]
(map (fn [[title description status deleted-at deleted-by]]
{:owner owner-id
:title title
:description description
:status status
:deleted-at deleted-at
:deleted-by deleted-by})
results)))

(get-tasks [this owner-id]
(let [query `[:find ?title ?description ?status ?deleted-at ?deleted-by ?owner-id
:where
[?task :task/owner-id ?owner-id]
[(= ?owner-id ~owner-id)]
[?task :task/title ?title]
[?task :task/description ?description]
[?task :task/status ?status]
[?task :task/deleted-at ?deleted-at]
[?task :task/deleted-by ?deleted-by]]
results (d/q query db)]
(map (fn [[title description status deleted-at deleted-by]]
{:owner-id owner-id
:title title
:description description
:status status
:deleted-at deleted-at
:deleted-by deleted-by})
results)))
(get-task [this id]
(let [query '[:find ?owner ?title ?description ?status ?deleted-at ?deleted-by
(let [query '[:find ?owner-id ?title ?description ?status ?deleted-at ?deleted-by
:where
[?task :task/id ?id]
[?task :task/owner ?owner]
[?task :task/owner-id ?owner-id]
[?task :task/title ?title]
[?task :task/description ?description]
[?task :task/status ?status]
[?task :task/deleted-at ?deleted-at]
[?task :task/deleted-by ?deleted-by]]
results (d/q query db)]
(if (seq results)
(let [[owner title description status deleted-at deleted-by] (first results)]
(let [[owner-id title description status deleted-at deleted-by] (first results)]
{:id id
:owner owner
:owner-id owner-id
:title title
:description description
:status status
Expand All @@ -51,7 +50,7 @@
(d/transact db `[(-> :task
(d/tempid :db.part/user)
:task/id ~(-> task :id)
:task/owner ~(-> task :owner-id)
:task/owner-id ~(-> task :owner-id)
:task/title ~(-> task :title)
:task/description ~(-> task :description)
:task/status ~(-> task :status)
Expand All @@ -67,7 +66,7 @@
(update-in [:deleted-at] #(.toMillis %))
(d/tempid :db.part/user)
:task/id ~(-> task :id)
:task/owner ~(-> task :owner-id)
:task/owner-id ~(-> task :owner-id)
:task/title ~(-> task :title)
:task/description ~(-> task :description)
:task/status ~(-> task :status)
Expand Down
48 changes: 35 additions & 13 deletions backend-rest-clojure/src/kadai/tasks.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns kadai.tasks
(:require
[clojure.data.json :as json]
[malli.core :as m]))
[clojure.data.json :as json]))

(def TaskRequest [
:map [
[:title :string {:min 1, :max 200}]
[:description :string {:min 1, :max 3000}]
]
])
(require '[malli.core :as m])

(def TaskRequest
[:map
[:title :string ]
[:description :string ]
])

(defprotocol TasksDataSource
(get-tasks [this owner-id])
Expand All @@ -18,18 +18,40 @@
(update-task [this task]))



(defn handle-get-tasks [db req]
(let [
owner-id "03c6eb30-e65f-49fd-92c2-a207f03bbf51"
body (json/write-str (get-tasks db owner-id))]
{:status 200
:headers {"Content-Type" "application/json"}
:body (json/write-str (get-tasks db "03c6eb30-e65f-49fd-92c2-a207f03bbf51"))}
:body body})
)

(defn- new-task [title description owner-id]
{
:id "8ea6c732-4d15-44a6-8864-0b5e0a7042fd"
:title title
:description description
:status "pending"
:owner-id owner-id
})

(defn handle-post-tasks [db req]
(let [body (:body req)]
(if (m/validate TaskRequest body)
{:status 400}
{:status 200 :headers {"Content-Type" "application/json" :body nil}})))

(try (m/validate TaskRequest body)
(let [
title (:title body)
description (:description body)
user-id "66f2ce13-d1e7-4b06-ab5a-c5e7c6a55fcf" ;; get user id from header
task (new-task title description user-id)]
(add-task db task)
{:status 200
:headers {"Content-Type" "application/json"}
:body task})
(catch Exception e
{:status 400
:body {:message (.getMessage e)}}))))

(defn handle-undo-delete-task [db req]
{:status 202
Expand Down
7 changes: 6 additions & 1 deletion webapp-nextjs/src/app/api/tasks/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const GET = async function getTasks() {

export const POST = async function addTask(req: Request) {
const reqData = await req.json();

console.log("adding a new task");
const res = await fetch(buildUrl("/v0/tasks"), {
method: "POST",
body: JSON.stringify({
Expand All @@ -41,9 +41,14 @@ export const POST = async function addTask(req: Request) {

if (!res.ok) {
// This will activate the closest `error.js` Error Boundary
console.log("failed to add a new task");
console.error(await res.status);
console.error(await res.text());
throw new Error("Failed to add task");
}

console.log("task added successfully");

const resData = res.json();
return Response.json(resData);
};

0 comments on commit 74eacf5

Please sign in to comment.