You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package card
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"spahl.ddns.net/jasper/wok-able/auth"
|
|
"spahl.ddns.net/jasper/wok-able/models"
|
|
)
|
|
|
|
func getCardById(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.ParseUint(idStr, 10, 64)
|
|
if err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
var card models.Card
|
|
err = models.DB.Scopes(auth.UserScope(c)).First(&card, id).Error
|
|
if err != nil {
|
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Card not found"})
|
|
return
|
|
}
|
|
c.IndentedJSON(http.StatusOK, card.ToDto())
|
|
}
|
|
|
|
func createCard(c *gin.Context) {
|
|
var card models.CardDto
|
|
if err := c.BindJSON(&card); err != nil {
|
|
return
|
|
}
|
|
cardDbo := card.ToDbo()
|
|
cardDbo.UserID = uint(c.GetFloat64("user_id"))
|
|
|
|
if models.DB.Create(&cardDbo).Save(&cardDbo).Error != nil {
|
|
return
|
|
}
|
|
c.IndentedJSON(http.StatusCreated, cardDbo.ToDto())
|
|
}
|
|
|
|
func updateCard(c *gin.Context) {
|
|
var cardDto models.CardDto
|
|
if err := c.BindJSON(&cardDto); err != nil {
|
|
return
|
|
}
|
|
var card models.Card
|
|
if err := models.DB.Scopes(auth.UserScope(c)).First(&card, cardDto.ID).Error; err != nil {
|
|
c.IndentedJSON(http.StatusNotFound, err.Error())
|
|
}
|
|
card.Front = cardDto.Front
|
|
card.Back = cardDto.Back
|
|
card.Hint = cardDto.Hint
|
|
card.CardDeckID = cardDto.CardDeckID
|
|
|
|
if err := models.DB.Scopes(auth.UserScope(c)).Save(&card).Error; err != nil {
|
|
c.IndentedJSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
c.IndentedJSON(http.StatusAccepted, card.ToDto())
|
|
}
|
|
|
|
func deleteCard(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.ParseUint(idStr, 10, 64)
|
|
if err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
err = models.DB.Scopes(auth.UserScope(c)).Delete(&models.Card{}, id).Error
|
|
if err != nil {
|
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusAccepted)
|
|
}
|