Commit 126d0122 authored by bensinober's avatar bensinober
Browse files

Default to apiVer 2 and sort by float

parent ba76949a
......@@ -62,7 +62,7 @@
<label for="v1">v1</label>
<input id="apiV1" type="radio" name="listPath">
<label for="v2">v2</label>
<input id="apiV2" type="radio" name="listPath">
<input id="apiV2" type="radio" name="listPath" checked="checked">
</div>
<select id="select-branch">
<option value="">Velg avdeling</option>
......
......@@ -8,6 +8,7 @@ import (
"log"
"net/http"
"regexp"
"sort"
"strings"
"sync"
"time"
......@@ -123,26 +124,32 @@ const (
)
type server struct {
kohaApi string
listPath string
branch string
kohaApi string
branch string
apiVer int
mu sync.RWMutex
db map[string]record
}
func newServer(koha, pick string) *server {
func newServer(koha string, apiVer int) *server {
return &server{
db: make(map[string]record),
kohaApi: koha,
listPath: "/picklist",
branch: "bjor", // default
db: make(map[string]record),
kohaApi: koha,
apiVer: apiVer,
branch: "bjor", // default
}
}
func (s *server) collect() {
var res []record
url := s.kohaApi + s.listPath + "?branch=" + s.branch
var url string
switch s.apiVer {
case 1:
url = s.kohaApi + "/picklist"
case 2:
url = s.kohaApi + "/picklist_v2" + "?branch=" + s.branch
}
log.Printf("Fetching list from : %s", url)
resp, err := http.Get(url)
if err != nil {
......@@ -197,10 +204,10 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
case [2]string{"GET", "/setApiV1"}:
log.Println("Setting api v1")
s.listPath = "/picklist"
s.apiVer = 1
case [2]string{"GET", "/setApiV2"}:
log.Println("Setting api v2")
s.listPath = "/picklist_v2"
s.apiVer = 2
case [2]string{"GET", "/holds"}:
// processed below
default:
......@@ -311,6 +318,7 @@ type biblio struct {
Dewey string
Format string
SortName string
Sorting float64
Location string
NoSpaceLocation string
OnlyEx string
......@@ -370,6 +378,7 @@ type record struct {
Dewey string `json:"dewey"`
Format string `json:"format"`
SortName string `json:"sort_author"`
Sorting float64 `json:"sorting"`
Age float64 `json:"age"`
Holds []struct {
Branch string `json:"branch"`
......@@ -432,7 +441,6 @@ func (s *server) holdsFor(branchcode, zoneCode, cat string, onlyHere, pickupHere
if zoneCode != "" && branchcode != "hutl" {
return nil, nil, nil, errors.New("Zones are only applicable to branch: hutl")
}
zonesMap := make(map[string]int)
categoriesMap := make(map[string]int)
holdsByLocation := make(map[string]*biblio)
......@@ -443,6 +451,7 @@ func (s *server) holdsFor(branchcode, zoneCode, cat string, onlyHere, pickupHere
continue
}
// ?
if onlyHere && len(record.Pick) > 1 {
continue
}
......@@ -492,6 +501,7 @@ func (s *server) holdsFor(branchcode, zoneCode, cat string, onlyHere, pickupHere
Location: i.Location,
NoSpaceLocation: onlyCharsAndDigits.ReplaceAllString(i.Location+i.Ccode+i.OnlyEx, ""),
SortName: record.SortName,
Sorting: record.Sorting,
Barcodes: record.Barcodes,
Items: []item{item{Barcode: i.Barcode, Zone: i.Ccode, Category: i.Category, Branch: branchcode, Itemnumber: i.Itemnumber, IPP: i.Itemlost == "5"}},
}
......@@ -538,8 +548,16 @@ func (s *server) holdsFor(branchcode, zoneCode, cat string, onlyHere, pickupHere
res = append(res, *h)
}
/*
/* SORTING */
if s.apiVer == 2 {
sort.Slice(res, func(i, j int) bool {
return res[i].Sorting < res[j].Sorting
})
} else {
sort.Slice(res, func(i, j int) bool {
// First sort by sorting float, if any
iLocation := res[i].Location
jLocation := res[j].Location
iFormat := res[i].Format
......@@ -624,7 +642,8 @@ func (s *server) holdsFor(branchcode, zoneCode, cat string, onlyHere, pickupHere
// 7: tittel
return strings.ToLower(res[i].Title) < strings.ToLower(res[j].Title)
})
*/
}
zones := make([]zone, 0, len(zonesMap))
for k, v := range zonesMap {
zones = append(zones, zone{Label: zoneLabels[k], ID: k, Count: v})
......@@ -633,14 +652,17 @@ func (s *server) holdsFor(branchcode, zoneCode, cat string, onlyHere, pickupHere
for k, v := range categoriesMap {
categories = append(categories, category{Label: k, Count: v})
}
/*
if s.apiVer == 1 {
sort.Slice(zones, func(i, j int) bool {
return zones[i].Label < zones[j].Label
})
sort.Slice(categories, func(i, j int) bool {
return categories[i].Label < categories[j].Label
})
*/
}
return categories, zones, res, nil
}
......@@ -648,11 +670,11 @@ func main() {
freq := flag.Duration("freq", 1*time.Minute, "Polling frequency")
httpAddr := flag.String("http", ":7777", "HTTP serve address")
kohaApi := flag.String("koha", "http://koha:8081/api", "Koha API")
pickApi := flag.String("pick", "http://koha:8081/api/picklist", "picklist API")
apiVer := flag.Int("apiVer", 2, "API version")
flag.Parse()
srv := newServer(*kohaApi, *pickApi)
srv := newServer(*kohaApi, *apiVer)
go func() {
for {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment