Skip to content

Commit

Permalink
Notification actions require account headers
Browse files Browse the repository at this point in the history
  • Loading branch information
b-tarczynski committed Oct 28, 2020
1 parent 51f54f6 commit 0ef0d89
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
12 changes: 9 additions & 3 deletions api/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func (h *NotificationHandlers) GetNotification(c *gin.Context) {
notification := h.notificationPool.Get().(*models.Notification)
defer h.returnNotification(notification)

account := GetAccount(c)

notification.ID = id
notification.UserID = account.ID
err = h.storage.GetNotification(notification)
if err != nil {
handlePostgresError(c, h.logger, err, notificationResource)
Expand All @@ -61,7 +64,8 @@ func (h *NotificationHandlers) UpdateNotification(c *gin.Context) {
return
}

err = h.storage.ReadNotification(id, read)
account := GetAccount(c)
err = h.storage.ReadNotification(id, account.ID, read)
if err != nil {
handlePostgresError(c, h.logger, err, notificationResource)
return
Expand All @@ -77,7 +81,8 @@ func (h *NotificationHandlers) DeleteNotification(c *gin.Context) {
return
}

err = h.storage.DeleteNotification(id)
account := GetAccount(c)
err = h.storage.DeleteNotification(id, account.ID)
if err != nil {
handlePostgresError(c, h.logger, err, notificationResource)
return
Expand All @@ -87,7 +92,8 @@ func (h *NotificationHandlers) DeleteNotification(c *gin.Context) {
}

func (h *NotificationHandlers) ListNotifications(c *gin.Context) {
notifications, err := h.storage.ListNotifications()
account := GetAccount(c)
notifications, err := h.storage.ListNotifications(account.ID)
if err != nil {
handlePostgresError(c, h.logger, err, notificationResource)
return
Expand Down
20 changes: 12 additions & 8 deletions storage/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package storage
import "github.com/BarTar213/notificator/models"

func (p *Postgres) GetNotification(notification *models.Notification) error {
return p.db.Model(notification).WherePK().Select()
return p.db.Model(notification).
WherePK().
Where("user_id=?user_id").
Select()
}

func (p *Postgres) ReadNotification(id int, read bool) error {
_, err := p.db.ExecOne("UPDATE notifications SET read = ? WHERE id = ?", read, id)
func (p *Postgres) ReadNotification(id, userID int, read bool) error {
_, err := p.db.ExecOne("UPDATE notifications SET read=? WHERE id=? AND user_id=?", read, id, userID)

return err
}
Expand All @@ -24,16 +27,17 @@ func (p *Postgres) BatchAddNotifications(notifications []*models.Notification) e
return err
}


func (p *Postgres) DeleteNotification(ID int) error {
_, err := p.db.Exec("DELETE FROM templates WHERE id=?", ID)
func (p *Postgres) DeleteNotification(notificationID, userID int) error {
_, err := p.db.Exec("DELETE FROM templates WHERE id=? AND user_id=?", notificationID, userID)

return err
}

func (p *Postgres) ListNotifications() ([]models.Notification, error) {
func (p *Postgres) ListNotifications(userID int) ([]models.Notification, error) {
notifications := make([]models.Notification, 0)
err := p.db.Model(&notifications).Select()
err := p.db.Model(&notifications).
Where("user_id=?", userID).
Select()
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type Storage interface {
ListTemplates() ([]models.Template, error)

GetNotification(notification *models.Notification) error
ReadNotification(id int, read bool) error
ReadNotification(notificationID, userID int, read bool) error
AddNotification(notification *models.Notification) error
BatchAddNotifications(notifications []*models.Notification) error
DeleteNotification(id int) error
ListNotifications() ([]models.Notification, error)
DeleteNotification(notificationID, userID int) error
ListNotifications(userID int) ([]models.Notification, error)
}

func NewPostgres(config *config.Postgres) (Storage, error) {
Expand Down
10 changes: 8 additions & 2 deletions tester/notifications.http
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
GET http://localhost:8082/notifications/1
GET http://localhost:8082/notifications/2
Accept: application/json
X-Account: login
X-Account-Id: 1

###
GET http://localhost:8082/notifications
Accept: application/json
X-Account: login
X-Account-Id: 1

###
PATCH http://localhost:8082/notifications/1?read=true
PATCH http://localhost:8082/notifications/2?read=true
Accept: application/json
X-Account: login
X-Account-Id: 1

###

0 comments on commit 0ef0d89

Please sign in to comment.