2021-10-26 20:19:12 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ViewMiddleware ensures a key exists and loads it to the context
|
|
|
|
func ViewMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
viewID, err := helpers.GetIntParam("view_id", w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
view, err := helpers.Store(r).GetView(project.ID, viewID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Set(r, "view", view)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-27 21:48:51 +02:00
|
|
|
func GetViewTemplates(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
view := context.Get(r, "view").(db.View)
|
|
|
|
|
|
|
|
templates, err := helpers.Store(r).GetViewTemplates(project.ID, view.ID, helpers.QueryParams(r.URL))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, templates)
|
|
|
|
}
|
|
|
|
|
2021-10-26 20:19:12 +02:00
|
|
|
// GetViews retrieves sorted keys from the database
|
|
|
|
func GetViews(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if view := context.Get(r, "view"); view != nil {
|
|
|
|
k := view.(db.View)
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, k)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
var views []db.View
|
|
|
|
|
|
|
|
views, err := helpers.Store(r).GetViews(project.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, views)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddView adds a new key to the database
|
|
|
|
func AddView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
var view db.View
|
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &view) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if view.ProjectID != project.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:43:04 +02:00
|
|
|
if err := view.Validate(); err != nil {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-26 20:19:12 +02:00
|
|
|
newView, err := helpers.Store(r).CreateView(view)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
|
|
|
objType := db.EventKey
|
|
|
|
|
|
|
|
desc := "View " + view.Title + " created"
|
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
|
|
|
UserID: &user.ID,
|
|
|
|
ProjectID: &newView.ProjectID,
|
|
|
|
ObjectType: &objType,
|
|
|
|
ObjectID: &newView.ID,
|
|
|
|
Description: &desc,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 18:22:52 +02:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, newView)
|
2021-10-26 20:19:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:43:04 +02:00
|
|
|
func SetViewPositions(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var positions map[int]int
|
|
|
|
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &positions) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := helpers.Store(r).SetViewPositions(project.ID, positions)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-26 20:19:12 +02:00
|
|
|
// UpdateView updates key in database
|
|
|
|
// nolint: gocyclo
|
|
|
|
func UpdateView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var view db.View
|
|
|
|
oldView := context.Get(r, "view").(db.View)
|
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &view) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-27 20:05:54 +02:00
|
|
|
if view.ID != oldView.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "View ID in URL and in body must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:43:04 +02:00
|
|
|
if err := view.Validate(); err != nil {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-26 20:19:12 +02:00
|
|
|
if err := helpers.Store(r).UpdateView(view); err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
|
|
|
desc := "View " + view.Title + " updated"
|
|
|
|
objType := db.EventView
|
|
|
|
|
|
|
|
_, err := helpers.Store(r).CreateEvent(db.Event{
|
|
|
|
UserID: &user.ID,
|
|
|
|
ProjectID: &oldView.ProjectID,
|
|
|
|
Description: &desc,
|
|
|
|
ObjectID: &oldView.ID,
|
|
|
|
ObjectType: &objType,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveView deletes a view from the database
|
|
|
|
func RemoveView(w http.ResponseWriter, r *http.Request) {
|
|
|
|
view := context.Get(r, "view").(db.View)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = helpers.Store(r).DeleteView(view.ProjectID, view.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
|
|
|
desc := "View " + view.Title + " deleted"
|
|
|
|
|
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
|
|
|
UserID: &user.ID,
|
|
|
|
ProjectID: &view.ProjectID,
|
|
|
|
Description: &desc,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|