2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2020-12-04 23:41:26 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/context"
|
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-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
projects, err := helpers.Store(r).GetProjects(user.ID)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.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-04 23:41:26 +01:00
|
|
|
var body db.Project
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &body) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
body, err := helpers.Store(r).CreateProject(body)
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateProjectUser(db.ProjectUser{ProjectID: body.ID, UserID: user.ID, Admin: true})
|
2020-12-01 20:06:49 +01:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Project Created"
|
|
|
|
oType := "Project"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
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)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, body)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|