数据结构与Protobuf

本章对我们项目的数据结构和proto进行定义。

数据结构

CREATE TABLE categories  (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  is_del BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE topics (
  id BIGSERIAL PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  category_id INT NOT NULL,
  summary VARCHAR(255) NOT NULL,
  content VARCHAR NOT NULL,
  hit INT NOT NULL DEFAULT 0,
  dateline TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
  is_del BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE admins (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) NOT NULL,
  password VARCHAR(255) NOT NULL,
  is_del BOOLEAN NOT NULL DEFAULT FALSE
);

分类服务的定义

syntax = "proto3";

package pb;

message Category {
  int32 id = 1;
  string name = 2;
  bool is_del = 3;
}

// --- 创建分类
message CreateCategoryRequest { string name = 1; }
message CreateCategoryReply { int32 id = 1; }

// -- 修改分类
message EditCategoryRequest {
  int32 id = 1;
  string name = 2;
}
message EditCategoryReply {
  int32 id = 1;
  bool ok = 2;
}

// -- 分类列表
message ListCategoryRequest {
  optional string name = 1; // 根据分类名称查找
  optional bool is_del = 2; // 是否删除
}
message ListCategoryReply { repeated Category categories = 1; }

// -- 删除/恢复分类
message ToggleCategoryRequest { int32 id = 1; }
message ToggleCategoryReply {
  int32 id = 1;
  bool is_del = 2;
}

// -- 分类是否存在
message CategoryExistsRequest {
  oneof condition {
    string name = 1;
    int32 id = 2;
  }
}
message CategoryExistsReply { bool exists = 1; }

// -- 分类详情
message GetCategoryRequest {
  int32 id = 1;
  optional bool is_del = 2;
}
message GetCategoryReply { optional Category category = 1; }

service CategoryService {
  // 创建分类
  rpc CreateCategory(CreateCategoryRequest) returns (CreateCategoryReply);
  // 修改分类
  rpc EditCategory(EditCategoryRequest) returns (EditCategoryReply);
  // 分类列表
  rpc ListCategory(ListCategoryRequest) returns (ListCategoryReply);
  // 删除/恢复分类
  rpc ToggleCategory(ToggleCategoryRequest) returns (ToggleCategoryReply);
  // 分类是否存在
  rpc CategoryExists(CategoryExistsRequest) returns (CategoryExistsReply);
  // 获取分类详情
  rpc GetCategory(GetCategoryRequest) returns (GetCategoryReply);
}

本章代码位于01/数据结构与protobuf分支

要查看完整内容,请先登录