作者 yangfu

增加服务 client-version

... ... @@ -226,6 +226,8 @@ github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+
github.com/go-openapi/jsonreference v0.0.0-20160704190145-13c6e3589ad9/go.mod h1:W3Z9FmVs9qj+KR4zFKmDPGiLdk1D9Rlm7cyMvf57TTg=
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nAiCcj+friV/PDoE1/3eeccG9LYBs0tYvLOWc=
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
github.com/go-pg/pg v8.0.7+incompatible h1:ty/sXL1OZLo+47KK9N8llRcmbA9tZasqbQ/OO4ld53g=
github.com/go-pg/pg v8.0.7+incompatible/go.mod h1:a2oXow+aFOrvwcKs3eIA0lNFmMilrxK2sOkB5NWe0vA=
github.com/go-pg/pg/v10 v10.0.0-beta.2/go.mod h1:UAuqGPC94ySi4rJ3DC5e4SY1rlwugZbJA/XoJ/kf5Rw=
github.com/go-pg/pg/v10 v10.7.3 h1:oL/Hz5MJie/9epmwxlZjfReO+2wlLPOK6BeGb9I+XHk=
github.com/go-pg/pg/v10 v10.7.3/go.mod h1:UsDYtA+ihbBNX1OeIvDejxkL4RXzL3wsZYoEv5NUEqM=
... ...
package command
import (
"fmt"
"github.com/tiptok/godevp/pkg/domain"
"github.com/astaxie/beego/validation"
)
type CreateClientVersionCommand struct {
// 提交人
Commiter string `json:"commiter,omitempty"`
// 项目名称
ProjectName string `json:"projectName,omitempty"`
// 版本号
Version string `json:"version,omitempty"`
// 标题
Title string `json:"title,omitempty"`
// 其他备注信息
Remark string `json:"remark,omitempty"`
// 客户端安装包信息
ClientPackageInfo []*domain.ClientPackageInfo `json:"clientPackageInfo,omitempty"`
}
func (createClientVersionCommand *CreateClientVersionCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createClientVersionCommand *CreateClientVersionCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createClientVersionCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type RemoveClientVersionCommand struct {
// dcc
Id int64 `json:"id" valid:"Required"`
}
func (removeClientVersionCommand *RemoveClientVersionCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeClientVersionCommand *RemoveClientVersionCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeClientVersionCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/tiptok/godevp/pkg/domain"
"github.com/astaxie/beego/validation"
)
type UpdateClientVersionCommand struct {
// dcc
Id int64 `json:"id" valid:"Required"`
// 提交人
Commiter string `json:"commiter,omitempty"`
// 项目名称
ProjectName string `json:"projectName,omitempty"`
// 版本号
Version string `json:"version,omitempty"`
// 标题
Title string `json:"title,omitempty"`
// 其他备注信息
Remark string `json:"remark,omitempty"`
// 客户端安装包信息
ClientPackageInfo []*domain.ClientPackageInfo `json:"clientPackageInfo,omitempty"`
}
func (updateClientVersionCommand *UpdateClientVersionCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateClientVersionCommand *UpdateClientVersionCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateClientVersionCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package query
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type GetClientVersionQuery struct {
// dcc
Id int64 `json:"id" valid:"Required"`
}
func (getClientVersionQuery *GetClientVersionQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getClientVersionQuery *GetClientVersionQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getClientVersionQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package query
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type ListClientVersionQuery struct {
// 查询偏离量
Offset int `json:"offset" valid:"Required"`
// 查询限制
Limit int `json:"limit" valid:"Required"`
}
func (listClientVersionQuery *ListClientVersionQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listClientVersionQuery *ListClientVersionQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listClientVersionQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"github.com/tiptok/godevp/pkg/application/clientVersion/command"
"github.com/tiptok/godevp/pkg/application/clientVersion/query"
"github.com/tiptok/godevp/pkg/application/factory"
"github.com/tiptok/godevp/pkg/domain"
)
// 客户端版本服务
type ClientVersionService struct {
}
// 创建
func (clientVersionService *ClientVersionService) CreateClientVersion(createClientVersionCommand *command.CreateClientVersionCommand) (interface{}, error) {
if err := createClientVersionCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
newClientVersion := &domain.ClientVersion{
Commiter: createClientVersionCommand.Commiter,
ProjectName: createClientVersionCommand.ProjectName,
Version: createClientVersionCommand.Version,
Title: createClientVersionCommand.Title,
Remark: createClientVersionCommand.Remark,
ClientPackageInfo: createClientVersionCommand.ClientPackageInfo,
}
var clientVersionRepository domain.ClientVersionRepository
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
clientVersionRepository = value
}
if clientVersion, err := clientVersionRepository.Save(newClientVersion); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return clientVersion, nil
}
}
// 返回
func (clientVersionService *ClientVersionService) GetClientVersion(getClientVersionQuery *query.GetClientVersionQuery) (interface{}, error) {
if err := getClientVersionQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var clientVersionRepository domain.ClientVersionRepository
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
clientVersionRepository = value
}
clientVersion, err := clientVersionRepository.FindOne(map[string]interface{}{"clientVersionId": getClientVersionQuery.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if clientVersion == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getClientVersionQuery.Id)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return clientVersion, nil
}
}
// 返回列表
func (clientVersionService *ClientVersionService) ListClientVersion(listClientVersionQuery *query.ListClientVersionQuery) (interface{}, error) {
if err := listClientVersionQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var clientVersionRepository domain.ClientVersionRepository
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
clientVersionRepository = value
}
if count, clientVersions, err := clientVersionRepository.Find(tool_funs.SimpleStructToMap(listClientVersionQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"clientVersions": clientVersions,
}, nil
}
}
// 移除
func (clientVersionService *ClientVersionService) RemoveClientVersion(removeClientVersionCommand *command.RemoveClientVersionCommand) (interface{}, error) {
if err := removeClientVersionCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var clientVersionRepository domain.ClientVersionRepository
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
clientVersionRepository = value
}
clientVersion, err := clientVersionRepository.FindOne(map[string]interface{}{"clientVersionId": removeClientVersionCommand.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if clientVersion == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeClientVersionCommand.Id)))
}
if clientVersion, err := clientVersionRepository.Remove(clientVersion); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return clientVersion, nil
}
}
// 更新
func (clientVersionService *ClientVersionService) UpdateClientVersion(updateClientVersionCommand *command.UpdateClientVersionCommand) (interface{}, error) {
if err := updateClientVersionCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var clientVersionRepository domain.ClientVersionRepository
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
clientVersionRepository = value
}
clientVersion, err := clientVersionRepository.FindOne(map[string]interface{}{"clientVersionId": updateClientVersionCommand.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if clientVersion == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateClientVersionCommand.Id)))
}
if err := clientVersion.Update(tool_funs.SimpleStructToMap(updateClientVersionCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if clientVersion, err := clientVersionRepository.Save(clientVersion); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return clientVersion, nil
}
}
func NewClientVersionService(options map[string]interface{}) *ClientVersionService {
newClientVersionService := &ClientVersionService{}
return newClientVersionService
}
... ...
... ... @@ -39,3 +39,11 @@ func CreateRoleAccessRepository(options map[string]interface{}) (domain.RoleAcce
}
return repository.NewRoleAccessRepository(transactionContext)
}
func CreateClientVersionRepository(options map[string]interface{}) (domain.ClientVersionRepository, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewClientVersionRepository(transactionContext)
}
... ...
package domain
// 客户端安装包信息
type ClientPackageInfo struct {
// 文件名
FileName string `json:"fileName"`
// 文件路径
Path string `json:"path"`
}
... ...
package domain
import "time"
// 客户端版本信息
type ClientVersion struct {
// dcc
Id int64 `json:"id"`
// 提交人
Commiter string `json:"commiter"`
// 项目名称
ProjectName string `json:"projectName"`
// 版本号
Version string `json:"version"`
// 标题
Title string `json:"title"`
// 其他备注信息
Remark string `json:"remark"`
// 客户端安装包信息
ClientPackageInfo []*ClientPackageInfo `json:"clientPackageInfo"`
// 创建时间
CreateTime time.Time `json:"createTime"`
}
type ClientVersionRepository interface {
Save(clientVersion *ClientVersion) (*ClientVersion, error)
Remove(clientVersion *ClientVersion) (*ClientVersion, error)
FindOne(queryOptions map[string]interface{}) (*ClientVersion, error)
Find(queryOptions map[string]interface{}) (int64, []*ClientVersion, error)
}
func (clientVersion *ClientVersion) Identify() interface{} {
if clientVersion.Id == 0 {
return nil
}
return clientVersion.Id
}
func (clientVersion *ClientVersion) Update(data map[string]interface{}) error {
if Id, ok := data["Id"]; ok {
clientVersion.Id = Id.(int64)
}
if commiter, ok := data["commiter"]; ok {
clientVersion.Commiter = commiter.(string)
}
if projectName, ok := data["projectName"]; ok {
clientVersion.ProjectName = projectName.(string)
}
if version, ok := data["version"]; ok {
clientVersion.Version = version.(string)
}
if title, ok := data["title"]; ok {
clientVersion.Title = title.(string)
}
if remark, ok := data["remark"]; ok {
clientVersion.Remark = remark.(string)
}
if clientPackageInfo, ok := data["clientPackageInfo"]; ok {
clientVersion.ClientPackageInfo = clientPackageInfo.([]*ClientPackageInfo)
}
if createTime, ok := data["createTime"]; ok {
clientVersion.CreateTime = createTime.(time.Time)
}
return nil
}
... ...
package models
import (
"github.com/tiptok/godevp/pkg/domain"
"time"
)
type ClientVersion struct {
TableName string `pg:"client_versions,alias:client_version"`
// dcc
Id int64
// 提交人
Commiter string
// 项目名称
ProjectName string
// 版本号
Version string
// 标题
Title string
// 其他备注信息
Remark string
// 客户端安装包信息
ClientPackageInfo []*domain.ClientPackageInfo `pg:",array"`
// 创建时间
CreateTime time.Time
}
... ...
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/tiptok/godevp/pkg/domain"
"github.com/tiptok/godevp/pkg/infrastructure/pg/models"
)
type ClientVersionRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *ClientVersionRepository) nextIdentify() (int64, error) {
return 0, nil
}
func (repository *ClientVersionRepository) Save(clientVersion *domain.ClientVersion) (*domain.ClientVersion, error) {
tx := repository.transactionContext.PgTx
if clientVersion.Identify() == nil {
_, err := repository.nextIdentify()
if err != nil {
return clientVersion, err
}
if _, err := tx.QueryOne(
pg.Scan(&clientVersion.Id, &clientVersion.Commiter, &clientVersion.ProjectName, &clientVersion.Version, &clientVersion.Title, &clientVersion.Remark, pg.Array(&clientVersion.ClientPackageInfo), &clientVersion.CreateTime),
"INSERT INTO client_versions (id, commiter, project_name, version, title, remark, client_package_info, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id, commiter, project_name, version, title, remark, client_package_info, create_time",
clientVersion.Id, clientVersion.Commiter, clientVersion.ProjectName, clientVersion.Version, clientVersion.Title, clientVersion.Remark, pg.Array(clientVersion.ClientPackageInfo), clientVersion.CreateTime); err != nil {
return clientVersion, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(&clientVersion.Id, &clientVersion.Commiter, &clientVersion.ProjectName, &clientVersion.Version, &clientVersion.Title, &clientVersion.Remark, pg.Array(&clientVersion.ClientPackageInfo), &clientVersion.CreateTime),
"UPDATE client_versions SET id=?, commiter=?, project_name=?, version=?, title=?, remark=?, client_package_info=?, create_time=? WHERE id=? RETURNING id, commiter, project_name, version, title, remark, client_package_info, create_time",
clientVersion.Id, clientVersion.Commiter, clientVersion.ProjectName, clientVersion.Version, clientVersion.Title, clientVersion.Remark, pg.Array(clientVersion.ClientPackageInfo), clientVersion.CreateTime, clientVersion.Identify()); err != nil {
return clientVersion, err
}
}
return clientVersion, nil
}
func (repository *ClientVersionRepository) Remove(clientVersion *domain.ClientVersion) (*domain.ClientVersion, error) {
tx := repository.transactionContext.PgTx
clientVersionModel := new(models.ClientVersion)
clientVersionModel.Id = clientVersion.Identify().(int64)
if _, err := tx.Model(clientVersionModel).WherePK().Delete(); err != nil {
return clientVersion, err
}
return clientVersion, nil
}
func (repository *ClientVersionRepository) FindOne(queryOptions map[string]interface{}) (*domain.ClientVersion, error) {
tx := repository.transactionContext.PgTx
clientVersionModel := new(models.ClientVersion)
query := tx.Model(clientVersionModel)
if clientVersionId, ok := queryOptions["clientVersionId"]; ok {
query = query.Where("client_version.id = ?", clientVersionId)
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if clientVersionModel.Id == 0 {
return nil, nil
} else {
return repository.transformPgModelToDomainModel(clientVersionModel)
}
}
func (repository *ClientVersionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ClientVersion, error) {
tx := repository.transactionContext.PgTx
var clientVersionModels []*models.ClientVersion
clientVersions := make([]*domain.ClientVersion, 0)
query := tx.Model(&clientVersionModels)
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
query = query.Offset(offset)
}
} else {
query = query.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
query = query.Limit(limit)
}
} else {
query = query.Limit(20)
}
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
return 0, clientVersions, err
} else {
for _, clientVersionModel := range clientVersionModels {
if clientVersion, err := repository.transformPgModelToDomainModel(clientVersionModel); err != nil {
return 0, clientVersions, err
} else {
clientVersions = append(clientVersions, clientVersion)
}
}
return int64(count), clientVersions, nil
}
}
func (repository *ClientVersionRepository) transformPgModelToDomainModel(clientVersionModel *models.ClientVersion) (*domain.ClientVersion, error) {
return &domain.ClientVersion{
Id: clientVersionModel.Id,
Commiter: clientVersionModel.Commiter,
ProjectName: clientVersionModel.ProjectName,
Version: clientVersionModel.Version,
Title: clientVersionModel.Title,
Remark: clientVersionModel.Remark,
ClientPackageInfo: clientVersionModel.ClientPackageInfo,
CreateTime: clientVersionModel.CreateTime,
}, nil
}
func NewClientVersionRepository(transactionContext *pgTransaction.TransactionContext) (*ClientVersionRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ClientVersionRepository{
transactionContext: transactionContext,
}, nil
}
}
... ...
package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/linmadan/egglib-go/web/beego/utils"
"github.com/tiptok/godevp/pkg/application/clientVersion/command"
"github.com/tiptok/godevp/pkg/application/clientVersion/query"
"github.com/tiptok/godevp/pkg/application/clientVersion/service"
)
type ClientVersionController struct {
beego.Controller
}
func (controller *ClientVersionController) CreateClientVersion() {
clientVersionService := service.NewClientVersionService(nil)
createClientVersionCommand := &command.CreateClientVersionCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createClientVersionCommand)
data, err := clientVersionService.CreateClientVersion(createClientVersionCommand)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *ClientVersionController) UpdateClientVersion() {
clientVersionService := service.NewClientVersionService(nil)
updateClientVersionCommand := &command.UpdateClientVersionCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateClientVersionCommand)
id, _ := controller.GetInt64(":id")
updateClientVersionCommand.Id = id
data, err := clientVersionService.UpdateClientVersion(updateClientVersionCommand)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *ClientVersionController) GetClientVersion() {
clientVersionService := service.NewClientVersionService(nil)
getClientVersionQuery := &query.GetClientVersionQuery{}
id, _ := controller.GetInt64(":id")
getClientVersionQuery.Id = id
data, err := clientVersionService.GetClientVersion(getClientVersionQuery)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *ClientVersionController) RemoveClientVersion() {
clientVersionService := service.NewClientVersionService(nil)
removeClientVersionCommand := &command.RemoveClientVersionCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), removeClientVersionCommand)
id, _ := controller.GetInt64(":id")
removeClientVersionCommand.Id = id
data, err := clientVersionService.RemoveClientVersion(removeClientVersionCommand)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *ClientVersionController) ListClientVersion() {
clientVersionService := service.NewClientVersionService(nil)
listClientVersionQuery := &query.ListClientVersionQuery{}
offset, _ := controller.GetInt("offset")
listClientVersionQuery.Offset = offset
limit, _ := controller.GetInt("limit")
listClientVersionQuery.Limit = limit
data, err := clientVersionService.ListClientVersion(listClientVersionQuery)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
... ...
package routers
import (
"github.com/astaxie/beego"
"github.com/tiptok/godevp/pkg/port/beego/controllers"
)
func init() {
beego.Router("/client-versions/", &controllers.ClientVersionController{}, "Post:CreateClientVersion")
beego.Router("/client-versions/:id", &controllers.ClientVersionController{}, "Put:UpdateClientVersion")
beego.Router("/client-versions/:id", &controllers.ClientVersionController{}, "Get:GetClientVersion")
beego.Router("/client-versions/:id", &controllers.ClientVersionController{}, "Delete:RemoveClientVersion")
beego.Router("/client-versions/", &controllers.ClientVersionController{}, "Get:ListClientVersion")
}
... ...
package client_version
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/astaxie/beego"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "github.com/tiptok/godevp/pkg/infrastructure/pg"
_ "github.com/tiptok/godevp/pkg/port/beego"
)
func TestClientVersion(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port ClientVersion Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = beego.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...
package client_version
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
)
var _ = Describe("创建", func() {
Describe("提交数据创建", func() {
Context("提交正确的新客户端版本信息数据", func() {
It("返回客户端版本信息数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"commiter": "string",
"projectName": "string",
"version": "string",
"title": "string",
"remark": "string",
"clientPackageInfo": "array",
}
httpExpect.POST("/client-versions/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("id").ValueNotEqual("id", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM client_versions WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package client_version
import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
)
var _ = Describe("返回", func() {
var clientVersionId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&clientVersionId),
"INSERT INTO client_versions (id, commiter, project_name, version, title, remark, client_package_info, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
"testId", "testCommiter", "testProjectName", "testVersion", "testTitle", "testRemark", "testClientPackageInfo", "testCreateTime")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据clientVersionId参数返回客户端版本信息", func() {
Context("传入有效的clientVersionId", func() {
It("返回客户端版本信息数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/client-versions/{id}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM client_versions WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package client_version
import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
)
var _ = Describe("返回列表", func() {
var clientVersionId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&clientVersionId),
"INSERT INTO client_versions (id, commiter, project_name, version, title, remark, client_package_info, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
"testId", "testCommiter", "testProjectName", "testVersion", "testTitle", "testRemark", "testClientPackageInfo", "testCreateTime")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回客户端版本信息列表", func() {
Context("传入有效的参数", func() {
It("返回客户端版本信息数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/client-versions/").
WithQuery("offset", "int").
WithQuery("limit", "int").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("clientVersions").Value("clientVersions").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM client_versions WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package client_version
import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
)
var _ = Describe("移除", func() {
var clientVersionId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&clientVersionId),
"INSERT INTO client_versions (id, commiter, project_name, version, title, remark, client_package_info, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
"testId", "testCommiter", "testProjectName", "testVersion", "testTitle", "testRemark", "testClientPackageInfo", "testCreateTime")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除", func() {
Context("传入有效的clientVersionId", func() {
It("返回被移除客户端版本信息的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/client-versions/{id}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM client_versions WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package client_version
import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
)
var _ = Describe("更新", func() {
var clientVersionId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&clientVersionId),
"INSERT INTO client_versions (id, commiter, project_name, version, title, remark, client_package_info, create_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id",
"testId", "testCommiter", "testProjectName", "testVersion", "testTitle", "testRemark", "testClientPackageInfo", "testCreateTime")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新", func() {
Context("提交正确的客户端版本信息数据", func() {
It("返回更新后的客户端版本信息数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"Id": "int64",
"commiter": "string",
"projectName": "string",
"version": "string",
"title": "string",
"remark": "string",
"clientPackageInfo": "array",
}
httpExpect.PUT("/client-versions/{id}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("id").ValueEqual("id", clientVersionId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM client_versions WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
... ... @@ -4,7 +4,7 @@ import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
... ...
... ... @@ -4,7 +4,7 @@ import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
... ...
... ... @@ -4,7 +4,7 @@ import (
"net/http"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg"
"github.com/go-pg/pg/v10"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "github.com/tiptok/godevp/pkg/infrastructure/pg"
... ...