Compare commits
5 commits
c60a0e1de0
...
5245c9a2dd
| Author | SHA1 | Date | |
|---|---|---|---|
| 5245c9a2dd | |||
| 5fad04d04c | |||
| d3babebcc4 | |||
| 7d497191cb | |||
| c34908ac8f |
20 changed files with 489 additions and 105 deletions
178
CLAUDE.md
Normal file
178
CLAUDE.md
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
A REST API backend for managing code snippets, written in Clojure using XTDB for data storage. The API provides endpoints for creating, reading, editing, and deleting snippets, with support for tagging and slug-based lookup.
|
||||
|
||||
**Related Projects:**
|
||||
- [Snippets CMS](https://git.travisshears.com/travisshears/snippets_cms) - TUI CMS and MCP server companion project
|
||||
|
||||
## Development Commands
|
||||
|
||||
### Run Development Server
|
||||
|
||||
```sh
|
||||
clojure -M -m snippets.main
|
||||
```
|
||||
|
||||
Starts the API server on the configured host/port (default: localhost:8080). The REPL can be used to develop with the server:
|
||||
|
||||
```clojure
|
||||
; Start server in background from REPL:
|
||||
(def server (jetty/run-jetty #'app {:port 3000 :join? false}))
|
||||
; Stop server:
|
||||
(.stop server)
|
||||
```
|
||||
|
||||
### Build Uberjar
|
||||
|
||||
```sh
|
||||
clojure -T:build uber
|
||||
```
|
||||
|
||||
Creates a standalone JAR file at `target/snippets-standalone.jar` that includes all dependencies.
|
||||
|
||||
### REPL Commands
|
||||
|
||||
Common REPL operations:
|
||||
|
||||
```clojure
|
||||
; Pretty-print data structures
|
||||
(clojure.pprint/pprint {:name "Alice" :age 30})
|
||||
|
||||
; Require a namespace (fresh import)
|
||||
(require 'clojure.string)
|
||||
(require '[clojure.string :as str])
|
||||
(require 'my.namespace :reload)
|
||||
|
||||
; List namespace contents
|
||||
(dir clojure.string)
|
||||
|
||||
; Switch namespaces
|
||||
(in-ns 'my.namespace)
|
||||
(in-ns 'user)
|
||||
```
|
||||
|
||||
### Linting
|
||||
|
||||
The project uses clj-kondo for static analysis. Configuration is in `.clj-kondo/config.edn`. Linting is integrated into most IDEs via LSP.
|
||||
|
||||
## Code Architecture
|
||||
|
||||
### Directory Structure
|
||||
|
||||
- **src/snippets/infra/** - Infrastructure layer (HTTP API, database, configuration)
|
||||
- `api.clj` - HTTP routing and handlers using reitit
|
||||
- `db.clj` - XTDB queries and transactions
|
||||
- `config.clj` - Configuration loading (config.edn + environment variables)
|
||||
- **src/snippets/use_cases/** - Business logic layer
|
||||
- `view.clj` - Query/read operations, includes serialization for JSON output
|
||||
- `create.clj` - Create snippets
|
||||
- `edit.clj` - Patch snippets with validation
|
||||
- `delete.clj` - Delete snippets
|
||||
- **test/** - Test suite
|
||||
|
||||
### Architecture Pattern
|
||||
|
||||
Clean separation between layers:
|
||||
|
||||
1. **Infrastructure (infra/)**: Handles external concerns
|
||||
- HTTP/REST via reitit + ring
|
||||
- XTDB database client and queries
|
||||
- Environment configuration
|
||||
|
||||
2. **Use Cases**: Business logic implementing application features
|
||||
- Each use case is a module with focused responsibility
|
||||
- Calls infrastructure layer (infra.db, infra.config) as needed
|
||||
- Performs serialization/transformation for API responses
|
||||
|
||||
3. **Main Entry Point (main.clj)**: Minimal bootstrap that runs the API server
|
||||
|
||||
### Data Model
|
||||
|
||||
Snippets are XTDB documents with:
|
||||
- `xt/id` - UUID identifier (set by create use case)
|
||||
- `title` - Snippet title
|
||||
- `slug` - URL-friendly identifier for query lookups
|
||||
- `markdown` - Snippet content in markdown format
|
||||
- `tags` - Vector of strings for categorization
|
||||
- `pub-date` - Date object (serialized to ISO-8601 strings for API responses)
|
||||
|
||||
### API Endpoints
|
||||
|
||||
All routes under `/api`:
|
||||
|
||||
- `GET /api/ping` - Health check
|
||||
- `GET /api/snippets` - List snippets (with optional `limit` and `skip` query params)
|
||||
- `GET /api/snippet?id=<id>` - Get snippet by UUID
|
||||
- `GET /api/snippet-by-slug?slug=<slug>` - Get snippet by slug
|
||||
- `POST /api/snippet` - Create snippet
|
||||
- `PATCH /api/snippet?id=<id>` - Edit snippet (patches are validated against schema)
|
||||
- `DELETE /api/snippet?id=<id>` - Delete snippet
|
||||
- `GET /api/tags` - List all tags with counts
|
||||
- `GET /api/tag?tag=<tag>` - Get snippets by tag
|
||||
|
||||
### Key Dependencies
|
||||
|
||||
- **reitit** (0.9.1) - HTTP routing and coercion
|
||||
- **ring** (1.13.0) - HTTP server (jetty adapter)
|
||||
- **XTDB** (2.0.0-beta9) - Temporal database
|
||||
- **malli** (0.18.0) - Schema validation and generation
|
||||
- **muuntaja** (0.6.11) - JSON/EDN encoding/decoding
|
||||
- **telemere** (1.0.0) - Structured logging
|
||||
- **environ** (1.2.0) - Environment variable loading
|
||||
|
||||
### Configuration
|
||||
|
||||
Configuration comes from `config.edn` with environment variable overrides:
|
||||
|
||||
```edn
|
||||
{:jetty {:host "localhost" :port 8080}
|
||||
:xtdb {:host "192.168.1.157" :port "5007" :user "xtdb" :dbname "xtdb"}}
|
||||
```
|
||||
|
||||
Environment variables can override specific values:
|
||||
- `HOST` - Jetty host
|
||||
- `PORT` - Jetty port (parsed as integer)
|
||||
- `XTDB_HOST`, `XTDB_PORT`, `XTDB_USER`, `XTDB_DBNAME` - XTDB connection details
|
||||
|
||||
See `infra/config.clj` for how overrides are applied.
|
||||
|
||||
## Testing
|
||||
|
||||
Run the test suite:
|
||||
|
||||
```sh
|
||||
clojure -M:test:clojure.test/run
|
||||
```
|
||||
|
||||
Or from REPL:
|
||||
|
||||
```clojure
|
||||
(clojure.test/run-tests 'snippets-test)
|
||||
```
|
||||
|
||||
The test namespace `snippets-test` includes:
|
||||
- Basic arithmetic tests
|
||||
- XTDB integration tests using test node
|
||||
- HTML rendering tests using rum
|
||||
|
||||
## Deployment
|
||||
|
||||
Docker image built and pushed to AWS ECR via `build.sh`:
|
||||
|
||||
```sh
|
||||
./build.sh
|
||||
```
|
||||
|
||||
Requires AWS CLI configured with `personal` profile and permission to push to ECR repository.
|
||||
|
||||
## Notes
|
||||
|
||||
- XTDB queries use XTQL (temporal query language)
|
||||
- Date serialization happens in `view.clj` - pub-date objects are converted to ISO-8601 strings for JSON responses
|
||||
- Patch validation is strict (`{:closed true}`) - only specific fields can be updated
|
||||
- String formatting in some XTQL queries uses `eval` and `read-string` for dynamic query construction
|
||||
- UUIDs used for snippet IDs; generated client-side on creation
|
||||
10
README.md
10
README.md
|
|
@ -18,8 +18,16 @@ This project is written in [Clojure](https://clojure.org/) and data is stored in
|
|||
|
||||
### How to run dev server
|
||||
|
||||
Run the server
|
||||
|
||||
```
|
||||
$ clojure -M -m snippets.infra.api
|
||||
$ clojure -M -m snippets.main
|
||||
```
|
||||
|
||||
Hot reload:
|
||||
|
||||
```shell
|
||||
$ fd -e clj | entr -r clojure -M -m snippets.main
|
||||
```
|
||||
|
||||
### Repl
|
||||
|
|
|
|||
|
|
@ -12,9 +12,9 @@ post {
|
|||
|
||||
body:json {
|
||||
{
|
||||
"title": "TEST from Bruno",
|
||||
"title": "Mock snippet sent via Bruno",
|
||||
"slug": "bruno-test",
|
||||
"markdown": "this is a test",
|
||||
"tags": ["test"]
|
||||
"markdown": "## MOCK SNIPPET\nCreated via bruno",
|
||||
"tags": ["mock"]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ meta {
|
|||
}
|
||||
|
||||
delete {
|
||||
url: {{host}}/api/snippet?id=d77d3463-c76e-4c53-a1d5-ecaf16c6c54e
|
||||
url: {{host}}/api/snippet?slug=bruno-test
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
id: d77d3463-c76e-4c53-a1d5-ecaf16c6c54e
|
||||
slug: bruno-test
|
||||
}
|
||||
|
||||
body:json {
|
||||
|
|
|
|||
|
|
@ -5,18 +5,19 @@ meta {
|
|||
}
|
||||
|
||||
patch {
|
||||
url: {{host}}/api/snippet?id=680a3508-7709-4f71-b5c3-3dcbffe6f5cf
|
||||
url: {{host}}/api/snippet?slug=bruno-test
|
||||
body: json
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
id: 680a3508-7709-4f71-b5c3-3dcbffe6f5cf
|
||||
slug: bruno-test
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"title": "quick way to push last jj commit to git",
|
||||
"tags": ["jj", "git"]
|
||||
"title": "Mock snippet sent via Bruno with updated title",
|
||||
"markdown": "## MOCK SNIPPET\nUpdated via bruno",
|
||||
"tags": ["mock", "updated"]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
meta {
|
||||
name: get_snippet
|
||||
type: http
|
||||
seq: 6
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{host}}/api/snippet?id=aea69336-5116-49ac-ab52-bc221bdb7830
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
id: aea69336-5116-49ac-ab52-bc221bdb7830
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"title": "Test Snippet",
|
||||
"markdown": "## Cool Snippet\ndoes a cool thing",
|
||||
"tags": ["git", "jj"]
|
||||
}
|
||||
}
|
||||
|
|
@ -5,13 +5,13 @@ meta {
|
|||
}
|
||||
|
||||
get {
|
||||
url: {{host}}/api/snippet-by-slug?slug=netcat-over-ping
|
||||
url: {{host}}/api/snippet-by-slug?slug=bruno-test
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
slug: netcat-over-ping
|
||||
slug: bruno-test
|
||||
}
|
||||
|
||||
body:json {
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ meta {
|
|||
}
|
||||
|
||||
get {
|
||||
url: {{host}}/api/snippets?limit=25&skip=0
|
||||
url: {{host}}/api/snippets?limit=2&skip=0
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
limit: 25
|
||||
limit: 2
|
||||
skip: 0
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ meta {
|
|||
}
|
||||
|
||||
get {
|
||||
url: {{host}}/api/tag?tag=git
|
||||
url: {{host}}/api/tag?tag=mock
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
|
||||
params:query {
|
||||
tag: git
|
||||
tag: mock
|
||||
}
|
||||
|
||||
body:json {
|
||||
|
|
|
|||
1
deps.edn
1
deps.edn
|
|
@ -5,6 +5,7 @@
|
|||
org.slf4j/slf4j-simple {:mvn/version "2.0.16"}
|
||||
|
||||
;; db
|
||||
com.datomic/local {:mvn/version "1.0.291"}
|
||||
com.xtdb/xtdb-api {:mvn/version "2.0.0-beta9"}
|
||||
com.github.seancorfield/next.jdbc {:mvn/version "1.3.1002"}
|
||||
org.postgresql/postgresql {:mvn/version "42.7.6"}
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@
|
|||
{:status 200, :body "snippet created"})
|
||||
|
||||
(defn handle-edit-snippet [{body :body-params params :query-params}]
|
||||
(let [id (get params "id")]
|
||||
(t/log! {:level :info, :data {:body body :id id}} "Received request to edit snippet")
|
||||
(let [{success :success :as res} (snippets.use-cases.edit/edit-snippet id body)]
|
||||
(let [slug (get params "slug")]
|
||||
(t/log! {:level :info, :data {:body body :slug slug}} "Received request to edit snippet")
|
||||
(let [{success :success :as res} (snippets.use-cases.edit/edit-snippet slug body)]
|
||||
(cond
|
||||
success {:status 200, :body "snippet updated"}
|
||||
(= (:reason res) :invalid-patch) {:status 400, :body "invalid patch"}
|
||||
|
|
@ -38,18 +38,16 @@
|
|||
{:status 200
|
||||
:body (snippets.use-cases.view/view-snippets {:limit limit-num :skip skip-num})})
|
||||
{:status 200
|
||||
:body (snippets.use-cases.view/view-snippets)}))
|
||||
|
||||
(defn handle-view-snippet [{params :query-params}]
|
||||
(let [id (get params "id")]
|
||||
{:status 200
|
||||
:body (snippets.use-cases.view/view-snippet id)}))
|
||||
:body (snippets.use-cases.view/view-snippets nil)}))
|
||||
|
||||
(defn handle-delete-snippet [{params :query-params}]
|
||||
(let [id (get params "id")]
|
||||
(snippets.use-cases.delete/delete-snippet id)
|
||||
(let [slug (get params "slug")
|
||||
res (snippets.use-cases.delete/delete-snippet slug)]
|
||||
(if (nil? res)
|
||||
{:status 404
|
||||
:body "No snippet with that slug found"}
|
||||
{:status 200
|
||||
:body (format "Deleted snippet with id: %s if it existed" id)}))
|
||||
:body (format "Deleted snippet with slug: %s if it existed" slug)})))
|
||||
|
||||
(defn handle-view-tags [_args]
|
||||
(let [tags (snippets.use-cases.view/view-tags)]
|
||||
|
|
@ -62,9 +60,13 @@
|
|||
:body (snippets.use-cases.view/view-snippets-by-tag tag)}))
|
||||
|
||||
(defn handle-view-snippet-by-slug [{params :query-params}]
|
||||
(let [slug (get params "slug")]
|
||||
(let [slug (get params "slug")
|
||||
snippet (snippets.use-cases.view/view-snippet-by-slug slug)]
|
||||
(if (nil? snippet)
|
||||
{:status 404
|
||||
:body "No snippet with that slug found"}
|
||||
{:status 200
|
||||
:body (snippets.use-cases.view/view-snippet-by-slug slug)}))
|
||||
:body snippet})))
|
||||
|
||||
(defn wrap [handler id]
|
||||
(fn [request]
|
||||
|
|
@ -82,7 +84,6 @@
|
|||
["/snippet-by-slug" {:get handle-view-snippet-by-slug}]
|
||||
["/snippets" {:get handle-view-snippets}]
|
||||
["/snippet" {:post handle-create-snippet
|
||||
:get handle-view-snippet
|
||||
:patch handle-edit-snippet
|
||||
:delete handle-delete-snippet}]])
|
||||
(rr/create-default-handler)))
|
||||
|
|
|
|||
218
src/snippets/infra/db2.clj
Normal file
218
src/snippets/infra/db2.clj
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
(ns snippets.infra.db2
|
||||
(:require
|
||||
[clojure.set :as set]
|
||||
[datomic.client.api :as d]
|
||||
[malli.core :as m]
|
||||
[taoensso.telemere :as t]))
|
||||
|
||||
;; Initialize the Datomic Local client
|
||||
;; :system "dev" groups your databases in the "dev" system
|
||||
;; In production, you'd set :storage-dir to a persistent path
|
||||
;; TODO: add save file location for prod
|
||||
(def client (d/client {:server-type :datomic-local
|
||||
:system "dev"}))
|
||||
|
||||
(def db-name "snippets")
|
||||
|
||||
;; Create the database if it doesn't exist
|
||||
(defn- ensure-db
|
||||
"Check if db exists, create it if not."
|
||||
[]
|
||||
(d/create-database client {:db-name db-name})
|
||||
(t/log! {:level :info} "Snippets database created if needed"))
|
||||
|
||||
;; Get a connection to the database
|
||||
(defn- get-conn []
|
||||
(d/connect client {:db-name db-name}))
|
||||
|
||||
;; Define the schema for snippets
|
||||
;; Transact this once to set up the database structure
|
||||
(def snippet-schema
|
||||
[{:db/ident :snippet/title
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one}
|
||||
{:db/ident :snippet/slug
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one
|
||||
:db/unique :db.unique/value}
|
||||
{:db/ident :snippet/markdown
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/one}
|
||||
{:db/ident :snippet/tags
|
||||
:db/valueType :db.type/string
|
||||
:db/cardinality :db.cardinality/many}
|
||||
{:db/ident :snippet/pub-date
|
||||
:db/valueType :db.type/instant
|
||||
:db/cardinality :db.cardinality/one}])
|
||||
|
||||
(defn- ensure-schema
|
||||
"Transact the schema if it doesn't exist. Call this once on startup."
|
||||
[]
|
||||
(let [conn (get-conn)]
|
||||
(d/transact conn {:tx-data snippet-schema})
|
||||
(t/log! {:level :info} "Snippet schema created if needed")))
|
||||
|
||||
(defn start-up-check
|
||||
"Should be run at startup to ensure the database and schema are created."
|
||||
[]
|
||||
(ensure-db)
|
||||
(ensure-schema))
|
||||
|
||||
(defn- snippet-to-entity
|
||||
"Convert a snippet map to a Datomic DB entity."
|
||||
[snippet]
|
||||
{:snippet/title (:title snippet)
|
||||
:snippet/slug (:slug snippet)
|
||||
:snippet/markdown (:markdown snippet)
|
||||
:snippet/tags (:tags snippet)
|
||||
:snippet/pub-date (:pub-date snippet)})
|
||||
|
||||
(defn- entity-to-snippet
|
||||
"Convert a Datomic DB entity to a snippet map."
|
||||
[entity]
|
||||
{:title (:snippet/title entity)
|
||||
:slug (:snippet/slug entity)
|
||||
:markdown (:snippet/markdown entity)
|
||||
:tags (:snippet/tags entity)
|
||||
:pub-date (:snippet/pub-date entity)})
|
||||
|
||||
(defn- wrap-snippet-return
|
||||
"Wraps an fn that returns snippet, snippet[], or nil; converting the entity to a snippet map."
|
||||
[snippet-fn]
|
||||
(fn [& args]
|
||||
(let [res (apply snippet-fn args)]
|
||||
(cond
|
||||
(nil? res) nil
|
||||
:else (if (sequential? res)
|
||||
(map entity-to-snippet res)
|
||||
(entity-to-snippet res))))))
|
||||
|
||||
;; create
|
||||
(def create-schema
|
||||
"Malli schema for a valid snippet entity creation."
|
||||
[:map
|
||||
[:snippet/title :string]
|
||||
[:snippet/slug :string]
|
||||
[:snippet/markdown :string]
|
||||
[:snippet/tags [:vector :string]]
|
||||
[:snippet/pub-date [:fn #(instance? java.util.Date %)]]])
|
||||
|
||||
(defn- valid-create?
|
||||
"Check if a snippet map is a valid Datomic entity."
|
||||
[entity]
|
||||
(m/validate create-schema entity))
|
||||
|
||||
(defn- put-snippets
|
||||
"Create new snippets in the database."
|
||||
[snippets]
|
||||
(t/log! {:level :info, :data {:slugs (map :slug snippets)}} "Saving new snippets to db")
|
||||
(let [conn (get-conn)
|
||||
entities (map snippet-to-entity snippets)]
|
||||
(if (every? valid-create? entities)
|
||||
(d/transact conn {:tx-data entities})
|
||||
(throw (ex-info "Invalid snippet entity" {:entities entities})))))
|
||||
|
||||
(def create-snippets
|
||||
(wrap-snippet-return put-snippets))
|
||||
|
||||
;; read
|
||||
(defn- get-snippet-by-slug-from-db
|
||||
"Get a single snippet by its slug."
|
||||
[slug]
|
||||
(let [conn (get-conn)
|
||||
db (d/db conn)
|
||||
query '[:find (pull ?e [*])
|
||||
:in $ ?slug
|
||||
:where [?e :snippet/slug ?slug]]
|
||||
snippet (ffirst (d/q query db slug))]
|
||||
(t/log! {:level :info, :data {:slug slug :snippet snippet}} "Got snippet by slug")
|
||||
snippet))
|
||||
|
||||
(def get-snippet-by-slug
|
||||
(wrap-snippet-return get-snippet-by-slug-from-db))
|
||||
|
||||
;; update
|
||||
(def update-schema
|
||||
"Malli schema for a valid update to a snippet entity."
|
||||
[:map
|
||||
[:db/id :int]
|
||||
[:snippet/title {:optional true} :string]
|
||||
[:snippet/slug {:optional true} :string]
|
||||
[:snippet/markdown {:optional true} :string]
|
||||
[:snippet/tags {:optional true} [:vector :string]]])
|
||||
|
||||
(defn- to-update [patch]
|
||||
(cond-> {}
|
||||
(some? (:title patch)) (assoc :snippet/title (:title patch))
|
||||
(some? (:slug patch)) (assoc :snippet/slug (:slug patch))
|
||||
(some? (:markdown patch)) (assoc :snippet/markdown (:markdown patch))
|
||||
(some? (:tags patch)) (assoc :snippet/tags (:tags patch))))
|
||||
|
||||
(defn- patch-snippet-in-db
|
||||
"Update specific fields of a snippet."
|
||||
[slug raw-patch]
|
||||
(let [conn (get-conn)
|
||||
snippet (get-snippet-by-slug-from-db slug)
|
||||
eid (:db/id snippet)
|
||||
new-tags (get raw-patch :tags '[])
|
||||
existing-tags (get snippet :snippet/tags '[])
|
||||
tags-to-remove (vec (set/difference (set existing-tags) (set new-tags)))
|
||||
retracts (map #(vector :db/retract eid :snippet/tags %) tags-to-remove)
|
||||
patch (merge (to-update raw-patch) {:db/id eid})]
|
||||
(t/log! {:level :info, :data {:patch patch :retracts retracts :slug slug :eid eid}} "Patching snippet")
|
||||
(when (nil? eid)
|
||||
(throw (ex-info "Snippet not found" {:slug slug})))
|
||||
(when-not (m/validate update-schema patch)
|
||||
(throw (ex-info "Invalid patch" {:errors (m/explain update-schema patch) :patch patch})))
|
||||
(d/transact conn {:tx-data (into [patch] retracts)})))
|
||||
|
||||
(defn update-snippet [& args]
|
||||
(let [res (apply patch-snippet-in-db args)]
|
||||
(t/log! {:level :info, :data {:res res :args args}} "Finished patching snippet")))
|
||||
|
||||
(defn list-snippets-in-db
|
||||
"List all the snippets"
|
||||
[]
|
||||
(let [conn (get-conn)
|
||||
db (d/db conn)
|
||||
query '[:find (pull ?e [*])
|
||||
:where
|
||||
[?e :snippet/slug]]]
|
||||
(->> (d/q query db)
|
||||
(map first))))
|
||||
|
||||
(def list-snippets (wrap-snippet-return list-snippets-in-db))
|
||||
|
||||
(defn delete-snippet-by-slug
|
||||
"Soft delete a snippet (retract its entity)."
|
||||
[slug]
|
||||
(t/log! {:level :info, :data {:slug slug}} "Retracting snippet")
|
||||
(let [conn (get-conn)
|
||||
eid (:db/id (get-snippet-by-slug-from-db slug))]
|
||||
(if (nil? eid)
|
||||
nil
|
||||
(d/transact conn {:tx-data [[:db/retractEntity eid]]}))))
|
||||
|
||||
(defn list-tags
|
||||
"List all tags used in snippets with their counts."
|
||||
[]
|
||||
(let [conn (get-conn)
|
||||
db (d/db conn)
|
||||
query '[:find ?tag (count ?e)
|
||||
:where
|
||||
[?e :snippet/tags ?tag]]]
|
||||
(d/q query db)))
|
||||
|
||||
(defn get-snippets-by-tag-in-db
|
||||
"Get all snippets that have a specific tag."
|
||||
[tag]
|
||||
(let [conn (get-conn)
|
||||
db (d/db conn)
|
||||
query '[:find (pull ?e [*])
|
||||
:in $ ?tag
|
||||
:where
|
||||
[?e :snippet/tags ?tag]]
|
||||
results (d/q query db tag)]
|
||||
(mapv first results)))
|
||||
|
||||
(def get-snippets-by-tag (wrap-snippet-return get-snippets-by-tag-in-db))
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
(ns snippets.main
|
||||
(:require [snippets.infra.api :as api])
|
||||
(:require
|
||||
[snippets.infra.api :as api]
|
||||
[snippets.infra.db2 :refer [start-up-check]])
|
||||
(:gen-class))
|
||||
|
||||
(defn -main []
|
||||
(start-up-check)
|
||||
(api/run-server))
|
||||
|
|
|
|||
7
src/snippets/mocks.clj
Normal file
7
src/snippets/mocks.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(ns snippets.mocks)
|
||||
|
||||
(def mock-snippet {:slug "mock-1"
|
||||
:title "Mock Snippet 1"
|
||||
:markdown "## Mock Snippet 1"
|
||||
:tags ["mock"]
|
||||
:pub-date (java.util.Date/from (java.time.Instant/parse "2020-01-01T00:00:00Z"))})
|
||||
15
src/snippets/use_cases/backfill_db2.clj
Normal file
15
src/snippets/use_cases/backfill_db2.clj
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(ns snippets.use-cases.backfill-db2
|
||||
(:require
|
||||
[snippets.infra.db :as old-db]
|
||||
[snippets.infra.db2 :as new-db]
|
||||
[taoensso.telemere :as t]))
|
||||
|
||||
(defn- zdt-to-date [zdt]
|
||||
(java.util.Date/from (.toInstant zdt)))
|
||||
|
||||
(defn backfill []
|
||||
(t/log! {:level :info} "Backfilling DB2")
|
||||
(let [old-snippets (old-db/list-snippets {})
|
||||
new-snippets (map #(assoc % :pub-date (zdt-to-date (:pub-date %))) old-snippets)]
|
||||
(t/log! {:level :info :data {:count (count new-snippets)}} "Creating snippets")
|
||||
(new-db/create-snippets new-snippets)))
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
(ns snippets.use-cases.backfill-from-file
|
||||
(:require
|
||||
[clojure.java.io :as io]
|
||||
[clojure.pprint :as pprint]
|
||||
[clojure.string :as str]
|
||||
[frontmatter.core :as fm]))
|
||||
|
||||
(defn scrape-files []
|
||||
(let [dir "./old_snippets"]
|
||||
(->> (io/file dir)
|
||||
(.listFiles)
|
||||
(map #(.getName %))
|
||||
(map #(hash-map :slug (first (str/split % #"\.")) :full-path (str dir "/" %)))
|
||||
;; (map #(fm/parse (:full-path %))))))
|
||||
(map #(let [{frontmatter :frontmatter body :body} (fm/parse (:full-path %))]
|
||||
(assoc %
|
||||
:title (:title frontmatter)
|
||||
:pub-date (:date frontmatter)
|
||||
:markdown body
|
||||
:scraped true
|
||||
:tags (:snippet_types frontmatter))))
|
||||
(map #(dissoc % :full-path)))))
|
||||
|
||||
;; used repl to do backfill
|
||||
;; (doseq [s old-snippets] (xt/execute-tx db/client [[:put-docs :snippets (merge {:xt/id (:slug s)} s)]]))
|
||||
|
|
@ -1,12 +1,9 @@
|
|||
(ns snippets.use-cases.create
|
||||
(:require
|
||||
[taoensso.telemere :as t]
|
||||
[snippets.infra.db :as db]))
|
||||
|
||||
(defn- uuid [] (str (java.util.UUID/randomUUID)))
|
||||
[snippets.infra.db2 :as db]))
|
||||
|
||||
(defn create-snippet [{:keys [title slug markdown tags]}]
|
||||
(let [id (uuid)
|
||||
pub-date (java.util.Date.)]
|
||||
(t/log! {:level :info, :data {:title title :slug slug :id id}} "Creating snippet")
|
||||
(db/put-snippet id {:title title :slug slug :markdown markdown :tags tags :pub-date pub-date})))
|
||||
(let [pub-date (java.util.Date.)]
|
||||
(t/log! {:level :info, :data {:title title :slug slug}} "Creating snippet")
|
||||
(db/create-snippets [{:title title :slug slug :markdown markdown :tags tags :pub-date pub-date}])))
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
(ns snippets.use-cases.delete
|
||||
(:require
|
||||
[snippets.infra.db :as db]
|
||||
[snippets.infra.db2 :as db]
|
||||
[taoensso.telemere :as t]))
|
||||
|
||||
(defn delete-snippet [key]
|
||||
(t/log! {:level :info, :data {:key key}} "Deleting snippet by id")
|
||||
(db/delete-snippet key))
|
||||
(defn delete-snippet [slug]
|
||||
(t/log! {:level :info, :data {:slug slug}} "Deleting snippet by slug")
|
||||
(db/delete-snippet-by-slug slug))
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
(:require
|
||||
[taoensso.telemere :as t]
|
||||
[malli.core :as m]
|
||||
[snippets.infra.db :as db]))
|
||||
[snippets.infra.db2 :as db]))
|
||||
|
||||
(def valid-patch?
|
||||
(m/validator
|
||||
|
|
@ -12,11 +12,11 @@
|
|||
[:tags {:optional true} [:seqable :string]]
|
||||
[:slug {:optional true} :string]]))
|
||||
|
||||
(defn edit-snippet [id patch]
|
||||
(t/log! {:level :info, :data {:patch patch :id id}} "Editing snippet")
|
||||
(defn edit-snippet [slug patch]
|
||||
(t/log! {:level :info, :data {:patch patch :slug slug}} "Editing snippet")
|
||||
(if (valid-patch? patch)
|
||||
(do
|
||||
(t/log! {:level :info, :data {:patch patch :id id}} "Valid changes editing snippet")
|
||||
(db/patch-snippet id patch)
|
||||
(t/log! {:level :info, :data {:patch patch :slug slug}} "Valid changes editing snippet")
|
||||
(db/update-snippet slug patch)
|
||||
{:success true})
|
||||
{:success false :reason :invalid-patch}))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(ns snippets.use-cases.view
|
||||
(:require
|
||||
[taoensso.telemere :as t]
|
||||
[snippets.infra.db :as db]))
|
||||
[snippets.infra.db2 :as db]))
|
||||
|
||||
(defn serialize-snippet
|
||||
"Converts snippet pub-date to ISO-8601 string for EDN serialization"
|
||||
|
|
@ -9,12 +9,15 @@
|
|||
(when snippet
|
||||
(assoc snippet :pub-date (.toString (:pub-date snippet)))))
|
||||
|
||||
(defn view-snippet [key]
|
||||
(t/log! {:level :info, :data {:key key}} "Viewing snippet by id")
|
||||
(serialize-snippet (db/get-snippet-by-id key)))
|
||||
|
||||
(defn view-snippets [& args]
|
||||
(map serialize-snippet (db/list-snippets args)))
|
||||
(defn view-snippets [options]
|
||||
(if (nil? options)
|
||||
(map serialize-snippet (db/list-snippets))
|
||||
(let [limit (:limit options)
|
||||
skip (:skip options)]
|
||||
(->> (db/list-snippets)
|
||||
(drop skip)
|
||||
(take limit)
|
||||
(map serialize-snippet)))))
|
||||
|
||||
(defn view-tags []
|
||||
(t/log! {:level :info} "Viewing tags")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue