/*
* This file is part of laurelin_api
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
package handlers
import (
"api/db"
"api/models"
"api/apierror"
"net/http"
"gorm.io/datatypes"
"github.com/gin-gonic/gin"
)
func CreateAction(c *gin.Context) {
var input models.PostAction
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": apierror.InvalidInput})
c.Abort()
return
}
var action models.Action
action.GameID = input.GameID
action.Invoker = input.Invoker
action.Target = input.Target
action.Command = datatypes.JSON(input.Command)
entry := db.DbConn.Create(&action)
if entry.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.ActionCreationFailed})
c.Abort()
return
}
c.JSON(http.StatusCreated, action)
}