-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.go
142 lines (120 loc) · 3.4 KB
/
notification.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"database/sql"
"fmt"
"time"
)
type Notification struct {
ID int `json:"id,omitempty"`
Read string `json:"read,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
IssueId int `json:"issue_id,omitempty"`
UserId string `json:"user_id,omitempty"`
RepoId string `json:"repo_id,omitempty"`
}
func CreateNotification(db *sql.DB, issueId int, userId string, repoId string) error {
_, err := db.Exec(`INSERT INTO notifications ( read, created_at, updated_at, issue_id, user_id, repo_id)
VALUES ( $1 , $2, $3, $4, $5, $6)`,
"unread",
time.Now().Format(time.RFC3339),
time.Now().Format(time.RFC3339),
issueId,
userId,
repoId)
if err != nil {
return err
}
return nil
}
func ReadNotification(db *sql.DB, id int) (Notification, error) {
var read, createdAt, userId, repoId string
var issueId int
row := db.QueryRow("SELECT id,read,created_at,issue_id,,user_id,repo_id FROM notifications WHERE id=$1", id)
err := row.Scan(&id, &read, &createdAt, &issueId, &userId, &repoId)
if err != nil {
return Notification{}, err
}
fmt.Println(id)
CreatedAt, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
return Notification{}, err
}
notification := Notification{
ID: id,
Read: read,
CreatedAt: CreatedAt,
IssueId: issueId,
UserId: userId,
RepoId: repoId,
}
return notification, nil
}
// func CommentNotifications(db *sql.DB, issueId int, currentRepo CurrentRepo, currentUser User) error {
// notificationUserIds := map[string]int{}
// rows, err := db.Query(`SELECT DISTINCT users.id FROM USERS JOIN comments ON
// comments.user_id = users.id WHERE issue_id = $1`, issueId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// for rows.Next() {
// var UsersId string
// err = rows.Scan(&UsersId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// notificationUserIds[UsersId] = 0
// }
// rows, err = db.Query(`SELECT user_id FROM collaborators WHERE repo_id = $1`, currentRepo.RepoId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// for rows.Next() {
// var userId string
// err = rows.Scan(&userId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// notificationUserIds[userId] = 0
// }
// notificationUserIds[currentRepo.UserId] = 0
// delete(notificationUserIds, currentUser.ID)
// for UserId, _ := range notificationUserIds {
// err = CreateNotification(db, issueId, UserId, currentRepo.RepoId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// }
// return nil
// }
// func CollaboratorNotifications(db *sql.DB, currentRepo CurrentRepo, IssueId int) error {
// notificationUserIds := map[string]int{}
// rows, err := db.Query(`SELECT user_id FROM collaborators WHERE repo_id = $1`, currentRepo.RepoId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// for rows.Next() {
// var userId string
// err = rows.Scan(&userId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// notificationUserIds[userId] = 0
// }
// notificationUserIds[currentRepo.UserId] = 0
// for UserId, _ := range notificationUserIds {
// err = CreateNotification(db, IssueId, UserId, currentRepo.RepoId)
// if err != nil {
// fmt.Println(err)
// return err
// }
// }
// return nil
// }