Skip to content

Commit

Permalink
Internal notification improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
b-tarczynski committed Jan 3, 2021
1 parent dc6fb71 commit 5ccab3f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
5 changes: 4 additions & 1 deletion models/notification.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package models

import (
"github.com/BarTar213/notificator/utils"
"time"

"github.com/BarTar213/notificator/utils"
)

type Notification struct {
ID int `json:"id"`
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"`
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions senders/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion storage/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tester/notifications.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions tester/templates.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

###
Expand All @@ -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"
}
}

###
Expand Down

0 comments on commit 5ccab3f

Please sign in to comment.