2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2017-02-23 06:12:16 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-01 20:06:49 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/models"
|
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2019-07-09 19:45:27 +02:00
|
|
|
"github.com/gorilla/context"
|
|
|
|
"github.com/masterminds/squirrel"
|
2016-04-02 14:40:07 +02:00
|
|
|
)
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetProjects returns all projects in this users context
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetProjects(w http.ResponseWriter, r *http.Request) {
|
2020-12-01 20:06:49 +01:00
|
|
|
user := context.Get(r, "user").(*models.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2019-07-09 18:11:01 +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
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
util.LogWarning(err)
|
2020-12-01 20:06:49 +01:00
|
|
|
var projects []models.Project
|
|
|
|
if _, err := context.Get(r, "store").(db.Store).Sql().Select(&projects, query, args...); err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2019-07-09 19:45:27 +02:00
|
|
|
util.WriteJSON(w, http.StatusOK, projects)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddProject adds a new project to the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddProject(w http.ResponseWriter, r *http.Request) {
|
2020-12-01 20:06:49 +01:00
|
|
|
var body models.Project
|
|
|
|
|
|
|
|
user := context.Get(r, "user").(*models.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
err := util.Bind(w, r, &body)
|
|
|
|
if err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
body, err = context.Get(r, "store").(db.Store).CreateProject(body)
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
_, err = context.Get(r, "store").(db.Store).CreateProjectUser(models.ProjectUser{ProjectID: body.ID, UserID: user.ID, Admin: true})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Project Created"
|
|
|
|
oType := "Project"
|
2020-12-01 20:06:49 +01:00
|
|
|
_, err = context.Get(r, "store").(db.Store).CreateEvent(models.Event{
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &body.ID,
|
|
|
|
Description: &desc,
|
|
|
|
ObjectType: &oType,
|
|
|
|
ObjectID: &body.ID,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2019-07-09 19:45:27 +02:00
|
|
|
util.WriteJSON(w, http.StatusCreated, body)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|