• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

014-GoWeb对pg增删改查测试

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1:data/data.go

package data

import(
    "fmt"
    "database/sql"
    _"github.com/lib/pq"
)

const(
    host = "192.168.72.128"
    port = 5432
    user = "test"
    password = "test"
    dbname = "testdb"
)

var Db *sql.DB

func init(){
    var err error

    pgInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", 
        host, port, user, password, dbname)
    
    Db, err = sql.Open("postgres", pgInfo)

    if err != nil{
        panic(err)
    }
}

 

2:post/post.go

package post

import(
    ."Chapter10/data"
)

type Post struct{
    ID        int     `json:"id"`
    Content    string     `json:"content"`
    Author    string    `json:"author"`
}

func (post *Post) AddPost()(err error){
    sql := "insert into post(content,author) values($1,$2) returning id"
    stmt, err := Db.Prepare(sql)
    if err != nil{
        return
    }
    defer stmt.Close()
    err = stmt.QueryRow(post.Content, post.Author).Scan(&post.ID)
    return
}

func (post *Post) DelPost()(err error){
    sql := "delete from post where id=$1"
    _,err = Db.Exec(sql, post.ID)
    return
}

func (post *Post) EditPost()(err error){
    sql := "update post set content=$1 where id=$2"
    _,err = Db.Exec(sql, post.Content, post.ID)
    return
}

func FindPost(id int)(err error, post Post){
    sql := "select id, content, author from post where id=$1"
    post = Post{}
    err = Db.QueryRow(sql, id).Scan(&post.ID, &post.Content, &post.Author)
    return
}

 

3:main.go

package main

import(
    "encoding/json"
    "net/http"
    "path"
    "strconv"
    "time"
    ."Chapter10/post"
)

type Result struct{
    No    int             `json:"no"`
    Msg    string         `json:"msg"`
    Obj interface{} `json:"obj,omitempty"`
    Time time.Time  `json:"response_time"`
}

func main(){
    server := http.Server{
        Addr: "127.0.0.1:8080",
    }
    http.HandleFunc("/post/", handlerRequest)
    server.ListenAndServe()
}

func handlerRequest(w http.ResponseWriter, r *http.Request){
    var err error
    switch r.Method{
    case "GET":
        err = handleGet(w, r)
    case "POST":
        err = handlePost(w, r)
    case "PUT":
        err = handlePut(w, r)
    }

    if err != nil{
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}



func handleGet(w http.ResponseWriter, r *http.Request)(err error){
    id, err := strconv.Atoi(path.Base(r.URL.Path))
    if err != nil{
        return
    }
    err,post := FindPost(id)
    if err != nil{
        return
    }

    var result = Result{
        No:100,
        Msg:"获取POST信息",
        Obj:post,
        Time:time.Now(),
    }

    output, err := json.Marshal(&result)
    if err != nil{
        return
    }
    w.Header().Set("Content-Type", "application/json")
    w.Write(output)
    return
}

func handlePost(w http.ResponseWriter, r *http.Request)(err error){
    len := r.ContentLength
    body := make([]byte, len)
    r.Body.Read(body)

    var post Post
    json.Unmarshal(body, &post)

    err = post.AddPost()
    if err != nil{
        return
    }

    var result = Result{
        No:100,
        Msg:"保存POST信息",
        Time:time.Now(),
    }
    output, err := json.Marshal(&result)
    if err != nil{
        return
    }
    w.Header().Set("Content-Type", "application/json")
    w.Write(output)
    return
}    

func handlePut(w http.ResponseWriter, r *http.Request)(err error){
    len := r.ContentLength
    body := make([]byte, len)
    r.Body.Read(body)

    var post Post
    json.Unmarshal(body, &post)

    err = post.EditPost()
    if err != nil{
        return
    }

    var result = Result{
        No:100,
        Msg:"修改POST信息",
        Time:time.Now(),
    }
    output, err := json.Marshal(&result)
    if err != nil{
        return
    }
    w.Header().Set("Content-Type", "application/json")
    w.Write(output)
    return
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go语言 - 关于常用插件不能安装的处理办法发布时间:2022-07-10
下一篇:
在Go中简单实现HTTP服务中间件发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap