Commit 746d6cdc by Torkel Ödegaard

refactoring: changed name on compare command to make properties more explainatory

parent 9c140184
...@@ -2,6 +2,7 @@ package api ...@@ -2,6 +2,7 @@ package api
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"path" "path"
...@@ -323,34 +324,34 @@ func GetDashboardVersion(c *middleware.Context) Response { ...@@ -323,34 +324,34 @@ func GetDashboardVersion(c *middleware.Context) Response {
return Json(200, dashVersionMeta) return Json(200, dashVersionMeta)
} }
func createCompareDashboardVersionCommand(c *middleware.Context) (m.CompareDashboardVersionsCommand, error) { func createCompareDashboardVersionCommand(c *middleware.Context) (*m.CompareDashboardVersionsCommand, error) {
cmd := m.CompareDashboardVersionsCommand{} cmd := &m.CompareDashboardVersionsCommand{}
dashboardIdStr := c.Params(":dashboardId") dashId := c.ParamsInt64(":dashboardId")
dashboardId, err := strconv.Atoi(dashboardIdStr) if dashId == 0 {
if err != nil { return nil, errors.New("Missing dashboardId")
return cmd, err
} }
versionStrings := strings.Split(c.Params(":versions"), "...") versionStrings := strings.Split(c.Params(":versions"), "...")
if len(versionStrings) != 2 { if len(versionStrings) != 2 {
return cmd, fmt.Errorf("bad format: urls should be in the format /versions/0...1") return nil, fmt.Errorf("bad format: urls should be in the format /versions/0...1")
} }
originalDash, err := strconv.Atoi(versionStrings[0]) BaseVersion, err := strconv.Atoi(versionStrings[0])
if err != nil { if err != nil {
return cmd, fmt.Errorf("bad format: first argument is not of type int") return nil, fmt.Errorf("bad format: first argument is not of type int")
} }
newDash, err := strconv.Atoi(versionStrings[1]) newVersion, err := strconv.Atoi(versionStrings[1])
if err != nil { if err != nil {
return cmd, fmt.Errorf("bad format: second argument is not of type int") return nil, fmt.Errorf("bad format: second argument is not of type int")
} }
cmd.DashboardId = int64(dashboardId) cmd.DashboardId = dashId
cmd.OrgId = c.OrgId cmd.OrgId = c.OrgId
cmd.Original = originalDash cmd.BaseVersion = BaseVersion
cmd.New = newDash cmd.NewVersion = newVersion
return cmd, nil return cmd, nil
} }
...@@ -360,6 +361,7 @@ func CompareDashboardVersions(c *middleware.Context) Response { ...@@ -360,6 +361,7 @@ func CompareDashboardVersions(c *middleware.Context) Response {
if err != nil { if err != nil {
return ApiError(500, err.Error(), err) return ApiError(500, err.Error(), err)
} }
cmd.DiffType = m.DiffDelta cmd.DiffType = m.DiffDelta
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
...@@ -376,8 +378,8 @@ func CompareDashboardVersions(c *middleware.Context) Response { ...@@ -376,8 +378,8 @@ func CompareDashboardVersions(c *middleware.Context) Response {
return Json(200, util.DynMap{ return Json(200, util.DynMap{
"meta": util.DynMap{ "meta": util.DynMap{
"original": cmd.Original, "baseVersion": cmd.BaseVersion,
"new": cmd.New, "newVersion": cmd.NewVersion,
}, },
"delta": deltaMap, "delta": deltaMap,
}) })
...@@ -392,7 +394,7 @@ func CompareDashboardVersionsJSON(c *middleware.Context) Response { ...@@ -392,7 +394,7 @@ func CompareDashboardVersionsJSON(c *middleware.Context) Response {
} }
cmd.DiffType = m.DiffJSON cmd.DiffType = m.DiffJSON
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(cmd); err != nil {
return ApiError(500, err.Error(), err) return ApiError(500, err.Error(), err)
} }
...@@ -406,9 +408,10 @@ func CompareDashboardVersionsBasic(c *middleware.Context) Response { ...@@ -406,9 +408,10 @@ func CompareDashboardVersionsBasic(c *middleware.Context) Response {
if err != nil { if err != nil {
return ApiError(500, err.Error(), err) return ApiError(500, err.Error(), err)
} }
cmd.DiffType = m.DiffBasic cmd.DiffType = m.DiffBasic
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(cmd); err != nil {
return ApiError(500, err.Error(), err) return ApiError(500, err.Error(), err)
} }
......
...@@ -86,8 +86,8 @@ type GetDashboardVersionsQuery struct { ...@@ -86,8 +86,8 @@ type GetDashboardVersionsQuery struct {
type CompareDashboardVersionsCommand struct { type CompareDashboardVersionsCommand struct {
OrgId int64 OrgId int64
DashboardId int64 DashboardId int64
Original int BaseVersion int
New int NewVersion int
DiffType DiffType DiffType DiffType
Delta []byte `json:"delta"` Delta []byte `json:"delta"`
......
...@@ -30,17 +30,17 @@ func init() { ...@@ -30,17 +30,17 @@ func init() {
// CompareDashboardVersionsCommand computes the JSON diff of two versions, // CompareDashboardVersionsCommand computes the JSON diff of two versions,
// assigning the delta of the diff to the `Delta` field. // assigning the delta of the diff to the `Delta` field.
func CompareDashboardVersionsCommand(cmd *m.CompareDashboardVersionsCommand) error { func CompareDashboardVersionsCommand(cmd *m.CompareDashboardVersionsCommand) error {
original, err := getDashboardVersion(cmd.DashboardId, cmd.Original) baseVersion, err := getDashboardVersion(cmd.DashboardId, cmd.BaseVersion)
if err != nil { if err != nil {
return err return err
} }
newDashboard, err := getDashboardVersion(cmd.DashboardId, cmd.New) newVersion, err := getDashboardVersion(cmd.DashboardId, cmd.NewVersion)
if err != nil { if err != nil {
return err return err
} }
left, jsonDiff, err := getDiff(original, newDashboard) left, jsonDiff, err := getDiff(baseVersion, newVersion)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -117,8 +117,8 @@ func TestCompareDashboardVersions(t *testing.T) { ...@@ -117,8 +117,8 @@ func TestCompareDashboardVersions(t *testing.T) {
cmd := m.CompareDashboardVersionsCommand{ cmd := m.CompareDashboardVersionsCommand{
DashboardId: savedDash.Id, DashboardId: savedDash.Id,
Original: query.Result[0].Version, BaseVersion: query.Result[0].Version,
New: query.Result[1].Version, NewVersion: query.Result[1].Version,
DiffType: m.DiffDelta, DiffType: m.DiffDelta,
} }
...@@ -130,8 +130,8 @@ func TestCompareDashboardVersions(t *testing.T) { ...@@ -130,8 +130,8 @@ func TestCompareDashboardVersions(t *testing.T) {
Convey("Compare two versions that are the same", func() { Convey("Compare two versions that are the same", func() {
cmd := m.CompareDashboardVersionsCommand{ cmd := m.CompareDashboardVersionsCommand{
DashboardId: savedDash.Id, DashboardId: savedDash.Id,
Original: savedDash.Version, BaseVersion: savedDash.Version,
New: savedDash.Version, NewVersion: savedDash.Version,
DiffType: m.DiffDelta, DiffType: m.DiffDelta,
} }
...@@ -143,8 +143,8 @@ func TestCompareDashboardVersions(t *testing.T) { ...@@ -143,8 +143,8 @@ func TestCompareDashboardVersions(t *testing.T) {
Convey("Compare two versions that don't exist", func() { Convey("Compare two versions that don't exist", func() {
cmd := m.CompareDashboardVersionsCommand{ cmd := m.CompareDashboardVersionsCommand{
DashboardId: savedDash.Id, DashboardId: savedDash.Id,
Original: 123, BaseVersion: 123,
New: 456, NewVersion: 456,
DiffType: m.DiffDelta, DiffType: m.DiffDelta,
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment