Skip to content

Commit

Permalink
support pagination for List API; improve code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Mar 16, 2020
1 parent b808fc3 commit 620873e
Show file tree
Hide file tree
Showing 43 changed files with 909 additions and 392 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@

All notable changes to this project will be documented in this file starting from version **v1.0.0**.
This project adheres to [Semantic Versioning](http://semver.org/).

-----

## [1.1.0] - 2020-03-14

**Changed:**

- Support kind for group.
- Support pagination for List API.
- Improve SQL schemas.
- Improve code.
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addr: ":3000"
addr: ":8080"
cert_file:
key_file:
logger:
Expand Down
75 changes: 38 additions & 37 deletions sql/schema.sql
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
-- Keywords and Reserved Words https://dev.mysql.com/doc/refman/5.7/en/keywords.html name status channel value values group user
CREATE DATABASE IF NOT EXISTS `urbs` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE DATABASE IF NOT EXISTS `urbs` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`urbs_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`active_at` bigint NOT NULL DEFAULT 0,
`uid` varchar(63) NOT NULL,
`labels` varchar(8190) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_uid` (`uid`),
KEY `idx_user_active_at` (`active_at`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`urbs_group` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`sync_at` bigint NOT NULL DEFAULT 0,
`uid` varchar(63) NOT NULL,
`kind` varchar(63) NOT NULL DEFAULT '',
`description` varchar(1022) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_group_uid` (`uid`),
KEY `idx_group_sync_at` (`sync_at`)
) ENGINE=InnoDB;
KEY `idx_group_kind` (`kind`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`urbs_product` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` datetime DEFAULT NULL,
`offline_at` datetime DEFAULT NULL,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`deleted_at` datetime(3) DEFAULT NULL,
`offline_at` datetime(3) DEFAULT NULL,
`name` varchar(63) NOT NULL,
`description` varchar(1022) NOT NULL DEFAULT '',
`status` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_product_name` (`name`),
KEY `idx_product_created_at` (`created_at`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`urbs_label` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`offline_at` datetime DEFAULT NULL,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`offline_at` datetime(3) DEFAULT NULL,
`product_id` bigint NOT NULL,
`name` varchar(63) NOT NULL,
`description` varchar(1022) NOT NULL DEFAULT '',
Expand All @@ -51,26 +52,26 @@ CREATE TABLE IF NOT EXISTS `urbs`.`urbs_label` (
`status` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_label_product_id_name` (`product_id`,`name`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`urbs_module` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`offline_at` datetime DEFAULT NULL,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`offline_at` datetime(3) DEFAULT NULL,
`product_id` bigint NOT NULL,
`name` varchar(63) NOT NULL,
`description` varchar(1022) NOT NULL DEFAULT '',
`status` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_module_product_id_name` (`product_id`,`name`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`urbs_setting` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`offline_at` datetime DEFAULT NULL,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`offline_at` datetime(3) DEFAULT NULL,
`module_id` bigint NOT NULL,
`name` varchar(63) NOT NULL,
`description` varchar(1022) NOT NULL DEFAULT '',
Expand All @@ -80,57 +81,57 @@ CREATE TABLE IF NOT EXISTS `urbs`.`urbs_setting` (
`status` bigint NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_setting_module_id_name` (`module_id`,`name`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`user_group` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`sync_at` bigint NOT NULL,
`user_id` bigint NOT NULL,
`group_id` bigint NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_group_user_id_group_id` (`user_id`,`group_id`),
KEY `idx_user_group_sync_at` (`sync_at`)
) ENGINE=InnoDB;
KEY `idx_user_group_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`user_label` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`user_id` bigint NOT NULL,
`label_id` bigint NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_label_user_id_label_id` (`user_id`,`label_id`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`user_setting` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`user_id` bigint NOT NULL,
`setting_id` bigint NOT NULL,
`value` varchar(255) NOT NULL DEFAULT '',
`last_value` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_user_setting_user_id_setting_id` (`user_id`,`setting_id`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`group_label` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`group_id` bigint NOT NULL,
`label_id` bigint NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uk_group_label_group_id_label_id` (`group_id`,`label_id`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE IF NOT EXISTS `urbs`.`group_setting` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`group_id` bigint NOT NULL,
`setting_id` bigint NOT NULL,
`value` varchar(255) NOT NULL DEFAULT '',
`last_value` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_group_setting_group_id_setting_id` (`group_id`,`setting_id`)
) ENGINE=InnoDB;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
61 changes: 61 additions & 0 deletions sql/update_20200314.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
ALTER TABLE `urbs_user` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_user` MODIFY COLUMN `uid` varchar(63) NOT NULL COLLATE utf8mb4_bin;
ALTER TABLE `urbs_user` MODIFY COLUMN `labels` varchar(8190) NOT NULL COLLATE utf8mb4_bin DEFAULT '';

COLLATE=utf8mb4_bin

ALTER TABLE `urbs_group` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_group` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_group` MODIFY COLUMN `uid` varchar(63) NOT NULL COLLATE utf8mb4_bin;
ALTER TABLE `urbs_group` MODIFY COLUMN `description` varchar(1022) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_group` ADD COLUMN `kind` varchar(63) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_group` DROP INDEX `idx_group_sync_at`;
ALTER TABLE `urbs_group` ADD INDEX `idx_group_kind` (`kind`);

ALTER TABLE `urbs_product` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_product` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_product` MODIFY COLUMN `deleted_at` datetime(3) DEFAULT NULL;
ALTER TABLE `urbs_product` MODIFY COLUMN `offline_at` datetime(3) DEFAULT NULL;
ALTER TABLE `urbs_product` MODIFY COLUMN `name` varchar(63) NOT NULL COLLATE utf8mb4_bin;
ALTER TABLE `urbs_product` MODIFY COLUMN `description` varchar(1022) NOT NULL COLLATE utf8mb4_bin DEFAULT '';

ALTER TABLE `urbs_label` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_label` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_label` MODIFY COLUMN `offline_at` datetime(3) DEFAULT NULL;
ALTER TABLE `urbs_label` MODIFY COLUMN `name` varchar(63) NOT NULL COLLATE utf8mb4_bin;
ALTER TABLE `urbs_label` MODIFY COLUMN `description` varchar(1022) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_label` MODIFY COLUMN `channels` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_label` MODIFY COLUMN `clients` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';

ALTER TABLE `urbs_module` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_module` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_module` MODIFY COLUMN `offline_at` datetime(3) DEFAULT NULL;
ALTER TABLE `urbs_module` MODIFY COLUMN `name` varchar(63) NOT NULL COLLATE utf8mb4_bin;
ALTER TABLE `urbs_module` MODIFY COLUMN `description` varchar(1022) NOT NULL COLLATE utf8mb4_bin DEFAULT '';

ALTER TABLE `urbs_setting` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_setting` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `urbs_setting` MODIFY COLUMN `offline_at` datetime(3) DEFAULT NULL;
ALTER TABLE `urbs_setting` MODIFY COLUMN `name` varchar(63) NOT NULL COLLATE utf8mb4_bin;
ALTER TABLE `urbs_setting` MODIFY COLUMN `description` varchar(1022) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_setting` MODIFY COLUMN `channels` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_setting` MODIFY COLUMN `clients` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `urbs_setting` MODIFY COLUMN `vals` varchar(1022) NOT NULL COLLATE utf8mb4_bin DEFAULT '';

ALTER TABLE `user_group` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `user_group` DROP INDEX `idx_user_group_sync_at`;
ALTER TABLE `user_group` ADD INDEX `idx_user_group_group_id` (`group_id`);

ALTER TABLE `user_label` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);

ALTER TABLE `user_setting` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `user_setting` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `user_setting` MODIFY COLUMN `value` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `user_setting` MODIFY COLUMN `last_value` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';

ALTER TABLE `group_label` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);

ALTER TABLE `group_setting` MODIFY COLUMN `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3);
ALTER TABLE `group_setting` MODIFY COLUMN `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3);
ALTER TABLE `group_setting` MODIFY COLUMN `value` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
ALTER TABLE `group_setting` MODIFY COLUMN `last_value` varchar(255) NOT NULL COLLATE utf8mb4_bin DEFAULT '';
18 changes: 18 additions & 0 deletions src/api/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"os"
"testing"

"github.com/DavidCai1993/request"
Expand Down Expand Up @@ -37,6 +38,23 @@ func SetUpTestTools() (tt *TestTools, cleanup func()) {
}
}

