Semaphore/api/projects/projects.go

73 lines
1.6 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/models"
"net/http"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gorilla/context"
"github.com/masterminds/squirrel"
2016-04-02 14:40:07 +02:00
)
// GetProjects returns all projects in this users context
func GetProjects(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user").(*models.User)
2016-04-02 14:40:07 +02:00
query, args, err := squirrel.Select("p.*").
From("project as p").
Join("project__user as pu on pu.project_id=p.id").
Where("pu.user_id=?", user.ID).
OrderBy("p.name").
ToSql()
2016-04-02 14:40:07 +02:00
util.LogWarning(err)
var projects []models.Project
if _, err := helpers.Store(r).Sql().Select(&projects, query, args...); err != nil {
panic(err)
}
2016-04-02 14:40:07 +02:00
helpers.WriteJSON(w, http.StatusOK, projects)
2016-04-02 14:40:07 +02:00
}
// AddProject adds a new project to the database
func AddProject(w http.ResponseWriter, r *http.Request) {
var body models.Project
user := context.Get(r, "user").(*models.User)
2016-04-02 14:40:07 +02:00
if !helpers.Bind(w, r, &body) {
return
}
2016-04-02 14:40:07 +02:00
body, err := helpers.Store(r).CreateProject(body)
if err != nil {
panic(err)
}
2016-04-02 14:40:07 +02:00
_, err = helpers.Store(r).CreateProjectUser(models.ProjectUser{ProjectID: body.ID, UserID: user.ID, Admin: true})
if err != nil {
panic(err)
}
2016-04-02 14:40:07 +02:00
desc := "Project Created"
oType := "Project"
_, err = helpers.Store(r).CreateEvent(models.Event{
ProjectID: &body.ID,
Description: &desc,
ObjectType: &oType,
ObjectID: &body.ID,
})
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
2016-04-17 02:20:23 +02:00
helpers.WriteJSON(w, http.StatusCreated, body)
2016-04-02 14:40:07 +02:00
}