Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,3 @@
|
||||
开始任务前先读取 `/dm/AGENTS.md`,再读取目标路径适用的子级规则。
|
||||
|
||||
本项目以 CMS 为例演示 Dever 的数据模型与 HTTP 绑定,后台使用受保护入口,前台只公开已发布文章。普通 CRUD 优先 REST;有状态约束才写业务 flow。不默认加入版本控制和无用途的写后回查。真实登录 Provider 尚未接入,不将本例声称为完整可运行后台,不添加假 verify 或匿名管理入口。
|
||||
@@ -0,0 +1,78 @@
|
||||
# 小 CMS:文章、分类与评论
|
||||
|
||||
本例使用数据模型和事务管理文章、分类与评论。前台可匿名阅读已发布的文章;后台 HTTP 入口需要配置身份 Provider。示例不提供管理网页或登录服务。[发布流程测试](test/content/article/publishing.dv)和[分类与评论测试](test/content/article/discussion.dv)使用临时 SQLite 数据库。
|
||||
|
||||
在项目目录执行 `dever check .` 和 `dever test .`;测试会隔离数据库,不读写示例 data。源码工作流见自动生成的 [link.md](module/content/article/link.md)。
|
||||
|
||||
## 基础 CRUD
|
||||
|
||||
普通文章数据模型配上 REST 入口,核心就是:
|
||||
|
||||
~~~dever
|
||||
type Article {
|
||||
title: Text(1, 200)
|
||||
body: Text raw
|
||||
}
|
||||
~~~
|
||||
|
||||
~~~dever
|
||||
rest data
|
||||
~~~
|
||||
|
||||
常规 CRUD 直接使用 REST 绑定,无需编写转发流程。REST 的入口解码、字段约束、数据库操作和权限由既有能力承担;它仍需要真实身份配置,不是匿名接口。
|
||||
|
||||
## 有发布规则时才写 App
|
||||
|
||||
本例额外要求:只编辑和删除草稿,发布后可公开阅读,撤回后不再公开。因此文章使用明确业务入口,不能用无限制 replace 绕过这些规则。
|
||||
|
||||
- [data/cms.dv](module/content/article/data/article.dv):文章类型、字段约束与默认值;分类和评论分别在 [category.dv](module/content/article/data/category.dv)、[comment.dv](module/content/article/data/comment.dv)。
|
||||
- [manage.dv](module/content/article/api/admin/manage.dv):后台 HTTP 绑定。
|
||||
- [flow/cms.dv](module/content/article/flow/article.dv):真正需要的文章业务。
|
||||
- [browse.dv](module/content/article/api/front/browse.dv):公开阅读和评论绑定。
|
||||
|
||||
后台入口绑定到本领域 App:
|
||||
|
||||
~~~dever
|
||||
post create = app.create
|
||||
put edit = app.edit
|
||||
post publish = app.publish
|
||||
post unpublish = app.unpublish
|
||||
delete remove = app.remove
|
||||
get list = app.list
|
||||
~~~
|
||||
|
||||
## 请求怎么写
|
||||
|
||||
以下是接好真实认证后的请求合同。后台请求必须携带有效身份,并具有对应入口权限;本例业务 Test 不代替 HTTP 权限验收。
|
||||
|
||||
| 操作 | HTTP 路径 | 输入 |
|
||||
| --- | --- | --- |
|
||||
| 创建 | POST /content/article/admin/manage/create | JSON:title、body |
|
||||
| 编辑 | PUT /content/article/admin/manage/edit | JSON:article_id、title、body |
|
||||
| 发布 | POST /content/article/admin/manage/publish | JSON:article_id |
|
||||
| 撤回 | POST /content/article/admin/manage/unpublish | JSON:article_id |
|
||||
| 删除 | DELETE /content/article/admin/manage/remove | query:article_id |
|
||||
| 后台列表 | GET /content/article/admin/manage/list | query:page |
|
||||
| 公开列表 | GET /content/article/front/browse/list | query:page |
|
||||
| 公开详情 | GET /content/article/front/browse/detail | query:article_id |
|
||||
| 创建分类 | POST /content/article/admin/manage/createCategory | JSON:name |
|
||||
| 设置分类 | PUT /content/article/admin/manage/assignCategory | JSON:article_id、category_id |
|
||||
| 提交评论 | POST /content/article/front/browse/comment | JSON:article_id、author、body |
|
||||
| 审核评论 | POST /content/article/admin/manage/moderate | JSON:comment_id、approve |
|
||||
| 公开评论 | GET /content/article/front/browse/comments | query:target_id、page |
|
||||
|
||||
创建正文例如 {"title":"第一篇文章","body":"你好,Dever。"}。创建返回 Article,后续使用实际返回的 id;编辑、发布、撤回和删除成功时 data 为 null,不为返回整条记录额外查询。
|
||||
|
||||
## 保留什么,省掉什么
|
||||
|
||||
字段长度、标题规范化和默认值只在数据模型声明。状态条件与修改在同一条 SQL 中执行,未更新到记录则报 Conflict;公开详情未找到则报 NotFound。不先查询再重复检查,不吞错或自动重试。
|
||||
|
||||
两个合法编辑按最后一次成功写入生效,本例不提供协同编辑冲突检测。发布/撤回保留状态约束,失败回滚。
|
||||
|
||||
评论默认待审核,只能提交给已发布文章;审核通过后才公开。提交与撤回通过同一文章行的条件更新串行,撤回后不再公开评论。审核使用 rule 表,取批准或拒绝的状态;没有另建审核服务或转换层。
|
||||
|
||||
## 认证接入与验收边界
|
||||
|
||||
认证按 [当前身份与权限合同](../../../LANGUAGE.md) 接入真实账号、会话及 Provider。本例没有假 verify。运行前必须完成 admin 站点的 Provider、verify、登录和授权配置;缺失时明确拒绝启动,不能将受保护接口降为匿名。
|
||||
|
||||
当前 Test 已验证真实创建、字段规范化、草稿隐藏、编辑、发布、公开查询、撤回隐藏和删除,以及分类外键、评论默认状态、审核和撤回后的评论隐藏。仍需随真实认证环境验收后台匿名/越权拒绝、状态冲突和 HTTP 错误响应;数据库并发错误照常传播。
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"database": {
|
||||
"default": {
|
||||
"type": "sqlite",
|
||||
"path": "data/cms.db"
|
||||
}
|
||||
},
|
||||
"sites": {
|
||||
"admin": { "path": "admin" },
|
||||
"front": { "path": "front" }
|
||||
},
|
||||
"http": { "host": "127.0.0.1", "port": 18090 },
|
||||
"log": { "level": "info" }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
负责文章草稿、发布和公开阅读。字段见 [data/article.dv](data/article.dv),主流程见 [flow/article.dv](flow/article.dv),完整用法见 [项目说明](../../../README.md),符号说明见 [文章主题](doc/article.md)。
|
||||
@@ -0,0 +1,17 @@
|
||||
post create = app.create
|
||||
|
||||
put edit = app.edit
|
||||
|
||||
post publish = app.publish
|
||||
|
||||
post unpublish = app.unpublish
|
||||
|
||||
delete remove = app.remove
|
||||
|
||||
get list = app.list
|
||||
|
||||
post createCategory = app.createCategory
|
||||
|
||||
put assignCategory = app.assignCategory
|
||||
|
||||
post moderate = app.moderate
|
||||
@@ -0,0 +1,7 @@
|
||||
public get list = app.browse
|
||||
|
||||
public get detail = app.detail
|
||||
|
||||
public post comment = app.comment
|
||||
|
||||
public get comments = app.comments
|
||||
@@ -0,0 +1,12 @@
|
||||
type Status {
|
||||
Draft
|
||||
Published
|
||||
}
|
||||
|
||||
type Article {
|
||||
title: Text(1, 120)
|
||||
body: Text(1, 20000) raw
|
||||
status: Status default Draft index
|
||||
published_at: DateTime?
|
||||
category_id: data.category.id?
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
type Category {
|
||||
name: Text(1, 80) unique
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
type Status {
|
||||
Pending
|
||||
Approved
|
||||
Rejected
|
||||
}
|
||||
|
||||
type Comment {
|
||||
article_id: data.id
|
||||
author: Text(1, 40)
|
||||
body: Text(1, 2000)
|
||||
status: Status default Pending index
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
# 文章
|
||||
|
||||
## content.article.app.publish
|
||||
|
||||
只有草稿可以发布。条件更新与结果检查在同一个事务内完成;成功无业务输出,不为回传整条文章再查询一次。
|
||||
|
||||
## content.article.app.edit
|
||||
|
||||
仅编辑草稿。数据模型处理字段规范化和长度约束,业务不重复检查。本例采用最后一次成功写入生效,不提供多人编辑的版本冲突检测。
|
||||
|
||||
## content.article.app.browse
|
||||
|
||||
公开返回已发布文章,每页十条。后台完整列表绑定需要身份与角色权限的 HTTP GET。
|
||||
|
||||
## content.article.app.comment
|
||||
|
||||
只有已发布的文章可以评论,其他情况按不存在处理。在事务中以条件更新锁定文章,再写评论,避免检查发布状态后被并发撤回。评论默认待审核,不在公开列表中出现。`article_id` 是普通外键;自动关系不参与默认接口输出。
|
||||
|
||||
## content.article.app.moderate
|
||||
|
||||
审核只处理待审核的评论,决定由 `app.verdict` 的规则表选择,条件更新与结果检查在同一个事务内完成;已审核的评论再次审核按冲突处理。
|
||||
|
||||
## content.article.app.comments
|
||||
|
||||
公开返回已发布文章通过审核的评论,每页二十条,按创建时间升序。撤回后不再公开评论。
|
||||
|
||||
## content.article.app.createCategory
|
||||
|
||||
分类名称在数据模型中唯一,重复创建由数据库约束拒绝。
|
||||
|
||||
## content.article.app.assignCategory
|
||||
|
||||
把文章归入分类;文章不存在时按不存在处理。`category_id` 是普通外键,文章直接输出存储字段。
|
||||
@@ -0,0 +1,65 @@
|
||||
transaction flow create(title: Text, body: Text) (article: data.Article) {
|
||||
article = data.create({ title = title, body = body })
|
||||
}
|
||||
|
||||
transaction flow edit(article_id: data.id, title: Text, body: Text) () {
|
||||
changed = data.update({ where = id == article_id and status == Draft }, { title = title, body = body })
|
||||
check changed == 1 else Conflict
|
||||
}
|
||||
|
||||
transaction flow publish(article_id: data.id) () {
|
||||
now = dever.time.now()
|
||||
changed = data.update({ where = id == article_id and status == Draft }, { status = Published, published_at = now })
|
||||
check changed == 1 else Conflict
|
||||
}
|
||||
|
||||
transaction flow unpublish(article_id: data.id) () {
|
||||
changed = data.update({ where = id == article_id and status == Published }, { status = Draft, published_at = null })
|
||||
check changed == 1 else Conflict
|
||||
}
|
||||
|
||||
transaction flow remove(article_id: data.id) () {
|
||||
changed = data.delete({ where = id == article_id and status == Draft })
|
||||
check changed == 1 else Conflict
|
||||
}
|
||||
|
||||
flow list(page: Int >= 1) (articles: data.Page) {
|
||||
articles = data.page(order = [created_at.desc, id.desc], page = page, size = 10)
|
||||
}
|
||||
|
||||
flow browse(page: Int >= 1) (articles: data.Page) {
|
||||
articles = data.page(status == Published, order = [published_at.desc, id.desc], page = page, size = 10)
|
||||
}
|
||||
|
||||
flow detail(article_id: data.id) (article: data.Article) {
|
||||
found = data.first(id == article_id and status == Published)
|
||||
check found != null else NotFound
|
||||
article = found
|
||||
}
|
||||
|
||||
transaction flow createCategory(name: Text) (category: data.category.Category) {
|
||||
category = data.category.create({ name = name })
|
||||
}
|
||||
|
||||
transaction flow assignCategory(article_id: data.id, category_id: data.category.id) () {
|
||||
changed = data.update({ where = id == article_id }, { category_id = category_id })
|
||||
check changed == 1 else NotFound
|
||||
}
|
||||
|
||||
transaction flow comment(article_id: data.id, author: Text, body: Text) (comment: data.comment.Comment) {
|
||||
accepted = data.update({ where = id == article_id and status == Published }, { status = Published })
|
||||
check accepted == 1 else NotFound
|
||||
comment = data.comment.create({ article_id = article_id, author = author, body = body })
|
||||
}
|
||||
|
||||
transaction flow moderate(comment_id: data.comment.id, approve: Bool) () {
|
||||
chosen = app.verdict(approve)
|
||||
changed = data.comment.update({ where = id == comment_id and status == Pending }, { status = chosen })
|
||||
check changed == 1 else Conflict
|
||||
}
|
||||
|
||||
flow comments(target_id: data.id, page: Int >= 1) (comments: data.comment.Page) {
|
||||
article = data.first(id == target_id and status == Published)
|
||||
check article != null else NotFound
|
||||
comments = data.comment.page(article_id == target_id and status == Approved, order = [created_at.asc, id.asc], page = page, size = 20)
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,5 @@
|
||||
rule verdict(approve: Bool) (status: data.comment.Status)
|
||||
approve {
|
||||
true => Approved
|
||||
false => Rejected
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
flow discussion() ()
|
||||
| step | kind | do |
|
||||
| article | flow | app.create("讨论文章", "正文") |
|
||||
| - | flow | app.publish(article.id) |
|
||||
| first | flow | app.comment(article.id, "小明", "写得好") |
|
||||
| second | flow | app.comment(article.id, "小红", "广告内容") |
|
||||
| - | calc | assertEq(first.status, data.comment.Status.Pending) |
|
||||
| pending | flow | app.comments(article.id, 1) |
|
||||
| - | calc | assertEq(pending.total, 0) |
|
||||
| - | flow | app.moderate(first.id, true) |
|
||||
| - | flow | app.moderate(second.id, false) |
|
||||
| visible | flow | app.comments(article.id, 1) |
|
||||
| - | calc | assertEq(visible.total, 1) |
|
||||
| category | flow | app.createCategory("新闻") |
|
||||
| - | flow | app.assignCategory(article.id, category.id) |
|
||||
| assigned | flow | app.detail(article.id) |
|
||||
| - | calc | assertEq(assigned.category_id, category.id) |
|
||||
| - | flow | app.unpublish(article.id) |
|
||||
| hidden | flow | app.browse(1) |
|
||||
| - | calc | assertEq(hidden.total, 0) |
|
||||
@@ -0,0 +1,21 @@
|
||||
flow publishing() ()
|
||||
| step | kind | do |
|
||||
| created | flow | app.create(" 第一篇 ", "原始正文") |
|
||||
| - | calc | assertEq(created.title, "第一篇") |
|
||||
| - | calc | assertEq(created.status, data.Status.Draft) |
|
||||
| drafts | flow | app.browse(1) |
|
||||
| - | calc | assertEq(drafts.total, 0) |
|
||||
| - | flow | app.edit(created.id, "修改后的标题", "修改后的正文") |
|
||||
| - | flow | app.publish(created.id) |
|
||||
| published | flow | app.detail(created.id) |
|
||||
| - | calc | assertEq(published.status, data.Status.Published) |
|
||||
| - | calc | assertEq(published.title, "修改后的标题") |
|
||||
| - | calc | assert(published.published_at != null) |
|
||||
| visible | flow | app.browse(1) |
|
||||
| - | calc | assertEq(visible.total, 1) |
|
||||
| - | flow | app.unpublish(created.id) |
|
||||
| hidden | flow | app.browse(1) |
|
||||
| - | calc | assertEq(hidden.total, 0) |
|
||||
| - | flow | app.remove(created.id) |
|
||||
| remaining | flow | app.list(1) |
|
||||
| - | calc | assertEq(remaining.total, 0) |
|
||||
Reference in new issue
Block a user