From 5ccab3f521e056bbc04e4b30b498612ae72741c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Tarczy=C5=84ski?= Date: Sun, 3 Jan 2021 12:22:15 +0100 Subject: [PATCH] Internal notification improvement --- api/api.go | 4 ++-- models/notification.go | 5 ++++- senders/internal.go | 10 ++++++---- storage/notifications.go | 2 +- tester/notifications.http | 2 +- tester/templates.http | 20 +++++++++++--------- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/api/api.go b/api/api.go index f74d34c..d26f379 100644 --- a/api/api.go +++ b/api/api.go @@ -1,14 +1,14 @@ package api import ( - "github.com/BarTar213/notificator/middleware" - "github.com/BarTar213/notificator/models" "log" "net/http" "sync" "github.com/BarTar213/notificator/config" "github.com/BarTar213/notificator/email" + "github.com/BarTar213/notificator/middleware" + "github.com/BarTar213/notificator/models" "github.com/BarTar213/notificator/storage" "github.com/gin-gonic/gin" ) diff --git a/models/notification.go b/models/notification.go index c683455..903cf97 100644 --- a/models/notification.go +++ b/models/notification.go @@ -1,8 +1,9 @@ package models import ( - "github.com/BarTar213/notificator/utils" "time" + + "github.com/BarTar213/notificator/utils" ) type Notification struct { @@ -10,6 +11,7 @@ type Notification struct { Message string `json:"message"` UserID int `json:"user_id"` ResourceID int `json:"resource_id"` + Resource string `json:"resource"` Tag string `json:"tag"` CreateDate time.Time `json:"create_date"` Read bool `json:"read"` @@ -20,6 +22,7 @@ func (n *Notification) Reset() { n.Message = utils.EmptyStr n.UserID = 0 n.ResourceID = 0 + n.Resource = utils.EmptyStr n.Tag = utils.EmptyStr n.CreateDate = time.Time{} n.Read = false diff --git a/senders/internal.go b/senders/internal.go index 4ebbb4d..64070f8 100644 --- a/senders/internal.go +++ b/senders/internal.go @@ -8,10 +8,11 @@ import ( ) type Internal struct { - ResourceID int `json:"resource_id"` - Tag string `json:"tag"` - Recipients []int `json:"recipients"` - Data map[string]string `json:"data"` + ResourceID int `json:"resource_id"` + Resource string `json:"resource"` + Tag string `json:"tag"` + Recipients []int `json:"recipients"` + Data map[string]string `json:"data"` } func (i *Internal) Send(s storage.Storage, message string) error { @@ -22,6 +23,7 @@ func (i *Internal) Send(s storage.Storage, message string) error { notifications[j] = &models.Notification{ Message: message, UserID: i.Recipients[j], + Resource: i.Resource, ResourceID: i.ResourceID, Tag: i.Tag, CreateDate: createDate, diff --git a/storage/notifications.go b/storage/notifications.go index 4a43bdc..fb3d48e 100644 --- a/storage/notifications.go +++ b/storage/notifications.go @@ -28,7 +28,7 @@ func (p *Postgres) BatchAddNotifications(notifications []*models.Notification) e } func (p *Postgres) DeleteNotification(notificationID, userID int) error { - _, err := p.db.Exec("DELETE FROM templates WHERE id=? AND user_id=?", notificationID, userID) + _, err := p.db.Exec("DELETE FROM notifications WHERE id=? AND user_id=?", notificationID, userID) return err } diff --git a/tester/notifications.http b/tester/notifications.http index 93c8a3c..b6d5e47 100644 --- a/tester/notifications.http +++ b/tester/notifications.http @@ -7,7 +7,7 @@ X-Account-Id: 1 GET http://localhost:8082/notifications Accept: application/json X-Account: login -X-Account-Id: 1 +X-Account-Id: 3 ### PATCH http://localhost:8082/notifications/2?read=true diff --git a/tester/templates.http b/tester/templates.http index a0959fa..4332b9b 100644 --- a/tester/templates.http +++ b/tester/templates.http @@ -12,9 +12,9 @@ POST http://localhost:8082/templates Content-Type: application/json { - "name": "mailVerification", - "message": "", - "HTML": true + "name": "commentLike", + "message": "User {{.user}} liked your comment", + "HTML": false } ### @@ -35,17 +35,19 @@ Accept: application/json ### -POST http://localhost:8082/templates/auth/send?type=internal +POST http://localhost:8082/templates/commentLike/send?type=internal Content-Type: application/json { - "resource_id": 1, - "tag": "account", + "resource": "comment", + "resource_id": 26, + "tag": "movie/299534", "recipients": [ - 1, - 2 + 3 ], - "data": {} + "data": { + "user": "User" + } } ###