Semaphore/api/projects/projects.go

58 lines
1.3 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
2017-02-23 00:21:49 +01:00
"net/http"
2016-05-24 11:55:48 +02:00
database "github.com/ansible-semaphore/semaphore/db"
2016-04-02 14:40:07 +02:00
"github.com/ansible-semaphore/semaphore/models"
2017-02-23 00:21:49 +01:00
"github.com/castawaylabs/mulekick"
"github.com/gorilla/context"
2016-04-02 14:40:07 +02:00
"github.com/masterminds/squirrel"
)
2017-02-22 23:17:36 +01:00
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, _ := 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()
var projects []models.Project
if _, err := database.Mysql.Select(&projects, query, args...); err != nil {
panic(err)
}
2017-02-23 00:21:49 +01:00
mulekick.WriteJSON(w, http.StatusOK, projects)
2016-04-02 14:40:07 +02:00
}
2017-02-22 23:17:36 +01:00
func AddProject(w http.ResponseWriter, r *http.Request) {
2016-04-02 14:40:07 +02:00
var body models.Project
user := context.Get(r, "user").(*models.User)
2016-04-02 14:40:07 +02:00
if err := mulekick.Bind(w, r, &body); err != nil {
2016-04-02 14:40:07 +02:00
return
}
err := body.CreateProject()
if err != nil {
panic(err)
}
if _, err := database.Mysql.Exec("insert into project__user set project_id=?, user_id=?, admin=1", body.ID, user.ID); err != nil {
2016-04-02 14:40:07 +02:00
panic(err)
}
2016-04-17 02:20:23 +02:00
desc := "Project Created"
if err := (models.Event{
ProjectID: &body.ID,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
2017-02-23 00:21:49 +01:00
mulekick.WriteJSON(w, http.StatusCreated, body)
2016-04-02 14:40:07 +02:00
}