@@ -30,6 +30,7 @@ func (h *HostsController) Post() {
3030 }
3131 h .Ctx .Output .SetStatus (http .StatusBadRequest )
3232 h .ServeJSON ()
33+ return
3334 }
3435 beego .Debug ("[C] Got data:" , host )
3536 id , err := models .AddHost (host )
@@ -41,6 +42,7 @@ func (h *HostsController) Post() {
4142 }
4243 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
4344 h .ServeJSON ()
45+ return
4446 }
4547
4648 beego .Debug ("[C] Got id:" , id )
@@ -49,6 +51,7 @@ func (h *HostsController) Post() {
4951 }
5052 h .Ctx .Output .SetStatus (http .StatusCreated )
5153 h .ServeJSON ()
54+ return
5255}
5356
5457// @Title getHost
@@ -73,15 +76,18 @@ func (h *HostsController) Get() {
7376 beego .Warn ("[C] Got error:" , err )
7477 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
7578 h .ServeJSON ()
79+ return
7680 }
7781 h .Data ["json" ] = hosts
7882 if len (hosts ) == 0 {
7983 beego .Debug ("[C] Got nothing with name:" , name )
8084 h .Ctx .Output .SetStatus (http .StatusNotFound )
8185 h .ServeJSON ()
86+ return
8287 } else {
8388 h .Ctx .Output .SetStatus (http .StatusOK )
8489 h .ServeJSON ()
90+ return
8591 }
8692 }
8793}
@@ -101,15 +107,18 @@ func (h *HostsController) GetAll() {
101107 beego .Warn ("[C] Got error:" , err )
102108 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
103109 h .ServeJSON ()
110+ return
104111 }
105112 h .Data ["json" ] = hosts
106113 if len (hosts ) == 0 {
107114 beego .Debug ("[C] Got nothing" )
108115 h .Ctx .Output .SetStatus (http .StatusNotFound )
109116 h .ServeJSON ()
117+ return
110118 } else {
111119 h .Ctx .Output .SetStatus (http .StatusOK )
112120 h .ServeJSON ()
121+ return
113122 }
114123}
115124
@@ -134,11 +143,13 @@ func (h *HostsController) Delete() {
134143 beego .Warn ("[C] Got error:" , err )
135144 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
136145 h .ServeJSON ()
146+ return
137147 }
138148 if len (hosts ) == 0 {
139149 beego .Debug ("[C] Got nothing with name:" , name )
140150 h .Ctx .Output .SetStatus (http .StatusNotFound )
141151 h .ServeJSON ()
152+ return
142153 }
143154 err = models .DeleteHost (hosts [0 ])
144155 if err != nil {
@@ -149,10 +160,12 @@ func (h *HostsController) Delete() {
149160 beego .Warn ("[C] Got error:" , err )
150161 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
151162 h .ServeJSON ()
163+ return
152164
153165 }
154166 h .Ctx .Output .SetStatus (http .StatusNoContent )
155167 h .ServeJSON ()
168+ return
156169 }
157170}
158171
@@ -167,6 +180,8 @@ func (h *HostsController) Put() {
167180 if name != "" {
168181 host := & models.Hosts {
169182 Name : name ,
183+ // 外键关系也需要初始化,否则会出现问题,反向关系则不用
184+ AppSet : new (models.AppSets ),
170185 }
171186 hosts , err := models .GetHosts (host )
172187 if err != nil {
@@ -177,15 +192,16 @@ func (h *HostsController) Put() {
177192 beego .Warn ("[C] Got error:" , err )
178193 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
179194 h .ServeJSON ()
195+ return
180196 }
181197 if len (hosts ) == 0 {
182198 beego .Debug ("[C] Got nothing with name:" , name )
183199 h .Ctx .Output .SetStatus (http .StatusNotFound )
184200 h .ServeJSON ()
201+ return
185202 }
186203
187204 err = json .Unmarshal (h .Ctx .Input .RequestBody , host )
188- host .Id = hosts [0 ].Id
189205 if err != nil {
190206 beego .Warn ("[C] Got error:" , err )
191207 h .Data ["json" ] = map [string ]string {
@@ -194,6 +210,11 @@ func (h *HostsController) Put() {
194210 }
195211 h .Ctx .Output .SetStatus (http .StatusBadRequest )
196212 h .ServeJSON ()
213+ return
214+ }
215+ host .Id = hosts [0 ].Id
216+ if host .AppSet != nil {
217+ host .AppSet = hosts [0 ].AppSet
197218 }
198219 beego .Debug ("[C] Got host data:" , host )
199220 err = models .UpdateHost (host )
@@ -205,9 +226,11 @@ func (h *HostsController) Put() {
205226 beego .Warn ("[C] Got error:" , err )
206227 h .Ctx .Output .SetStatus (http .StatusInternalServerError )
207228 h .ServeJSON ()
229+ return
208230
209231 }
210232 h .Ctx .Output .SetStatus (http .StatusAccepted )
211233 h .ServeJSON ()
234+ return
212235 }
213236}
0 commit comments