scrumli/pg-datastore.lisp

105 lines
4.5 KiB
Common Lisp
Raw Normal View History

2013-06-30 22:38:05 +02:00
(in-package #:scrumli.pg-datastore)
(defclass pg-datastore ()
((connection-spec :initarg :connection-spec
:accessor connection-spec)))
(defclass story ()
((id :col-type serial :reader story-id)
(state :col-type string :reader state :initform "TODO")
(role :col-type string :reader role :initarg :role)
(necessity :col-type string :reader necessity :initarg :necessity)
(title :col-type string :reader title :initarg :title)
(priority :col-type integer :reader priority :initarg :priority)
(content :col-type string :reader content :initarg :content)
(reporter :col-type string :reader reporter :initarg :reporter)
(assignee :col-type string :reader assignee :initarg :assignee))
(:metaclass dao-class)
(:keys id))
2013-07-04 21:51:31 +02:00
(defclass task ()
((id :col-type serial :reader story-id)
(state :col-type string :reader state :initform "TODO")
(description :col-type string :reader description :initarg :description)
(priority :col-type integer :reader priority :initarg :priority)
(reporter :col-type string :reader reporter :initarg :reporter)
(assignee :col-type string :reader assignee :initarg :assignee)
(story-id :col-type integer :reader story-id :initarg :story-id))
(:metaclass dao-class)
(:keys id))
(deftable task
(!dao-def)
(!foreign 'story 'story-id 'id))
2013-06-30 22:38:05 +02:00
(defmethod datastore-init ((datastore pg-datastore))
(with-connection (connection-spec datastore)
(unless (table-exists-p 'story)
2013-07-04 21:51:31 +02:00
(execute (dao-table-definition 'story)))
(unless (table-exists-p 'task)
(execute (dao-table-definition 'task)))))
2013-06-30 22:38:05 +02:00
(defmethod datastore-get-all-stories ((datastore pg-datastore))
(with-connection (connection-spec datastore)
(query (:order-by (:select :* :from 'story) 'priority) :alists)))
(defmethod datastore-get-story ((datastore pg-datastore) id)
(with-connection (connection-spec datastore)
2013-07-04 21:51:31 +02:00
(append (query (:select :* :from 'story :where (:= 'id id)) :alist)
`((tasks . ,(datastore-get-tasks-for-story datastore id))))))
(defmethod datastore-get-tasks-for-story ((datastore pg-datastore) id)
(with-connection (connection-spec datastore)
(query (:order-by (:select :* :from 'task :where (:= 'story-id id))
'priority)
:alists)))
2013-06-30 22:38:05 +02:00
(defmethod datastore-post-story
((datastore pg-datastore) role necessity title content reporter)
(with-connection (connection-spec datastore)
(let ((obj (make-instance
'story :role role :necessity necessity :title title
:priority (+ 1 (or (query (:select
(:coalesce (:max 'priority) 0)
:from 'story) :single)
0))
:content content :assignee "" :reporter reporter)))
(save-dao obj))))
2013-07-04 21:51:31 +02:00
(defmethod datastore-post-task
((datastore pg-datastore) story-id description reporter)
(with-connection (connection-spec datastore)
(let ((obj (make-instance
'task :description description
:priority (+ 1 (query (:select
(:coalesce (:max 'priority) 0)
:from 'task) :single))
:reporter reporter :story-id (parse-integer story-id)
:assignee "")))
(save-dao obj))))
2013-07-04 23:55:43 +02:00
(defmethod datastore-story-get-state ((datastore pg-datastore) type id)
2013-06-30 22:38:05 +02:00
(with-connection (connection-spec datastore)
2013-07-04 23:55:43 +02:00
(query (:select 'state :from type :where (:= 'id id)) :single)))
2013-06-30 22:38:05 +02:00
2013-07-04 23:55:43 +02:00
(defmethod datastore-story-set-state
((datastore pg-datastore) type id state)
2013-06-30 22:38:05 +02:00
(with-connection (connection-spec datastore)
2013-07-04 23:55:43 +02:00
(execute (:update type :set 'state state :where (:= 'id id)))))
2013-06-30 22:38:05 +02:00
(defmethod datastore-story-change-priority
2013-07-04 23:55:43 +02:00
((datastore pg-datastore) type id dir)
2013-06-30 22:38:05 +02:00
(with-connection (connection-spec datastore)
2013-07-04 23:55:43 +02:00
(let* ((current-priority (query (:select 'priority :from type
2013-06-30 22:38:05 +02:00
:where (:= 'id id))
:single))
(next-priority (funcall (ecase dir (:up #'-) (:down #'+))
current-priority 1))
(max-priority (query (:select (:max 'priority) :from type)
:single)))
2013-07-04 23:55:43 +02:00
(execute (:update type :set 'priority current-priority
2013-06-30 22:38:05 +02:00
:where (:= 'priority next-priority)))
(execute (:update type :set
'priority (max 1 (min next-priority max-priority))
2013-06-30 22:38:05 +02:00
:where (:= 'id id))))))