Add district resource and an API to RMO
We're going to need an API for the single-page frontend
This commit is contained in:
parent
4f9617aa2f
commit
bfecae7d61
9 changed files with 114 additions and 16 deletions
47
resource/district.go
Normal file
47
resource/district.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform"
|
||||
"net/http"
|
||||
//"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type districtR struct {
|
||||
router *router
|
||||
}
|
||||
|
||||
type district struct {
|
||||
Name string `json:"name"`
|
||||
URLLogo string `json:"url_logo"`
|
||||
}
|
||||
|
||||
func District(r *router) *districtR {
|
||||
return &districtR{
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (res *districtR) List(ctx context.Context, r *http.Request, query QueryParams) ([]*district, *nhttp.ErrorWithStatus) {
|
||||
organizations, err := platform.OrganizationList(ctx)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("list orgs: %w", err)
|
||||
}
|
||||
districts := make([]*district, 0)
|
||||
for _, org := range organizations {
|
||||
slug := org.Slug()
|
||||
if slug == "" {
|
||||
continue
|
||||
}
|
||||
logo, err := res.router.SlugToURI("district.logo.BySlug", slug)
|
||||
if err != nil {
|
||||
return nil, nhttp.NewError("logo url: %w", err)
|
||||
}
|
||||
districts = append(districts, &district{
|
||||
Name: org.Name(),
|
||||
URLLogo: logo,
|
||||
})
|
||||
}
|
||||
return districts, nil
|
||||
}
|
||||
|
|
@ -59,6 +59,18 @@ func (r *router) IDToURI(route string, id int) (string, error) {
|
|||
uri.Scheme = "https"
|
||||
return uri.String(), nil
|
||||
}
|
||||
func (r *router) SlugToURI(route string, slug string) (string, error) {
|
||||
handler := r.router.Get(route)
|
||||
if handler == nil {
|
||||
return "", fmt.Errorf("nil handler '%s'", route)
|
||||
}
|
||||
uri, err := handler.URL("slug", slug)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build uri: %w", err)
|
||||
}
|
||||
uri.Scheme = "https"
|
||||
return uri.String(), nil
|
||||
}
|
||||
|
||||
func (r *router) UUIDToURI(route string, u *uuid.UUID) (*string, error) {
|
||||
if u == nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue