lockserver/httpserver/user-codes-admin.go

52 lines
992 B
Go
Raw Normal View History

package httpserver
import (
"database/sql"
"errors"
"net/http"
"git.janky.solutions/finn/lockserver/db"
"git.janky.solutions/finn/lockserver/openapi"
echo "github.com/labstack/echo/v4"
)
func (l lockserver) AddUserCode(c echo.Context) error {
queries, dbc, err := db.Get()
if err != nil {
return err
}
defer dbc.Close()
ctx := c.Request().Context()
if _, err = queries.CreateUserCode(ctx, db.CreateUserCodeParams{}); err != nil {
return err
}
return c.NoContent(http.StatusInternalServerError)
}
func (l lockserver) GetAllUserCodes(c echo.Context) error {
queries, dbc, err := db.Get()
if err != nil {
return err
}
defer dbc.Close()
ctx := c.Request().Context()
resp := []openapi.UserCode{}
codes, err := queries.GetAllUserCodes(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.JSON(http.StatusOK, resp)
}
return err
}
for _, code := range codes {
resp = append(resp, code.OpenAPI())
}
return c.JSON(http.StatusOK, resp)
}