diff --git a/main.go b/main.go index 5ae519a..d02ea06 100644 --- a/main.go +++ b/main.go @@ -568,29 +568,57 @@ func main() { return c.JSON(http.StatusBadRequest, map[string]string{"error": "language is required"}) } - notification := []struct { - Id string `db:"category" json:"id"` - Category string `db:"category" json:"category"` - Title string `db:"title" json:"title"` - Message string `db:"message" json:"message"` - }{} - categoryColumn := "category_" + language titleColumn := "title_" + language messageColumn := "message_" + language - query := ` SELECT id` + categoryColumn + ` AS category,` + titleColumn + ` AS title,` + messageColumn + ` AS message FROM notification ` - err := app.DB().NewQuery(query).All(¬ification) + + id := c.QueryParam("id") + + if id != "" { + // Define a single notification struct, not a slice + var notification struct { + Id string `db:"id" json:"id"` + Category string `db:"category" json:"category"` + Title string `db:"title" json:"title"` + Message string `db:"message" json:"message"` + Created string `db:"created" json:"created"` + } + + query := `SELECT id, created, ` + categoryColumn + ` AS category, ` + titleColumn + ` AS title, ` + messageColumn + ` AS message FROM notification WHERE id = {:id}` + err := app.DB().NewQuery(query).Bind(dbx.Params{"id": id}).One(¬ification) + + if err != nil { + log.Printf("notification not found: %v", err) + return c.JSON(http.StatusNotFound, map[string]string{"error": "notification not found"}) + } + + return c.JSON(http.StatusOK, map[string]interface{}{ + "message": "Success", + "data": notification, + }) + } + + // Fetch all notifications if no ID is provided + var notifications []struct { + Id string `db:"id" json:"id"` + Category string `db:"category" json:"category"` + Title string `db:"title" json:"title"` + Message string `db:"message" json:"message"` + Created string `db:"created" json:"created"` + } + + query := `SELECT id, created, ` + categoryColumn + ` AS category, ` + titleColumn + ` AS title, ` + messageColumn + ` AS message FROM notification` + err := app.DB().NewQuery(query).All(¬ifications) if err != nil { - log.Printf("notification not found: %v", err) - return c.JSON(http.StatusNotFound, map[string]string{"error": "notification not found"}) + log.Printf("notifications not found: %v", err) + return c.JSON(http.StatusNotFound, map[string]string{"error": "notifications not found"}) } return c.JSON(http.StatusOK, map[string]interface{}{ "message": "Success", - "data": notification, + "data": notifications, }) - }) e.Router.GET("/api/pushNotification", func(c echo.Context) error { @@ -812,7 +840,7 @@ func main() { } var cardResults []map[string]string - if record.GetString("is_chart") == "false" { // card data calculation + if record.GetString("is_chart") == "false" { // card data calculations // Extract `card_type` JSON from `record.Get("card_type")` requestData := record.Get("card_type_json") diff --git a/pb_data/logs.db b/pb_data/logs.db index 6afffdc..4da74c9 100644 Binary files a/pb_data/logs.db and b/pb_data/logs.db differ