Debian Codegraph @

cms/project.go

raw symbols SCIP
1 package cms
2
3 import (
4 "encoding/json"
5 // "errors"
6 // "fmt"
7 // "io/ioutil"
8 // "net/http"
9 "net/url"
10 // "strings"
11 )
12
13 // 调用接口返回的实体
14 // 新增加的结构首字母一定要大写,否则json编码或者解析编码会出现问题
15 type ResultModel struct {
16 Code string `json:"code"`
17 Message string `json:"message"`
18 Success bool `json:"success"`
19 }
20
21 //project的实体类,主要用于发送请求,更新或者添加Project
22 type ProjectModel struct {
23 ProjectName string `json:"projectName"`
24 ProjectDesc string `json:"projectDesc"`
25 ProjectOwner string `json:"projectOwner"`
26 }
27
28 //project返回实体,继承于ProjectModel
29 type ProjectResultModel struct {
30 ProjectModel
31 Id int64 `json:"id"`
32 GmtModified int64 `json:"gmtModified"`
33 GmtCreate int64 `json:"gmtCreate"`
34 Status int `json:"status"`
35 Creator string `json:"creator"`
36 }
37
38 //取得多个project的结构
39 type ProjectListResultModel struct {
40 Total int `json:"total"`
41 Datapoints []ProjectResultModel `json:"datapoints"`
42 }
43
44 //返回的实体类型
45 type GetProjectResult struct {
46 Result ProjectResultModel `json:"result"`
47 Code string `json:"code"`
48 Success bool `json:"success"`
49 }
50
51 //列表批量取得Project的请求参数
52 type ListProjectRequestModel struct {
53 Page string `json:"page"`
54 PageSize string `json:"pageSize"`
55 }
56
57 /**
58 * 创建一个Project
59 * @params
60 * ProjectModel 创建模块的模型
61 * @return
62 * result 创建返回的模型
63 * error 错误,如果一切Ok,error为 nil
64 */
65 func (c *Client) CreateProject(projectName string, projectDesc string, projectOwner string) (result ResultModel, err error) {
66
67 project := &ProjectModel{
68 projectName,
69 projectDesc,
70 projectOwner,
71 }
72
73 postObjContent, _ := json.Marshal(project)
74 postJson := string(postObjContent)
75
76 requestUrl := c.GetUrl("projects", "", "")
77 requestPath := GetRequestPath("projects", "", "")
78
79 responseResult, err := c.GetResponseJson("POST", requestUrl, requestPath, postJson)
80
81 if err != nil {
82 return result, err
83 }
84
85 err = json.Unmarshal([]byte(responseResult), &result)
86
87 return result, err
88 }
89
90 /**
91 * 删除一个Project
92 * @params
93 * projectName 创建模块的模型
94 * @return
95 * result 返回的模型
96 * error 错误,如果一切Ok,error为 nil
97 */
98 func (c *Client) DeleteProject(projectName string) (result ResultModel, err error) {
99
100 requestUrl := c.GetUrl("projects", "", projectName)
101 requestPath := GetRequestPath("projects", "", projectName)
102
103 responseResult, err := c.GetResponseJson("DELETE", requestUrl, requestPath, "")
104
105 if err != nil {
106 return result, err
107 }
108
109 err = json.Unmarshal([]byte(responseResult), &result)
110
111 return result, err
112
113 }
114
115 /**
116 * 更新一个Project
117 * @params
118 * projectName 创建模块的模型
119 * project 修改的模型,这个只能修改projectDesc和ProjectOwer
120 * @return
121 * result 返回的模型
122 * error 错误,如果一切Ok,error为 nil
123 */
124 func (c *Client) UpdateProject(projectName string, project *ProjectModel) (result ResultModel, err error) {
125
126 postObjContent, _ := json.Marshal(project)
127 postJson := string(postObjContent)
128
129 requestUrl := c.GetUrl("projects", "", projectName)
130 requestPath := GetRequestPath("projects", "", projectName)
131
132 responseResult, err := c.GetResponseJson("PUT", requestUrl, requestPath, postJson)
133
134 if err != nil {
135 return result, err
136 }
137
138 err = json.Unmarshal([]byte(responseResult), &result)
139
140 return result, err
141 }
142
143 /**
144 * 取得一个Project
145 * @params
146 * projectName projectName不能为空
147 * @return
148 * result 返回的模型
149 * error 错误,如果一切Ok,error为 nil
150 */
151 func (c *Client) GetProject(projectName string) (result GetProjectResult, err error) {
152
153 requestUrl := c.GetUrl("projects", "", projectName)
154 requestPath := GetRequestPath("projects", "", projectName)
155
156 responseResult, err := c.GetResponseJson("GET", requestUrl, requestPath, "")
157
158 if err != nil {
159 return result, err
160 }
161
162 err = json.Unmarshal([]byte(responseResult), &result)
163
164 return result, err
165 }
166
167 /**
168 * 取得一个多个Project
169 * @params
170 * page: 页码
171 * pageSize: 每夜记录条数
172 * projectOwner 所属用户
173 * @return
174 * result 返回的模型
175 * error 错误,如果一切Ok,error为 nil
176 */
177 func (c *Client) GetProjectList(page string, pageSize string, projectOwner string) (result ProjectListResultModel, err error) {
178
179 params := url.Values{}
180 params.Add("Page", page)
181 params.Add("PageSize", pageSize)
182
183 if projectOwner != "" {
184 params.Add("ProjectOwner", projectOwner)
185 }
186
187 paramsString := params.Encode()
188
189 requestUrl := c.GetUrl("projects", "", "")
190 requestPath := GetRequestPath("projects", "", "")
191
192 responseResult, _ := c.GetResponseJson("GET", requestUrl+"?"+paramsString+"&resource", requestPath+"?"+paramsString+"&resource", "")
193
194 err = json.Unmarshal([]byte(responseResult), &result)
195
196 return result, err
197 }