1 Star 0 Fork 0

megnhao / restful-api-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
model.go 4.54 KB
一键复制 编辑 原始数据 按行查看 历史
megnhao 提交于 2022-05-14 09:40 . 添加更新的逻辑
package host
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/go-playground/validator/v10"
)
var (
validate = validator.New()
)
func NewHostSet() *HostSet {
return &HostSet{
Items: []*Host{},
}
}
type HostSet struct {
Total int `json:"total"`
Items []*Host `json:"items"`
}
func (s *HostSet) Add(item *Host) {
s.Items = append(s.Items, item)
}
func NewHost() *Host {
return &Host{
Resource: &Resource{},
Describe: &Describe{},
}
}
//Host 模型的定义
type Host struct {
//资源公共属性部分
*Resource
//资源独有属性部署
*Describe
}
//对象的全量更新
func (h *Host) Put(obj *Host) error {
if obj.Id != h.Id {
return fmt.Errorf("id not equal")
}
*h.Resource = *obj.Resource
*h.Describe = *obj.Describe
return nil
}
//对象的局部更新
func (h *Host) Patch(obj *Host) error {
if obj.Name != "" {
h.Name = obj.Name
}
if obj.CPU != 0 {
h.CPU = obj.CPU
}
return nil
}
func (h *Host) Validate() error {
return validate.Struct(h)
}
func (h *Host) InjectDefault() {
if h.CreateAt == 0 {
h.CreateAt = time.Now().UnixMilli()
}
}
type Vendor int
const (
//枚举的默认值
PRIVATEIP Vendor = iota + 1
// 阿里云
ALIYUN
// 腾讯云
TXYUN
)
type Resource struct {
Id string `json:"id" validate:"required"` // 全局唯一Id
Vendor Vendor `json:"vendor"` // 厂商
Region string `json:"region" validate:"required"` // 地域
CreateAt int64 `json:"create_at"` // 创建时间
ExpireAt int64 `json:"expire_at"` // 过期时间
Type string `json:"type" validate:"required"` // 规格
Name string `json:"name" validate:"required"` // 名称
Description string `json:"description"` // 描述
Status string `json:"status"` // 服务商中的状态
Tags map[string]string `json:"tags"` // 标签
UpdateAt int64 `json:"update_at"` // 更新时间
SyncAt int64 `json:"sync_at"` // 同步时间
Account string `json:"accout"` // 资源的所属账号
PublicIP string `json:"public_ip"` // 公网IP
PrivateIP string `json:"private_ip"` // 内网IP
}
type Describe struct {
CPU int `json:"cpu" validate:"required"` // 核数
Memory int `json:"memory" validate:"required"` // 内存
GPUAmount int `json:"gpu_amount"` // GPU数量
GPUSpec string `json:"gpu_spec"` // GPU类型
OSType string `json:"os_type"` // 操作系统类型,分为Windows和Linux
OSName string `json:"os_name"` // 操作系统名称
SerialNumber string `json:"serial_number"` // 序列号
}
func NewQueryHostFromHTTP(r *http.Request) *QueryHostRequest {
//query string
req := NewQueryHostRequest()
qs := r.URL.Query()
pss := qs.Get("page_size")
if pss != "" {
req.PageSize, _ = strconv.Atoi(pss)
}
pns := qs.Get("page_number")
if pns != "" {
req.PageNumber, _ = strconv.Atoi(pns)
}
req.Keywords = qs.Get("kws")
return req
}
func NewQueryHostRequest() *QueryHostRequest {
return &QueryHostRequest{
PageSize: 20,
PageNumber: 1,
}
}
type QueryHostRequest struct {
PageSize int `json:"page_size"`
PageNumber int `json:"page_number"`
Keywords string `json:"kws"`
}
func (req *QueryHostRequest) GetPageSize() uint {
return uint(req.PageSize)
}
func (req *QueryHostRequest) OffSet() int64 {
return int64((req.PageNumber - 1) * req.PageSize)
}
func NewDescribeHostRequestWithId(id string) *DescribeHostRequest {
return &DescribeHostRequest{
Id: id,
}
}
type DescribeHostRequest struct {
Id string
}
type UPDATE_MODE string
const (
//全量更新
UPDATE_MODE_PUT UPDATE_MODE = "PUT"
//局部更新
UPDATE_MODE_PATCH UPDATE_MODE = "PATCH"
)
func NewPutUpdateHostRequest(id string) *UpdateHostRequest {
h := NewHost()
h.Id = id
return &UpdateHostRequest{
UpdateMode: UPDATE_MODE_PUT,
Host: h,
}
}
func NewPatchUpdateHostRequest(id string) *UpdateHostRequest {
h := NewHost()
h.Id = id
return &UpdateHostRequest{
UpdateMode: UPDATE_MODE_PATCH,
Host: h,
}
}
type UpdateHostRequest struct {
// Id string `json:"id"`
UpdateMode UPDATE_MODE `json:"update_mode"`
*Host
}
type DeleteHostRequest struct {
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/megnhao/restful-api-demo.git
git@gitee.com:megnhao/restful-api-demo.git
megnhao
restful-api-demo
restful-api-demo
e06e71524eb4

搜索帮助

344bd9b3 5694891 D2dac590 5694891