本文同步發表於: Sponge Records
由於我們的專案有設計圖表系統,而目前設計是由 ai 端丟預測的資料到 api 中儲存,前端要呈現時索取需要的資訊
而本次先以儲存圖片的 url 為範例,直接吐圖片的 url 給前端作呈現
這裡會將架構分為 controllers\chatsControllers.go 、 models\chats.go
我們將會實作 controllers\chatsControllers.go 中的 CreatePic,先將圖表的名稱與 url 寫入 db
controllers\chatsControllers.go - var CreatePic
package controllers
import (
"encoding/json"
"golang-api/models"
u "golang-api/utils"
"net/http"
)
var CreatePic = func(w http.ResponseWriter, r *http.Request) {
pic := &models.Pic{}
err := json.NewDecoder(r.Body).Decode(pic)
//如果輸入的請求錯誤
if err != nil {
u.Respond(w, u.Message(false, "Invalid request"))
return
}
resp := pic.PicCreate()
u.Respond(w, resp)
}
實作增加圖片 url 的功能
models\chats.go - PicCreate
package models
import (
u "golang-api/utils"
"github.com/jinzhu/gorm"
)
//圖片 url 儲存
type Pic struct {
gorm.Model
PicName string `json:"PicName"`
PicUrl string `json:"PicUrl"`
}
func (pic *Pic) PicCreate() map[string]interface{} {
GetDB().Create(pic)
response := u.Message(true, "Pic has been created")
return response
}
完成了寫入後,再來要來實作讀取,透過傳入圖片名稱即可得到 url
controllers\chatsControllers.go - var GetPic
var GetPic = func(w http.ResponseWriter, r *http.Request) {
chat := &models.Pic{}
err := json.NewDecoder(r.Body).Decode(chat)
//如果輸入的請求錯誤
if err != nil {
u.Respond(w, u.Message(false, "Invalid request"))
return
}
resp := models.PicGet(chat.PicName)
u.Respond(w, resp)
}
再來要實作讀取的功能
models\chats.go - PicGet
func PicGet(PicName string) map[string]interface{} {
pic := &Pic{}
err := GetDB().Table("pic").Where("PicName = ?", PicName).First(PicName).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return u.Message(false, "pic not found")
}
return u.Message(false, "Connection error")
}
resp := u.Message(true, "pic get")
resp["pic"] = pic
return resp
}
controllers\chatsControllers.go
package controllers
import (
"encoding/json"
"golang-api/models"
u "golang-api/utils"
"net/http"
)
var CreatePic = func(w http.ResponseWriter, r *http.Request) {
pic := &models.Pic{}
err := json.NewDecoder(r.Body).Decode(pic)
//如果輸入的請求錯誤
if err != nil {
u.Respond(w, u.Message(false, "Invalid request"))
return
}
resp := pic.PicCreate()
u.Respond(w, resp)
}
var GetPic = func(w http.ResponseWriter, r *http.Request) {
chat := &models.Pic{}
err := json.NewDecoder(r.Body).Decode(chat)
//如果輸入的請求錯誤
if err != nil {
u.Respond(w, u.Message(false, "Invalid request"))
return
}
resp := models.PicGet(chat.PicName)
u.Respond(w, resp)
}
models\chats.go
package models
import (
u "golang-api/utils"
"github.com/jinzhu/gorm"
)
//圖片 url 儲存
type Pic struct {
gorm.Model
PicName string `json:"PicName"`
PicUrl string `json:"PicUrl"`
}
func (pic *Pic) PicCreate() map[string]interface{} {
GetDB().Create(pic)
response := u.Message(true, "Pic has been created")
return response
}
func PicGet(PicName string) map[string]interface{} {
pic := &Pic{}
err := GetDB().Table("pic").Where("PicName = ?", PicName).First(PicName).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return u.Message(false, "pic not found")
}
return u.Message(false, "Connection error")
}
resp := u.Message(true, "pic get")
resp["pic"] = pic
return resp
}
我們本次先設計了儲存與讀取圖片 url 的功能,下回會進入該如何儲存來自 ai 分析的資料與呈現