Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

response table and some display elements

goenninge056e3c82fe2
diff --git a/app/models/feedback.go b/app/models/feedback.go
index 1b9478c4c..089dbca26 100644
--- a/app/models/feedback.go
+++ b/app/models/feedback.go
@@ -1,20 +1,47 @@
package models
import (
+ "encoding/json"
"time"
+
+ "github.com/getfider/fider/app/pkg/dbx"
)
//Idea represents an idea on a tenant board
type Idea struct {
- ID int `json:"id" db:"id"`
- Number int `json:"number" db:"number"`
- Title string `json:"title" db:"title"`
- Slug string `json:"slug" db:"slug"`
- Description string `json:"description" db:"description"`
- CreatedOn time.Time `json:"createdOn" db:"created_on"`
- User *User `json:"user" db:"user"`
- TotalSupporters int `json:"totalSupporters" db:"supporters"`
- Status IdeaStatus `json:"status" db:"status"`
+ ID int `json:"id" db:"id"`
+ Number int `json:"number" db:"number"`
+ Title string `json:"title" db:"title"`
+ Slug string `json:"slug" db:"slug"`
+ Description string `json:"description" db:"description"`
+ CreatedOn time.Time `json:"createdOn" db:"created_on"`
+ User *User `json:"user" db:"user"`
+ TotalSupporters int `json:"totalSupporters" db:"supporters"`
+ Status IdeaStatus `json:"status" db:"status"`
+ Response *IdeaResponse `json:"response" db:"response"`
+}
+
+//IdeaResponse is a staff response to a given idea
+type IdeaResponse struct {
+ Text dbx.NullString `db:"text"`
+ CreatedOn dbx.NullTime `db:"date"`
+ UserID dbx.NullInt `db:"user_id"`
+}
+
+// MarshalJSON interface redefinition
+func (r IdeaResponse) MarshalJSON() ([]byte, error) {
+ if r.Text.Valid || r.CreatedOn.Valid || r.UserID.Valid {
+ return json.Marshal(struct {
+ Text dbx.NullString `json:"text"`
+ CreatedOn dbx.NullTime `json:"createdOn"`
+ UserID dbx.NullInt `json:"userId"`
+ }{
+ Text: r.Text,
+ CreatedOn: r.CreatedOn,
+ UserID: r.UserID,
+ })
+ }
+ return json.Marshal(nil)
}
//IdeaStatus represents the status of an idea
diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.go
index bad4d8dfb..d614c8785 100644
--- a/app/pkg/dbx/dbx.go
+++ b/app/pkg/dbx/dbx.go
@@ -109,7 +109,7 @@ func (db Database) Get(data interface{}, command string, args ...interface{}) er
defer rows.Close()
if rows.Next() {
- return scan(rows, data)
+ return fill(rows, data)
}
return sql.ErrNoRows
@@ -128,7 +128,7 @@ func (db Database) Select(data interface{}, command string, args ...interface{})
itemType := sliceType.Elem().Elem()
for rows.Next() {
item := reflect.New(itemType)
- err = scan(rows, item.Interface())
+ err = fill(rows, item.Interface())
if err != nil {
return err
}
@@ -139,34 +139,42 @@ func (db Database) Select(data interface{}, command string, args ...interface{})
return nil
}
-func scan(rows *sql.Rows, data interface{}) error {
- columns, _ := rows.Columns()
+func scan(prefix string, data interface{}) map[string]interface{} {
fields := make(map[string]interface{})
s := reflect.ValueOf(data).Elem()
+
numfield := s.NumField()
for i := 0; i < numfield; i++ {
field := s.Field(i)
typeField := s.Type().Field(i)
tag := typeField.Tag.Get("db")
- if typeField.Type.Kind() != reflect.Ptr {
- if tag != "" {
- fields[tag] = field.Addr().Interface()
- }
- } else {
- obj := reflect.New(field.Type().Elem()).Elem()
- field.Set(obj.Addr())
- nested := field.Elem()
- nestedNumField := nested.NumField()
- for j := 0; j < nestedNumField; j++ {
- nestedTag := nested.Type().Field(j).Tag.Get("db")
- if nestedTag != "" {
- fields[tag+"_"+nestedTag] = nested.Field(j).Addr().Interface()
+
+ if tag != "" {
+ if typeField.Type.Kind() != reflect.Ptr {
+ fields[prefix+tag] = field.Addr().Interface()
+ } else if field.Type().Elem().Kind() != reflect.Struct || field.Type().Elem().String() == "time.Time" {
+ obj := reflect.New(field.Type().Elem()).Elem()
+ field.Set(obj.Addr())
+ fields[prefix+tag] = field.Interface()
+ } else {
+ obj := reflect.New(field.Type().Elem()).Elem()
+ field.Set(obj.Addr())
+ nestedFields := scan(prefix+tag+"_", field.Interface())
+ for name, address := range nestedFields {
+ fields[name] = address
}
}
}
}
+ return fields
+}
+
+func fill(rows *sql.Rows, data interface{}) error {
+ columns, _ := rows.Columns()
+ fields := scan("", data)
+
pointers := make([]interface{}, len(columns))
for i, column := range columns {
if pointer, ok := fields[column]; ok {
diff --git a/app/pkg/dbx/dbx_test.go b/app/pkg/dbx/dbx_test.go
index f6e6a35f5..e477adc80 100644
--- a/app/pkg/dbx/dbx_test.go
+++ b/app/pkg/dbx/dbx_test.go
@@ -20,9 +20,14 @@ type userWithTenant struct {
Tenant *tenant `db:"tenant"`
}
+type userProvider struct {
+ Provider string `db:"provider"`
+ User *userWithTenant `db:"user"`
+}
+
type tenant struct {
- ID int `db:"id"`
- Name string `db:"name"`
+ ID int `db:"id"`
+ Name *string `db:"name"`
}
func TestMain(m *testing.M) {
@@ -49,6 +54,27 @@ func TestBind_SimpleStruct(t *testing.T) {
Expect(u.IgnoreThis).To(Equal(""))
}
+func TestBind_DeepNestedStruct(t *testing.T) {
+ RegisterTestingT(t)
+ db, _ := dbx.New()
+ defer db.Close()
+
+ u := userProvider{}
+
+ err := db.Get(&u, `SELECT up.provider,
+ up.user_id,
+ u.tenant_id AS user_tenant_id
+ FROM user_providers up
+ INNER JOIN users u
+ ON up.user_id = u.id
+ WHERE provider_uid = 'FB2222'`)
+ Expect(err).To(BeNil())
+ Expect(u.Provider).To(Equal("facebook"))
+ Expect(u.User.ID).To(Equal(400))
+ Expect(u.User.Tenant.ID).To(Equal(400))
+
+}
+
func TestBind_SimpleStruct_SingleField(t *testing.T) {
RegisterTestingT(t)
db, _ := dbx.New()
@@ -97,7 +123,7 @@ func TestBind_NestedStruct(t *testing.T) {
Expect(u.Name).To(Equal("Jon Snow"))
Expect(u.Tenant).NotTo(BeNil())
Expect(u.Tenant.ID).To(Equal(300))
- Expect(u.Tenant.Name).To(Equal("Demonstration"))
+ Expect(*u.Tenant.Name).To(Equal("Demonstration"))
}
func TestBind_NestedStruct_Multiple(t *testing.T) {
@@ -117,9 +143,9 @@ func TestBind_NestedStruct_Multiple(t *testing.T) {
Expect(err).To(BeNil())
Expect(len(u)).To(Equal(2))
Expect(u[0].Name).To(Equal("Jon Snow"))
- Expect(u[0].Tenant.Name).To(Equal("Demonstration"))
+ Expect(*u[0].Tenant.Name).To(Equal("Demonstration"))
Expect(u[1].Name).To(Equal("Arya Stark"))
- Expect(u[1].Tenant.Name).To(Equal("Demonstration"))
+ Expect(*u[1].Tenant.Name).To(Equal("Demonstration"))
}
func TestExists_True(t *testing.T) {
diff --git a/app/pkg/dbx/scan_test.go b/app/pkg/dbx/scan_test.go
new file mode 100644
index 000000000..10464e102
--- /dev/null
+++ b/app/pkg/dbx/scan_test.go
@@ -0,0 +1,68 @@
+package dbx
+
+import (
+ "testing"
+ "time"
+
+ . "github.com/onsi/gomega"
+)
+
+type tenantWithStringPointer struct {
+ ID int `db:"id"`
+ Name *string `db:"name"`
+ Time *time.Time `db:"time"`
+}
+
+type tenant struct {
+ ID int `db:"id"`
+ Name string `db:"name"`
+}
+
+type user struct {
+ ID int `db:"id"`
+ Name string `db:"name"`
+ Tenant *tenant `db:"tenant"`
+ IgnoreThis bool
+}
+
+type userProvider struct {
+ Name string `db:"provider"`
+ User *user `db:"user"`
+}
+
+func TestScan(t *testing.T) {
+ RegisterTestingT(t)
+
+ u := user{}
+ result := scan("", &u)
+ Expect(len(result)).To(Equal(4))
+ Expect(result["id"]).To(Equal(&u.ID))
+ Expect(result["name"]).To(Equal(&u.Name))
+ Expect(result["tenant_id"]).To(Equal(&u.Tenant.ID))
+ Expect(result["tenant_name"]).To(Equal(&u.Tenant.Name))
+
+}
+
+func TestScan_WithStringPointer(t *testing.T) {
+ RegisterTestingT(t)
+
+ u := tenantWithStringPointer{}
+ result := scan("", &u)
+ Expect(len(result)).To(Equal(3))
+ Expect(result["id"]).To(Equal(&u.ID))
+ Expect(result["name"]).To(Equal(u.Name))
+ Expect(result["time"]).To(Equal(u.Time))
+}
+
+func TestDeepScan(t *testing.T) {
+ RegisterTestingT(t)
+
+ u := userProvider{}
+ result := scan("", &u)
+ Expect(len(result)).To(Equal(5))
+ Expect(result["provider"]).To(Equal(&u.Name))
+ Expect(result["user_id"]).To(Equal(&u.User.ID))
+ Expect(result["user_name"]).To(Equal(&u.User.Name))
+ Expect(result["user_tenant_id"]).To(Equal(&u.User.Tenant.ID))
+ Expect(result["user_tenant_name"]).To(Equal(&u.User.Tenant.Name))
+}
diff --git a/app/pkg/dbx/types.go b/app/pkg/dbx/types.go
new file mode 100644
index 000000000..1ad4a1e80
--- /dev/null
+++ b/app/pkg/dbx/types.go
@@ -0,0 +1,47 @@
+package dbx
+
+import (
+ "database/sql"
+ "encoding/json"
+
+ "github.com/lib/pq"
+)
+
+// NullInt representa a nullable integer
+type NullInt struct {
+ sql.NullInt64
+}
+
+// MarshalJSON interface redefinition
+func (r NullInt) MarshalJSON() ([]byte, error) {
+ if r.Valid {
+ return json.Marshal(r.Int64)
+ }
+ return json.Marshal(nil)
+}
+
+// NullString representa a nullable string
+type NullString struct {
+ sql.NullString
+}
+
+// MarshalJSON interface redefinition
+func (r NullString) MarshalJSON() ([]byte, error) {
+ if r.Valid {
+ return json.Marshal(r.String)
+ }
+ return json.Marshal(nil)
+}
+
+// NullTime representa a nullable time.Time
+type NullTime struct {
+ pq.NullTime
+}
+
+// MarshalJSON interface redefinition
+func (r NullTime) MarshalJSON() ([]byte, error) {
+ if r.Valid {
+ return json.Marshal(r.Time)
+ }
+ return json.Marshal(nil)
+}
diff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.go
index e9b4720a6..f9cad4053 100644
--- a/app/storage/postgres/idea.go
+++ b/app/storage/postgres/idea.go
@@ -27,7 +27,10 @@ var (
i.status,
u.id AS user_id,
u.name AS user_name,
- u.email AS user_email
+ u.email AS user_email,
+ i.response AS response_text,
+ i.response_date,
+ i.response_user_id
FROM ideas i
INNER JOIN users u
ON u.id = i.user_id
diff --git a/migrations/201705202300_add_idea_response.up.sql b/migrations/201705202300_add_idea_response.up.sql
new file mode 100644
index 000000000..9b7557997
--- /dev/null
+++ b/migrations/201705202300_add_idea_response.up.sql
@@ -0,0 +1,3 @@
+ALTER TABLE ideas ADD response text null;
+ALTER TABLE ideas ADD response_user_id int null;
+ALTER TABLE ideas ADD response_date timestamptz null;
\ No newline at end of file
diff --git a/public/models.ts b/public/models.ts
index 3affc7e38..1ca892b97 100644
--- a/public/models.ts
+++ b/public/models.ts
@@ -19,10 +19,18 @@ export interface Idea {
title: string;
description: string;
createdOn: string;
+ status: number;
user: User;
+ response: IdeaResponse;
totalSupporters: number;
}
+export interface IdeaResponse {
+ userId: number;
+ text: string;
+ createdOn: Date;
+}
+
export interface Comment {
id: number;
content: string;
diff --git a/public/shared/Common.tsx b/public/shared/Common.tsx
index 90e3fecfa..ae6050f9b 100644
--- a/public/shared/Common.tsx
+++ b/public/shared/Common.tsx
@@ -1,7 +1,9 @@
import * as React from "react";
+import * as moment from "moment";
import md5 = require("md5");
import { AxiosError, AxiosResponse } from "axios";
import { get, getCurrentUser } from "../storage";
+import { User } from "../models";
export const Gravatar = (props: {email?: string}) => {
const hash = props.email ? md5(props.email) : "";
@@ -43,3 +45,37 @@ export const EnvironmentInfo = () => {
}
return <div/>;
};
+
+export const IdeaStatusRibbon = (props: { status: number }) => {
+ const statusText = props.status === 1 ? "Started" :
+ props.status === 2 ? "Completed" :
+ props.status === 3 ? "Declined" : "Unknown";
+ const color = props.status === 1 ? "blue" :
+ props.status === 2 ? "green" :
+ props.status === 3 ? "red" : "black";
+
+ return <span className={`ui ribbon label ${color}`}>{ statusText }</span>;
+};
+
+interface IdeaResponseProps {
+ status: number;
+ user: User;
+ response?: string;
+ createdOn?: Date;
+}
+
+export const IdeaResponse = (props: IdeaResponseProps): JSX.Element => {
+ if (props.createdOn) {
+ return <div className="fdr-response item ui raised segment">
+ <IdeaStatusRibbon status={props.status} />
+ <div className="info">
+ <Gravatar email={props.user.email}/> <u>{props.user.name}</u>
+ <span title={props.createdOn.toString()}>{ moment(props.createdOn).fromNow() }</span>
+ </div>
+ <div className="content">
+ <MultiLineText text={ props.response } />
+ </div>
+ </div>;
+ }
+ return <div/>;
+};
diff --git a/public/site/HomePage.tsx b/public/site/HomePage.tsx
index 241d24792..9bc8253bc 100644
--- a/public/site/HomePage.tsx
+++ b/public/site/HomePage.tsx
@@ -2,7 +2,7 @@ import * as moment from "moment";
import * as React from "react";
import { Idea, User } from "../models";
import * as storage from "../storage";
-import { Gravatar, MultiLineText } from "../shared/Common";
+import { Gravatar, MultiLineText, IdeaResponse } from "../shared/Common";
import { SupportCounter } from "../shared/SupportCounter";
import { Footer } from "../shared/Footer";
@@ -34,6 +34,10 @@ export class HomePage extends React.Component<{}, {}> {
#{ x.number } shared by <Gravatar email={x.user.email}/> <u>{x.user.name}</u>
<span title={x.createdOn}>{ moment(x.createdOn).fromNow() }</span>
</div>
+ <IdeaResponse status={x.status}
+ response={x.response ? x.response.text : undefined }
+ createdOn={x.response ? x.response.createdOn : undefined }
+ user={x.user} />
</div>
</div>);
@@ -54,7 +58,7 @@ export class HomePage extends React.Component<{}, {}> {
<IdeaInput />
- <div className="ui container ideas-list">
+ <div className="ui container fdr-idea-list">
{ displayIdeas }
</div>
</div>
diff --git a/public/site/ShowIdeaPage.tsx b/public/site/ShowIdeaPage.tsx
index 33563eb0c..8f97162c9 100644
--- a/public/site/ShowIdeaPage.tsx
+++ b/public/site/ShowIdeaPage.tsx
@@ -4,7 +4,7 @@ import * as storage from "../storage";
import { User, Comment, Idea } from "../models";
import { CommentInput } from "../shared/CommentInput";
-import { Gravatar, MultiLineText } from "../shared/Common";
+import { Gravatar, MultiLineText, IdeaResponse } from "../shared/Common";
import { SocialSignInButton } from "../shared/SocialSignInButton";
import { SupportCounter } from "../shared/SupportCounter";
@@ -66,6 +66,11 @@ export class ShowIdeaPage extends React.Component<{}, {}> {
<MultiLineText text={ this.idea.description } />
+ <IdeaResponse status={this.idea.status}
+ response={this.idea.response ? this.idea.response.text : undefined }
+ createdOn={this.idea.response ? this.idea.response.createdOn : undefined }
+ user={this.idea.user} />
+
<div className="ui comments">
<h3 className="ui dividing header">Comments</h3>
{ commentsList }
diff --git a/public/style/main.scss b/public/style/main.scss
index 49449e3b1..3bcf84cbb 100644
--- a/public/style/main.scss
+++ b/public/style/main.scss
@@ -2,14 +2,6 @@
margin-bottom: 20px;
}
-.ideas-list {
- margin-top: 40px;
-}
-
-.ideas-list .ui.items > .item > .content {
- margin-left: 20px;
-}
-
.idea-header {
margin-left: 20px;
}
@@ -47,6 +39,38 @@
padding: 10px 5px !important;
}
+.fdr-idea-list {
+ margin-top: 40px;
+
+ .ui.items > .item > .content {
+ margin-left: 20px;
+ }
+}
+
+.fdr-response {
+ .content {
+ margin-top: 10px;
+ }
+
+ .info {
+ display: inline !important;
+ position: relative;
+ top: 0px;
+ left: 0px;
+ color: rgba(0, 0, 0, 0.4);
+ box-shadow: none;
+ background: 0px 0px;
+ margin: 0.5rem 0px 0px;
+ padding: 0px;
+ transition: color 0.1s ease;
+ border-top: none;
+
+ span {
+ margin-left: 6px;
+ }
+ }
+}
+
#footer {
margin-top: 50px;