iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
0
Modern Web

BeeGo系列 第 7

Controller and View(2)

今天要試驗的是 BeeGo 的 CRUD。

還是先用 bee 這工具幫我們產生 Controller 與 Views

bee generate controller user
bee generate view user

先看一下 controllers/user.go ,嗯,很好,這已經把 model 的 CRUD API 都產生出來了,跟 Django Rest framework 的 ModelViewSet 很類似。注意看 Post() 的最後,他回傳的是 JSON 格式。

package controllers                                                                                                                                                                            
                                                                                                                                                                                               
import (                                                                                                                                                                                       
    "encoding/json"                                                                                                                                                                            
    "errors"                                                                                                                                                                                   
    "my/hello/models"                                                                                                                                                                          
    "strconv"                                                                                                                                                                                  
    "strings"                                                                                                                                                                                  
                                                                                                                                                                                               
    "github.com/astaxie/beego"                                                                                                                                                                 
)                                                                                                                                                                                              
                                                                                                                                                                                               
//  UserController operations for User                                                                                                                                                         
type UserController struct {                                                                                                                                                                   
    beego.Controller                                                                                                                                                                           
}                                                                                                                                                                                              
                                                                                                                                                                                               
// URLMapping ...                                                                                                                                                                              
func (c *UserController) URLMapping() {                                                                                                                                                        
    c.Mapping("Post", c.Post)                                                                                                                                                                  
    c.Mapping("GetOne", c.GetOne)                                                                                                                                                              
    c.Mapping("GetAll", c.GetAll)                                                                                                                                                              
    c.Mapping("Put", c.Put)                                                                                                                                                                    
    c.Mapping("Delete", c.Delete)                                                                                                                                                              
}                                                                                                                                                                                              
                                                                                                                                                                                               
// Post ...                                                                                                                                                                                    
// @Title Post                                                                                                                                                                                 
// @Description create User                                                                                                                                                                    
// @Param   body        body    models.User true        "body for User content"                                                                                                                
// @Success 201 {int} models.User                                                                                                                                                              
// @Failure 403 body is empty                                                                                                                                                                  
// @router / [post]                                                                                                                                                                            
func (c *UserController) Post() {                                                                                                                                                              
    var v models.User                                                                                                                                                                          
    json.Unmarshal(c.Ctx.Input.RequestBody, &v)                                                                                                                                                
    if _, err := models.AddUser(&v); err == nil {                                                                                                                                              
        c.Ctx.Output.SetStatus(201)                                                                                                                                                            
        c.Data["json"] = v                                                                                                                                                                     
    } else {                                                                                                                                                                                   
        c.Data["json"] = err.Error()                                                                                                                                                           
    }                                                                                                                                                                                          
    c.ServeJSON()                                                                                                                                                                              
}

// 後面還有,限於篇幅,這裡不全部貼上來。
// ...

接著來看一下產生出來的 view,

$ /usr/bin/tree views/user
views/user
├── create.tpl
├── edit.tpl
├── index.tpl
└── show.tpl

0 directories, 4 files

可以看到裏面已經有4個檔案,但是打開一看,會發現裏面只有檔名。

$ cat views/user/index.tpl
/home/ubuntu/go/src/my/hello/views/user/index.tpl

原本以為是我用錯指令,不過在去看過 bee 的原始碼以後,發現確實就是只寫入檔名。

好吧,看來這部份得自己來做了。下一篇,先來了解一下 Template 的使用並做出 List。


上一篇
Controller and View(1)
下一篇
Template
系列文
BeeGo30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言