Yu-Gi-Oh! Deck Building and Card Inventory Management web interface written in Common Lisp, utilizing HTMX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.9KB

  1. #|
  2. I didn't want to keep the query stuff in the model as it's not really
  3. got anything to do with the model and more with the database. I tried
  4. the DATABASE-OBJECT approach and that was clumsy too.
  5. I just want to write queries. Maybe I should have used CL-YESQL.
  6. TODO I have multiple of these too: saved-deck-list,
  7. saved-category-list, constructed-deck-list ... where should these go?
  8. Into their respective pacakges?
  9. |#
  10. (in-package #:cl-deck-builder2.web.query)
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. (defmacro select-attachment (&body body)
  13. `(select-dao 'attachment
  14. (sxql:order-by :created-at :desc)
  15. ,@body))
  16. (defun select-attachment-by-id (id)
  17. (first (select-attachment
  18. (sxql:where (:= :id id)))))
  19. (defmacro select-category (&body body)
  20. `(select-dao 'category
  21. ;; Why did I use this?
  22. ;; (sxql:group-by name)
  23. (sxql:order-by :created-at :desc)
  24. ,@body))
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. (defmacro select-feedback (&body body)
  27. `(select-dao 'feedback
  28. (mito:includes 'user)
  29. (sxql:order-by :created-at :desc)
  30. ,@body))
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32. ;; TODO Now where does this go? db? toolkit?
  33. (defun %sxql-like (field value)
  34. "Helper function. Turn \"value\" into \"%value%\" for SQL LIKE queries."
  35. (list :like field (format nil "%%~a%%" value)))
  36. (defmacro select-ydk-deck (&body body)
  37. `(select-dao 'ydk-deck
  38. (sxql:order-by :asc :created-at)
  39. ,@body))
  40. (defmacro select-deck-item (&body body)
  41. `(select-dao 'deck-item
  42. ;; (mito:includes 'ydk-deck)
  43. (sxql:order-by :asc :created-at)
  44. ,@body))
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. (defmacro select-constructed-decks (&body body)
  47. `(select-dao 'constructed-deck
  48. (mito:includes 'ydk-deck)
  49. (sxql:order-by :asc :created-at)
  50. ,@body))
  51. (defmacro select-constructed-deck-items (&body body)
  52. `(select-dao 'constructed-deck-item
  53. (mito:includes 'ygo-card 'deck-item 'ygo-set-item)
  54. (sxql:order-by :asc :created-at)
  55. ,@body))
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;; builder.lisp
  58. ;; (defun unique-cards-in-decks ()
  59. ;; "Select the unique cards in all DECK-ITEMs"
  60. ;; (let ((deck-items
  61. ;; (with-connection (db)
  62. ;; (mito:select-dao 'deck-item
  63. ;; (sxql:group-by :passcode))))
  64. ;; (ht (make-hash-table)))
  65. ;; (dolist (deck-item deck-items ht)
  66. ;; (push deck-item (gethash (deck-id-of deck-item) ht)))))
  67. ;; Asked for this one. For every card in a deck, show what deck the card is in...
  68. (defun cards-in-all-decks ()
  69. "Select the unique cards in all DECK-ITEMs"
  70. (let ((deck-items
  71. (with-connection (db)
  72. (with-transaction
  73. (select-deck-item))))
  74. (ht (make-hash-table)))
  75. (with-connection (db)
  76. (with-transaction
  77. (dolist (deck-item (reverse deck-items) ht)
  78. (pushnew (list :deck-id (deck-id-of deck-item)
  79. :ydk-deck (mito:find-dao 'ydk-deck :id (deck-id-of deck-item))
  80. :ygo-card (mito:find-dao 'ygo-card :id (deck-passcode-of deck-item)))
  81. (gethash (deck-passcode-of deck-item) ht)
  82. :key (lambda (plist) (getf plist :deck-id))))))))
  83. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84. (defun select-variant-condition ()
  85. (select-dao 'variant-condition))
  86. (defun ygo-set-item-by-item-id (item-id)
  87. (with-connection (db)
  88. (with-transaction
  89. (let ((set-items
  90. (mito:select-dao 'ygo-set-item
  91. (mito:includes 'ygo-set 'variant-condition)
  92. (sxql:where (:= :item-id item-id)))))
  93. (dolist (set-item set-items set-items)
  94. (setf (item-of set-item)
  95. (ygo-set-by-id (mito:object-id set-item))))))))
  96. (defun ygo-set-item-by-id (id)
  97. (with-connection (db)
  98. (with-transaction
  99. (let ((set-item (mito:select-dao 'ygo-set-item
  100. (mito:includes 'ygo-set
  101. 'variant-condition)
  102. (sxql:where (:= :id id)))))
  103. (setf (item-of set-item)
  104. (ygo-set-by-id (mito:object-id set-item)))
  105. set-item))))
  106. (defun select-ygo-cc-item-variants (id)
  107. "YGO-CC-ITEM is a relatively new addition, a database relationship between CC-ITEMs and VARIANT. Actually now that I think about it as I write this, YGO-CC-ITEM is a misnomer, and this is actually a CC-ITEM-VARIANT.
  108. SELECT all the variants available for this CC-ITEM ID.
  109. This will generate a blank of 5 items if they don't already have corresponding YGO-SET-ITEMS in the database
  110. It will also fetch all corresponding YGO-SET-ITEMS. If they do exist, REMOVE-DUPLICATES will use MITO:OBJECT-ID to remove them by VARIANT-OF.
  111. TODO FIXME"
  112. (with-connection (db)
  113. (with-transaction
  114. (remove-duplicates
  115. ;; Append the blanks and the existing data from the database, and remove-duplicates
  116. (append
  117. ;; Create a list of blanks
  118. (mapcar (lambda (v)
  119. (make-instance 'ygo-set-item :item-id id
  120. :item (ygo-set-by-id id)
  121. :variant v
  122. :variant-id (mito:object-id v)))
  123. (select-variant-condition))
  124. ;; Select what exists from the database, and fill in the YGO-SET info with the macro we wrote
  125. (ygo-set-item-by-item-id id))
  126. :key (alexandria:compose #'mito:object-id #'variant-of)))))
  127. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  128. ;; TODO: old ygoprodeck code
  129. (defun ygo-select-info-by-id (id &rest fields-and-values)
  130. (apply #'find-dao 'ygo-info :id id fields-and-values))
  131. (defun ygo-select-info-by-passcode (passcode &rest fields-and-values)
  132. (apply #'find-dao 'ygo-info :passcode passcode fields-and-values))