feat: add vue-golang-mysql sample
Signed-off-by: fffzlfk <1319933925qq@gmail.com>
This commit is contained in:
30
vue-golang-mysql/server/controllers/create.go
Normal file
30
vue-golang-mysql/server/controllers/create.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"code-paste/database"
|
||||
"code-paste/model"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func generateUUID() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
|
||||
func CreatePaste(c *gin.Context) {
|
||||
var p model.Paste
|
||||
if err := c.BindJSON(&p); err != nil {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
p.ExpiredAt = time.Now().AddDate(0, 0, p.ExpiredDays)
|
||||
p.ID = generateUUID()
|
||||
database.DB.Create(&p)
|
||||
c.JSON(http.StatusAccepted, gin.H{
|
||||
"status": "ok",
|
||||
"uuid": p.ID,
|
||||
})
|
||||
}
|
26
vue-golang-mysql/server/controllers/read.go
Normal file
26
vue-golang-mysql/server/controllers/read.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"code-paste/database"
|
||||
"code-paste/model"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ReadPaste(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var p model.Paste
|
||||
res := database.DB.First(&p, "id = ?", id)
|
||||
if res.Error != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if p.ExpiredAt.Before(time.Now()) {
|
||||
database.DB.Delete(&p)
|
||||
ReadPaste(c)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusAccepted, p)
|
||||
}
|
Reference in New Issue
Block a user