Skip to content

Commit

Permalink
Merge pull request #34 from eiladin/remove-id
Browse files Browse the repository at this point in the history
feat: don't return internal ids and change FriendlyName to Name
  • Loading branch information
eiladin committed Jan 31, 2021
2 parents 8e67fea + e4eb599 commit e19adb1
Show file tree
Hide file tree
Showing 29 changed files with 96 additions and 96 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# filepath: simple-startpage.yaml
filepath: simple-startpage.yaml
database:
driver: "sqlite"
name: "simple-startpage.db"
Expand Down
2 changes: 1 addition & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (d *DB) GetNetwork(net *models.Network) error {
}

func (d *DB) GetSite(site *models.Site) error {
result := d.conn.First(site)
result := d.conn.Where("name = ?", site.Name).First(site)
return handleError(result.Error)
}

Expand Down
10 changes: 5 additions & 5 deletions internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func (suite *DatabaseSuite) TestDBFunctions() {
{Name: "test-link-2"},
},
Sites: []models.Site{
{FriendlyName: "test-site-1"},
{FriendlyName: "test-site-2"},
{Name: "test-site-1"},
{Name: "test-site-2"},
},
}
suite.NoError(db.CreateNetwork(&net))
Expand All @@ -118,13 +118,13 @@ func (suite *DatabaseSuite) TestDBFunctions() {
suite.NoError(db.GetNetwork(&findNet))
// GetNetwork assertions
suite.Equal("test", findNet.Network, "Network should be 'test'")
suite.Equal("test-site-1", findNet.Sites[0].FriendlyName, "Site FriendlyName should be 'test-site-1'")
suite.Equal("test-site-1", findNet.Sites[0].Name, "Site Name should be 'test-site-1'")
suite.Equal("test-link-1", findNet.Links[0].Name, "Link Name should be 'test-link-1'")

findSite := models.Site{ID: 1}
findSite := models.Site{Name: "test-site-1"}
suite.NoError(db.GetSite(&findSite))
// GetSite assertions
suite.Equal("test-site-1", findSite.FriendlyName, "Site FriendlyName should be 'test-site-1'")
suite.Equal("test-site-1", findSite.Name, "Site Name should be 'test-site-1'")

missingSite := models.Site{ID: 3}
err = db.GetSite(&missingSite)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ var doc = `{
"models.Site": {
"type": "object",
"properties": {
"friendlyName": {
"Name": {
"type": "string"
},
"icon": {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"models.Site": {
"type": "object",
"properties": {
"friendlyName": {
"Name": {
"type": "string"
},
"icon": {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ definitions:
type: object
models.Site:
properties:
friendlyName:
Name:
type: string
icon:
type: string
Expand Down
4 changes: 2 additions & 2 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite ServerSuite) TestNew() {
"POST /api/network",
"GET /api/healthz",
"GET /api/appconfig",
"GET /api/status/:id",
"GET /api/status/:name",
"GET /swagger/doc.json",
}},
{
Expand All @@ -66,7 +66,7 @@ func (suite ServerSuite) TestNew() {
"POST /api/network",
"GET /api/healthz",
"GET /api/appconfig",
"GET /api/status/:id",
"GET /api/status/:name",
"GET /swagger/*",
}},
}
Expand Down
17 changes: 6 additions & 11 deletions internal/yamlstore/yamlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ func New(filepath string) (store.Store, error) {
}

func (d *YamlStore) CreateNetwork(net *models.Network) error {
net.ID = 1
for i := range net.Sites {
net.Sites[i].ID = uint(i + 1)
}
for i := range net.Links {
net.Links[i].ID = uint(i + 1)
}
b, err := yaml.Marshal(net)
if err != nil {
return err
Expand All @@ -51,13 +44,15 @@ func (d *YamlStore) GetNetwork(net *models.Network) error {

func (d *YamlStore) GetSite(site *models.Site) error {
net := models.Network{}
d.GetNetwork(&net)
err := d.GetNetwork(&net)
if err != nil {
return err
}

found := false
for _, s := range net.Sites {
if site.ID == s.ID {
site.ID = s.ID
site.FriendlyName = s.FriendlyName
if site.Name == s.Name {
site.Name = s.Name
site.CreatedAt = s.CreatedAt
site.Icon = s.Icon
site.IsSupportedApp = s.IsSupportedApp
Expand Down
32 changes: 22 additions & 10 deletions internal/yamlstore/yamlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,41 @@ func (suite *YamlStoreSuite) TestFunctions() {
{Name: "test-link-2"},
},
Sites: []models.Site{
{FriendlyName: "test-site-1"},
{FriendlyName: "test-site-2"},
{
Name: "test-site-1",
Tags: []models.Tag{
{Value: "tag-1"},
},
},
{
Name: "test-site-2",
Tags: []models.Tag{
{Value: "tag-2"},
},
},
},
}
suite.NoError(f.CreateNetwork(&net))
// CreateNetwork assertions
suite.Equal(uint(1), net.ID, "Network ID should be '1'")
suite.Equal(uint(1), net.Sites[0].ID, "Site ID should be '1'")
suite.Equal(uint(2), net.Sites[1].ID, "Site ID should be '2'")
suite.Equal(uint(1), net.Links[0].ID, "Link ID should be '1'")
suite.Equal(uint(2), net.Links[1].ID, "Link ID should be '2'")
suite.Equal("test", net.Network, "Network should be 'test'")
suite.Equal("test-site-1", net.Sites[0].Name, "Site Name should be 'test-site-1'")
suite.Equal("test-site-2", net.Sites[1].Name, "Site Name should be 'test-site-2'")
suite.Equal("test-link-1", net.Links[0].Name, "Link Name should be 'test-link-1'")
suite.Equal("test-link-2", net.Links[1].Name, "Link Name should be 'test-link-2'")
suite.Equal("tag-1", net.Sites[0].Tags[0].Value, "Tag Value should be 'tag-1'")
suite.Equal("tag-2", net.Sites[1].Tags[0].Value, "Tag Value should be 'tag-2'")

findNet := models.Network{ID: 1}
suite.NoError(f.GetNetwork(&findNet))
// GetNetwork assertions
suite.Equal("test", findNet.Network, "Network should be 'test'")
suite.Equal("test-site-1", findNet.Sites[0].FriendlyName, "Site FriendlyName should be 'test-site-1'")
suite.Equal("test-site-1", findNet.Sites[0].Name, "Site Name should be 'test-site-1'")
suite.Equal("test-link-1", findNet.Links[0].Name, "Link Name should be 'test-link-1'")

findSite := models.Site{ID: 1}
findSite := models.Site{Name: "test-site-1"}
suite.NoError(f.GetSite(&findSite))
// GetSite assertions
suite.Equal("test-site-1", findSite.FriendlyName, "Site FriendlyName should be 'test-site-1'")
suite.Equal("test-site-1", findSite.Name, "Site Name should be 'test-site-1'")

missingSite := models.Site{ID: 3}
err = f.GetSite(&missingSite)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type NetworkHandler struct {
func (c *NetworkHandler) Create(ctx echo.Context) error {
net := new(models.Network)

if err := ctx.Bind(net); err != nil || (net.Network == "" && net.ID == 0 && net.Links == nil && net.Sites == nil) {
if err := ctx.Bind(net); err != nil || (net.Network == "" && net.Links == nil && net.Sites == nil) {
if err == nil {
err = errors.New("empty request recieved")
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/handlers/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (suite *NetworkSuite) TestGet() {
network: models.Network{
Network: "test-network",
Sites: []models.Site{
{ID: 1, FriendlyName: "z"},
{ID: 2, FriendlyName: "a"},
{ID: 1, Name: "z"},
{ID: 2, Name: "a"},
},
},
err: nil,
Expand Down Expand Up @@ -113,10 +113,8 @@ func (suite *NetworkSuite) TestGet() {
if suite.NoError(dec.Decode(&net)) {
suite.Equal("test-network", net.Network, "Get Network should return 'test-network'")
suite.Len(net.Sites, 2, "There should be 2 sites")
suite.Equal("z", net.Sites[0].FriendlyName, "The first site in the list should have FriendlyName 'z'")
suite.Equal(uint(1), net.Sites[0].ID, "The first site in the list should be ID '1'")
suite.Equal("a", net.Sites[1].FriendlyName, "The second site in the list should have FriendlyName 'a'")
suite.Equal(uint(2), net.Sites[1].ID, "The second site in the list should have ID '2'")
suite.Equal("z", net.Sites[0].Name, "The first site in the list should have Name 'z'")
suite.Equal("a", net.Sites[1].Name, "The second site in the list should have Name 'a'")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func RegisterRoutes(e *echo.Echo, prv *providers.Provider) {
e.POST("/api/network", netHandler.Create)

statusHandler := &StatusHandler{prv.Status}
e.GET("/api/status/:id", statusHandler.Get)
e.GET("/api/status/:name", statusHandler.Get)

cfgHandler := &ConfigHandler{prv.Config}
e.GET("/api/appconfig", cfgHandler.Get)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (suite RouteSuite) TestRegisterRoutes() {
suite.Contains(e, "POST /api/network")
suite.Contains(e, "GET /api/healthz")
suite.Contains(e, "GET /api/appconfig")
suite.Contains(e, "GET /api/status/:id")
suite.Contains(e, "GET /api/status/:name")
}

func TestRouteSuite(t *testing.T) {
Expand Down
14 changes: 5 additions & 9 deletions pkg/handlers/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handlers
import (
"errors"
"net/http"
"strconv"

"github.com/eiladin/go-simple-startpage/pkg/usecases/status"
"github.com/labstack/echo/v4"
Expand All @@ -24,18 +23,15 @@ type StatusHandler struct {
// @Failure 400 {object} httperror.HTTPError
// @Failure 404 {object} httperror.HTTPError
// @Failure 500 {object} httperror.HTTPError
// @Router /api/status/{id} [get]
// @Router /api/status/{name} [get]
func (c *StatusHandler) Get(ctx echo.Context) error {
// httpClient.Timeout = time.Millisecond * time.Duration(c.config.Timeout)
id, err := strconv.Atoi(ctx.Param("id"))
if err != nil || id < 1 {
if err == nil {
err = errors.New("invalid id received: " + ctx.Param("id"))
}
return echo.ErrBadRequest.SetInternal(err)
name := ctx.Param("name")
if name == "" {
return echo.ErrBadRequest
}

s, err := c.StatusUseCase.Get(uint(id))
s, err := c.StatusUseCase.Get(name)
if err != nil {
if errors.Is(err, status.ErrNotFound) {
return echo.ErrNotFound
Expand Down
30 changes: 14 additions & 16 deletions pkg/handlers/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type mockStatusUseCase struct {
mock.Mock
}

func (m *mockStatusUseCase) Get(id uint) (*models.Status, error) {
args := m.Called(id)
func (m *mockStatusUseCase) Get(name string) (*models.Status, error) {
args := m.Called(name)
data := args.Get(0).(models.Status)
return &data, args.Error(1)
}
Expand All @@ -39,32 +39,30 @@ func (suite *StatusSuite) TestGet() {
throwErr error
wantErr error
}{
{id: 1, param: "1", uri: "https://my.test.site", isUp: true, throwErr: nil, wantErr: nil},
{id: 1, param: "1", uri: "https://my.fail.site", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "1", uri: "https://^^invalidurl^^", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "1", uri: "ssh://localhost:22224", isUp: true, throwErr: nil, wantErr: nil},
{id: 1, param: "1", uri: "ssh://localhost:1234", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "1", uri: "https://500.test.site", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "abc", uri: "https://400.test.site", isUp: false, throwErr: errors.New("bad request"), wantErr: echo.ErrBadRequest},
{id: 1, param: "test-site-1", uri: "https://my.test.site", isUp: true, throwErr: nil, wantErr: nil},
{id: 1, param: "test-site-2", uri: "https://my.fail.site", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "test-site-3", uri: "https://^^invalidurl^^", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "test-site-4", uri: "ssh://localhost:22224", isUp: true, throwErr: nil, wantErr: nil},
{id: 1, param: "test-site-5", uri: "ssh://localhost:1234", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "test-site-6", uri: "https://500.test.site", isUp: false, throwErr: nil, wantErr: nil},
{id: 1, param: "", uri: "https://no-id.test.site", isUp: false, throwErr: errors.New("bad request"), wantErr: echo.ErrBadRequest},
{id: 12345, param: "12345", uri: "https://bigid.test.site", isUp: false, throwErr: status.ErrNotFound, wantErr: echo.ErrNotFound},
{id: 1, param: "1", uri: "https://error.test.site", isUp: false, throwErr: errors.New("internal server error"), wantErr: echo.ErrInternalServerError},
{id: 1, param: "0", uri: "https://my.test.site", isUp: false, throwErr: errors.New("bad request"), wantErr: echo.ErrBadRequest},
{id: 1, param: "1", uri: "https://timeout.test.site", isUp: false, throwErr: nil, wantErr: nil},
{id: 12345, param: "test-site-9", uri: "https://bigid.test.site", isUp: false, throwErr: status.ErrNotFound, wantErr: echo.ErrNotFound},
{id: 1, param: "test-site-10", uri: "https://error.test.site", isUp: false, throwErr: errors.New("internal server error"), wantErr: echo.ErrInternalServerError},
{id: 1, param: "tste-site-11", uri: "https://timeout.test.site", isUp: false, throwErr: nil, wantErr: nil},
}

for _, c := range cases {
uc := new(mockStatusUseCase)
if !errors.Is(c.wantErr, echo.ErrBadRequest) {
uc.On("Get", c.id).Return(models.Status{IsUp: c.isUp}, c.throwErr)
uc.On("Get", c.param).Return(models.Status{IsUp: c.isUp}, c.throwErr)
}
ss := StatusHandler{StatusUseCase: uc}

req := httptest.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
ctx := app.NewContext(req, rec)
ctx.SetPath("/:id")
ctx.SetParamNames("id")
ctx.SetPath("/:name")
ctx.SetParamNames("name")
ctx.SetParamValues(c.param)
err := ss.Get(ctx)
uc.AssertExpectations(suite.T())
Expand Down
6 changes: 3 additions & 3 deletions pkg/models/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Network struct {
}

type NetworkID struct {
ID uint `json:"id" yaml:"id"`
ID uint `json:"id" yaml:"-"`
}

type Link struct {
Expand All @@ -31,12 +31,12 @@ type Link struct {
}

type Site struct {
ID uint `json:"id" gorm:"primaryKey" yaml:"id"`
ID uint `json:"-" gorm:"primaryKey" yaml:"-"`
CreatedAt time.Time `json:"-" yaml:"-"`
UpdatedAt time.Time `json:"-" yaml:"-"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index" yaml:"-"`
NetworkID uint `json:"-" yaml:"-"`
FriendlyName string `json:"friendlyName" yaml:"friendlyName"`
Name string `json:"name" yaml:"name"`
URI string `json:"uri" yaml:"uri"`
Icon string `json:"icon" yaml:"icon"`
IsSupportedApp bool `json:"isSupportedApp" yaml:"isSupportedApp"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

type Status struct {
ID uint `json:"id"`
Name string `json:"name"`
IsUp bool `json:"isUp"`
IP string `json:"ip"`
}

func NewStatus(timeout int, s *Site) Status {
res := Status{
ID: s.ID,
Name: s.Name,
IsUp: false,
}
url, err := url.Parse(s.URI)
Expand Down
2 changes: 1 addition & 1 deletion pkg/usecases/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *service) Create(net *models.Network) error {

func sortSitesByName(sites []models.Site) {
sort.Slice(sites, func(p, q int) bool {
return sites[p].FriendlyName < sites[q].FriendlyName
return sites[p].Name < sites[q].Name
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/usecases/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ func (suite *NetworkSuite) TestGet() {

func (suite NetworkSuite) TestSortSitesByName() {
sites := []models.Site{
{ID: 1, FriendlyName: "z"},
{ID: 2, FriendlyName: "a"},
{ID: 1, Name: "z"},
{ID: 2, Name: "a"},
}

sortSitesByName(sites)

suite.Equal(uint(2), sites[0].ID)
suite.Equal("a", sites[0].FriendlyName)
suite.Equal("a", sites[0].Name)
suite.Equal(uint(1), sites[1].ID)
suite.Equal("z", sites[1].FriendlyName)
suite.Equal("z", sites[1].Name)
}

func TestNetworkSuite(t *testing.T) {
Expand Down
Loading

0 comments on commit e19adb1

Please sign in to comment.