Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Create 11A18633-9318-4EAD-A762-3E838CE7CF1A #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions 11A18633/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
)

type TODO struct {
Content string `json:"content"`
Done bool `json:"done"`
}

var todos []TODO

type Response struct {
Status string `json:"status"`
Data interface{} `json:"data"`
}

func main() {
r := gin.Default()

// 添加TODO
r.POST("/todo", func(c *gin.Context) {
var todo TODO
if err := c.ShouldBindJSON(&todo); err != nil {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: err.Error()})
return
}
todos = append(todos, todo)
c.JSON(http.StatusOK, Response{Status: "ok"})
})

// 删除TODO
r.DELETE("/todo/:index", func(c *gin.Context) {
index, err := strconv.Atoi(c.Param("index"))
if err != nil {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: err.Error()})
return
}
if index < 0 || index >= len(todos) {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: "index out of range"})
return
}
todos = append(todos[:index], todos[index+1:]...)
c.JSON(http.StatusOK, Response{Status: "ok"})
})

// 修改TODO
r.PUT("/todo/:index", func(c *gin.Context) {
index, err := strconv.Atoi(c.Param("index"))
if err != nil {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: err.Error()})
return
}
var todo TODO
if err := c.ShouldBindJSON(&todo); err != nil {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: err.Error()})
return
}
todos[index] = todo
c.JSON(http.StatusOK, Response{Status: "ok"})
})

// 获取TODO列表
r.GET("/todo", func(c *gin.Context) {
c.JSON(http.StatusOK, todos)
})

// 获取TODO详情
r.GET("/todo/:index", func(c *gin.Context) {
index, err := strconv.Atoi(c.Param("index"))
if err != nil {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: err.Error()})
return
}
if index < 0 || index >= len(todos) {
c.JSON(http.StatusBadRequest, Response{Status: "error", Data: "index out of range"})
return
}
c.JSON(http.StatusOK, todos[index])
})

r.Run(":8080")
}