-
Notifications
You must be signed in to change notification settings - Fork 0
/
relations.go
49 lines (39 loc) · 1.18 KB
/
relations.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
package database
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/shujanpannag/iot_project_api/internal/model"
"github.com/shujanpannag/iot_project_api/internal/util"
"github.com/shujanpannag/iot_project_api/pkg/smtp"
"gorm.io/gorm"
)
func GetNodelog(db *gorm.DB) func(*gin.Context) {
return func(c *gin.Context) {
date := c.Param("date")
var rv []model.Camlog
q_date := date + "%"
result := db.Where("datetime LIKE ?", q_date).Find(&rv)
if result.Error != nil {
c.JSON(409, gin.H{"error": result.Error})
return
}
c.PureJSON(200, rv)
}
}
func PostNodelog(db *gorm.DB) func(*gin.Context) {
return func(c *gin.Context) {
var f model.Camlog
c.BindJSON(&f)
result := db.Create(&f)
if result.Error != nil {
c.JSON(409, gin.H{"error": result.Error, "insert_query": f})
return
}
// email notification
subject := "GodsEye Logger\n"
body := fmt.Sprintf("Someone at your doorstep\r\nName: %v\r\nTime: %v\r\n", f.RelationshipName, f.Datetime)
to := util.GetEmailList(db, "Family")
err := smtp.SendEmail(to, subject, body)
c.JSON(201, gin.H{"db_error": result.Error, "insert_query": f, "rows_affected": result.RowsAffected, "smtp_error": err})
}
}