yats.git

ref: 9bb99dda673271328bf65b4d4fa43b32875aa3a4

server/rest/rest-event.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */

package rest

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"yats-server/db"
	"yats-server/model"
)

func WriteEventcAt(c *gin.Context) {
	var event model.EventModel

	if err := c.BindJSON(&event); err != nil {
		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
		return
	}

	clientCN := GetClientCN(c)
	fmt.Printf("%s / %s ", clientCN, event.Name)
	if event.Etime == 0 {
		unixTimeUTC := time.Unix(event.Etime, 0)
		db.SaveEventAt(clientCN, unixTimeUTC, event.Name)
	} else {
		db.SaveEvent(clientCN, event.Name)
	}
	c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
}

func GetEvents(c *gin.Context) {
	var event model.EventSearchRequest

	if err := c.BindJSON(&event); err != nil {
		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
		return
	}

	clientCN := GetClientCN(c)
	fmt.Printf("Client ID: %s\n ", clientCN)

	var eventsPack []model.EventModel
	if event.To == 0 {
		unixTimeUTC := time.Unix(event.From, 0)
		timeAsBytes, _ := unixTimeUTC.UTC().MarshalText()
		eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100)
	} else if event.From != 0 {
		fromUnixTimeUTC := time.Unix(event.From, 0)
		toUnixTimeUTC := time.Unix(event.To, 0)
		fromInBytes, _ := fromUnixTimeUTC.UTC().MarshalText()
		toInBytes, _ := toUnixTimeUTC.UTC().MarshalText()
		eventsPack = db.EventsBetween(db.Session, clientCN, string(fromInBytes), string(toInBytes), 100)
	}
	c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": eventsPack})
}