DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: dev deck-builder/api/handlers/createaction.go -rw-r--r-- 927 bytes
1904a6d3Jonni Liljamo feat(client): sorta kinda check for end conditions 1 year, 4 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
45
/*
 * 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/apierror"
	"api/db"
	"api/models"
	"net/http"

	"github.com/gin-gonic/gin"
	"gorm.io/datatypes"
)

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)
	action.Seed = input.Seed

	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)
}