-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
129 lines (112 loc) · 3.08 KB
/
Copy pathhandlers.go
File metadata and controls
129 lines (112 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"log"
"net/http"
"strings"
"time"
srvConfig "github.com/CHESSComputing/golib/config"
"github.com/gin-gonic/gin"
)
// SearchHandler handles GET /search?did=<value>[&text=<phrase>]
//
// Query logic:
// - `did` (required): exact match on the primary DID field.
// - `text` (optional): MongoDB $text search across entries.text.
//
// Results are rendered via the "search.html" template with entries sorted
// newest-first inside the template layer (Go sort on the slice).
func SearchHandler(c *gin.Context) {
var records []map[string]any
did := c.Query("did")
if did == "" {
c.JSON(http.StatusBadRequest, records)
return
}
text := c.Query("text")
spec := map[string]any{"did": did}
if text != "" {
spec["$text"] = map[string]any{"$search": text}
}
skeys := []string{"date"}
sOrder := -1 // 1 ascending, -1 descending
records = metaDB.GetSorted(
srvConfig.Config.ELogData.DBName,
srvConfig.Config.ELogData.DBColl,
spec, skeys, sOrder, 0, -1)
if Verbose > 0 {
log.Println("RecordHandler", spec, records)
}
c.JSON(http.StatusOK, records)
}
// UpdateHandler handles /update end-point
func UpdateHandler(c *gin.Context) {
var did, user, text, image_url string
// Detect content type
contentType := c.GetHeader("Content-Type")
if strings.HasPrefix(contentType, "application/json") {
// JSON input
var payload struct {
DID string `json:"did"`
User string `json:"user"`
Text string `json:"text"`
ImageURL string `json:"image_url"`
}
if err := c.ShouldBindJSON(&payload); err != nil {
log.Printf("ERROR: unable to bind JSON, error %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid JSON"})
return
}
did = payload.DID
user = payload.User
text = payload.Text
image_url = payload.ImageURL
} else {
// Form input (default)
did = c.PostForm("did")
user = c.PostForm("user")
text = c.PostForm("text")
image_url = c.PostForm("image_url")
}
// Validate input
if user == "" || did == "" {
log.Printf("ERROR: either user=%s, did=%s are empty.", user, did)
c.JSON(http.StatusBadRequest, gin.H{"error": "missing required fields"})
return
}
// Create record
rec := map[string]any{
"date": time.Now().UnixNano(),
"text": text,
"user": user,
"image_url": image_url,
"did": did,
}
metaDB.InsertRecord(
srvConfig.Config.ELogData.DBName,
srvConfig.Config.ELogData.DBColl,
rec,
)
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
type Params struct {
dids []string `form:"did"`
}
// RecordsHandler provids all records for dids payload
func RecordsHandler(c *gin.Context) {
var records []map[string]any
var dids []string
if err := c.ShouldBindJSON(&dids); err != nil {
log.Printf("ERROR: no dids list is provided")
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dids list"})
return
}
spec := map[string]any{"did": map[string]any{"$in": dids}}
records = metaDB.Get(
srvConfig.Config.ELogData.DBName,
srvConfig.Config.ELogData.DBColl,
spec, 0, -1)
if Verbose > 0 {
log.Println("RecordHandler", spec, records)
}
c.JSON(http.StatusOK, records)
}