updates on the admincontroller and the hubcity fix and queue orders
This commit is contained in:
@@ -1623,9 +1623,57 @@ func GetHubs(c *fiber.Ctx) error {
|
||||
if err := query.Find(&hubs).Error; err != nil {
|
||||
return utils.Internal(c, "failed to fetch hubs")
|
||||
}
|
||||
attachHubCities(hubs)
|
||||
return utils.List(c, hubs, int64(len(hubs)))
|
||||
}
|
||||
|
||||
// attachHubCities fills Hub.City from applocations, in ONE query for the whole
|
||||
// page rather than one per hub — this list is read on every console page load
|
||||
// through ZoneContext, so a per-row lookup would be twenty round trips for a
|
||||
// field that comes from a five-row table.
|
||||
//
|
||||
// A hub whose applocationid matches nothing keeps an empty City. That is the
|
||||
// honest answer, and callers already treat "" as "unknown": ZoneContext skips
|
||||
// its city comparison rather than matching everything.
|
||||
func attachHubCities(hubs []models.Hub) {
|
||||
if len(hubs) == 0 {
|
||||
return
|
||||
}
|
||||
ids := make([]int, 0, len(hubs))
|
||||
seen := map[int]bool{}
|
||||
for _, h := range hubs {
|
||||
if h.Applocationid != 0 && !seen[h.Applocationid] {
|
||||
seen[h.Applocationid] = true
|
||||
ids = append(ids, h.Applocationid)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var locs []models.AppLocation
|
||||
if err := db.DB.Where("applocationid IN ?", ids).Find(&locs).Error; err != nil {
|
||||
// A failed lookup leaves every City empty, which is the same state the
|
||||
// response had before this existed. Refusing the whole hub list because
|
||||
// one derived label could not be resolved would be worse.
|
||||
return
|
||||
}
|
||||
byID := make(map[int]string, len(locs))
|
||||
for _, l := range locs {
|
||||
byID[l.Applocationid] = l.Applocationname
|
||||
}
|
||||
applyHubCities(hubs, byID)
|
||||
}
|
||||
|
||||
// applyHubCities writes the resolved city onto each hub. Split from the query so
|
||||
// the mapping — including what happens to a hub whose applocation is missing —
|
||||
// can be tested without a database.
|
||||
func applyHubCities(hubs []models.Hub, byID map[int]string) {
|
||||
for i := range hubs {
|
||||
hubs[i].City = byID[hubs[i].Applocationid]
|
||||
}
|
||||
}
|
||||
|
||||
func CreateHub(c *fiber.Ctx) error {
|
||||
req := new(dto.HubCreateRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
@@ -1659,7 +1707,12 @@ func GetHubDetails(c *fiber.Ctx) error {
|
||||
if err := db.DB.Where("hubid = ? AND deletedat IS NULL", id).First(&hub).Error; err != nil {
|
||||
return utils.NotFound(c, "hub not found")
|
||||
}
|
||||
return utils.OK(c, hub)
|
||||
// A one-element slice, because attachHubCities writes THROUGH the slice —
|
||||
// handing it `[]models.Hub{hub}` would fill a copy and return the original
|
||||
// with City still empty.
|
||||
one := []models.Hub{hub}
|
||||
attachHubCities(one)
|
||||
return utils.OK(c, one[0])
|
||||
}
|
||||
|
||||
func UpdateHub(c *fiber.Ctx) error {
|
||||
|
||||
Reference in New Issue
Block a user