WIP: XZH-21 后端改动 + 测试验收附加用例
This commit is contained in:
commit
1bd637528d
168 files changed
+13874
No files matched your search
@@ -0,0 +1,148 @@
|
||||
package com.myspace.admin;
|
||||
|
||||
import com.myspace.common.WorkspaceNotifications;
|
||||
import com.myspace.library.LibraryFields;
|
||||
import com.myspace.library.TrashService;
|
||||
import com.myspace.library.LibraryController;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/planner")
|
||||
public class PlannerController {
|
||||
private static final Set<String> KINDS = Set.of("tasks","events","snippets");
|
||||
private static final String FIELDS = """
|
||||
id,kind,title,body,category,tags,completed,priority,
|
||||
to_char(starts_at,'YYYY-MM-DD"T"HH24:MI') as starts_at,
|
||||
to_char(ends_at,'YYYY-MM-DD"T"HH24:MI') as ends_at,language,created_at,updated_at
|
||||
""";
|
||||
private final JdbcTemplate db;
|
||||
private final TrashService trash;
|
||||
public PlannerController(JdbcTemplate db,TrashService trash) { this.db = db; this.trash = trash; }
|
||||
|
||||
public record Input(@NotBlank @Size(max=160) String title, @NotNull @Size(max=100000) String body,
|
||||
@Size(max=60) String category, @Size(max=1500) String tags, boolean completed,
|
||||
@Pattern(regexp="low|normal|high") String priority, LocalDateTime starts_at,
|
||||
LocalDateTime ends_at, @Size(max=40) String language) {}
|
||||
public record Completion(boolean completed) {}
|
||||
|
||||
@GetMapping("/{kind}")
|
||||
public List<Map<String,Object>> list(@PathVariable String kind,
|
||||
@RequestParam(defaultValue="") String q, @RequestParam(defaultValue="all") String status,
|
||||
@RequestParam(defaultValue="0") @Min(0) int offset,
|
||||
@RequestParam(required=false) LocalDateTime from,
|
||||
@RequestParam(required=false) LocalDateTime until) {
|
||||
check(kind);
|
||||
return db.queryForList("select "+FIELDS+" from planner_items where kind=? and deleted_at is null"
|
||||
+ " and position(lower(?) in lower(title || ' ' || body || ' ' || tags || ' ' || category))>0"
|
||||
+ " and (?='all' or completed=(?='done'))"
|
||||
+ " and (?::timestamp is null or coalesce(ends_at,starts_at)>=?)"
|
||||
+ " and (?::timestamp is null or starts_at<?)"
|
||||
+ " order by starts_at nulls last,updated_at desc,id limit 100 offset ?",
|
||||
kind,q,status,status,from,from,until,until,offset);
|
||||
}
|
||||
|
||||
@PostMapping("/{kind}/batch")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
@Transactional
|
||||
public void batch(@PathVariable String kind,@Valid @RequestBody LibraryController.Batch input) {
|
||||
check(kind);
|
||||
for(UUID id:input.ids().stream().distinct().sorted().toList()) {
|
||||
switch(input.action()) {
|
||||
case "trash" -> { if(trashOne(kind,id)!=1) throw missing(); }
|
||||
case "category" -> {
|
||||
if(input.value()==null || input.value().isBlank() || input.value().strip().length()>60)
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"请输入有效分类");
|
||||
require(db.update("update planner_items set category=?,updated_at=now() where kind=? and id=? and deleted_at is null",
|
||||
input.value().strip(),kind,id));
|
||||
}
|
||||
case "tags" -> {
|
||||
var tags=db.queryForList("select tags from planner_items where kind=? and id=? and deleted_at is null for update",String.class,kind,id)
|
||||
.stream().findFirst().orElseThrow(PlannerController::missing);
|
||||
require(db.update("update planner_items set tags=?,updated_at=now() where id=?",
|
||||
LibraryFields.tags(tags+","+(input.value()==null?"":input.value())),id));
|
||||
}
|
||||
default -> throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"操作不适用于此模块");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/{kind}/{id}")
|
||||
public Map<String,Object> get(@PathVariable String kind,@PathVariable UUID id) {
|
||||
check(kind);
|
||||
return db.queryForList("select "+FIELDS+" from planner_items where kind=? and id=? and deleted_at is null",kind,id)
|
||||
.stream().findFirst().orElseThrow(PlannerController::missing);
|
||||
}
|
||||
|
||||
@PostMapping("/{kind}")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@Transactional
|
||||
public Map<String,Object> create(@PathVariable String kind,@Valid @RequestBody Input input) {
|
||||
validate(kind,input);
|
||||
UUID id=UUID.randomUUID();
|
||||
db.update("insert into planner_items(id,kind,title,body,category,tags,completed,priority,starts_at,ends_at,language)"
|
||||
+ " values(?,?,?,?,?,?,?,?,?,?,?)",id,kind,input.title().strip(),input.body(),category(input),
|
||||
LibraryFields.tags(input.tags()),input.completed(),priority(input),input.starts_at(),input.ends_at(),language(input));
|
||||
return get(kind,id);
|
||||
}
|
||||
|
||||
@PutMapping("/{kind}/{id}")
|
||||
@Transactional
|
||||
public Map<String,Object> update(@PathVariable String kind,@PathVariable UUID id,@Valid @RequestBody Input input) {
|
||||
validate(kind,input);
|
||||
require(db.update("update planner_items set title=?,body=?,category=?,tags=?,completed=?,priority=?,"
|
||||
+ " starts_at=?,ends_at=?,language=?,updated_at=now() where kind=? and id=? and deleted_at is null",
|
||||
input.title().strip(),input.body(),category(input),LibraryFields.tags(input.tags()),input.completed(),priority(input),
|
||||
input.starts_at(),input.ends_at(),language(input),kind,id));
|
||||
return get(kind,id);
|
||||
}
|
||||
|
||||
@PutMapping("/tasks/{id}/completion")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void completion(@PathVariable UUID id,@RequestBody Completion input) {
|
||||
require(db.update("update planner_items set completed=?,updated_at=now() where kind='tasks' and id=? and deleted_at is null",
|
||||
input.completed(),id));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{kind}/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable String kind,@PathVariable UUID id) { trashOne(kind,id); }
|
||||
|
||||
/** 先写通知再删除:通知需要读取标题,而 liveTitle 只命中未删除记录。 */
|
||||
private int trashOne(String kind,UUID id) {
|
||||
check(kind);
|
||||
trash.notifyLive(kind,id,"select title from planner_items where kind=? and id=? and deleted_at is null");
|
||||
require(db.update("update planner_items set deleted_at=now() where kind=? and id=? and deleted_at is null",kind,id));
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static void validate(String kind,Input input) {
|
||||
check(kind);
|
||||
if ((kind.equals("events") && input.starts_at()==null)
|
||||
|| (input.ends_at()!=null && (input.starts_at()==null || input.ends_at().isBefore(input.starts_at()))))
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"请检查日程开始和结束时间");
|
||||
if (kind.equals("snippets") && input.body().isBlank())
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"请输入代码");
|
||||
}
|
||||
|
||||
private static String category(Input input) {
|
||||
return input.category()==null || input.category().isBlank() ? "未分类" : input.category().strip();
|
||||
}
|
||||
private static String priority(Input input) { return input.priority()==null ? "normal" : input.priority(); }
|
||||
private static String language(Input input) { return input.language()==null ? "" : input.language().strip(); }
|
||||
private static void check(String kind) { if (!KINDS.contains(kind)) throw missing(); }
|
||||
private static void require(int count) { if (count==0) throw missing(); }
|
||||
private static ResponseStatusException missing() {
|
||||
return new ResponseStatusException(HttpStatus.NOT_FOUND,"记录不存在");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user