Rethinking the Model of Go Basic Learning Record-Writing Web Application-Blog Editing Function

Keywords: Go SQL github git SQLite

Every time I put my practice code on github and typed a tag, which is convenient for the students to use later. Here I share my practice with the code of the following branches.

https://github.com/durban89/typescript_demo.git
tag: 1.1.2

Last article[ Go Basic Learning Record - Writing Web Application - Blog Editor Perfect Update Function ] In the process of modification, I encountered a problem that the Update function conflicts with the function in sqlite. The prompt content probably means redefining the function. This time, I changed the updated logic, redefined the lower Model, and updated the QueryOne function.

Step 1: Redefining Model

Redefining Model mainly abstracts some logic conceptually, such as where, select and update part of a query statement. I treat this part as a small block and deal with the code logic as follows. First, I build a model/models.go file, which is as follows.

package models

import (
    "fmt"
    "strings"
)

// Model model
type Model interface {
    QueryOne()
    Update()
}

// Where Condition Where Conditions
type WhereCondition struct {
    Operator string
    Value    string
}

// Where Values Where Conditional Value
type WhereValues map[string]WhereCondition

// Update Values update condition value
type UpdateValues map[string]string

// SelectValues select condition value
type SelectValues []string

// Merge where Merge where Conditions
func (w WhereValues) MergeWhere() string {
    where := []string{}
    for k, v := range w {
        if v.Operator == "" {
            s := fmt.Sprintf("%s = %s", k, v.Value)
            where = append(where, s)
        } else {
            s := fmt.Sprintf("%s %s %s", k, v.Operator, v.Value)
            where = append(where, s)
        }
    }

    return strings.Join(where, " AND ")
}

// Merge update Merge update Conditions
func (u UpdateValues) MergeUpdate() string {
    update := []string{}
    for k, v := range u {
        s := fmt.Sprintf("%s = '%s'", k, v)
        update = append(update, s)
    }

    return strings.Join(update, ", ")
}

// MergeSelect Merge select Conditions
func (s SelectValues) MergeSelect() string {
    return strings.Join(s, ", ")
}

So I think it's a little clearer, and put it in a model. go file, which means that almost all models should have these methods.

After modifying the method, you need to modify the controller

Step 2: Modify the Model

Here, we modify the models/blog.go, modifying the QueryOne and Update functions respectively. The modified code is as follows.

// QueryOne Gets a Data
func (b *Blog) QueryOne(where WhereValues) (helpers.Page, error) {
    var selectString = SelectValues{
        "*",
    }.MergeSelect()

    var whereString = where.MergeWhere()

    sql := fmt.Sprintf("SELECT %s FROM %s WHERE %s LIMIT 0, 1", selectString, tableName, whereString)

    rows, err := Conn.Query(sql)

    var res = helpers.Page{}

    if err != nil {
        return res, err
    }

    for rows.Next() {
        var autokid int
        var title string
        err = rows.Scan(&autokid, &title)

        if err != nil {
            return res, err
        }

        p := helpers.Page{
            ID:    autokid,
            Title: title,
        }

        res = p
    }

    if res.ID == 0 {
        return res, errors.New("The article does not exist")
    }

    return res, nil
}

In addition, the Update function is as follows

// Update updates data
func (b *Blog) Update(update UpdateValues, where WhereValues) (int64, error) {
    var updateString = update.MergeUpdate()
    var whereString = where.MergeWhere()

    sql := fmt.Sprintf("UPDATE %s SET %s WHERE %s", tableName, updateString, whereString)

    stmt, err := Conn.Prepare(sql)
    if err != nil {
        return 0, err
    }

    res, err := stmt.Exec()
    if err != nil {
        return 0, err
    }

    affect, err := res.RowsAffected()
    if err != nil {
        return 0, err
    }

    return affect, nil
}

There is no unit test written for the time being.

Step 3: Modify Controller

The main modification of controller is to modify the ArticleEdit function in the article, which is as follows

// ArticleEdit Edits Articles
func ArticleEdit(w http.ResponseWriter, r *http.Request) {
    id := r.URL.Query().Get("id")
    if id == "" {
        http.NotFound(w, r)
        return
    }

    where := models.WhereValues{
        "autokid": models.WhereCondition{
            Operator: "=",
            Value:    id,
        },
    }

    update := map[string]string{}

    if strings.ToLower(r.Method) == "get" {

        blogModel := &models.Blog{}

        p, err := blogModel.QueryOne(where)

        if err != nil {
            http.NotFound(w, r)
            return
        }

        crutime := time.Now().Unix()
        h := md5.New()
        io.WriteString(h, strconv.FormatInt(crutime, 10))
        token := fmt.Sprintf("%x", h.Sum(nil))

        p.Token = token
        helpers.RenderTemplate(w, "edit", &p)
    } else if strings.ToLower(r.Method) == "post" {
        title := r.FormValue("title")
        if title == "" {
            http.Redirect(w, r, fmt.Sprintf("/edit?id=%s", id), http.StatusFound)
            return
        }

        update["title"] = title

        blogModel := &models.Blog{}

        _, err := blogModel.Update(update, where)

        if err != nil {
            http.NotFound(w, r)
            return
        }

        http.Redirect(w, r, fmt.Sprintf("/edit?id=%s", id), http.StatusFound)
        return
    }

}

After the previous modifications, I feel more golang programming.

Re-edit the code and run it. Everything is OK. If you want to see the complete code, you can download it by booting below.

Project Update Address

https://github.com/durban89/typescript_demo.git
tag: 1.1.3

Posted by pluto on Thu, 31 Jan 2019 20:15:15 -0800