More API progress

This time mainly helper functions for converting an account and
associated types into their API representation
This commit is contained in:
Melody Becker 2024-11-04 07:48:46 +01:00
parent 873f52d64f
commit a653477e7f
8 changed files with 201 additions and 7 deletions

View file

@ -3,7 +3,10 @@ package server
import (
"net/http"
"github.com/google/jsonapi"
"github.com/rs/zerolog/hlog"
"gitlab.com/mstarongitlab/goutils/other"
"gitlab.com/mstarongitlab/linstrom/storage"
)
// No create account. That happens during passkey registration
@ -11,8 +14,45 @@ import (
func linstromGetAccount(w http.ResponseWriter, r *http.Request) {
store := StorageFromRequest(r)
log := hlog.FromRequest(r)
accId := AccountIdFromRequest(r)
acc, err := store.FindAccountById(accId)
switch err {
case nil:
// Ok, do nothing
case storage.ErrEntryNotFound:
other.HttpErr(w, HttpErrIdNotFound, "account not found", http.StatusNotFound)
return
default:
log.Error().Err(err).Str("account-id", accId).Msg("Failed to get account from storage")
other.HttpErr(
w,
HttpErrIdDbFailure,
"Failed to get account from storage",
http.StatusInternalServerError,
)
return
}
// TODO: Check if caller is actually allowed to view the account requested.
outAccount, err := convertAccountStorageToLinstrom(acc, store)
if err != nil {
log.Error().
Err(err).
Msg("Failed to convert storage account (and attached data) into linstrom API representation")
other.HttpErr(
w,
HttpErrIdConverionFailure,
"Failed to convert storage account and attached data into API representation",
http.StatusInternalServerError,
)
return
}
err = jsonapi.MarshalPayload(w, outAccount)
if err != nil {
log.Error().Err(err).Any("account", outAccount).Msg("Failed to marshal and write account")
}
}
func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {}
func linstromDeleteAccount(w http.ResponseWriter, r *http.Request) {}