Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: replace storage types (#324)

Guilherme Oenning8700a6a9c3be
diff --git a/app/actions/idea.go b/app/actions/idea.go
index fb1731aa8..1e8e69df3 100644
--- a/app/actions/idea.go
+++ b/app/actions/idea.go
@@ -55,6 +55,7 @@ func (input *CreateNewIdea) Validate(user *models.User, services *app.Services)
// UpdateIdea is used to edit an existing new idea
type UpdateIdea struct {
Model *models.UpdateIdea
+ Idea *models.Idea
}
// Initialize the model
@@ -96,6 +97,8 @@ func (input *UpdateIdea) Validate(user *models.User, services *app.Services) *va
result.AddFieldFailure("title", "This has already been posted before.")
}
+ input.Idea = idea
+
return result
}
@@ -198,7 +201,7 @@ func (input *DeleteIdea) Validate(user *models.User, services *app.Services) *va
return validate.Error(err)
}
- isReferenced, err := services.Ideas.IsReferenced(input.Model.Number)
+ isReferenced, err := services.Ideas.IsReferenced(idea)
if err != nil {
return validate.Error(err)
}
diff --git a/app/actions/idea_test.go b/app/actions/idea_test.go
index ffb2ea75c..127ed4316 100644
--- a/app/actions/idea_test.go
+++ b/app/actions/idea_test.go
@@ -12,7 +12,8 @@ import (
func TestCreateNewIdea_InvalidIdeaTitles(t *testing.T) {
RegisterTestingT(t)
- services.Ideas.Add("My great idea", "With a great description", 1)
+ services.SetCurrentUser(&models.User{ID: 1})
+ services.Ideas.Add("My great idea", "With a great description")
for _, title := range []string{
"me",
@@ -60,9 +61,10 @@ func TestSetResponse_InvalidStatus(t *testing.T) {
func TestDeleteIdea_WhenIsBeingReferenced(t *testing.T) {
RegisterTestingT(t)
- idea1, _ := services.Ideas.Add("Idea #1", "", 1)
- idea2, _ := services.Ideas.Add("Idea #2", "", 1)
- services.Ideas.MarkAsDuplicate(idea2.Number, idea1.Number, 1)
+ services.SetCurrentUser(&models.User{ID: 1})
+ idea1, _ := services.Ideas.Add("Idea #1", "")
+ idea2, _ := services.Ideas.Add("Idea #2", "")
+ services.Ideas.MarkAsDuplicate(idea2, idea1)
model := &models.DeleteIdea{
Number: idea2.Number,
diff --git a/app/handlers/idea.go b/app/handlers/idea.go
index 4e4226d32..2a2d0ca0a 100644
--- a/app/handlers/idea.go
+++ b/app/handlers/idea.go
@@ -1,7 +1,6 @@
package handlers
import (
- "github.com/getfider/fider/app"
"github.com/getfider/fider/app/actions"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/web"
@@ -62,12 +61,12 @@ func PostIdea() web.HandlerFunc {
}
ideas := c.Services().Ideas
- idea, err := ideas.Add(input.Model.Title, input.Model.Description, c.User().ID)
+ idea, err := ideas.Add(input.Model.Title, input.Model.Description)
if err != nil {
return c.Failure(err)
}
- if err := ideas.AddSupporter(idea.Number, c.User().ID); err != nil {
+ if err := ideas.AddSupporter(idea, c.User()); err != nil {
return c.Failure(err)
}
@@ -85,7 +84,7 @@ func UpdateIdea() web.HandlerFunc {
return c.HandleValidation(result)
}
- _, err := c.Services().Ideas.Update(input.Model.Number, input.Model.Title, input.Model.Description)
+ _, err := c.Services().Ideas.Update(input.Idea, input.Model.Title, input.Model.Description)
if err != nil {
return c.Failure(err)
}
@@ -102,7 +101,7 @@ func DeleteIdea() web.HandlerFunc {
return c.HandleValidation(result)
}
- err := c.Services().Ideas.SetResponse(input.Model.Number, input.Model.Text, c.User().ID, models.IdeaDeleted)
+ err := c.Services().Ideas.SetResponse(input.Idea, input.Model.Text, models.IdeaDeleted)
if err != nil {
return c.Failure(err)
}
@@ -125,7 +124,7 @@ func IdeaDetails() web.HandlerFunc {
return c.Failure(err)
}
- comments, err := ideas.GetCommentsByIdea(number)
+ comments, err := ideas.GetCommentsByIdea(idea)
if err != nil {
return c.Failure(err)
}
@@ -162,7 +161,7 @@ func PostComment() web.HandlerFunc {
return c.Failure(err)
}
- _, err = c.Services().Ideas.AddComment(input.Model.Number, input.Model.Content, c.User().ID)
+ _, err = c.Services().Ideas.AddComment(idea, input.Model.Content)
if err != nil {
return c.Failure(err)
}
@@ -204,9 +203,9 @@ func SetResponse() web.HandlerFunc {
}
if input.Model.Status == models.IdeaDuplicate {
- err = c.Services().Ideas.MarkAsDuplicate(input.Model.Number, input.Original.Number, c.User().ID)
+ err = c.Services().Ideas.MarkAsDuplicate(idea, input.Original)
} else {
- err = c.Services().Ideas.SetResponse(input.Model.Number, input.Model.Text, c.User().ID, input.Model.Status)
+ err = c.Services().Ideas.SetResponse(idea, input.Model.Text, input.Model.Status)
}
if err != nil {
return c.Failure(err)
@@ -246,17 +245,19 @@ func Unsubscribe() web.HandlerFunc {
}
}
-func addOrRemove(c web.Context, addOrRemove func(number, userID int) error) error {
- ideaNumber, err := c.ParamAsInt("number")
+func addOrRemove(c web.Context, addOrRemove func(idea *models.Idea, user *models.User) error) error {
+ number, err := c.ParamAsInt("number")
if err != nil {
return c.Failure(err)
}
- err = addOrRemove(ideaNumber, c.User().ID)
+ idea, err := c.Services().Ideas.GetByNumber(number)
+ if err != nil {
+ return c.Failure(err)
+ }
+
+ err = addOrRemove(idea, c.User())
if err != nil {
- if err == app.ErrNotFound {
- return c.NotFound()
- }
return c.Failure(err)
}
diff --git a/app/handlers/idea_test.go b/app/handlers/idea_test.go
index efc238934..bd09a46a0 100644
--- a/app/handlers/idea_test.go
+++ b/app/handlers/idea_test.go
@@ -26,7 +26,9 @@ func TestDetailsHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My Idea", "My Idea Description", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea, _ := services.Ideas.Add("My Idea", "My Idea Description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -84,7 +86,9 @@ func TestUpdateIdeaHandler_TenantStaff(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea, _ := services.Ideas.Add("My First Idea", "With a description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -102,7 +106,9 @@ func TestUpdateIdeaHandler_NonAuthorized(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea, _ := services.Ideas.Add("My First Idea", "With a description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -117,7 +123,9 @@ func TestUpdateIdeaHandler_InvalidTitle(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea, _ := services.Ideas.Add("My First Idea", "With a description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -146,8 +154,10 @@ func TestUpdateIdeaHandler_DuplicateTitle(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea1, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)
- services.Ideas.Add("My Second Idea", "With a description", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea1, _ := services.Ideas.Add("My First Idea", "With a description")
+ services.Ideas.Add("My Second Idea", "With a description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -162,7 +172,9 @@ func TestPostCommentHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea, _ := services.Ideas.Add("My First Idea", "With a description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -177,7 +189,9 @@ func TestPostCommentHandler_WithoutContent(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea, _ := services.Ideas.Add("My First Idea", "With a description")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -192,8 +206,10 @@ func TestAddSupporterHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- first, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.JonSnow.ID)
- second, _ := services.Ideas.Add("The Idea #2", "The Description #2", mock.JonSnow.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ first, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ second, _ := services.Ideas.Add("The Idea #2", "The Description #2")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -226,9 +242,11 @@ func TestRemoveSupporterHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.JonSnow.ID)
- services.Ideas.AddSupporter(idea.Number, mock.JonSnow.ID)
- services.Ideas.AddSupporter(idea.Number, mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ services.Ideas.AddSupporter(idea, mock.JonSnow)
+ services.Ideas.AddSupporter(idea, mock.AryaStark)
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -246,8 +264,10 @@ func TestSetResponseHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
- services.Ideas.AddSupporter(idea.Number, mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ services.Ideas.AddSupporter(idea, mock.AryaStark)
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -267,8 +287,10 @@ func TestSetResponseHandler_Unauthorized(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
- services.Ideas.AddSupporter(idea.Number, mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ services.Ideas.AddSupporter(idea, mock.AryaStark)
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -286,8 +308,10 @@ func TestSetResponseHandler_Duplicate(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea1, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
- idea2, _ := services.Ideas.Add("The Idea #2", "The Description #2", mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea1, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ idea2, _ := services.Ideas.Add("The Idea #2", "The Description #2")
body := fmt.Sprintf(`{ "status": %d, "originalNumber": %d }`, models.IdeaDuplicate, idea2.Number)
code, _ := server.
@@ -308,7 +332,9 @@ func TestSetResponseHandler_Duplicate_NotFound(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea1, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea1, _ := services.Ideas.Add("The Idea #1", "The Description #1")
body := fmt.Sprintf(`{ "status": %d, "originalNumber": 9999 }`, models.IdeaDuplicate)
code, _ := server.
@@ -324,7 +350,9 @@ func TestSetResponseHandler_Duplicate_Itself(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
body := fmt.Sprintf(`{ "status": %d, "originalNumber": %d }`, models.IdeaDuplicate, idea.Number)
code, _ := server.
@@ -340,7 +368,9 @@ func TestAddCommentHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.AryaStark)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -349,7 +379,7 @@ func TestAddCommentHandler(t *testing.T) {
ExecutePost(handlers.PostComment(), `{ "content": "My first comment" }`)
Expect(code).To(Equal(http.StatusOK))
- comments, _ := services.Ideas.GetCommentsByIdea(idea.Number)
+ comments, _ := services.Ideas.GetCommentsByIdea(idea)
Expect(comments).To(HaveLen(1))
}
@@ -359,8 +389,8 @@ func TestUpdateCommentHandler_Authorized(t *testing.T) {
server, services := mock.NewServer()
services.SetCurrentTenant(mock.DemoTenant)
services.SetCurrentUser(mock.AryaStark)
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.AryaStark.ID)
- commentId, _ := services.Ideas.AddComment(idea.Number, "My first comment", mock.AryaStark.ID)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ commentId, _ := services.Ideas.AddComment(idea, "My first comment")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -380,8 +410,8 @@ func TestUpdateCommentHandler_Unauthorized(t *testing.T) {
server, services := mock.NewServer()
services.SetCurrentTenant(mock.DemoTenant)
services.SetCurrentUser(mock.JonSnow)
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.JonSnow.ID)
- commentId, _ := services.Ideas.AddComment(idea.Number, "My first comment", mock.JonSnow.ID)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
+ commentId, _ := services.Ideas.AddComment(idea, "My first comment")
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -401,7 +431,7 @@ func TestDeleteIdeaHandler_Authorized(t *testing.T) {
server, services := mock.NewServer()
services.SetCurrentTenant(mock.DemoTenant)
services.SetCurrentUser(mock.JonSnow)
- idea, _ := services.Ideas.Add("The Idea #1", "The Description #1", mock.JonSnow.ID)
+ idea, _ := services.Ideas.Add("The Idea #1", "The Description #1")
code, _ := server.
OnTenant(mock.DemoTenant).
diff --git a/app/handlers/notification_test.go b/app/handlers/notification_test.go
index 27b1fd91f..6131403dc 100644
--- a/app/handlers/notification_test.go
+++ b/app/handlers/notification_test.go
@@ -13,8 +13,8 @@ func TestTotalUnreadNotificationsHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- services.SetCurrentUser(mock.JonSnow)
- services.Notifications.Insert(mock.JonSnow, "Title", "http://example.com", 1, mock.AryaStark.ID)
+ services.SetCurrentUser(mock.AryaStark)
+ services.Notifications.Insert(mock.JonSnow, "Title", "http://example.com", 1)
code, query := server.
OnTenant(mock.DemoTenant).
@@ -29,9 +29,9 @@ func TestNotificationsHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- services.SetCurrentUser(mock.JonSnow)
- services.Notifications.Insert(mock.JonSnow, "Title", "http://example.com", 1, mock.AryaStark.ID)
- services.Notifications.Insert(mock.JonSnow, "Title 2", "http://example.com", 1, mock.AryaStark.ID)
+ services.SetCurrentUser(mock.AryaStark)
+ services.Notifications.Insert(mock.JonSnow, "Title", "http://example.com", 1)
+ services.Notifications.Insert(mock.JonSnow, "Title 2", "http://example.com", 1)
code, _ := server.
OnTenant(mock.DemoTenant).
@@ -46,8 +46,9 @@ func TestReadNotificationHandler(t *testing.T) {
server, services := mock.NewServer()
services.SetCurrentUser(mock.JonSnow)
- not1, _ := services.Notifications.Insert(mock.JonSnow, "Title", "/abc", 1, mock.AryaStark.ID)
- not2, _ := services.Notifications.Insert(mock.JonSnow, "Title 2", "/def", 1, mock.AryaStark.ID)
+ services.SetCurrentUser(mock.AryaStark)
+ not1, _ := services.Notifications.Insert(mock.JonSnow, "Title", "/abc", 1)
+ not2, _ := services.Notifications.Insert(mock.JonSnow, "Title 2", "/def", 1)
code, resp := server.
OnTenant(mock.DemoTenant).
@@ -66,9 +67,9 @@ func TestReadAllNotificationsHandler(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
- services.SetCurrentUser(mock.JonSnow)
- not1, _ := services.Notifications.Insert(mock.JonSnow, "Title", "/abc", 1, mock.AryaStark.ID)
- not2, _ := services.Notifications.Insert(mock.JonSnow, "Title 2", "/def", 1, mock.AryaStark.ID)
+ services.SetCurrentUser(mock.AryaStark)
+ not1, _ := services.Notifications.Insert(mock.JonSnow, "Title", "/abc", 1)
+ not2, _ := services.Notifications.Insert(mock.JonSnow, "Title 2", "/def", 1)
code, _ := server.
OnTenant(mock.DemoTenant).
diff --git a/app/handlers/tag.go b/app/handlers/tag.go
index 0be0bc868..719599a4a 100644
--- a/app/handlers/tag.go
+++ b/app/handlers/tag.go
@@ -34,7 +34,7 @@ func CreateEditTag() web.HandlerFunc {
)
if input.Model.Slug != "" {
- tag, err = c.Services().Tags.Update(input.Tag.ID, input.Model.Name, input.Model.Color, input.Model.IsPublic)
+ tag, err = c.Services().Tags.Update(input.Tag, input.Model.Name, input.Model.Color, input.Model.IsPublic)
} else {
tag, err = c.Services().Tags.Add(input.Model.Name, input.Model.Color, input.Model.IsPublic)
}
@@ -55,7 +55,7 @@ func DeleteTag() web.HandlerFunc {
return c.HandleValidation(result)
}
- err := c.Services().Tags.Delete(input.Tag.ID)
+ err := c.Services().Tags.Delete(input.Tag)
if err != nil {
return c.Failure(err)
}
@@ -72,7 +72,7 @@ func AssignTag() web.HandlerFunc {
return c.HandleValidation(result)
}
- err := c.Services().Tags.AssignTag(input.Tag.ID, input.Idea.ID, c.User().ID)
+ err := c.Services().Tags.AssignTag(input.Tag, input.Idea)
if err != nil {
return c.Failure(err)
}
@@ -89,7 +89,7 @@ func UnassignTag() web.HandlerFunc {
return c.HandleValidation(result)
}
- err := c.Services().Tags.UnassignTag(input.Tag.ID, input.Idea.ID)
+ err := c.Services().Tags.UnassignTag(input.Tag, input.Idea)
if err != nil {
return c.Failure(err)
}
diff --git a/app/handlers/tag_test.go b/app/handlers/tag_test.go
index 80e1d0586..2702ff62d 100644
--- a/app/handlers/tag_test.go
+++ b/app/handlers/tag_test.go
@@ -182,8 +182,10 @@ func TestAssignTagHandler_Success(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
tag, _ := services.Tags.Add("Bug", "0000FF", true)
- idea, _ := services.Ideas.Add("Idea Title", "Idea Description", mock.JonSnow.ID)
+ idea, _ := services.Ideas.Add("Idea Title", "Idea Description")
status, _ := server.
AsUser(mock.JonSnow).
@@ -191,7 +193,7 @@ func TestAssignTagHandler_Success(t *testing.T) {
AddParam("number", idea.Number).
Execute(handlers.AssignTag())
- tags, err := services.Tags.GetAssigned(idea.ID)
+ tags, err := services.Tags.GetAssigned(idea)
Expect(status).To(Equal(http.StatusOK))
Expect(err).To(BeNil())
Expect(tags[0]).To(Equal(tag))
@@ -221,8 +223,10 @@ func TestAssignOrUnassignTagHandler_Unauthorized(t *testing.T) {
for _, handler := range testCases {
server, services := mock.NewServer()
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
tag, _ := services.Tags.Add("Bug", "0000FF", true)
- idea, _ := services.Ideas.Add("Idea Title", "Idea Description", mock.JonSnow.ID)
+ idea, _ := services.Ideas.Add("Idea Title", "Idea Description")
status, _ := server.
AsUser(mock.AryaStark).
@@ -238,9 +242,11 @@ func TestUnassignTagHandler_Success(t *testing.T) {
RegisterTestingT(t)
server, services := mock.NewServer()
+ services.SetCurrentTenant(mock.DemoTenant)
+ services.SetCurrentUser(mock.JonSnow)
tag, _ := services.Tags.Add("Bug", "0000FF", true)
- idea, _ := services.Ideas.Add("Idea Title", "Idea Description", mock.JonSnow.ID)
- services.Tags.AssignTag(tag.ID, idea.ID, mock.JonSnow.ID)
+ idea, _ := services.Ideas.Add("Idea Title", "Idea Description")
+ services.Tags.AssignTag(tag, idea)
status, _ := server.
AsUser(mock.JonSnow).
@@ -248,7 +254,7 @@ func TestUnassignTagHandler_Success(t *testing.T) {
AddParam("number", idea.Number).
Execute(handlers.UnassignTag())
- tags, err := services.Tags.GetAssigned(idea.ID)
+ tags, err := services.Tags.GetAssigned(idea)
Expect(status).To(Equal(http.StatusOK))
Expect(err).To(BeNil())
Expect(len(tags)).To(Equal(0))
diff --git a/app/storage/inmemory/idea.go b/app/storage/inmemory/idea.go
index b8f5ddeca..4ca492bc6 100644
--- a/app/storage/inmemory/idea.go
+++ b/app/storage/inmemory/idea.go
@@ -53,14 +53,10 @@ func (s *IdeaStorage) GetByID(ideaID int) (*models.Idea, error) {
}
// Update given idea
-func (s *IdeaStorage) Update(number int, title, description string) (*models.Idea, error) {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return nil, err
- }
+func (s *IdeaStorage) Update(idea *models.Idea, title, description string) (*models.Idea, error) {
idea.Title = title
idea.Description = description
- return idea, err
+ return idea, nil
}
// GetByNumber returns idea by tenant and number
@@ -99,16 +95,12 @@ func (s *IdeaStorage) Search(query, filter string, tags []string) ([]*models.Ide
}
// GetCommentsByIdea returns all comments from given idea
-func (s *IdeaStorage) GetCommentsByIdea(number int) ([]*models.Comment, error) {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return nil, err
- }
+func (s *IdeaStorage) GetCommentsByIdea(idea *models.Idea) ([]*models.Comment, error) {
return s.ideaComments[idea.ID], nil
}
// Add a new idea in the database
-func (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea, error) {
+func (s *IdeaStorage) Add(title, description string) (*models.Idea, error) {
s.lastID = s.lastID + 1
idea := &models.Idea{
ID: s.lastID,
@@ -116,30 +108,21 @@ func (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea,
Title: title,
Slug: slug.Make(title),
Description: description,
- User: &models.User{
- ID: userID,
- },
+ User: s.user,
}
s.ideas = append(s.ideas, idea)
- s.ideasSupportedBy[userID] = append(s.ideasSupportedBy[userID], idea.ID)
+ s.ideasSupportedBy[s.user.ID] = append(s.ideasSupportedBy[s.user.ID], idea.ID)
return idea, nil
}
// AddComment places a new comment on an idea
-func (s *IdeaStorage) AddComment(number int, content string, userID int) (int, error) {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return 0, err
- }
+func (s *IdeaStorage) AddComment(idea *models.Idea, content string) (int, error) {
s.lastCommentID++
s.ideaComments[idea.ID] = append(s.ideaComments[idea.ID], &models.Comment{
ID: s.lastCommentID,
Content: content,
CreatedOn: time.Now(),
- User: &models.User{
- ID: userID,
- Tenant: s.tenant,
- },
+ User: s.user,
})
return s.lastCommentID, nil
@@ -171,26 +154,17 @@ func (s *IdeaStorage) UpdateComment(id int, content string) error {
}
// AddSupporter adds user to idea list of supporters
-func (s *IdeaStorage) AddSupporter(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
- s.ideasSupportedBy[userID] = append(s.ideasSupportedBy[userID], idea.ID)
+func (s *IdeaStorage) AddSupporter(idea *models.Idea, user *models.User) error {
+ s.ideasSupportedBy[user.ID] = append(s.ideasSupportedBy[user.ID], idea.ID)
idea.TotalSupporters = idea.TotalSupporters + 1
return nil
}
// RemoveSupporter removes user from idea list of supporters
-func (s *IdeaStorage) RemoveSupporter(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
- for i, id := range s.ideasSupportedBy[userID] {
+func (s *IdeaStorage) RemoveSupporter(idea *models.Idea, user *models.User) error {
+ for i, id := range s.ideasSupportedBy[user.ID] {
if id == idea.ID {
- s.ideasSupportedBy[userID] = append(s.ideasSupportedBy[userID][:i], s.ideasSupportedBy[userID][i+1:]...)
+ s.ideasSupportedBy[user.ID] = append(s.ideasSupportedBy[user.ID][:i], s.ideasSupportedBy[user.ID][i+1:]...)
break
}
}
@@ -199,16 +173,16 @@ func (s *IdeaStorage) RemoveSupporter(number, userID int) error {
}
// SetResponse changes current idea response
-func (s *IdeaStorage) SetResponse(number int, text string, userID, status int) error {
- for i, idea := range s.ideas {
- if idea.Number == number {
+func (s *IdeaStorage) SetResponse(idea *models.Idea, text string, status int) error {
+ for i, storedIdea := range s.ideas {
+ if storedIdea.Number == idea.Number {
if status == models.IdeaDeleted {
s.ideas = append(s.ideas[:i], s.ideas[i+1:]...)
} else {
- idea.Status = status
- idea.Response = &models.IdeaResponse{
+ storedIdea.Status = status
+ storedIdea.Response = &models.IdeaResponse{
Text: text,
- User: &models.User{ID: userID},
+ User: s.user,
RespondedOn: time.Now(),
}
}
@@ -218,16 +192,7 @@ func (s *IdeaStorage) SetResponse(number int, text string, userID, status int) e
}
// MarkAsDuplicate set idea as a duplicate of another idea
-func (s *IdeaStorage) MarkAsDuplicate(number, originalNumber, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
- original, err := s.GetByNumber(originalNumber)
- if err != nil {
- return err
- }
-
+func (s *IdeaStorage) MarkAsDuplicate(idea *models.Idea, original *models.Idea) error {
idea.Status = models.IdeaDuplicate
idea.Response = &models.IdeaResponse{
Original: &models.OriginalIdea{
@@ -237,16 +202,16 @@ func (s *IdeaStorage) MarkAsDuplicate(number, originalNumber, userID int) error
Status: original.Status,
},
Text: "",
- User: &models.User{ID: userID},
+ User: s.user,
RespondedOn: time.Now(),
}
return nil
}
// IsReferenced returns true if another idea is referencing given idea
-func (s *IdeaStorage) IsReferenced(number int) (bool, error) {
+func (s *IdeaStorage) IsReferenced(idea *models.Idea) (bool, error) {
for _, i := range s.ideas {
- if i.Status == models.IdeaDuplicate && i.Response.Original.Number == number {
+ if i.Status == models.IdeaDuplicate && i.Response.Original.Number == idea.Number {
return true, nil
}
}
@@ -254,29 +219,20 @@ func (s *IdeaStorage) IsReferenced(number int) (bool, error) {
}
// SupportedBy returns a list of Idea ID supported by given user
-func (s *IdeaStorage) SupportedBy(userID int) ([]int, error) {
- return s.ideasSupportedBy[userID], nil
+func (s *IdeaStorage) SupportedBy() ([]int, error) {
+ return s.ideasSupportedBy[s.user.ID], nil
}
// AddSubscriber adds user to the idea list of subscribers
-func (s *IdeaStorage) AddSubscriber(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
- s.ideaSubscribers[idea.ID] = append(s.ideaSubscribers[idea.ID], userID)
+func (s *IdeaStorage) AddSubscriber(idea *models.Idea, user *models.User) error {
+ s.ideaSubscribers[idea.ID] = append(s.ideaSubscribers[idea.ID], user.ID)
return nil
}
// RemoveSubscriber removes user from idea list of subscribers
-func (s *IdeaStorage) RemoveSubscriber(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
+func (s *IdeaStorage) RemoveSubscriber(idea *models.Idea, user *models.User) error {
for i, id := range s.ideaSubscribers[idea.ID] {
- if id == userID {
+ if id == user.ID {
s.ideaSubscribers[idea.ID] = append(s.ideasSupportedBy[idea.ID][:i], s.ideasSupportedBy[idea.ID][i+1:]...)
break
}
diff --git a/app/storage/inmemory/notifications.go b/app/storage/inmemory/notifications.go
index b1ca7a02e..ccffc9cef 100644
--- a/app/storage/inmemory/notifications.go
+++ b/app/storage/inmemory/notifications.go
@@ -34,7 +34,7 @@ func (s *NotificationStorage) SetCurrentUser(user *models.User) {
}
// Insert notification for given user
-func (s *NotificationStorage) Insert(user *models.User, title, link string, ideaID, authorID int) (*models.Notification, error) {
+func (s *NotificationStorage) Insert(user *models.User, title, link string, ideaID int) (*models.Notification, error) {
s.lastID = s.lastID + 1
newNotification := &models.Notification{
ID: s.lastID,
@@ -43,11 +43,11 @@ func (s *NotificationStorage) Insert(user *models.User, title, link string, idea
Read: false,
CreatedOn: time.Now(),
}
- all, ok := s.notifications[s.user]
+ all, ok := s.notifications[user]
if !ok {
all = make([]*models.Notification, 0)
}
- s.notifications[s.user] = append(all, newNotification)
+ s.notifications[user] = append(all, newNotification)
return newNotification, nil
}
diff --git a/app/storage/inmemory/tag.go b/app/storage/inmemory/tag.go
index d5e5693b3..db79f063e 100644
--- a/app/storage/inmemory/tag.go
+++ b/app/storage/inmemory/tag.go
@@ -58,13 +58,13 @@ func (s *TagStorage) GetBySlug(slug string) (*models.Tag, error) {
}
// Update a tag with given input
-func (s *TagStorage) Update(tagID int, name, color string, isPublic bool) (*models.Tag, error) {
- for _, tag := range s.tags {
- if tag.ID == tagID {
- tag.Name = name
- tag.Slug = slug.Make(name)
- tag.Color = color
- tag.IsPublic = isPublic
+func (s *TagStorage) Update(tag *models.Tag, name, color string, isPublic bool) (*models.Tag, error) {
+ for _, storedTag := range s.tags {
+ if storedTag.ID == tag.ID {
+ storedTag.Name = name
+ storedTag.Slug = slug.Make(name)
+ storedTag.Color = color
+ storedTag.IsPublic = isPublic
return tag, nil
}
}
@@ -72,9 +72,9 @@ func (s *TagStorage) Update(tagID int, name, color string, isPublic bool) (*mode
}
// Delete a tag by its id
-func (s *TagStorage) Delete(tagID int) error {
- for i, tag := range s.tags {
- if tag.ID == tagID {
+func (s *TagStorage) Delete(tag *models.Tag) error {
+ for i, storedTag := range s.tags {
+ if storedTag.ID == tag.ID {
s.tags = append(s.tags[:i], s.tags[i+1:]...)
break
}
@@ -83,39 +83,27 @@ func (s *TagStorage) Delete(tagID int) error {
}
// AssignTag adds a tag to an idea
-func (s *TagStorage) AssignTag(tagID, ideaID, userID int) error {
- var tagToAssign *models.Tag
- for _, tag := range s.tags {
- if tag.ID == tagID {
- tagToAssign = tag
- break
- }
- }
-
- if tagToAssign == nil {
- return app.ErrNotFound
- }
-
- assigned := s.assigned[ideaID]
+func (s *TagStorage) AssignTag(tag *models.Tag, idea *models.Idea) error {
+ assigned := s.assigned[idea.ID]
if assigned != nil {
- for _, tag := range assigned {
- if tag.ID == tagID {
+ for _, assignedTag := range assigned {
+ if assignedTag.ID == tag.ID {
return nil
}
}
}
- s.assigned[ideaID] = append(s.assigned[ideaID], tagToAssign)
+ s.assigned[idea.ID] = append(s.assigned[idea.ID], tag)
return nil
}
// UnassignTag removes a tag from an idea
-func (s *TagStorage) UnassignTag(tagID, ideaID int) error {
- assigned := s.assigned[ideaID]
+func (s *TagStorage) UnassignTag(tag *models.Tag, idea *models.Idea) error {
+ assigned := s.assigned[idea.ID]
if assigned != nil {
- for i, tag := range assigned {
- if tag.ID == tagID {
- s.assigned[ideaID] = append(s.assigned[ideaID][:i], s.assigned[ideaID][i+1:]...)
+ for i, assignedTag := range assigned {
+ if assignedTag.ID == tag.ID {
+ s.assigned[idea.ID] = append(s.assigned[idea.ID][:i], s.assigned[idea.ID][i+1:]...)
return nil
}
}
@@ -124,8 +112,8 @@ func (s *TagStorage) UnassignTag(tagID, ideaID int) error {
}
// GetAssigned returns all tags assigned to given idea
-func (s *TagStorage) GetAssigned(ideaID int) ([]*models.Tag, error) {
- assigned := s.assigned[ideaID]
+func (s *TagStorage) GetAssigned(idea *models.Idea) ([]*models.Tag, error) {
+ assigned := s.assigned[idea.ID]
if assigned != nil {
return assigned, nil
}
diff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.go
index 663b93704..bf8510e51 100644
--- a/app/storage/postgres/idea.go
+++ b/app/storage/postgres/idea.go
@@ -297,7 +297,7 @@ func (s *IdeaStorage) Search(query, filter string, tags []string) ([]*models.Ide
}
// GetCommentsByIdea returns all comments from given idea
-func (s *IdeaStorage) GetCommentsByIdea(number int) ([]*models.Comment, error) {
+func (s *IdeaStorage) GetCommentsByIdea(idea *models.Idea) ([]*models.Comment, error) {
comments := []*dbComment{}
err := s.trx.Select(&comments,
`SELECT c.id,
@@ -322,9 +322,9 @@ func (s *IdeaStorage) GetCommentsByIdea(number int) ([]*models.Comment, error) {
LEFT JOIN users e
ON e.id = c.edited_by_id
AND e.tenant_id = c.tenant_id
- WHERE i.number = $1
+ WHERE i.id = $1
AND i.tenant_id = $2
- ORDER BY c.created_on ASC`, number, s.tenant.ID)
+ ORDER BY c.created_on ASC`, idea.ID, s.tenant.ID)
if err != nil {
return nil, err
}
@@ -337,23 +337,27 @@ func (s *IdeaStorage) GetCommentsByIdea(number int) ([]*models.Comment, error) {
}
// Update given idea
-func (s *IdeaStorage) Update(number int, title, description string) (*models.Idea, error) {
+func (s *IdeaStorage) Update(idea *models.Idea, title, description string) (*models.Idea, error) {
_, err := s.trx.Execute(`UPDATE ideas SET title = $1, slug = $2, description = $3
- WHERE number = $4 AND tenant_id = $5`, title, slug.Make(title), description, number, s.tenant.ID)
+ WHERE id = $4 AND tenant_id = $5`, title, slug.Make(title), description, idea.ID, s.tenant.ID)
if err != nil {
return nil, err
}
- return s.GetByNumber(number)
+ idea.Slug = slug.Make(title)
+ idea.Title = title
+ idea.Description = description
+
+ return idea, nil
}
// Add a new idea in the database
-func (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea, error) {
+func (s *IdeaStorage) Add(title, description string) (*models.Idea, error) {
var id int
err := s.trx.Get(&id,
`INSERT INTO ideas (title, slug, number, description, tenant_id, user_id, created_on, supporters, status)
VALUES ($1, $2, (SELECT COALESCE(MAX(number), 0) + 1 FROM ideas i WHERE i.tenant_id = $4), $3, $4, $5, $6, 0, 0)
- RETURNING id`, title, slug.Make(title), description, s.tenant.ID, userID, time.Now())
+ RETURNING id`, title, slug.Make(title), description, s.tenant.ID, s.user.ID, time.Now())
if err != nil {
return nil, err
}
@@ -363,7 +367,7 @@ func (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea,
return nil, err
}
- if err := s.internalAddSubscriber(idea.Number, userID, false); err != nil {
+ if err := s.internalAddSubscriber(idea, s.user, false); err != nil {
return nil, err
}
@@ -371,20 +375,15 @@ func (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea,
}
// AddComment places a new comment on an idea
-func (s *IdeaStorage) AddComment(number int, content string, userID int) (int, error) {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return 0, err
- }
-
+func (s *IdeaStorage) AddComment(idea *models.Idea, content string) (int, error) {
var id int
if err := s.trx.Get(&id,
"INSERT INTO comments (tenant_id, idea_id, content, user_id, created_on) VALUES ($1, $2, $3, $4, $5) RETURNING id",
- s.tenant.ID, idea.ID, content, userID, time.Now()); err != nil {
+ s.tenant.ID, idea.ID, content, s.user.ID, time.Now()); err != nil {
return 0, err
}
- if err := s.internalAddSubscriber(number, userID, false); err != nil {
+ if err := s.internalAddSubscriber(idea, s.user, false); err != nil {
return 0, err
}
@@ -433,19 +432,14 @@ func (s *IdeaStorage) UpdateComment(id int, content string) error {
}
// AddSupporter adds user to idea list of supporters
-func (s *IdeaStorage) AddSupporter(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
+func (s *IdeaStorage) AddSupporter(idea *models.Idea, user *models.User) error {
if !idea.CanBeSupported() {
return nil
}
rows, err := s.trx.Execute(
`INSERT INTO idea_supporters (tenant_id, user_id, idea_id, created_on) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`,
- s.tenant.ID, userID, idea.ID, time.Now())
+ s.tenant.ID, user.ID, idea.ID, time.Now())
if err != nil {
return err
@@ -458,21 +452,16 @@ func (s *IdeaStorage) AddSupporter(number, userID int) error {
}
}
- return s.internalAddSubscriber(number, userID, false)
+ return s.internalAddSubscriber(idea, user, false)
}
// RemoveSupporter removes user from idea list of supporters
-func (s *IdeaStorage) RemoveSupporter(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
+func (s *IdeaStorage) RemoveSupporter(idea *models.Idea, user *models.User) error {
if !idea.CanBeSupported() {
return nil
}
- rows, err := s.trx.Execute(`DELETE FROM idea_supporters WHERE user_id = $1 AND idea_id = $2 AND tenant_id = $3`, userID, idea.ID, s.tenant.ID)
+ rows, err := s.trx.Execute(`DELETE FROM idea_supporters WHERE user_id = $1 AND idea_id = $2 AND tenant_id = $3`, user.ID, idea.ID, s.tenant.ID)
if rows == 1 {
_, err := s.trx.Execute(`UPDATE ideas SET supporters = supporters - 1 WHERE id = $1 AND tenant_id = $2`, idea.ID, s.tenant.ID)
@@ -484,41 +473,31 @@ func (s *IdeaStorage) RemoveSupporter(number, userID int) error {
}
// AddSubscriber adds user to the idea list of subscribers
-func (s *IdeaStorage) AddSubscriber(number, userID int) error {
- return s.internalAddSubscriber(number, userID, true)
+func (s *IdeaStorage) AddSubscriber(idea *models.Idea, user *models.User) error {
+ return s.internalAddSubscriber(idea, user, true)
}
-func (s *IdeaStorage) internalAddSubscriber(number, userID int, force bool) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
+func (s *IdeaStorage) internalAddSubscriber(idea *models.Idea, user *models.User, force bool) error {
conflict := " DO NOTHING"
if force {
conflict = "(user_id, idea_id) DO UPDATE SET status = $5, updated_on = $4"
}
- _, err = s.trx.Execute(fmt.Sprintf(`
+ _, err := s.trx.Execute(fmt.Sprintf(`
INSERT INTO idea_subscribers (tenant_id, user_id, idea_id, created_on, updated_on, status)
VALUES ($1, $2, $3, $4, $4, $5) ON CONFLICT %s`, conflict),
- s.tenant.ID, userID, idea.ID, time.Now(), models.SubscriberActive,
+ s.tenant.ID, user.ID, idea.ID, time.Now(), models.SubscriberActive,
)
return err
}
// RemoveSubscriber removes user from idea list of subscribers
-func (s *IdeaStorage) RemoveSubscriber(number, userID int) error {
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
- _, err = s.trx.Execute(`
+func (s *IdeaStorage) RemoveSubscriber(idea *models.Idea, user *models.User) error {
+ _, err := s.trx.Execute(`
INSERT INTO idea_subscribers (tenant_id, user_id, idea_id, created_on, updated_on, status)
VALUES ($1, $2, $3, $4, $4, 0) ON CONFLICT (user_id, idea_id)
DO UPDATE SET status = 0, updated_on = $4`,
- s.tenant.ID, userID, idea.ID, time.Now(),
+ s.tenant.ID, user.ID, idea.ID, time.Now(),
)
return err
}
@@ -590,52 +569,49 @@ func (s *IdeaStorage) GetActiveSubscribers(number int, channel models.Notificati
}
// SetResponse changes current idea response
-func (s *IdeaStorage) SetResponse(number int, text string, userID, status int) error {
+func (s *IdeaStorage) SetResponse(idea *models.Idea, text string, status int) error {
if status == models.IdeaDuplicate {
return errors.New("Use MarkAsDuplicate to change an idea status to Duplicate")
}
- idea, err := s.GetByNumber(number)
- if err != nil {
- return err
- }
-
respondedOn := time.Now()
if idea.Status == status && idea.Response != nil {
respondedOn = idea.Response.RespondedOn
}
- _, err = s.trx.Execute(`
+ _, err := s.trx.Execute(`
UPDATE ideas
SET response = $3, original_id = NULL, response_date = $4, response_user_id = $5, status = $6
WHERE id = $1 and tenant_id = $2
- `, idea.ID, s.tenant.ID, text, respondedOn, userID, status)
- return err
-}
-
-// MarkAsDuplicate set idea as a duplicate of another idea
-func (s *IdeaStorage) MarkAsDuplicate(number, originalNumber, userID int) error {
- idea, err := s.GetByNumber(number)
+ `, idea.ID, s.tenant.ID, text, respondedOn, s.user.ID, status)
if err != nil {
return err
}
- original, err := s.GetByNumber(originalNumber)
- if err != nil {
- return err
+
+ idea.Status = status
+ idea.Response = &models.IdeaResponse{
+ Text: text,
+ RespondedOn: respondedOn,
+ User: s.user,
}
+ return nil
+}
+// MarkAsDuplicate set idea as a duplicate of another idea
+func (s *IdeaStorage) MarkAsDuplicate(idea *models.Idea, original *models.Idea) error {
respondedOn := time.Now()
if idea.Status == models.IdeaDuplicate && idea.Response != nil {
respondedOn = idea.Response.RespondedOn
}
- users, err := s.trx.QueryIntArray("SELECT user_id FROM idea_supporters WHERE idea_id = $1 AND tenant_id = $2", idea.ID, s.tenant.ID)
+ var users []*dbUser
+ err := s.trx.Select(&users, "SELECT user_id AS id FROM idea_supporters WHERE idea_id = $1 AND tenant_id = $2", idea.ID, s.tenant.ID)
if err != nil {
return err
}
for _, u := range users {
- if err := s.AddSupporter(original.Number, u); err != nil {
+ if err := s.AddSupporter(original, u.toModel()); err != nil {
return err
}
}
@@ -644,22 +620,37 @@ func (s *IdeaStorage) MarkAsDuplicate(number, originalNumber, userID int) error
UPDATE ideas
SET response = '', original_id = $3, response_date = $4, response_user_id = $5, status = $6
WHERE id = $1 and tenant_id = $2
- `, idea.ID, s.tenant.ID, original.ID, respondedOn, userID, models.IdeaDuplicate)
- return err
+ `, idea.ID, s.tenant.ID, original.ID, respondedOn, s.user.ID, models.IdeaDuplicate)
+ if err != nil {
+ return err
+ }
+
+ idea.Status = models.IdeaDuplicate
+ idea.Response = &models.IdeaResponse{
+ RespondedOn: respondedOn,
+ User: s.user,
+ Original: &models.OriginalIdea{
+ Number: original.Number,
+ Title: original.Title,
+ Slug: original.Slug,
+ Status: original.Status,
+ },
+ }
+ return nil
}
// IsReferenced returns true if another idea is referencing given idea
-func (s *IdeaStorage) IsReferenced(number int) (bool, error) {
+func (s *IdeaStorage) IsReferenced(idea *models.Idea) (bool, error) {
return s.trx.Exists(`
SELECT 1 FROM ideas i
INNER JOIN ideas o
ON o.tenant_id = i.tenant_id
AND o.id = i.original_id
WHERE i.tenant_id = $1
- AND o.number = $2`, s.tenant.ID, number)
+ AND o.id = $2`, s.tenant.ID, idea.ID)
}
// SupportedBy returns a list of Idea ID supported by given user
-func (s *IdeaStorage) SupportedBy(userID int) ([]int, error) {
- return s.trx.QueryIntArray("SELECT idea_id FROM idea_supporters WHERE user_id = $1 AND tenant_id = $2", userID, s.tenant.ID)
+func (s *IdeaStorage) SupportedBy() ([]int, error) {
+ return s.trx.QueryIntArray("SELECT idea_id FROM idea_supporters WHERE user_id = $1 AND tenant_id = $2", s.user.ID, s.tenant.ID)
}
diff --git a/app/storage/postgres/idea_test.go b/app/storage/postgres/idea_test.go
index 773434cb4..238111c2f 100644
--- a/app/storage/postgres/idea_test.go
+++ b/app/storage/postgres/idea_test.go
@@ -51,7 +51,8 @@ func TestIdeaStorage_AddAndGet(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, err := ideas.Add("My new idea", "with this description", 1)
+ ideas.SetCurrentUser(jonSnow)
+ idea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
dbIdeaById, err := ideas.GetByID(idea.ID)
@@ -98,13 +99,16 @@ func TestIdeaStorage_AddAndReturnComments(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, err := ideas.Add("My new idea", "with this description", 1)
+ ideas.SetCurrentUser(jonSnow)
+ idea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
- ideas.AddComment(idea.Number, "Comment #1", 1)
- ideas.AddComment(idea.Number, "Comment #2", 2)
+ ideas.SetCurrentUser(jonSnow)
+ ideas.AddComment(idea, "Comment #1")
+ ideas.SetCurrentUser(aryaStark)
+ ideas.AddComment(idea, "Comment #2")
- comments, err := ideas.GetCommentsByIdea(idea.Number)
+ comments, err := ideas.GetCommentsByIdea(idea)
Expect(err).To(BeNil())
Expect(len(comments)).To(Equal(2))
@@ -119,10 +123,11 @@ func TestIdeaStorage_AddGetUpdateComment(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, err := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ idea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
- commentId, err := ideas.AddComment(idea.Number, "Comment #1", jonSnow.ID)
+ commentId, err := ideas.AddComment(idea, "Comment #1")
Expect(err).To(BeNil())
comment, err := ideas.GetCommentByID(commentId)
@@ -151,11 +156,13 @@ func TestIdeaStorage_AddAndGet_DifferentTenants(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- demoIdea, err := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ demoIdea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
ideas.SetCurrentTenant(avengersTenant)
- avengersIdea, err := ideas.Add("My other idea", "with other description", tonyStark.ID)
+ ideas.SetCurrentUser(tonyStark)
+ avengersIdea, err := ideas.Add("My other idea", "with other description")
Expect(err).To(BeNil())
ideas.SetCurrentTenant(demoTenant)
@@ -182,11 +189,12 @@ func TestIdeaStorage_Update(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(jonSnow)
- idea, err := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ idea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
- idea, err = ideas.Update(idea.Number, "The new comment", "With the new description")
+ idea, err = ideas.Update(idea, "The new comment", "With the new description")
Expect(err).To(BeNil())
Expect(idea.Title).To(Equal("The new comment"))
Expect(idea.Description).To(Equal("With the new description"))
@@ -201,17 +209,17 @@ func TestIdeaStorage_AddSupporter(t *testing.T) {
ideas.SetCurrentTenant(demoTenant)
ideas.SetCurrentUser(jonSnow)
- idea, err := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ idea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
- err = ideas.AddSupporter(idea.Number, aryaStark.ID)
+ err = ideas.AddSupporter(idea, aryaStark)
Expect(err).To(BeNil())
dbIdea, err := ideas.GetByNumber(1)
Expect(dbIdea.ViewerSupported).To(BeFalse())
Expect(dbIdea.TotalSupporters).To(Equal(1))
- err = ideas.AddSupporter(idea.Number, jonSnow.ID)
+ err = ideas.AddSupporter(idea, jonSnow)
Expect(err).To(BeNil())
dbIdea, err = ideas.GetByNumber(1)
@@ -225,12 +233,14 @@ func TestIdeaStorage_AddSupporter_Twice(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+
+ idea, _ := ideas.Add("My new idea", "with this description")
- err := ideas.AddSupporter(idea.Number, 1)
+ err := ideas.AddSupporter(idea, jonSnow)
Expect(err).To(BeNil())
- err = ideas.AddSupporter(idea.Number, 1)
+ err = ideas.AddSupporter(idea, jonSnow)
Expect(err).To(BeNil())
dbIdea, err := ideas.GetByNumber(1)
@@ -243,12 +253,14 @@ func TestIdeaStorage_RemoveSupporter(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+
+ idea, _ := ideas.Add("My new idea", "with this description")
- err := ideas.AddSupporter(idea.Number, 1)
+ err := ideas.AddSupporter(idea, jonSnow)
Expect(err).To(BeNil())
- err = ideas.RemoveSupporter(idea.Number, 1)
+ err = ideas.RemoveSupporter(idea, jonSnow)
Expect(err).To(BeNil())
dbIdea, err := ideas.GetByNumber(1)
@@ -261,15 +273,17 @@ func TestIdeaStorage_RemoveSupporter_Twice(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
- err := ideas.AddSupporter(idea.Number, 1)
+ idea, _ := ideas.Add("My new idea", "with this description")
+
+ err := ideas.AddSupporter(idea, jonSnow)
Expect(err).To(BeNil())
- err = ideas.RemoveSupporter(idea.Number, 1)
+ err = ideas.RemoveSupporter(idea, jonSnow)
Expect(err).To(BeNil())
- err = ideas.RemoveSupporter(idea.Number, 1)
+ err = ideas.RemoveSupporter(idea, jonSnow)
Expect(err).To(BeNil())
dbIdea, err := ideas.GetByNumber(1)
@@ -282,8 +296,10 @@ func TestIdeaStorage_SetResponse(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- err := ideas.SetResponse(idea.Number, "We liked this idea", jonSnow.ID, models.IdeaStarted)
+ ideas.SetCurrentUser(jonSnow)
+
+ idea, _ := ideas.Add("My new idea", "with this description")
+ err := ideas.SetResponse(idea, "We liked this idea", models.IdeaStarted)
Expect(err).To(BeNil())
@@ -298,8 +314,10 @@ func TestIdeaStorage_SetResponse_KeepOpen(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- err := ideas.SetResponse(idea.Number, "We liked this idea", jonSnow.ID, models.IdeaOpen)
+ ideas.SetCurrentUser(jonSnow)
+
+ idea, _ := ideas.Add("My new idea", "with this description")
+ err := ideas.SetResponse(idea, "We liked this idea", models.IdeaOpen)
Expect(err).To(BeNil())
}
@@ -308,16 +326,18 @@ func TestIdeaStorage_SetResponse_ChangeText(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- ideas.SetResponse(idea.Number, "We liked this idea", jonSnow.ID, models.IdeaStarted)
+ ideas.SetCurrentUser(jonSnow)
+
+ idea, _ := ideas.Add("My new idea", "with this description")
+ ideas.SetResponse(idea, "We liked this idea", models.IdeaStarted)
idea, _ = ideas.GetByID(idea.ID)
respondedOn := idea.Response.RespondedOn
- ideas.SetResponse(idea.Number, "We liked this idea and we'll work on it", jonSnow.ID, models.IdeaStarted)
+ ideas.SetResponse(idea, "We liked this idea and we'll work on it", models.IdeaStarted)
idea, _ = ideas.GetByID(idea.ID)
Expect(idea.Response.RespondedOn).To(Equal(respondedOn))
- ideas.SetResponse(idea.Number, "We finished it", jonSnow.ID, models.IdeaCompleted)
+ ideas.SetResponse(idea, "We finished it", models.IdeaCompleted)
idea, _ = ideas.GetByID(idea.ID)
Expect(idea.Response.RespondedOn).Should(BeTemporally(">", respondedOn))
}
@@ -327,12 +347,17 @@ func TestIdeaStorage_SetResponse_AsDuplicate(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea1, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- ideas.AddSupporter(idea1.Number, jonSnow.ID)
- idea2, _ := ideas.Add("My other idea", "with similar description", aryaStark.ID)
- ideas.AddSupporter(idea2.Number, aryaStark.ID)
+ ideas.SetCurrentUser(jonSnow)
+
+ idea1, _ := ideas.Add("My new idea", "with this description")
+ ideas.AddSupporter(idea1, jonSnow)
+
+ ideas.SetCurrentUser(aryaStark)
+ idea2, _ := ideas.Add("My other idea", "with similar description")
+ ideas.AddSupporter(idea2, aryaStark)
- ideas.MarkAsDuplicate(idea2.Number, idea1.Number, jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ ideas.MarkAsDuplicate(idea2, idea1)
idea1, _ = ideas.GetByID(idea1.ID)
Expect(idea1.TotalSupporters).To(Equal(2))
@@ -356,10 +381,11 @@ func TestIdeaStorage_SetResponse_AsDeleted(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, err := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ idea, err := ideas.Add("My new idea", "with this description")
Expect(err).To(BeNil())
- ideas.SetResponse(idea.Number, "Spam!", jonSnow.ID, models.IdeaDeleted)
+ ideas.SetResponse(idea, "Spam!", models.IdeaDeleted)
idea1, err := ideas.GetByNumber(idea.Number)
Expect(err).To(Equal(app.ErrNotFound))
@@ -375,11 +401,12 @@ func TestIdeaStorage_AddSupporter_ClosedIdea(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- ideas.SetResponse(idea.Number, "We liked this idea", jonSnow.ID, models.IdeaCompleted)
- ideas.AddSupporter(idea.Number, 1)
+ ideas.SetCurrentUser(jonSnow)
+ idea, _ := ideas.Add("My new idea", "with this description")
+ ideas.SetResponse(idea, "We liked this idea", models.IdeaCompleted)
+ ideas.AddSupporter(idea, jonSnow)
- dbIdea, err := ideas.GetByNumber(1)
+ dbIdea, err := ideas.GetByNumber(idea.Number)
Expect(err).To(BeNil())
Expect(dbIdea.TotalSupporters).To(Equal(0))
}
@@ -389,12 +416,13 @@ func TestIdeaStorage_RemoveSupporter_ClosedIdea(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- ideas.AddSupporter(idea.Number, jonSnow.ID)
- ideas.SetResponse(idea.Number, "We liked this idea", jonSnow.ID, models.IdeaCompleted)
- ideas.RemoveSupporter(idea.Number, jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ idea, _ := ideas.Add("My new idea", "with this description")
+ ideas.AddSupporter(idea, jonSnow)
+ ideas.SetResponse(idea, "We liked this idea", models.IdeaCompleted)
+ ideas.RemoveSupporter(idea, jonSnow)
- dbIdea, err := ideas.GetByNumber(1)
+ dbIdea, err := ideas.GetByNumber(idea.Number)
Expect(err).To(BeNil())
Expect(dbIdea.TotalSupporters).To(Equal(1))
}
@@ -404,13 +432,17 @@ func TestIdeaStorage_ListSupportedIdeas(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea1, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
- idea2, _ := ideas.Add("My other idea", "with better description", jonSnow.ID)
- ideas.AddSupporter(idea1.Number, aryaStark.ID)
- ideas.AddSupporter(idea2.Number, aryaStark.ID)
+ ideas.SetCurrentUser(jonSnow)
+ idea1, _ := ideas.Add("My new idea", "with this description")
+ idea2, _ := ideas.Add("My other idea", "with better description")
+ ideas.AddSupporter(idea1, aryaStark)
+ ideas.AddSupporter(idea2, aryaStark)
+
+ ideas.SetCurrentUser(jonSnow)
+ Expect(ideas.SupportedBy()).To(Equal([]int{}))
- Expect(ideas.SupportedBy(1)).To(Equal([]int{}))
- Expect(ideas.SupportedBy(2)).To(Equal([]int{idea1.ID, idea2.ID}))
+ ideas.SetCurrentUser(aryaStark)
+ Expect(ideas.SupportedBy()).To(Equal([]int{idea1.ID, idea2.ID}))
}
func TestIdeaStorage_WithTags(t *testing.T) {
@@ -418,14 +450,16 @@ func TestIdeaStorage_WithTags(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(aryaStark)
tags.SetCurrentTenant(demoTenant)
+ tags.SetCurrentUser(jonSnow)
- idea, _ := ideas.Add("My new idea", "with this description", jonSnow.ID)
+ idea, _ := ideas.Add("My new idea", "with this description")
bug, _ := tags.Add("Bug", "FF0000", true)
featureRequest, _ := tags.Add("Feature Request", "00FF00", false)
- tags.AssignTag(bug.ID, idea.ID, jonSnow.ID)
- tags.AssignTag(featureRequest.ID, idea.ID, jonSnow.ID)
+ tags.AssignTag(bug, idea)
+ tags.AssignTag(featureRequest, idea)
idea, _ = ideas.GetByID(idea.ID)
Expect(len(idea.Tags)).To(Equal(1))
@@ -443,14 +477,15 @@ func TestIdeaStorage_IsReferenced(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea1, _ := ideas.Add("My first idea", "with this description", jonSnow.ID)
- idea2, _ := ideas.Add("My second idea", "with this description", jonSnow.ID)
- idea3, _ := ideas.Add("My third idea", "with this description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ idea1, _ := ideas.Add("My first idea", "with this description")
+ idea2, _ := ideas.Add("My second idea", "with this description")
+ idea3, _ := ideas.Add("My third idea", "with this description")
- ideas.MarkAsDuplicate(idea2.Number, idea3.Number, jonSnow.ID)
- ideas.MarkAsDuplicate(idea3.Number, idea1.Number, jonSnow.ID)
+ ideas.MarkAsDuplicate(idea2, idea3)
+ ideas.MarkAsDuplicate(idea3, idea1)
- Expect(ideas.IsReferenced(idea1.Number)).To(BeTrue())
- Expect(ideas.IsReferenced(idea2.Number)).To(BeFalse())
- Expect(ideas.IsReferenced(idea3.Number)).To(BeTrue())
+ Expect(ideas.IsReferenced(idea1)).To(BeTrue())
+ Expect(ideas.IsReferenced(idea2)).To(BeFalse())
+ Expect(ideas.IsReferenced(idea3)).To(BeTrue())
}
diff --git a/app/storage/postgres/notifications.go b/app/storage/postgres/notifications.go
index 2a066eb08..4c295b0ed 100644
--- a/app/storage/postgres/notifications.go
+++ b/app/storage/postgres/notifications.go
@@ -32,8 +32,8 @@ func (s *NotificationStorage) SetCurrentUser(user *models.User) {
}
// Insert notification for given user
-func (s *NotificationStorage) Insert(user *models.User, title, link string, ideaID, authorID int) (*models.Notification, error) {
- if user.ID == authorID {
+func (s *NotificationStorage) Insert(user *models.User, title, link string, ideaID int) (*models.Notification, error) {
+ if user.ID == s.user.ID {
return nil, nil
}
@@ -48,7 +48,7 @@ func (s *NotificationStorage) Insert(user *models.User, title, link string, idea
INSERT INTO notifications (tenant_id, user_id, title, link, read, idea_id, author_id, created_on, updated_on)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $8)
RETURNING id
- `, s.tenant.ID, user.ID, title, link, false, ideaID, authorID, now)
+ `, s.tenant.ID, user.ID, title, link, false, ideaID, s.user.ID, now)
if err != nil {
return nil, err
}
diff --git a/app/storage/postgres/notifications_test.go b/app/storage/postgres/notifications_test.go
index 1ff6b0f0a..eec6f1e0a 100644
--- a/app/storage/postgres/notifications_test.go
+++ b/app/storage/postgres/notifications_test.go
@@ -27,12 +27,14 @@ func TestNotificationStorage_Insert_Read_Count(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(jonSnow)
notifications.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("Title", "Description", jonSnow.ID)
+ notifications.SetCurrentUser(jonSnow)
+ idea, _ := ideas.Add("Title", "Description")
- not1, err := notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID, jonSnow.ID)
+ not1, err := notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID)
Expect(err).To(BeNil())
- not2, err := notifications.Insert(aryaStark, "Another thing happened", "http://www.google.com.br", idea.ID, jonSnow.ID)
+ not2, err := notifications.Insert(aryaStark, "Another thing happened", "http://www.google.com.br", idea.ID)
Expect(err).To(BeNil())
notifications.SetCurrentUser(aryaStark)
@@ -49,13 +51,15 @@ func TestNotificationStorage_GetActiveNotifications(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("Title", "Description", jonSnow.ID)
-
+ ideas.SetCurrentUser(jonSnow)
notifications.SetCurrentTenant(demoTenant)
- notifications.SetCurrentUser(aryaStark)
+ notifications.SetCurrentUser(jonSnow)
+ idea, _ := ideas.Add("Title", "Description")
+
+ notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID)
+ notifications.Insert(aryaStark, "Another thing happened", "http://www.google.com.br", idea.ID)
- notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID, jonSnow.ID)
- notifications.Insert(aryaStark, "Another thing happened", "http://www.google.com.br", idea.ID, jonSnow.ID)
+ notifications.SetCurrentUser(aryaStark)
allNotifications, err := notifications.GetActiveNotifications()
Expect(err).To(BeNil())
@@ -76,13 +80,16 @@ func TestNotificationStorage_ReadAll(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("Title", "Description", jonSnow.ID)
-
+ ideas.SetCurrentUser(jonSnow)
notifications.SetCurrentTenant(demoTenant)
- notifications.SetCurrentUser(aryaStark)
+ notifications.SetCurrentUser(jonSnow)
+
+ idea, _ := ideas.Add("Title", "Description")
- notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID, jonSnow.ID)
- notifications.Insert(aryaStark, "Another thing happened", "http://www.google.com.br", idea.ID, jonSnow.ID)
+ notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID)
+ notifications.Insert(aryaStark, "Another thing happened", "http://www.google.com.br", idea.ID)
+
+ notifications.SetCurrentUser(aryaStark)
allNotifications, err := notifications.GetActiveNotifications()
Expect(err).To(BeNil())
@@ -102,13 +109,15 @@ func TestNotificationStorage_GetNotificationById(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("Title", "Description", jonSnow.ID)
-
+ ideas.SetCurrentUser(jonSnow)
notifications.SetCurrentTenant(demoTenant)
- notifications.SetCurrentUser(aryaStark)
+ notifications.SetCurrentUser(jonSnow)
+ idea, _ := ideas.Add("Title", "Description")
- not1, err := notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID, jonSnow.ID)
+ not1, err := notifications.Insert(aryaStark, "Hello World", "http://www.google.com.br", idea.ID)
Expect(err).To(BeNil())
+
+ notifications.SetCurrentUser(aryaStark)
not1, err = notifications.GetNotification(not1.ID)
Expect(err).To(BeNil())
Expect(not1.Title).To(Equal("Hello World"))
@@ -121,13 +130,14 @@ func TestNotificationStorage_GetNotificationById_OtherUser(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea, _ := ideas.Add("Title", "Description", jonSnow.ID)
+ ideas.SetCurrentUser(jonSnow)
+ idea, _ := ideas.Add("Title", "Description")
notifications.SetCurrentTenant(demoTenant)
- not1, err := notifications.Insert(jonSnow, "Hello World", "http://www.google.com.br", idea.ID, aryaStark.ID)
+ notifications.SetCurrentUser(aryaStark)
+ not1, err := notifications.Insert(jonSnow, "Hello World", "http://www.google.com.br", idea.ID)
Expect(err).To(BeNil())
- notifications.SetCurrentUser(aryaStark)
not1, err = notifications.GetNotification(not1.ID)
Expect(err).To(Equal(app.ErrNotFound))
Expect(not1).To(BeNil())
diff --git a/app/storage/postgres/subscription_test.go b/app/storage/postgres/subscription_test.go
index 92d5c3337..163a8f77a 100644
--- a/app/storage/postgres/subscription_test.go
+++ b/app/storage/postgres/subscription_test.go
@@ -14,7 +14,8 @@ func TestSubscription_NoSettings(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea1, _ := ideas.Add("Idea #1", "Description #1", aryaStark.ID)
+ ideas.SetCurrentUser(aryaStark)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewIdea)
Expect(err).To(BeNil())
@@ -52,8 +53,9 @@ func TestSubscription_RemoveSubscriber(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
- idea1, _ := ideas.Add("Idea #1", "Description #1", aryaStark.ID)
- err := ideas.RemoveSubscriber(idea1.Number, aryaStark.ID)
+ ideas.SetCurrentUser(aryaStark)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
+ err := ideas.RemoveSubscriber(idea1, aryaStark)
Expect(err).To(BeNil())
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewIdea)
@@ -86,8 +88,9 @@ func TestSubscription_AdminSubmitted(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(jonSnow)
- idea1, _ := ideas.Add("Idea #1", "Description #1", jonSnow.ID)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewIdea)
Expect(err).To(BeNil())
@@ -119,10 +122,11 @@ func TestSubscription_AdminUnsubscribed(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(aryaStark)
- idea1, _ := ideas.Add("Idea #1", "Description #1", aryaStark.ID)
- ideas.RemoveSubscriber(idea1.Number, aryaStark.ID)
- ideas.RemoveSubscriber(idea1.Number, jonSnow.ID)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
+ ideas.RemoveSubscriber(idea1, aryaStark)
+ ideas.RemoveSubscriber(idea1, jonSnow)
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewComment)
Expect(err).To(BeNil())
@@ -143,14 +147,16 @@ func TestSubscription_DisabledEmail(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(aryaStark)
users.SetCurrentTenant(demoTenant)
users.SetCurrentUser(aryaStark)
+
err := users.UpdateSettings(map[string]string{
models.NotificationEventNewComment.UserSettingsKeyName: strconv.Itoa(int(models.NotificationChannelWeb)),
})
Expect(err).To(BeNil())
- idea1, _ := ideas.Add("Idea #1", "Description #1", aryaStark.ID)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewComment)
Expect(err).To(BeNil())
@@ -184,14 +190,16 @@ func TestSubscription_VisitorEnabledNewIdea(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(jonSnow)
users.SetCurrentTenant(demoTenant)
users.SetCurrentUser(aryaStark)
+
err := users.UpdateSettings(map[string]string{
models.NotificationEventNewIdea.UserSettingsKeyName: strconv.Itoa(int(models.NotificationChannelEmail | models.NotificationChannelWeb)),
})
Expect(err).To(BeNil())
- idea1, _ := ideas.Add("Idea #1", "Description #1", jonSnow.ID)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewIdea)
Expect(err).To(BeNil())
@@ -211,6 +219,7 @@ func TestSubscription_DisabledEverything(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(jonSnow)
users.SetCurrentTenant(demoTenant)
disableAll := map[string]string{
models.NotificationEventNewIdea.UserSettingsKeyName: "0",
@@ -222,7 +231,7 @@ func TestSubscription_DisabledEverything(t *testing.T) {
users.SetCurrentUser(aryaStark)
Expect(users.UpdateSettings(disableAll)).To(BeNil())
- idea1, _ := ideas.Add("Idea #1", "Description #1", jonSnow.ID)
+ idea1, _ := ideas.Add("Idea #1", "Description #1")
subscribers, err := ideas.GetActiveSubscribers(idea1.Number, models.NotificationChannelWeb, models.NotificationEventNewIdea)
Expect(err).To(BeNil())
diff --git a/app/storage/postgres/tag.go b/app/storage/postgres/tag.go
index 71de47ac7..ecc4453e7 100644
--- a/app/storage/postgres/tag.go
+++ b/app/storage/postgres/tag.go
@@ -80,11 +80,11 @@ func (s *TagStorage) GetBySlug(slug string) (*models.Tag, error) {
}
// Update a tag with given input
-func (s *TagStorage) Update(tagID int, name, color string, isPublic bool) (*models.Tag, error) {
+func (s *TagStorage) Update(tag *models.Tag, name, color string, isPublic bool) (*models.Tag, error) {
tagSlug := slug.Make(name)
_, err := s.trx.Execute(`UPDATE tags SET name = $1, slug = $2, color = $3, is_public = $4
- WHERE id = $5 AND tenant_id = $6`, name, tagSlug, color, isPublic, tagID, s.tenant.ID)
+ WHERE id = $5 AND tenant_id = $6`, name, tagSlug, color, isPublic, tag.ID, s.tenant.ID)
if err != nil {
return nil, err
}
@@ -93,18 +93,18 @@ func (s *TagStorage) Update(tagID int, name, color string, isPublic bool) (*mode
}
// Delete a tag by its id
-func (s *TagStorage) Delete(tagID int) error {
- _, err := s.trx.Execute(`DELETE FROM idea_tags WHERE tag_id = $1 AND tenant_id = $2`, tagID, s.tenant.ID)
+func (s *TagStorage) Delete(tag *models.Tag) error {
+ _, err := s.trx.Execute(`DELETE FROM idea_tags WHERE tag_id = $1 AND tenant_id = $2`, tag.ID, s.tenant.ID)
if err != nil {
return err
}
- _, err = s.trx.Execute(`DELETE FROM tags WHERE id = $1 AND tenant_id = $2`, tagID, s.tenant.ID)
+ _, err = s.trx.Execute(`DELETE FROM tags WHERE id = $1 AND tenant_id = $2`, tag.ID, s.tenant.ID)
return err
}
// AssignTag adds a tag to an idea
-func (s *TagStorage) AssignTag(tagID, ideaID, userID int) error {
- alreadyAssigned, err := s.trx.Exists("SELECT 1 FROM idea_tags WHERE idea_id = $1 AND tag_id = $2 AND tenant_id = $3", ideaID, tagID, s.tenant.ID)
+func (s *TagStorage) AssignTag(tag *models.Tag, idea *models.Idea) error {
+ alreadyAssigned, err := s.trx.Exists("SELECT 1 FROM idea_tags WHERE idea_id = $1 AND tag_id = $2 AND tenant_id = $3", idea.ID, tag.ID, s.tenant.ID)
if err != nil {
return err
}
@@ -115,22 +115,22 @@ func (s *TagStorage) AssignTag(tagID, ideaID, userID int) error {
_, err = s.trx.Execute(
`INSERT INTO idea_tags (tag_id, idea_id, created_on, created_by_id, tenant_id) VALUES ($1, $2, $3, $4, $5)`,
- tagID, ideaID, time.Now(), userID, s.tenant.ID,
+ tag.ID, idea.ID, time.Now(), s.user.ID, s.tenant.ID,
)
return err
}
// UnassignTag removes a tag from an idea
-func (s *TagStorage) UnassignTag(tagID, ideaID int) error {
+func (s *TagStorage) UnassignTag(tag *models.Tag, idea *models.Idea) error {
_, err := s.trx.Execute(
`DELETE FROM idea_tags WHERE tag_id = $1 AND idea_id = $2 AND tenant_id = $3`,
- tagID, ideaID, s.tenant.ID,
+ tag.ID, idea.ID, s.tenant.ID,
)
return err
}
// GetAssigned returns all tags assigned to given idea
-func (s *TagStorage) GetAssigned(ideaID int) ([]*models.Tag, error) {
+func (s *TagStorage) GetAssigned(idea *models.Idea) ([]*models.Tag, error) {
return s.getTags(`
SELECT t.id, t.name, t.slug, t.color, t.is_public
FROM tags t
@@ -138,7 +138,7 @@ func (s *TagStorage) GetAssigned(ideaID int) ([]*models.Tag, error) {
ON it.tag_id = t.id
AND it.tenant_id = t.tenant_id
WHERE it.idea_id = $1 AND t.tenant_id = $2
- `, ideaID, s.tenant.ID)
+ `, idea.ID, s.tenant.ID)
}
// GetAll returns all tags
diff --git a/app/storage/postgres/tag_test.go b/app/storage/postgres/tag_test.go
index d4103ccf8..3d92cdc0f 100644
--- a/app/storage/postgres/tag_test.go
+++ b/app/storage/postgres/tag_test.go
@@ -32,7 +32,7 @@ func TestTagStorage_AddUpdateAndGet(t *testing.T) {
tags.SetCurrentTenant(demoTenant)
tag, err := tags.Add("Feature Request", "FF0000", true)
- tag, err = tags.Update(tag.ID, "Bug", "000000", false)
+ tag, err = tags.Update(tag, "Bug", "000000", false)
dbTag, err := tags.GetBySlug("bug")
@@ -51,7 +51,7 @@ func TestTagStorage_AddDeleteAndGet(t *testing.T) {
tags.SetCurrentTenant(demoTenant)
tag, err := tags.Add("Bug", "FFFFFF", true)
- err = tags.Delete(tag.ID)
+ err = tags.Delete(tag)
Expect(err).To(BeNil())
dbTag, err := tags.GetBySlug("bug")
@@ -65,15 +65,17 @@ func TestTagStorage_Assign_Unassign(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(aryaStark)
tags.SetCurrentTenant(demoTenant)
+ tags.SetCurrentUser(aryaStark)
- idea, _ := ideas.Add("My great idea", "with a great description", 2)
+ idea, _ := ideas.Add("My great idea", "with a great description")
tag, _ := tags.Add("Bug", "FFFFFF", true)
- err := tags.AssignTag(tag.ID, idea.ID, 2)
+ err := tags.AssignTag(tag, idea)
Expect(err).To(BeNil())
- assigned, err := tags.GetAssigned(idea.ID)
+ assigned, err := tags.GetAssigned(idea)
Expect(err).To(BeNil())
Expect(len(assigned)).To(Equal(1))
Expect(assigned[0].ID).To(Equal(tag.ID))
@@ -82,10 +84,10 @@ func TestTagStorage_Assign_Unassign(t *testing.T) {
Expect(assigned[0].Color).To(Equal("FFFFFF"))
Expect(assigned[0].IsPublic).To(BeTrue())
- err = tags.UnassignTag(tag.ID, idea.ID)
+ err = tags.UnassignTag(tag, idea)
Expect(err).To(BeNil())
- assigned, err = tags.GetAssigned(idea.ID)
+ assigned, err = tags.GetAssigned(idea)
Expect(err).To(BeNil())
Expect(len(assigned)).To(Equal(0))
}
@@ -95,18 +97,20 @@ func TestTagStorage_Assign_DeleteTag(t *testing.T) {
defer TeardownDatabaseTest()
ideas.SetCurrentTenant(demoTenant)
+ ideas.SetCurrentUser(aryaStark)
tags.SetCurrentTenant(demoTenant)
+ tags.SetCurrentUser(aryaStark)
- idea, _ := ideas.Add("My great idea", "with a great description", 2)
+ idea, _ := ideas.Add("My great idea", "with a great description")
tag, _ := tags.Add("Bug", "FFFFFF", true)
- err := tags.AssignTag(tag.ID, idea.ID, 2)
+ err := tags.AssignTag(tag, idea)
Expect(err).To(BeNil())
- err = tags.Delete(tag.ID)
+ err = tags.Delete(tag)
Expect(err).To(BeNil())
- assigned, err := tags.GetAssigned(idea.ID)
+ assigned, err := tags.GetAssigned(idea)
Expect(err).To(BeNil())
Expect(len(assigned)).To(Equal(0))
}
diff --git a/app/storage/storage.go b/app/storage/storage.go
index d78e0fbda..567aeecb0 100644
--- a/app/storage/storage.go
+++ b/app/storage/storage.go
@@ -18,24 +18,24 @@ type Idea interface {
GetByID(ideaID int) (*models.Idea, error)
GetBySlug(slug string) (*models.Idea, error)
GetByNumber(number int) (*models.Idea, error)
- GetCommentsByIdea(number int) ([]*models.Comment, error)
+ GetCommentsByIdea(idea *models.Idea) ([]*models.Comment, error)
Search(query, filter string, tags []string) ([]*models.Idea, error)
GetAll() ([]*models.Idea, error)
CountPerStatus() (map[int]int, error)
- Add(title, description string, userID int) (*models.Idea, error)
- Update(number int, title, description string) (*models.Idea, error)
- AddComment(number int, content string, userID int) (int, error)
+ Add(title, description string) (*models.Idea, error)
+ Update(idea *models.Idea, title, description string) (*models.Idea, error)
+ AddComment(idea *models.Idea, content string) (int, error)
GetCommentByID(id int) (*models.Comment, error)
UpdateComment(id int, content string) error
- AddSupporter(number, userID int) error
- RemoveSupporter(number, userID int) error
- AddSubscriber(number, userID int) error
- RemoveSubscriber(number, userID int) error
+ AddSupporter(idea *models.Idea, user *models.User) error
+ RemoveSupporter(idea *models.Idea, user *models.User) error
+ AddSubscriber(idea *models.Idea, user *models.User) error
+ RemoveSubscriber(idea *models.Idea, user *models.User) error
GetActiveSubscribers(number int, channel models.NotificationChannel, event models.NotificationEvent) ([]*models.User, error)
- SetResponse(number int, text string, userID, status int) error
- MarkAsDuplicate(number, originalNumber, userID int) error
- IsReferenced(number int) (bool, error)
- SupportedBy(userID int) ([]int, error)
+ SetResponse(idea *models.Idea, text string, status int) error
+ MarkAsDuplicate(idea *models.Idea, original *models.Idea) error
+ IsReferenced(idea *models.Idea) (bool, error)
+ SupportedBy() ([]int, error)
}
// User is used for user operations
@@ -75,18 +75,18 @@ type Tag interface {
Base
Add(name, color string, isPublic bool) (*models.Tag, error)
GetBySlug(slug string) (*models.Tag, error)
- Update(tagID int, name, color string, isPublic bool) (*models.Tag, error)
- Delete(tagID int) error
- GetAssigned(ideaID int) ([]*models.Tag, error)
- AssignTag(tagID, ideaID, userID int) error
- UnassignTag(tagID, ideaID int) error
+ Update(tag *models.Tag, name, color string, isPublic bool) (*models.Tag, error)
+ Delete(tag *models.Tag) error
+ GetAssigned(idea *models.Idea) ([]*models.Tag, error)
+ AssignTag(tag *models.Tag, idea *models.Idea) error
+ UnassignTag(tag *models.Tag, idea *models.Idea) error
GetAll() ([]*models.Tag, error)
}
// Notification contains read and write operations for notifications
type Notification interface {
Base
- Insert(user *models.User, title, link string, ideaID, authorID int) (*models.Notification, error)
+ Insert(user *models.User, title, link string, ideaID int) (*models.Notification, error)
MarkAsRead(id int) error
MarkAllAsRead() error
TotalUnread() (int, error)
diff --git a/app/tasks/tasks.go b/app/tasks/tasks.go
index daec0c3f5..69b61b120 100644
--- a/app/tasks/tasks.go
+++ b/app/tasks/tasks.go
@@ -73,7 +73,7 @@ func NotifyAboutNewIdea(idea *models.Idea) worker.Task {
title := fmt.Sprintf("New idea: **%s**", idea.Title)
link := fmt.Sprintf("/ideas/%d/%s", idea.Number, idea.Slug)
for _, user := range users {
- if _, err = c.Services().Notifications.Insert(user, title, link, idea.ID, c.User().ID); err != nil {
+ if _, err = c.Services().Notifications.Insert(user, title, link, idea.ID); err != nil {
return err
}
}
@@ -114,7 +114,7 @@ func NotifyAboutNewComment(idea *models.Idea, comment *models.NewComment) worker
title := fmt.Sprintf("**%s** left a comment on **%s**", c.User().Name, idea.Title)
link := fmt.Sprintf("/ideas/%d/%s", idea.Number, idea.Slug)
for _, user := range users {
- if _, err = c.Services().Notifications.Insert(user, title, link, idea.ID, c.User().ID); err != nil {
+ if _, err = c.Services().Notifications.Insert(user, title, link, idea.ID); err != nil {
return err
}
}
@@ -161,7 +161,7 @@ func NotifyAboutStatusChange(idea *models.Idea, response *models.SetResponse) wo
title := fmt.Sprintf("**%s** changed status of **%s** to **%s**", c.User().Name, idea.Title, models.GetIdeaStatusName(response.Status))
link := fmt.Sprintf("/ideas/%d/%s", idea.Number, idea.Slug)
for _, user := range users {
- if _, err = c.Services().Notifications.Insert(user, title, link, idea.ID, c.User().ID); err != nil {
+ if _, err = c.Services().Notifications.Insert(user, title, link, idea.ID); err != nil {
return err
}
}