DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 443c9b0c1e96aadaa1aff06953210c400b7fc0c9 deck-builder/api/handlers/createaction.go -rw-r--r-- 901 bytes
443c9b0c — skye feat!(client): get in-game, and see some details 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * 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)
}