1 Star 0 Fork 0

叶明志 / golang练习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bookHandler.go 4.64 KB
一键复制 编辑 原始数据 按行查看 历史
package controller
import (
"GolangLearnPractice1/bookstore/dao"
"GolangLearnPractice1/bookstore/model"
"fmt"
"net/http"
"strconv"
"text/template"
)
/*
//IndexHandler 处理首页
func IndexHandler(w http.ResponseWriter, r *http.Request) {
pageNo := r.FormValue("pageNo")
if pageNo == "" {
pageNo = "1"
}
page, err := dao.GetPageBooks(pageNo)
if err != nil {
fmt.Println("分页获取所有图书失败")
}
t := template.Must(template.ParseFiles("view/index.html"))
t.Execute(w, page)
}
*/
/*
//GetBooks 获取所有图书
func GetBooks(w http.ResponseWriter, r *http.Request) {
books, err := dao.GetBooks()
if err != nil {
fmt.Println("获取所有图书失败")
}
t := template.Must(template.ParseFiles("view/pages/manager/book_manager.html"))
t.Execute(w, books)
}
*/
//GetPageBooks 分页获取所有图书
func GetPageBooks(w http.ResponseWriter, r *http.Request) {
pageNo := r.FormValue("pageNo")
if pageNo == "" {
pageNo = "1"
}
page, err := dao.GetPageBooks(pageNo)
if err != nil {
fmt.Println("分页获取所有图书失败")
}
t := template.Must(template.ParseFiles("view/pages/manager/book_manager.html"))
t.Execute(w, page)
}
//GetPageBooksByPrice 分页获取价格范围内的所有图书
func GetPageBooksByPrice(w http.ResponseWriter, r *http.Request) {
pageNo := r.FormValue("pageNo")
minPrice := r.FormValue("min")
maxPrice := r.FormValue("max")
if pageNo == "" {
pageNo = "1"
}
var page *model.Page
if minPrice == "" && maxPrice == "" {
//没有按价格查找,正常反馈
page, _ = dao.GetPageBooks(pageNo)
} else {
//价格查找商品
page, _ = dao.GetPageBooksByPrice(pageNo, minPrice, maxPrice)
page.MinPrice = minPrice
page.MaxPrice = maxPrice
}
/*
cookie, _ := r.Cookie("user")
if cookie != nil {
cookieValue := cookie.Value
sess, _ := dao.GetSessionByID(cookieValue)
if sess.UserID > 0 {
page.IsLogin = true
page.Username = sess.Username
}
}
*/
//-------------------
//调用IsLogin函数判断是否已经登录
flag, session := dao.IsLogin(r)
if flag {
//已经登录,设置page中的IsLogin字段和Username的字段值
page.IsLogin = true
page.Username = session.Username
page.UserID = session.UserID
page.IsAdmin = dao.UserIDIsAdmin(page.UserID)
}
t := template.Must(template.ParseFiles("view/index.html"))
t.Execute(w, page)
}
/*
//UpdateOrAddBook 修改或者添加图书
func UpdateOrAddBook(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("view/pages/manager/book_edit.html"))
title := r.PostFormValue("title")
author := r.PostFormValue("author")
price := r.PostFormValue("price")
sales := r.PostFormValue("sales")
stock := r.PostFormValue("stock")
fPrice, _ := strconv.ParseFloat(price, 64)
fSales, _ := strconv.ParseInt(sales, 10, 0)
fStcok, _ := strconv.ParseInt(stock, 10, 0)
book := &model.Book{
Title: title,
Author: author,
Price: fPrice,
Sales: int(fSales),
Stock: int(fStcok),
ImgPath: "/static/img/default.jpg",
}
dao.AddBook(book)
GetBooks(w, r)
}
*/
//ToPageAddBook 页面跳转到
func ToPageAddBook(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseFiles("view/pages/manager/book_edit.html"))
t.Execute(w, "")
}
//DeleteBook 从数据库里删除图书
func DeleteBook(w http.ResponseWriter, r *http.Request) {
bookID := r.FormValue("bookId")
dao.DeleteBookByID(bookID)
GetPageBooks(w, r)
}
//ToUpdateBookPage 网页转到更新或添加图书页面
func ToUpdateBookPage(w http.ResponseWriter, r *http.Request) {
bookID := r.FormValue("bookId")
book, _ := dao.GetBookByID(bookID)
if book.ID > 0 {
t := template.Must(template.ParseFiles("view/pages/manager/book_modify.html"))
t.Execute(w, book)
} else {
t := template.Must(template.ParseFiles("view/pages/manager/book_modify.html"))
t.Execute(w, "")
}
}
//UpdateOrAddBook 添加或更新图书的信息
func UpdateOrAddBook(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("view/pages/manager/book_modify.html"))
id := r.PostFormValue("bookId")
title := r.PostFormValue("title")
author := r.PostFormValue("author")
price := r.PostFormValue("price")
sales := r.PostFormValue("sales")
stock := r.PostFormValue("stock")
fPrice, _ := strconv.ParseFloat(price, 64)
fSales, _ := strconv.ParseInt(sales, 10, 0)
fStcok, _ := strconv.ParseInt(stock, 10, 0)
fID, _ := strconv.ParseInt(id, 10, 0)
book := &model.Book{
ID: int(fID),
Title: title,
Author: author,
Price: fPrice,
Sales: int(fSales),
Stock: int(fStcok),
ImgPath: "/static/img/default.jpg",
}
if book.ID > 0 {
dao.UpdateBook(book)
} else {
dao.AddBook(book)
}
GetPageBooks(w, r)
}
Go
1
https://gitee.com/yemingzhi/GolangLearnPractice1.git
git@gitee.com:yemingzhi/GolangLearnPractice1.git
yemingzhi
GolangLearnPractice1
golang练习
2bf136849dce

搜索帮助