lockserver/httpserver/user-codes-admin.go
Finn 350fc3b339
All checks were successful
/ build-container (push) Successful in 1m3s
rip out all auth stuff, add a containerfile
2024-04-23 17:34:57 -07:00

51 lines
992 B
Go

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