init(api): add quote update handler
This commit is contained in:
parent
08ac117b5a
commit
a6e9665413
@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/H1K0/SkazaNull/db"
|
||||
"github.com/H1K0/SkazaNull/models"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -148,6 +149,57 @@ func quoteAdd(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, quote)
|
||||
}
|
||||
|
||||
func quoteUpdate(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user_id, ok := session.Get("user_id").(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"})
|
||||
return
|
||||
}
|
||||
quote_id := c.Param("id")
|
||||
var body map[string]string
|
||||
err := c.BindJSON(&body)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "И чё за шнягу ты мне кинул?"})
|
||||
return
|
||||
}
|
||||
var quote models.Quote
|
||||
ctx := context.Background()
|
||||
newText, ok := body["text"]
|
||||
if ok && newText != "" {
|
||||
quote, err = db.QuoteUpdateText(ctx, user_id, quote_id, newText)
|
||||
if err != nil {
|
||||
status, message := HandleDBError(err)
|
||||
c.JSON(status, gin.H{"error": message})
|
||||
return
|
||||
}
|
||||
}
|
||||
newAuthor, ok := body["author"]
|
||||
if ok && newAuthor != "" {
|
||||
quote, err = db.QuoteUpdateAuthor(ctx, user_id, quote_id, newAuthor)
|
||||
if err != nil {
|
||||
status, message := HandleDBError(err)
|
||||
c.JSON(status, gin.H{"error": message})
|
||||
return
|
||||
}
|
||||
}
|
||||
datetimeStr, ok := body["datetime"]
|
||||
if ok && datetimeStr != "" {
|
||||
newDatetime, err := time.Parse(time.RFC3339, datetimeStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Чёт дата и время у тебя какие-то кривые..."})
|
||||
return
|
||||
}
|
||||
quote, err = db.QuoteUpdateDatetime(context.Background(), user_id, quote_id, newDatetime)
|
||||
if err != nil {
|
||||
status, message := HandleDBError(err)
|
||||
c.JSON(status, gin.H{"error": message})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, quote)
|
||||
}
|
||||
|
||||
func quoteDelete(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
user_id, ok := session.Get("user_id").(string)
|
||||
|
||||
@ -24,6 +24,7 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
api.GET("/quotes", quotesGet)
|
||||
api.POST("/quotes", quoteAdd)
|
||||
api.GET("/quotes/:id", quoteGet)
|
||||
api.PATCH("/quotes/:id", quoteUpdate)
|
||||
api.DELETE("/quotes/:id", quoteDelete)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user