package handlers import ( "net/http" "time" "github.com/andyleap/hnh-map/internal/app" ) // APIConfig handles GET /map/api/config. func (h *Handlers) APIConfig(rw http.ResponseWriter, req *http.Request) { s := h.requireSession(rw, req) if s == nil { return } ctx := req.Context() config, err := h.Map.GetConfig(ctx, s.Auths) if err != nil { HandleServiceError(rw, err) return } JSON(rw, http.StatusOK, config) } // CharacterResponse is the API shape for a character, including ownedByMe for the current session. type CharacterResponse struct { Name string `json:"name"` ID int `json:"id"` Map int `json:"map"` Position app.Position `json:"position"` Type string `json:"type"` Updated time.Time `json:"updated,omitempty"` OwnedByMe bool `json:"ownedByMe"` } // APIGetChars handles GET /map/api/v1/characters. func (h *Handlers) APIGetChars(rw http.ResponseWriter, req *http.Request) { s := h.requireSession(rw, req) if s == nil { return } if !h.canAccessMap(s) { JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED") return } if !s.Auths.Has(app.AUTH_MARKERS) && !s.Auths.Has(app.AUTH_ADMIN) { JSON(rw, http.StatusOK, []interface{}{}) return } chars := h.Map.GetCharacters() out := make([]CharacterResponse, 0, len(chars)) for _, c := range chars { out = append(out, CharacterResponse{ Name: c.Name, ID: c.ID, Map: c.Map, Position: c.Position, Type: c.Type, Updated: c.Updated.UTC(), OwnedByMe: c.Username == s.Username, }) } JSON(rw, http.StatusOK, out) } // APIGetMarkers handles GET /map/api/v1/markers. func (h *Handlers) APIGetMarkers(rw http.ResponseWriter, req *http.Request) { s := h.requireSession(rw, req) if s == nil { return } if !h.canAccessMap(s) { JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED") return } ctx := req.Context() if !s.Auths.Has(app.AUTH_MARKERS) && !s.Auths.Has(app.AUTH_ADMIN) { JSON(rw, http.StatusOK, []interface{}{}) return } markers, err := h.Map.GetMarkers(ctx) if err != nil { HandleServiceError(rw, err) return } JSON(rw, http.StatusOK, markers) } // APIGetMaps handles GET /map/api/maps. func (h *Handlers) APIGetMaps(rw http.ResponseWriter, req *http.Request) { ctx := req.Context() s := h.Auth.GetSession(ctx, req) if !h.canAccessMap(s) { JSONError(rw, http.StatusUnauthorized, "Unauthorized", "UNAUTHORIZED") return } showHidden := s.Auths.Has(app.AUTH_ADMIN) maps, err := h.Map.GetMaps(ctx, showHidden) if err != nil { HandleServiceError(rw, err) return } JSON(rw, http.StatusOK, maps) }