Skip to content

Commit

Permalink
refactor: refactor the HTTP route definition to make the API interfac…
Browse files Browse the repository at this point in the history
…e more compliant with standards
  • Loading branch information
OldSmokeGun committed Nov 24, 2023
1 parent e564e2c commit f857684
Show file tree
Hide file tree
Showing 12 changed files with 736 additions and 713 deletions.
477 changes: 244 additions & 233 deletions internal/app/adapter/server/http/api/docs/docs.go

Large diffs are not rendered by default.

472 changes: 240 additions & 232 deletions internal/app/adapter/server/http/api/docs/swagger.json

Large diffs are not rendered by default.

382 changes: 193 additions & 189 deletions internal/app/adapter/server/http/api/docs/swagger.yaml

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions internal/app/adapter/server/http/handler/v1/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,20 @@ func (h *PermissionHandler) Update(ctx echo.Context) error {
}

type PermissionDetailRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

type PermissionDetailResponse = PermissionInfo

// Detail 权限详情
//
// @Router /v1/permission [get]
// @Router /v1/permission/{id} [get]
// @Summary 权限详情
// @Description 权限详情
// @Tags 权限
// @Accept plain
// @Produce json
// @Param id query integer true "权限 id" format(uint) minimum(1)
// @Param id path integer true "权限 id" format(uint) minimum(1)
// @Success 200 {object} example.Success{data=PermissionDetailResponse} "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down Expand Up @@ -214,18 +214,18 @@ func (h *PermissionHandler) Detail(ctx echo.Context) error {
}

type PermissionDeleteRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

// Delete 权限删除
//
// @Router /v1/permission [delete]
// @Router /v1/permission/{id} [delete]
// @Summary 权限删除
// @Description 权限删除
// @Tags 权限
// @Accept plain
// @Produce json
// @Param id query integer true "权限 id" format(uint) minimum(1)
// @Param id path integer true "权限 id" format(uint) minimum(1)
// @Success 200 {object} example.Success "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down
12 changes: 6 additions & 6 deletions internal/app/adapter/server/http/handler/v1/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,20 @@ func (h *ProductHandler) Update(ctx echo.Context) error {
}

type ProductDetailRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

type ProductDetailResponse = ProductInfo

// Detail 产品详情
//
// @Router /v1/product [get]
// @Router /v1/product/{id} [get]
// @Summary 产品详情
// @Description 产品详情
// @Tags 产品
// @Accept plain
// @Produce json
// @Param id query integer true "产品 id" format(uint) minimum(1)
// @Param id path integer true "产品 id" format(uint) minimum(1)
// @Success 200 {object} example.Success{data=ProductDetailResponse} "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down Expand Up @@ -207,18 +207,18 @@ func (h *ProductHandler) Detail(ctx echo.Context) error {
}

type ProductDeleteRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

// Delete 产品删除
//
// @Router /v1/product [delete]
// @Router /v1/product/{id} [delete]
// @Summary 产品删除
// @Description 产品删除
// @Tags 产品
// @Accept plain
// @Produce json
// @Param id query integer true "产品 id" format(uint) minimum(1)
// @Param id path integer true "产品 id" format(uint) minimum(1)
// @Success 200 {object} example.Success "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down
12 changes: 6 additions & 6 deletions internal/app/adapter/server/http/handler/v1/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,20 @@ func (h *RoleHandler) Update(ctx echo.Context) error {
}

type RoleDetailRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

type RoleDetailResponse = RoleInfo

// Detail 角色详情
//
// @Router /v1/role [get]
// @Router /v1/role/{id} [get]
// @Summary 角色详情
// @Description 角色详情
// @Tags 角色
// @Accept plain
// @Produce json
// @Param id query integer true "权限 id" format(uint) minimum(1)
// @Param id path integer true "角色 id" format(uint) minimum(1)
// @Success 200 {object} example.Success{data=RoleDetailResponse} "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down Expand Up @@ -193,18 +193,18 @@ func (h *RoleHandler) Detail(ctx echo.Context) error {
}

type RoleDeleteRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

// Delete 角色删除
//
// @Router /v1/role [delete]
// @Router /v1/role/{id} [delete]
// @Summary 角色删除
// @Description 角色删除
// @Tags 角色
// @Accept plain
// @Produce json
// @Param id query integer true "权限 id" format(uint) minimum(1)
// @Param id path integer true "角色 id" format(uint) minimum(1)
// @Success 200 {object} example.Success "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down
16 changes: 8 additions & 8 deletions internal/app/adapter/server/http/handler/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type UserCreateRequest struct {
// @Tags 用户
// @Accept json
// @Produce json
// @Param data body UserCreateRequest true "权限信息" format(string)
// @Param data body UserCreateRequest true "用户信息" format(string)
// @Success 200 {object} example.Success "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down Expand Up @@ -135,7 +135,7 @@ type UserUpdateRequest struct {
// @Tags 用户
// @Accept json
// @Produce json
// @Param data body UserUpdateRequest true "权限信息" format(string)
// @Param data body UserUpdateRequest true "用户信息" format(string)
// @Success 200 {object} example.Success "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down Expand Up @@ -167,20 +167,20 @@ func (h *UserHandler) Update(ctx echo.Context) error {
}

type UserDetailRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

type UserDetailResponse = UserInfo

// Detail 用户详情
//
// @Router /v1/user [get]
// @Router /v1/user/{id} [get]
// @Summary 用户详情
// @Description 用户详情
// @Tags 用户
// @Accept plain
// @Produce json
// @Param id query integer true "权限 id" format(uint) minimum(1)
// @Param id path integer true "用户 id" format(uint) minimum(1)
// @Success 200 {object} example.Success{data=UserDetailResponse} "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down Expand Up @@ -211,18 +211,18 @@ func (h *UserHandler) Detail(ctx echo.Context) error {
}

type UserDeleteRequest struct {
ID int64 `query:"id"`
ID int64 `param:"id"`
}

// Delete 用户删除
//
// @Router /v1/user [delete]
// @Router /v1/user/{id} [delete]
// @Summary 用户删除
// @Description 用户删除
// @Tags 用户
// @Accept plain
// @Produce json
// @Param id query integer true "权限 id" format(uint) minimum(1)
// @Param id path integer true "权限 id" format(uint) minimum(1)
// @Success 200 {object} example.Success "成功响应"
// @Failure 500 {object} example.ServerError "服务器出错"
// @Failure 400 {object} example.ClientError "客户端请求错误(code 类型应为 int,string 仅为了表达多个错误码)"
Expand Down
2 changes: 1 addition & 1 deletion internal/app/adapter/server/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// @produce json
// @securityDefinitions.apikey Authorization
// @in header
// @name Token
// @name Authorization

var ProviderSet = wire.NewSet(
// handler
Expand Down
16 changes: 8 additions & 8 deletions internal/app/adapter/server/http/router/api_v1_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,31 @@ func (g *ApiV1Group) useRoutes() {
))

g.group.GET("/users", g.userHandler.List)
g.group.GET("/user", g.userHandler.Detail)
g.group.GET("/user/:id", g.userHandler.Detail)
g.group.POST("/user", g.userHandler.Create)
g.group.PUT("/user", g.userHandler.Update)
g.group.DELETE("/user", g.userHandler.Delete)
g.group.DELETE("/user/:id", g.userHandler.Delete)
g.group.GET("/user/roles", g.userHandler.GetRoles)
g.group.POST("/user/roles", g.userHandler.AssignRoles)

g.group.GET("/roles", g.roleHandler.List)
g.group.GET("/role", g.roleHandler.Detail)
g.group.GET("/role/:id", g.roleHandler.Detail)
g.group.POST("/role", g.roleHandler.Create)
g.group.PUT("/role", g.roleHandler.Update)
g.group.DELETE("/role", g.roleHandler.Delete)
g.group.DELETE("/role/:id", g.roleHandler.Delete)
g.group.GET("/role/permissions", g.roleHandler.GetPermissions)
g.group.POST("/role/permissions", g.roleHandler.GrantPermissions)

g.group.GET("/permissions", g.permissionHandler.List)
g.group.GET("/permission", g.permissionHandler.Detail)
g.group.GET("/permission/:id", g.permissionHandler.Detail)
g.group.POST("/permission", g.permissionHandler.Create)
g.group.PUT("/permission", g.permissionHandler.Update)
g.group.DELETE("/permission", g.permissionHandler.Delete)
g.group.DELETE("/permission/:id", g.permissionHandler.Delete)

g.group.GET("/products", g.productHandler.List)
g.group.GET("/product", g.productHandler.Detail)
g.group.GET("/product/:id", g.productHandler.Detail)
g.group.POST("/product", g.productHandler.Create)
g.group.PUT("/product", g.productHandler.Update)
g.group.DELETE("/product", g.productHandler.Delete)
g.group.DELETE("/product/:id", g.productHandler.Delete)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@ START TRANSACTION;
INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at) VALUES ('/users', '用户管理', 0, unix_timestamp(), unix_timestamp());
INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at)
VALUES ('GET /api/v1/users', '用户列表', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/user', '用户详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/user/:id', '用户详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('POST /api/v1/user', '用户新增', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('PUT /api/v1/user', '用户更新', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('DELETE /api/v1/user', '用户删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('DELETE /api/v1/user/:id', '用户删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('POST /api/v1/user/roles', '分配用户角色', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/user/roles', '获取用户角色', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/users') AS t), unix_timestamp(), unix_timestamp());

INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at) VALUES ('/roles', '角色管理', 0, unix_timestamp(), unix_timestamp());
INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at)
VALUES ('GET /api/v1/roles', '角色列表', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/role', '角色详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/role/:id', '角色详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('POST /api/v1/role', '角色新增', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('PUT /api/v1/role', '角色更新', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('DELETE /api/v1/role', '角色删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('DELETE /api/v1/role/:id', '角色删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('POST /api/v1/role/permissions', '授予角色权限', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/role/permissions', '获取角色权限', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/roles') AS t), unix_timestamp(), unix_timestamp());

INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at) VALUES ('/permissions', '权限管理', 0, unix_timestamp(), unix_timestamp());
INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at)
VALUES ('GET /api/v1/permissions', '权限列表', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/permission', '权限详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/permission/:id', '权限详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp()),
('POST /api/v1/permission', '权限新增', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp()),
('PUT /api/v1/permission', '权限更新', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp()),
('DELETE /api/v1/permission', '权限删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp());
('DELETE /api/v1/permission/:id', '权限删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/permissions') AS t), unix_timestamp(), unix_timestamp());

INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at) VALUES ('/products', '产品管理', 0, unix_timestamp(), unix_timestamp());
INSERT INTO permissions (`key`, name, parent_id, created_at, updated_at)
VALUES ('GET /api/v1/products', '产品列表', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/product', '产品详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp()),
('GET /api/v1/product/:id', '产品详情', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp()),
('POST /api/v1/product', '产品新增', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp()),
('PUT /api/v1/product', '产品更新', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp()),
('DELETE /api/v1/product', '产品删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp());
('DELETE /api/v1/product/:id', '产品删除', (SELECT id FROM (SELECT id FROM permissions WHERE `key` = '/products') AS t), unix_timestamp(), unix_timestamp());

COMMIT;

Expand Down
Loading

0 comments on commit f857684

Please sign in to comment.