Shun 发表于 2022-8-1 13:02:49

Gorilla Mux 和 GORM 的使用方法

package mainimport (
"encoding/json"
"net/http"
"mybook/model"
"github.com/gorilla/mux"
"gorm.io/gorm"
)
type Server struct {
db *gorm.DB
}
type UpdateBook struct {
Price       float64 `json:"price"`
Description string`json:"decription"`
Category    string`json:"category"`
}
func NewServer(db *gorm.DB) *Server {
return &Server{db: db}
}
func (s *Server) RegisterRouter(router *mux.Router) {
router.HandleFunc("/books", s.getBooks)
router.HandleFunc("/book/{id}", s.getBook).Methods("GET")
router.HandleFunc("/book", s.createBook).Methods("POST")
router.HandleFunc("/book/{id}", s.updateBook).Methods("PUT")
router.HandleFunc("/book/{id}", s.deleteBook).Methods("DELETE")
}
func (s *Server) getBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;")
var books []model.Book
if err := s.db.Find(&books).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(books)
}
func (s *Server) createBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var book model.Book
if err := json.NewDecoder(r.Body).Decode(&book); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newBook := model.Book{Price: book.Price, Description: book.Description, Category: book.Category}
if err := s.db.Create(newBook).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(newBook)
}
func (s *Server) getBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var book model.Book
vars := mux.Vars(r)
id := vars["id"]
if err := s.db.Where("id = ?", id).First(&book).Error; err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(book)
}
func (s *Server) updateBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var updateBook UpdateBook
var book model.Book
vars := mux.Vars(r)
id := vars["id"]
if err := json.NewDecoder(r.Body).Decode(&updateBook); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := s.db.Where("id = ?", id).First(&book).Error; err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if err := s.db.Model(&book).Updates(&model.Book{
Price:       updateBook.Price,
Description: updateBook.Description,
Category:    updateBook.Category}).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(book)
}
func (s *Server) deleteBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var book model.Book
vars := mux.Vars(r)
id := vars["id"]
if err := s.db.Where("id = ?", id).First(&book).Error; err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if err := s.db.Delete(&book).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("Book Deleted Successfully!")
}

http://blog.itpub.net/69955379/viewspace-2908269/
页: [1]
查看完整版本: Gorilla Mux 和 GORM 的使用方法