Search code examples
apigomodelcontrollerbeego

Beego update & get 1 by ID


I'm absolutely stuck at Beego, update & get. I already done getall, delete,

Problem is my ID is the primary key. I convert into a string to equal at the model. How to search from Ticket table by using ID and in model how to take as []array and return it.

controller

func (i *TicketController) GetTicket() {
    ID := i.GetInt("ID")
    stringID := strconv.Itoa(ID)
    if ID != "" {
        ticket := models.GetTicketById(stringID)
        fmt.Println(ticket)
        i.Data["json"] = ticket
    }
    i.ServeJSON()
}

model

func GetTicketById(id int64) Ticket {
    t := Ticket{ID: id}
    o := orm.NewOrm()
    err := o.Read(&t)
    if err == orm.ErrNoRows {
        fmt.Println("no result")
    } else if err == orm.ErrMissPK {
        fmt.Println("can't find PK")
    }
    return t
}

Solution

  • I think you are misunderstand many things. I fixed your code on this link.

    1. Assign as ID,err := i.GetInt64("ID")
    2. Check error as if err == nil && ID > 0 {}

    https://play.golang.org/p/PY-lc3AfhPw