func TestMain(m *testing.M) {
tt, cleanup := SetUpTestTools()
tt.DB.Exec("TRUNCATE TABLE urbs_user;")
tt.DB.Exec("TRUNCATE TABLE urbs_group;")
tt.DB.Exec("TRUNCATE TABLE urbs_product;")
tt.DB.Exec("TRUNCATE TABLE urbs_label;")
tt.DB.Exec("TRUNCATE TABLE urbs_module;")
tt.DB.Exec("TRUNCATE TABLE urbs_setting;")
tt.DB.Exec("TRUNCATE TABLE user_group;")
tt.DB.Exec("TRUNCATE TABLE user_label;")
tt.DB.Exec("TRUNCATE TABLE user_setting;")
tt.DB.Exec("TRUNCATE TABLE group_label;")
tt.DB.Exec("TRUNCATE TABLE group_setting;")
cleanup()
os.Exit(m.Run())
}

func TestApp(t *testing.T) {
tt, cleanup := SetUpTestTools()
defer cleanup()
Expand Down
25 changes: 16 additions & 9 deletions src/api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ type Group struct {

// List ..
func (a *Group) List(ctx *gear.Context) error {
res, err := a.blls.Group.List(ctx)
req := tpl.Pagination{}
if err := ctx.ParseURL(&req); err != nil {
return err
}

res, err := a.blls.Group.List(ctx, req)
if err != nil {
return err
}
Expand All @@ -25,12 +30,12 @@ func (a *Group) List(ctx *gear.Context) error {

// ListLables ..
func (a *Group) ListLables(ctx *gear.Context) error {
req := tpl.LabelsURL{}
req := tpl.UIDPaginationURL{}
if err := ctx.ParseURL(&req); err != nil {
return err
}

res, err := a.blls.Group.ListLables(ctx, req.UID, req.Product)
res, err := a.blls.Group.ListLables(ctx, req.UID, req.Pagination)
if err != nil {
return err
}
Expand All @@ -40,12 +45,12 @@ func (a *Group) ListLables(ctx *gear.Context) error {

// ListMembers ..
func (a *Group) ListMembers(ctx *gear.Context) error {
req := tpl.UIDURL{}
req := tpl.UIDPaginationURL{}
if err := ctx.ParseURL(&req); err != nil {
return err
}

res, err := a.blls.Group.ListMembers(ctx, req.UID)
res, err := a.blls.Group.ListMembers(ctx, req.UID, req.Pagination)
if err != nil {
return err
}
Expand Down Expand Up @@ -135,8 +140,9 @@ func (a *Group) RemoveLable(ctx *gear.Context) error {
return err
}
label := &schema.Label{}
if err := service.HIDer.PutHID(label, req.HID); err != nil {
return err
label.ID = service.HIDToID(req.HID, "label")
if label.ID == 0 {
return gear.ErrBadRequest.WithMsgf("invalid hid: %s", req.HID)
}
if err := a.blls.Group.RemoveLable(ctx, req.UID, label.ID); err != nil {
return err
Expand All @@ -157,8 +163,9 @@ func (a *Group) RemoveSetting(ctx *gear.Context) error {
return err
}
setting := &schema.Setting{}
if err := service.HIDer.PutHID(setting, req.HID); err != nil {
return err
setting.ID = service.HIDToID(req.HID, "setting")
if setting.ID == 0 {
return gear.ErrBadRequest.WithMsgf("invalid hid: %s", req.HID)
}
if err := a.blls.Group.RemoveSetting(ctx, req.UID, setting.ID); err != nil {
return err
Expand Down
Loading

0 comments on commit 620873e

Please sign in to comment.