2020-12-20 19:00:59 +01:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/masterminds/squirrel"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateProject(project db.Project) (newProject db.Project, err error) {
|
|
|
|
project.Created = time.Now()
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
insertId, err := d.insert(
|
|
|
|
"id",
|
|
|
|
"insert into project(name, created) values (?, ?)",
|
|
|
|
project.Name, project.Created)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newProject = project
|
2021-08-24 19:52:35 +02:00
|
|
|
newProject.ID = insertId
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-17 16:15:44 +02:00
|
|
|
func (d *SqlDb) GetAllProjects() (projects []db.Project, err error) {
|
|
|
|
query, args, err := squirrel.Select("p.*").
|
|
|
|
From("project as p").
|
|
|
|
OrderBy("p.name").
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.selectAll(&projects, query, args...)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
func (d *SqlDb) GetProjects(userID int) (projects []db.Project, err error) {
|
|
|
|
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=?", userID).
|
|
|
|
OrderBy("p.name").
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
_, err = d.selectAll(&projects, query, args...)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) GetProject(projectID int) (project db.Project, err error) {
|
|
|
|
query, args, err := squirrel.Select("p.*").
|
|
|
|
From("project as p").
|
|
|
|
Where("p.id=?", projectID).
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:20:34 +02:00
|
|
|
err = d.selectOne(&project, query, args...)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) DeleteProject(projectID int) error {
|
2023-09-17 21:55:14 +02:00
|
|
|
|
|
|
|
//tpls, err := d.GetTemplates(projectID, db.TemplateFilter{}, db.RetrieveQueryParams{})
|
|
|
|
//
|
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
|
|
|
// TODO: sort projects
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
tx, err := d.sql.Begin()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := []string{
|
|
|
|
"delete from project__template where project_id=?",
|
|
|
|
"delete from project__user where project_id=?",
|
|
|
|
"delete from project__repository where project_id=?",
|
|
|
|
"delete from project__inventory where project_id=?",
|
|
|
|
"delete from access_key where project_id=?",
|
|
|
|
"delete from project where id=?",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, statement := range statements {
|
2022-01-31 22:30:36 +01:00
|
|
|
_, err = tx.Exec(d.PrepareQuery(statement), projectID)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2023-09-17 21:55:14 +02:00
|
|
|
_ = tx.Rollback()
|
2020-12-20 19:00:59 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) UpdateProject(project db.Project) error {
|
2021-08-24 17:20:34 +02:00
|
|
|
_, err := d.exec(
|
2022-02-12 13:15:15 +01:00
|
|
|
"update project set name=?, alert=?, alert_chat=?, max_parallel_tasks=? where id=?",
|
2020-12-20 19:00:59 +01:00
|
|
|
project.Name,
|
|
|
|
project.Alert,
|
|
|
|
project.AlertChat,
|
2022-02-12 13:15:15 +01:00
|
|
|
project.MaxParallelTasks,
|
2020-12-20 19:00:59 +01:00
|
|
|
project.ID)
|
|
|
|
return err
|
|
|
|
}
|