diff --git a/main.go b/main.go index 183a1fe..805e963 100644 --- a/main.go +++ b/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 }) diff --git a/pb_data/data.db b/pb_data/data.db index 66f27e0..edcd31e 100644 Binary files a/pb_data/data.db and b/pb_data/data.db differ diff --git a/pb_data/data.db-shm b/pb_data/data.db-shm deleted file mode 100644 index fe9ac28..0000000 Binary files a/pb_data/data.db-shm and /dev/null differ diff --git a/pb_data/data.db-wal b/pb_data/data.db-wal deleted file mode 100644 index e69de29..0000000 diff --git a/pb_data/logs.db b/pb_data/logs.db index 5961a47..86ec80c 100644 Binary files a/pb_data/logs.db and b/pb_data/logs.db differ diff --git a/pb_data/logs.db-shm b/pb_data/logs.db-shm deleted file mode 100644 index a00c27d..0000000 Binary files a/pb_data/logs.db-shm and /dev/null differ diff --git a/pb_data/logs.db-wal b/pb_data/logs.db-wal deleted file mode 100644 index d446c3b..0000000 Binary files a/pb_data/logs.db-wal and /dev/null differ