http - Golang-firestore update and delete method not working
问题描述
I am working on Golang project with firestore as database. I have write controllers for GET, POST, UPDATE, DELETE. But only GET & POST are working. UPDATE and DELETE are not working. CreateGoal
, GetGoals
, GetGoal
are working. I can see data in firestore while using these methods. But can not see with the methods UpdateGoal
& DeleteGoal
. I am not understanding where I am doing mistakes. I have provided the code below.
You can find complete code on this link - https://github.com/RohitKuwar/goLang-restAPI
Thank you.
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/projgo/gotest/models"
)
var goals []models.Goals
// controllers
func GenerateGoals(w http.ResponseWriter,r *http.Request){
w.Header().Set("Content-Type","application/json")
goals = append(goals, models.Goals{Id:1,Title: "Read on event loop",Status: "completed"})
goals = append(goals, models.Goals{Id:2,Title: "read just javaScript articles",Status: "active"})
goals = append(goals, models.Goals{Id:3,Title: "learn go",Status: "closed"})
var status = map[string]string{"message":"Succesfully generated a goals"}
json.NewEncoder(w).Encode(status)
}
func CreateGoal(w http.ResponseWriter,r *http.Request){
w.Header().Set("Content-Type","application/json")
var goal models.Goals
_ =json.NewDecoder(r.Body).Decode(&goal)
goals=append(goals,goal)
var status = map[string]string{"message":"Succesfully created a goal"}
json.NewEncoder(w).Encode(status)
}
func GetGoals(w http.ResponseWriter,r *http.Request){
w.Header().Set("Content-Type","application/json")
if len(goals) ==0 {
var status = map[string]string{"message":"Generate goals by hiting / route"}
json.NewEncoder(w).Encode(status)
}else {
json.NewEncoder(w).Encode(goals)
}
}
func GetGoal(w http.ResponseWriter,r *http.Request){
w.Header().Set("Content-Type","application/json")
params := mux.Vars(r)
for _,goal:= range goals{
if strconv.Itoa(goal.Id) == params["id"] {
json.NewEncoder(w).Encode(goal)
return
}
}
http.Error(w,"No goal found",http. StatusBadRequest)
}
func UpdateGoal(w http.ResponseWriter,r *http.Request){
w.Header().Set("Content-Type","application/json")
params := mux.Vars(r)
for index,goal:= range goals{
if strconv.Itoa(goal.Id) == params["id"] {
goals = append(goals[:index], goals[index+1:]...) //remove a goal from map
var goal models.Goals
_ =json.NewDecoder(r.Body).Decode(&goal)
goals=append(goals,goal)
var status = map[string]string{"message":"Succesfully updated a goal"}
json.NewEncoder(w).Encode(status)
return
}
}
http.Error(w,"No goal found",http. StatusBadRequest)
}
func DeleteGoal(w http.ResponseWriter,r *http.Request){
w.Header().Set("Content-Type","application/json")
params := mux.Vars(r)
for index,goal:= range goals{
if strconv.Itoa(goal.Id) == params["id"] {
goals = append(goals[:index], goals[index+1:]...)
var status = map[string]string{"message":"Succesfully deleted a goal"}
json.NewEncoder(w).Encode(status)
return
}
}
http.Error(w,"No goal found",http. StatusBadRequest)
}```
解决方案
推荐阅读
- terraform - 如何将 AWS 警报 ARN 收集到 Terraform 中的字符串中以在 CloudWatch 仪表板创建 JSON 中使用?
- r - 在 Rstudio 中使用 keras 获得可重现的 LSTM
- npm - npm 退避库会在什么情况下尝试重试
- javascript - 如何将现有的 html 页面集成到 react-app 中?
- pattern-matching - 与地图内的地图进行模式匹配(Elixir)
- javascript - Sobel过滤器Javascript如何将黑色像素转换为白色和白色转换为黑色
- reactjs - 刷新页面时如何使 React 组件不更新?
- python - 如何部署 Azure 功能,并确保包得到更新
- java - 如何获取应用程序发送到网站的 xml 文件,并在测试中解析 xml 文件中的数据?
- json - 播音标签 API 调用