bigest part of the api done
This commit is contained in:
74
controllers/v1/card/card.controller.go
Normal file
74
controllers/v1/card/card.controller.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package card
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/auth"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/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.Card
|
||||
if err := c.BindJSON(&card); err != nil {
|
||||
return
|
||||
}
|
||||
card.UserID = c.GetUint("user_id")
|
||||
|
||||
if models.DB.Create(&card).Save(&card).Error != nil {
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusCreated, card.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)
|
||||
}
|
||||
Reference in New Issue
Block a user