-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
47 lines (31 loc) · 944 Bytes
/
service.go
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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func getAllUser(c *gin.Context) {
var usernames []User
userId := c.GetInt("user_id")
// Query only the 'id' column and scan results into the 'usernames' slice
db.Model(&Auth{}).Select("id, username").Where("id != ?", userId).Scan(&usernames)
c.JSON(http.StatusOK, usernames)
}
func getChatHistory(c *gin.Context) {
userId := c.GetInt("user_id")
// Initialize a slice to hold the results
var messages []Input
// Fetch messages where Sender or Target equals userId
err := db.Model(&Input{}).Where("sender = ? OR target = ?", userId, userId).Find(&messages).Error
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No messages"})
return
}
c.JSON(http.StatusOK, messages)
}
func getOnlineUser(c *gin.Context) {
keys := make([]int, 0, len(clientsMap))
for k := range clientsMap {
keys = append(keys, k)
}
c.JSON(http.StatusOK, keys)
}