Compare commits
No commits in common. "5245c9a2dd3aeb39e6ab53205e682d4a2d242eef" and "c60a0e1de066b18d1ae0adb3b815697c213c1c03" have entirely different histories.
5245c9a2dd
...
c60a0e1de0
20 changed files with 105 additions and 489 deletions
178
CLAUDE.md
178
CLAUDE.md
|
|
@ -1,178 +0,0 @@
|
||||||
# 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,16 +18,8 @@ This project is written in [Clojure](https://clojure.org/) and data is stored in
|
||||||
|
|
||||||
### How to run dev server
|
### How to run dev server
|
||||||
|
|
||||||
Run the server
|
|
||||||
|
|
||||||
```
|
```
|
||||||
$ clojure -M -m snippets.main
|
$ clojure -M -m snippets.infra.api
|
||||||
```
|
|
||||||
|
|
||||||
Hot reload:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
$ fd -e clj | entr -r clojure -M -m snippets.main
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Repl
|
### Repl
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ post {
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"title": "Mock snippet sent via Bruno",
|
"title": "TEST from Bruno",
|
||||||
"slug": "bruno-test",
|
"slug": "bruno-test",
|
||||||
"markdown": "## MOCK SNIPPET\nCreated via bruno",
|
"markdown": "this is a test",
|
||||||
"tags": ["mock"]
|
"tags": ["test"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,13 @@ meta {
|
||||||
}
|
}
|
||||||
|
|
||||||
delete {
|
delete {
|
||||||
url: {{host}}/api/snippet?slug=bruno-test
|
url: {{host}}/api/snippet?id=d77d3463-c76e-4c53-a1d5-ecaf16c6c54e
|
||||||
body: none
|
body: none
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
params:query {
|
params:query {
|
||||||
slug: bruno-test
|
id: d77d3463-c76e-4c53-a1d5-ecaf16c6c54e
|
||||||
}
|
}
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,18 @@ meta {
|
||||||
}
|
}
|
||||||
|
|
||||||
patch {
|
patch {
|
||||||
url: {{host}}/api/snippet?slug=bruno-test
|
url: {{host}}/api/snippet?id=680a3508-7709-4f71-b5c3-3dcbffe6f5cf
|
||||||
body: json
|
body: json
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
params:query {
|
params:query {
|
||||||
slug: bruno-test
|
id: 680a3508-7709-4f71-b5c3-3dcbffe6f5cf
|
||||||
}
|
}
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"title": "Mock snippet sent via Bruno with updated title",
|
"title": "quick way to push last jj commit to git",
|
||||||
"markdown": "## MOCK SNIPPET\nUpdated via bruno",
|
"tags": ["jj", "git"]
|
||||||
"tags": ["mock", "updated"]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
23
bruno/CodeSnippets/get_snippet.bru
Normal file
23
bruno/CodeSnippets/get_snippet.bru
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
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 {
|
get {
|
||||||
url: {{host}}/api/snippet-by-slug?slug=bruno-test
|
url: {{host}}/api/snippet-by-slug?slug=netcat-over-ping
|
||||||
body: none
|
body: none
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
params:query {
|
params:query {
|
||||||
slug: bruno-test
|
slug: netcat-over-ping
|
||||||
}
|
}
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,13 @@ meta {
|
||||||
}
|
}
|
||||||
|
|
||||||
get {
|
get {
|
||||||
url: {{host}}/api/snippets?limit=2&skip=0
|
url: {{host}}/api/snippets?limit=25&skip=0
|
||||||
body: none
|
body: none
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
params:query {
|
params:query {
|
||||||
limit: 2
|
limit: 25
|
||||||
skip: 0
|
skip: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,13 @@ meta {
|
||||||
}
|
}
|
||||||
|
|
||||||
get {
|
get {
|
||||||
url: {{host}}/api/tag?tag=mock
|
url: {{host}}/api/tag?tag=git
|
||||||
body: none
|
body: none
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
params:query {
|
params:query {
|
||||||
tag: mock
|
tag: git
|
||||||
}
|
}
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
|
|
|
||||||
1
deps.edn
1
deps.edn
|
|
@ -5,7 +5,6 @@
|
||||||
org.slf4j/slf4j-simple {:mvn/version "2.0.16"}
|
org.slf4j/slf4j-simple {:mvn/version "2.0.16"}
|
||||||
|
|
||||||
;; db
|
;; db
|
||||||
com.datomic/local {:mvn/version "1.0.291"}
|
|
||||||
com.xtdb/xtdb-api {:mvn/version "2.0.0-beta9"}
|
com.xtdb/xtdb-api {:mvn/version "2.0.0-beta9"}
|
||||||
com.github.seancorfield/next.jdbc {:mvn/version "1.3.1002"}
|
com.github.seancorfield/next.jdbc {:mvn/version "1.3.1002"}
|
||||||
org.postgresql/postgresql {:mvn/version "42.7.6"}
|
org.postgresql/postgresql {:mvn/version "42.7.6"}
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,9 @@
|
||||||
{:status 200, :body "snippet created"})
|
{:status 200, :body "snippet created"})
|
||||||
|
|
||||||
(defn handle-edit-snippet [{body :body-params params :query-params}]
|
(defn handle-edit-snippet [{body :body-params params :query-params}]
|
||||||
(let [slug (get params "slug")]
|
(let [id (get params "id")]
|
||||||
(t/log! {:level :info, :data {:body body :slug slug}} "Received request to edit snippet")
|
(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 slug body)]
|
(let [{success :success :as res} (snippets.use-cases.edit/edit-snippet id body)]
|
||||||
(cond
|
(cond
|
||||||
success {:status 200, :body "snippet updated"}
|
success {:status 200, :body "snippet updated"}
|
||||||
(= (:reason res) :invalid-patch) {:status 400, :body "invalid patch"}
|
(= (:reason res) :invalid-patch) {:status 400, :body "invalid patch"}
|
||||||
|
|
@ -38,16 +38,18 @@
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (snippets.use-cases.view/view-snippets {:limit limit-num :skip skip-num})})
|
:body (snippets.use-cases.view/view-snippets {:limit limit-num :skip skip-num})})
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (snippets.use-cases.view/view-snippets nil)}))
|
: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)}))
|
||||||
|
|
||||||
(defn handle-delete-snippet [{params :query-params}]
|
(defn handle-delete-snippet [{params :query-params}]
|
||||||
(let [slug (get params "slug")
|
(let [id (get params "id")]
|
||||||
res (snippets.use-cases.delete/delete-snippet slug)]
|
(snippets.use-cases.delete/delete-snippet id)
|
||||||
(if (nil? res)
|
|
||||||
{:status 404
|
|
||||||
:body "No snippet with that slug found"}
|
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (format "Deleted snippet with slug: %s if it existed" slug)})))
|
:body (format "Deleted snippet with id: %s if it existed" id)}))
|
||||||
|
|
||||||
(defn handle-view-tags [_args]
|
(defn handle-view-tags [_args]
|
||||||
(let [tags (snippets.use-cases.view/view-tags)]
|
(let [tags (snippets.use-cases.view/view-tags)]
|
||||||
|
|
@ -60,13 +62,9 @@
|
||||||
:body (snippets.use-cases.view/view-snippets-by-tag tag)}))
|
:body (snippets.use-cases.view/view-snippets-by-tag tag)}))
|
||||||
|
|
||||||
(defn handle-view-snippet-by-slug [{params :query-params}]
|
(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
|
{:status 200
|
||||||
:body snippet})))
|
:body (snippets.use-cases.view/view-snippet-by-slug slug)}))
|
||||||
|
|
||||||
(defn wrap [handler id]
|
(defn wrap [handler id]
|
||||||
(fn [request]
|
(fn [request]
|
||||||
|
|
@ -84,6 +82,7 @@
|
||||||
["/snippet-by-slug" {:get handle-view-snippet-by-slug}]
|
["/snippet-by-slug" {:get handle-view-snippet-by-slug}]
|
||||||
["/snippets" {:get handle-view-snippets}]
|
["/snippets" {:get handle-view-snippets}]
|
||||||
["/snippet" {:post handle-create-snippet
|
["/snippet" {:post handle-create-snippet
|
||||||
|
:get handle-view-snippet
|
||||||
:patch handle-edit-snippet
|
:patch handle-edit-snippet
|
||||||
:delete handle-delete-snippet}]])
|
:delete handle-delete-snippet}]])
|
||||||
(rr/create-default-handler)))
|
(rr/create-default-handler)))
|
||||||
|
|
|
||||||
|
|
@ -1,218 +0,0 @@
|
||||||
(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,9 +1,6 @@
|
||||||
(ns snippets.main
|
(ns snippets.main
|
||||||
(:require
|
(:require [snippets.infra.api :as api])
|
||||||
[snippets.infra.api :as api]
|
|
||||||
[snippets.infra.db2 :refer [start-up-check]])
|
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn -main []
|
(defn -main []
|
||||||
(start-up-check)
|
|
||||||
(api/run-server))
|
(api/run-server))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
(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"))})
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
(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)))
|
|
||||||
25
src/snippets/use_cases/backfill_from_file.clj
Normal file
25
src/snippets/use_cases/backfill_from_file.clj
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
(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,9 +1,12 @@
|
||||||
(ns snippets.use-cases.create
|
(ns snippets.use-cases.create
|
||||||
(:require
|
(:require
|
||||||
[taoensso.telemere :as t]
|
[taoensso.telemere :as t]
|
||||||
[snippets.infra.db2 :as db]))
|
[snippets.infra.db :as db]))
|
||||||
|
|
||||||
|
(defn- uuid [] (str (java.util.UUID/randomUUID)))
|
||||||
|
|
||||||
(defn create-snippet [{:keys [title slug markdown tags]}]
|
(defn create-snippet [{:keys [title slug markdown tags]}]
|
||||||
(let [pub-date (java.util.Date.)]
|
(let [id (uuid)
|
||||||
(t/log! {:level :info, :data {:title title :slug slug}} "Creating snippet")
|
pub-date (java.util.Date.)]
|
||||||
(db/create-snippets [{:title title :slug slug :markdown markdown :tags tags :pub-date pub-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})))
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
(ns snippets.use-cases.delete
|
(ns snippets.use-cases.delete
|
||||||
(:require
|
(:require
|
||||||
[snippets.infra.db2 :as db]
|
[snippets.infra.db :as db]
|
||||||
[taoensso.telemere :as t]))
|
[taoensso.telemere :as t]))
|
||||||
|
|
||||||
(defn delete-snippet [slug]
|
(defn delete-snippet [key]
|
||||||
(t/log! {:level :info, :data {:slug slug}} "Deleting snippet by slug")
|
(t/log! {:level :info, :data {:key key}} "Deleting snippet by id")
|
||||||
(db/delete-snippet-by-slug slug))
|
(db/delete-snippet key))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
(:require
|
(:require
|
||||||
[taoensso.telemere :as t]
|
[taoensso.telemere :as t]
|
||||||
[malli.core :as m]
|
[malli.core :as m]
|
||||||
[snippets.infra.db2 :as db]))
|
[snippets.infra.db :as db]))
|
||||||
|
|
||||||
(def valid-patch?
|
(def valid-patch?
|
||||||
(m/validator
|
(m/validator
|
||||||
|
|
@ -12,11 +12,11 @@
|
||||||
[:tags {:optional true} [:seqable :string]]
|
[:tags {:optional true} [:seqable :string]]
|
||||||
[:slug {:optional true} :string]]))
|
[:slug {:optional true} :string]]))
|
||||||
|
|
||||||
(defn edit-snippet [slug patch]
|
(defn edit-snippet [id patch]
|
||||||
(t/log! {:level :info, :data {:patch patch :slug slug}} "Editing snippet")
|
(t/log! {:level :info, :data {:patch patch :id id}} "Editing snippet")
|
||||||
(if (valid-patch? patch)
|
(if (valid-patch? patch)
|
||||||
(do
|
(do
|
||||||
(t/log! {:level :info, :data {:patch patch :slug slug}} "Valid changes editing snippet")
|
(t/log! {:level :info, :data {:patch patch :id id}} "Valid changes editing snippet")
|
||||||
(db/update-snippet slug patch)
|
(db/patch-snippet id patch)
|
||||||
{:success true})
|
{:success true})
|
||||||
{:success false :reason :invalid-patch}))
|
{:success false :reason :invalid-patch}))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
(ns snippets.use-cases.view
|
(ns snippets.use-cases.view
|
||||||
(:require
|
(:require
|
||||||
[taoensso.telemere :as t]
|
[taoensso.telemere :as t]
|
||||||
[snippets.infra.db2 :as db]))
|
[snippets.infra.db :as db]))
|
||||||
|
|
||||||
(defn serialize-snippet
|
(defn serialize-snippet
|
||||||
"Converts snippet pub-date to ISO-8601 string for EDN serialization"
|
"Converts snippet pub-date to ISO-8601 string for EDN serialization"
|
||||||
|
|
@ -9,15 +9,12 @@
|
||||||
(when snippet
|
(when snippet
|
||||||
(assoc snippet :pub-date (.toString (:pub-date snippet)))))
|
(assoc snippet :pub-date (.toString (:pub-date snippet)))))
|
||||||
|
|
||||||
(defn view-snippets [options]
|
(defn view-snippet [key]
|
||||||
(if (nil? options)
|
(t/log! {:level :info, :data {:key key}} "Viewing snippet by id")
|
||||||
(map serialize-snippet (db/list-snippets))
|
(serialize-snippet (db/get-snippet-by-id key)))
|
||||||
(let [limit (:limit options)
|
|
||||||
skip (:skip options)]
|
(defn view-snippets [& args]
|
||||||
(->> (db/list-snippets)
|
(map serialize-snippet (db/list-snippets args)))
|
||||||
(drop skip)
|
|
||||||
(take limit)
|
|
||||||
(map serialize-snippet)))))
|
|
||||||
|
|
||||||
(defn view-tags []
|
(defn view-tags []
|
||||||
(t/log! {:level :info} "Viewing tags")
|
(t/log! {:level :info} "Viewing tags")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue