Signing works
This commit is contained in:
parent
d272fa90b4
commit
da2a89010c
19 changed files with 348 additions and 100 deletions
|
@ -4,10 +4,9 @@ import (
|
|||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
httputils "git.mstar.dev/mstar/goutils/http"
|
||||
webutils "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gorm.io/gorm"
|
||||
|
@ -27,15 +26,28 @@ func postAs(w http.ResponseWriter, r *http.Request) {
|
|||
data := Inbound{}
|
||||
err := dec.Decode(&data)
|
||||
if err != nil {
|
||||
httputils.HttpErr(w, 0, "json decode failed", http.StatusBadRequest)
|
||||
webutils.ProblemDetails(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
"/errors/bad-request-data",
|
||||
"bad request data",
|
||||
nil,
|
||||
map[string]any{
|
||||
"sample": Inbound{
|
||||
Username: "bob",
|
||||
Content: "Heya there, this is sample data",
|
||||
},
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
user, err := dbgen.User.GetByUsername(data.Username)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
httputils.HttpErr(w, 0, "no user with that name", http.StatusNotFound)
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusNotFound)
|
||||
} else {
|
||||
log.Error().Err(err).Str("name", data.Username).Msg("Failed to find user")
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -69,13 +81,13 @@ func notesFrom(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
log.Error().Err(err).Str("name", username).Msg("Failed to get user")
|
||||
storage.HandleReconnectError(err)
|
||||
httputils.HttpErr(w, 0, "failed to get user", http.StatusInternalServerError)
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
notes, err := dbgen.Note.GetNotesPaged(user.ID, 0, uint8(models.NOTE_TARGET_PUBLIC))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("name", username).Msg("Failed to get notes")
|
||||
httputils.HttpErr(w, 0, "failed to get notes", http.StatusInternalServerError)
|
||||
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
publicNotes := sliceutils.Map(notes, func(t models.Note) webshared.Note {
|
||||
|
@ -83,11 +95,5 @@ func notesFrom(w http.ResponseWriter, r *http.Request) {
|
|||
n.FromModel(&t)
|
||||
return n
|
||||
})
|
||||
jsonNotes, err := json.Marshal(publicNotes)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to marshal notes")
|
||||
httputils.HttpErr(w, 0, "failed to marshal", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, string(jsonNotes))
|
||||
webutils.SendJson(w, publicNotes)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue