Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

replace second query with a subquery (#221)

Guilherme Oenningb7efd2ac6a08
diff --git a/app/middlewares/jwt.go b/app/middlewares/jwt.go
index cf48af06e..523b957f9 100644
--- a/app/middlewares/jwt.go
+++ b/app/middlewares/jwt.go
@@ -41,13 +41,6 @@ func JwtGetter() web.MiddlewareFunc {
if c.Tenant() != nil && user.Tenant.ID == c.Tenant().ID {
c.SetUser(user)
- if !c.IsAjax() {
- ids, err := services.Ideas.SupportedBy(user.ID)
- if err != nil {
- return err
- }
- c.AddRenderVar("supportedIdeas", ids)
- }
}
return next(c)
diff --git a/app/middlewares/jwt_test.go b/app/middlewares/jwt_test.go
index 913a70edb..a7f44749d 100644
--- a/app/middlewares/jwt_test.go
+++ b/app/middlewares/jwt_test.go
@@ -50,31 +50,6 @@ func TestJwtGetter_WithCookie(t *testing.T) {
Expect(response.Body.String()).To(Equal("Jon Snow"))
}
-func TestJwtGetter_WithCookie_ShouldSetSupportedIdeas(t *testing.T) {
- RegisterTestingT(t)
-
- server, services := mock.NewServer()
- idea, _ := services.Ideas.Add("My First Idea", "With a description", mock.JonSnow.ID)
-
- token, _ := jwt.Encode(&models.FiderClaims{
- UserID: mock.JonSnow.ID,
- UserName: mock.JonSnow.Name,
- })
-
- var ids []int
- server.Use(middlewares.JwtGetter())
- status, _ := server.
- OnTenant(mock.DemoTenant).
- AddCookie(web.CookieAuthName, token).
- Execute(func(c web.Context) error {
- ids = c.RenderVars()["supportedIdeas"].([]int)
- return c.NoContent(http.StatusOK)
- })
-
- Expect(status).To(Equal(http.StatusOK))
- Expect(ids).To(Equal([]int{idea.ID}))
-}
-
func TestJwtGetter_WithCookie_InvalidUser(t *testing.T) {
RegisterTestingT(t)
diff --git a/app/models/feedback.go b/app/models/feedback.go
index 2b6390fc3..a43f3871f 100644
--- a/app/models/feedback.go
+++ b/app/models/feedback.go
@@ -13,6 +13,7 @@ type Idea struct {
Description string `json:"description"`
CreatedOn time.Time `json:"createdOn"`
User *User `json:"user"`
+ ViewerSupported bool `json:"viewerSupported"`
TotalSupporters int `json:"totalSupporters"`
TotalComments int `json:"totalComments"`
Status int `json:"status"`
@@ -20,6 +21,11 @@ type Idea struct {
Tags []int64 `json:"tags"`
}
+// CanBeSupported returns true if this idea can be Supported/UnSupported
+func (i *Idea) CanBeSupported() bool {
+ return i.Status != IdeaCompleted && i.Status != IdeaDeclined
+}
+
// NewIdea represents a new idea
type NewIdea struct {
Title string `json:"title"`
diff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.go
index 438bd375b..1e6664201 100644
--- a/app/storage/postgres/idea.go
+++ b/app/storage/postgres/idea.go
@@ -20,6 +20,7 @@ type dbIdea struct {
Description string `db:"description"`
CreatedOn time.Time `db:"created_on"`
User *dbUser `db:"user"`
+ ViewerSupported bool `db:"viewer_supported"`
TotalSupporters int `db:"supporters"`
TotalComments int `db:"comments"`
Status int `db:"status"`
@@ -37,6 +38,7 @@ func (i *dbIdea) toModel() *models.Idea {
Slug: i.Slug,
Description: i.Description,
CreatedOn: i.CreatedOn,
+ ViewerSupported: i.ViewerSupported,
TotalSupporters: i.TotalSupporters,
TotalComments: i.TotalComments,
Status: i.Status,
@@ -114,7 +116,8 @@ var (
r.name AS response_user_name,
r.email AS response_user_email,
r.role AS response_user_role,
- array_remove(array_agg(t.id), NULL) AS tags
+ array_remove(array_agg(t.id), NULL) AS tags,
+ COALESCE(%s, false) AS viewer_supported
FROM ideas i
INNER JOIN users u
ON u.id = i.user_id
@@ -132,11 +135,15 @@ var (
// GetAll returns all tenant ideas
func (s *IdeaStorage) getIdeaQuery(filter, order string) string {
+ viewerSupportedSubQuery := "null"
+ if s.user != nil {
+ viewerSupportedSubQuery = fmt.Sprintf("(SELECT true FROM idea_supporters WHERE idea_id = i.id AND user_id = %d)", s.user.ID)
+ }
tagCondition := `AND t.is_public = true`
if s.user != nil && s.user.IsCollaborator() {
tagCondition = ``
}
- return fmt.Sprintf(sqlSelectIdeasWhere, tagCondition, filter, order)
+ return fmt.Sprintf(sqlSelectIdeasWhere, viewerSupportedSubQuery, tagCondition, filter, order)
}
// GetAll returns all tenant ideas
@@ -258,7 +265,8 @@ func (s *IdeaStorage) AddSupporter(number, userID int) error {
if err != nil {
return err
}
- if idea.Status >= models.IdeaCompleted {
+
+ if !idea.CanBeSupported() {
return nil
}
@@ -285,7 +293,7 @@ func (s *IdeaStorage) RemoveSupporter(number, userID int) error {
return err
}
- if idea.Status >= models.IdeaCompleted {
+ if !idea.CanBeSupported() {
return nil
}
diff --git a/app/storage/postgres/idea_test.go b/app/storage/postgres/idea_test.go
index 4f15f11d3..c04f45bf4 100644
--- a/app/storage/postgres/idea_test.go
+++ b/app/storage/postgres/idea_test.go
@@ -59,6 +59,7 @@ func TestIdeaStorage_AddAndGet(t *testing.T) {
Expect(err).To(BeNil())
Expect(dbIdeaById.ID).To(Equal(idea.ID))
Expect(dbIdeaById.Number).To(Equal(1))
+ Expect(dbIdeaById.ViewerSupported).To(BeFalse())
Expect(dbIdeaById.TotalSupporters).To(Equal(0))
Expect(dbIdeaById.Status).To(Equal(models.IdeaOpen))
Expect(dbIdeaById.Title).To(Equal("My new idea"))
@@ -72,6 +73,7 @@ func TestIdeaStorage_AddAndGet(t *testing.T) {
Expect(err).To(BeNil())
Expect(dbIdeaBySlug.ID).To(Equal(idea.ID))
Expect(dbIdeaBySlug.Number).To(Equal(1))
+ Expect(dbIdeaBySlug.ViewerSupported).To(BeFalse())
Expect(dbIdeaBySlug.TotalSupporters).To(Equal(0))
Expect(dbIdeaBySlug.Status).To(Equal(models.IdeaOpen))
Expect(dbIdeaBySlug.Title).To(Equal("My new idea"))
@@ -170,18 +172,27 @@ func TestIdeaStorage_AddSupporter(t *testing.T) {
defer TeardownDatabaseTest()
tenants := postgres.NewTenantStorage(trx)
+ users := postgres.NewUserStorage(trx)
+ users.SetCurrentTenant(demoTenant(tenants))
ideas := postgres.NewIdeaStorage(trx)
ideas.SetCurrentTenant(demoTenant(tenants))
- idea, _ := ideas.Add("My new idea", "with this description", 1)
-
- err := ideas.AddSupporter(idea.Number, 1)
+ ideas.SetCurrentUser(jonSnow(users))
+ idea, err := ideas.Add("My new idea", "with this description", 1)
Expect(err).To(BeNil())
- err = ideas.AddSupporter(idea.Number, 2)
+ err = ideas.AddSupporter(idea.Number, aryaStark(users).ID)
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(users).ID)
+ Expect(err).To(BeNil())
+
+ dbIdea, err = ideas.GetByNumber(1)
Expect(err).To(BeNil())
+ Expect(dbIdea.ViewerSupported).To(BeTrue())
Expect(dbIdea.TotalSupporters).To(Equal(2))
}
diff --git a/public/components/SupportCounter.tsx b/public/components/SupportCounter.tsx
index 24315ca4b..8f78a0025 100644
--- a/public/components/SupportCounter.tsx
+++ b/public/components/SupportCounter.tsx
@@ -25,10 +25,8 @@ export class SupportCounter extends React.Component<SupportCounterProps, Support
constructor(props: SupportCounterProps) {
super(props);
- const supportedIdeas = this.session.getArray<number>('supportedIdeas');
-
this.state = {
- supported: props.user && supportedIdeas && supportedIdeas.indexOf(props.idea.id) >= 0 ? true : false,
+ supported: props.idea.viewerSupported,
total: props.idea.totalSupporters
};
}
@@ -43,10 +41,10 @@ export class SupportCounter extends React.Component<SupportCounterProps, Support
const response = await action(this.props.idea.number);
if (response.ok) {
- this.setState({
- supported: !this.state.supported,
- total: this.state.total + (this.state.supported ? -1 : 1)
- });
+ this.setState((state) => ({
+ supported: !state.supported,
+ total: state.total + (state.supported ? -1 : 1)
+ }));
} else {
// TODO: handle this. we should have a global alert box
}
diff --git a/public/models/idea.ts b/public/models/idea.ts
index 6bbad0aa2..632b8df54 100644
--- a/public/models/idea.ts
+++ b/public/models/idea.ts
@@ -9,6 +9,7 @@ export interface Idea {
createdOn: string;
status: number;
user: User;
+ viewerSupported: boolean;
response: IdeaResponse;
totalSupporters: number;
totalComments: number;