home screen api : GWM
This commit is contained in:
parent
9bd86ba657
commit
065e62b750
95
main.go
95
main.go
@ -901,6 +901,101 @@ func main() {
|
||||
return c.JSON(http.StatusOK, aggregatedResponse)
|
||||
})
|
||||
|
||||
e.Router.GET("/api/getHomePageData", func(c echo.Context) error {
|
||||
|
||||
// Define the struct for holding the query results
|
||||
mainTopics := []struct {
|
||||
MainTopic string `db:"main_topic" json:"main_topic"`
|
||||
MainTopicListOrder string `db:"main_topic_list_order" json:"main_topic_list_order"`
|
||||
}{}
|
||||
|
||||
// Execute the query
|
||||
err := app.DB().
|
||||
Select("main_topic", "main_topic_list_order").
|
||||
From("home_screen").
|
||||
GroupBy("main_topic").
|
||||
OrderBy("main_topic_list_order ASC").
|
||||
All(&mainTopics)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch home_screen data: %v", err)
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
||||
}
|
||||
|
||||
// If no records are found
|
||||
if len(mainTopics) == 0 {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for home screen"})
|
||||
}
|
||||
|
||||
// Final result structure
|
||||
result := []map[string]interface{}{}
|
||||
|
||||
// Loop through main topics and fetch sub-topic data for each
|
||||
for _, mainTopic := range mainTopics {
|
||||
subTopics := []struct {
|
||||
SubTopic string `db:"sub_topic" json:"sub_topic"`
|
||||
SubTopicListOrder string `db:"sub_topic_list_order" json:"sub_topic_list_order"`
|
||||
}{}
|
||||
|
||||
// Query to get sub-topics for the current main topic
|
||||
err := app.DB().
|
||||
Select("sub_topic", "sub_topic_list_order").
|
||||
From("home_screen").
|
||||
Where(dbx.HashExp{"main_topic": mainTopic.MainTopic}).
|
||||
GroupBy("sub_topic").
|
||||
OrderBy("sub_topic_list_order ASC").
|
||||
All(&subTopics)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch sub-topics for main topic %s: %v", mainTopic.MainTopic, err)
|
||||
continue
|
||||
}
|
||||
|
||||
result2 := []map[string]interface{}{}
|
||||
// Loop through sub Topics topics and fetch dataSet data for each
|
||||
for _, subTopic := range subTopics {
|
||||
|
||||
dataSets := []struct {
|
||||
DataSet string `db:"data_set" json:"data_set"`
|
||||
DataSetTileHeading string `db:"data_set_tile_heading" json:"data_set_tile_heading"`
|
||||
ValueSource string `db:"value_source" json:"value_source"`
|
||||
Value string `db:"value" json:"value"`
|
||||
DataSetListOrder string `db:"data_set_list_order" json:"data_set_list_order"`
|
||||
}{}
|
||||
|
||||
// Query to get data-set for the current sub topic
|
||||
err := app.DB().
|
||||
Select("data_set", "data_set_tile_heading", "value_source", "value", "data_set_list_order").
|
||||
From("home_screen").
|
||||
Where(dbx.HashExp{"sub_topic": subTopic.SubTopic}).
|
||||
OrderBy("data_set_list_order ASC").
|
||||
All(&dataSets)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to fetch data-set for sub topic %s: %v", subTopic.SubTopic, err)
|
||||
continue
|
||||
}
|
||||
|
||||
result2 = append(result2, map[string]interface{}{
|
||||
"sub_topic": subTopic.SubTopic,
|
||||
"sub_topic_list_order": subTopic.SubTopicListOrder,
|
||||
"tile_data": dataSets,
|
||||
})
|
||||
}
|
||||
|
||||
// Add the main topic and its sub-topics to the result
|
||||
result = append(result, map[string]interface{}{
|
||||
"main_topic": mainTopic.MainTopic,
|
||||
"main_topic_list_order": mainTopic.MainTopicListOrder,
|
||||
"sub_topics": result2,
|
||||
})
|
||||
}
|
||||
|
||||
// Send the combined result as JSON
|
||||
return c.JSON(http.StatusOK, result)
|
||||
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
BIN
pb_data/data.db
BIN
pb_data/data.db
Binary file not shown.
Binary file not shown.
BIN
pb_data/logs.db
BIN
pb_data/logs.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user