diff --git a/core/groups.go b/core/groups.go index 5e01c4ae..fcd4d9f7 100644 --- a/core/groups.go +++ b/core/groups.go @@ -11,6 +11,10 @@ type Group struct { // Delete will remove a group func (g *Group) Delete() error { + for _, s := range g.Services() { + s.GroupId = 0 + s.Update(false) + } err := messagesDb().Delete(g) if err.Error != nil { return err.Error @@ -18,12 +22,6 @@ func (g *Group) Delete() error { return err.Error } -// Update will update a group in the database -func (g *Group) Update() error { - err := servicesDB().Update(&g) - return err.Error -} - // Create will create a group and insert it into the database func (g *Group) Create() (int64, error) { g.CreatedAt = time.Now() @@ -31,6 +29,17 @@ func (g *Group) Create() (int64, error) { return g.Id, db.Error } +// Services returns all services belonging to a group +func (g *Group) Services() []*Service { + var services []*Service + for _, s := range Services() { + if s.Select().GroupId == int(g.Id) { + services = append(services, s.(*Service)) + } + } + return services +} + // SelectGroups returns all groups func SelectGroups() []*Group { var groups []*Group diff --git a/core/sample.go b/core/sample.go index 7de53968..f4a72f2f 100644 --- a/core/sample.go +++ b/core/sample.go @@ -26,6 +26,9 @@ import ( // InsertSampleData will create the example/dummy services for a brand new Statping installation func InsertSampleData() error { utils.Log(1, "Inserting Sample Data...") + + insertSampleGroups() + s1 := ReturnService(&types.Service{ Name: "Google", Domain: "https://google.com", @@ -35,6 +38,7 @@ func InsertSampleData() error { Method: "GET", Timeout: 10, Order: 1, + GroupId: 1, }) s2 := ReturnService(&types.Service{ Name: "Statping Github", @@ -55,6 +59,8 @@ func InsertSampleData() error { Method: "GET", Timeout: 30, Order: 3, + Public: types.NewNullBool(true), + GroupId: 2, }) s4 := ReturnService(&types.Service{ Name: "JSON API Tester", @@ -67,6 +73,8 @@ func InsertSampleData() error { PostData: types.NewNullString(`{ "title": "statup", "body": "bar", "userId": 19999 }`), Timeout: 30, Order: 4, + Public: types.NewNullBool(true), + GroupId: 2, }) s5 := ReturnService(&types.Service{ Name: "Google DNS", @@ -76,6 +84,8 @@ func InsertSampleData() error { Port: 53, Timeout: 120, Order: 5, + Public: types.NewNullBool(true), + GroupId: 1, }) s1.Create(false) @@ -86,8 +96,6 @@ func InsertSampleData() error { insertMessages() - insertSampleGroups() - utils.Log(1, "Sample data has finished importing") return nil diff --git a/core/services_test.go b/core/services_test.go index c7e0c5ab..d08f3d9d 100644 --- a/core/services_test.go +++ b/core/services_test.go @@ -189,6 +189,7 @@ func TestCreateService(t *testing.T) { Type: "http", Method: "GET", Timeout: 20, + GroupId: 1, }) var err error newServiceId, err = s.Create(false) @@ -212,6 +213,7 @@ func TestCreateFailingHTTPService(t *testing.T) { Type: "http", Method: "GET", Timeout: 5, + GroupId: 1, }) var err error newServiceId, err = s.Create(false) @@ -238,6 +240,7 @@ func TestCreateFailingTCPService(t *testing.T) { Interval: 30, Type: "tcp", Timeout: 5, + GroupId: 1, }) var err error newServiceId, err = s.Create(false) diff --git a/handlers/groups.go b/handlers/groups.go index 9ee68852..bffff987 100644 --- a/handlers/groups.go +++ b/handlers/groups.go @@ -69,27 +69,6 @@ func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) { sendJsonAction(group, "create", w, r) } -func apiGroupUpdateHandler(w http.ResponseWriter, r *http.Request) { - if !IsFullAuthenticated(r) { - sendUnauthorizedJson(w, r) - return - } - vars := mux.Vars(r) - group := core.SelectGroup(utils.ToInt(vars["id"])) - if group == nil { - sendErrorJson(errors.New("group not found"), w, r) - return - } - decoder := json.NewDecoder(r.Body) - decoder.Decode(&group) - err := group.Update() - if err != nil { - sendErrorJson(err, w, r) - return - } - sendJsonAction(group, "update", w, r) -} - func apiGroupDeleteHandler(w http.ResponseWriter, r *http.Request) { if !IsFullAuthenticated(r) { sendUnauthorizedJson(w, r) diff --git a/handlers/handlers.go b/handlers/handlers.go index 342242d6..2c2cd9f8 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -195,8 +195,9 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap "Groups": func() []*core.Group { return core.SelectGroups() }, - "len": func(g []types.ServiceInterface) int { - return len(g) + "len": func(g interface{}) int { + val := reflect.ValueOf(g) + return val.Len() }, "IsNil": func(g interface{}) bool { return g == nil diff --git a/handlers/routes.go b/handlers/routes.go index 6e4e4fe6..1f1386cc 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -89,7 +89,6 @@ func Router() *mux.Router { r.Handle("/api/groups", http.HandlerFunc(apiAllGroupHandler)).Methods("GET") r.Handle("/api/groups", http.HandlerFunc(apiCreateGroupHandler)).Methods("POST") r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupHandler)).Methods("GET") - r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupUpdateHandler)).Methods("POST") r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupDeleteHandler)).Methods("DELETE") // API Routes diff --git a/source/tmpl/form_service.gohtml b/source/tmpl/form_service.gohtml index 6ca2bbaf..eada6fbd 100644 --- a/source/tmpl/form_service.gohtml +++ b/source/tmpl/form_service.gohtml @@ -1,6 +1,7 @@ {{define "form_service"}}