hongbo-miao/hongbomiao.com

View on GitHub
api-go/internal/graphql_server/controllers/update_seed.go

Summary

Maintainability
A
0 mins
Test Coverage
package controllers

import (
    "github.com/buger/jsonparser"
    "github.com/gin-gonic/gin"
    "github.com/hongbo-miao/hongbomiao.com/api-go/internal/graphql_server/utils"
    "github.com/rs/zerolog/log"
    "io"
    "net/http"
)

func UpdateSeed(c *gin.Context) {
    bodyBytes, _ := io.ReadAll(c.Request.Body)
    n, err := jsonparser.GetInt(bodyBytes, "input", "n")
    if err != nil {
        log.Error().Err(err).Bytes("bodyBytes", bodyBytes).Msg("jsonparser.GetInt n")
        c.JSON(http.StatusInternalServerError, gin.H{
            "error": "something bad happened",
        })
        return
    }

    seed, err := utils.SetSeed(int(n))
    if err != nil {
        log.Error().Err(err).Msg("utils.SetSeed")
        c.JSON(http.StatusInternalServerError, gin.H{
            "error": err.Error(),
        })
        return
    }

    c.JSON(http.StatusOK, seed)
}