2020-12-20 19:00:59 +01:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
2024-02-27 12:06:05 +01:00
|
|
|
"github.com/Masterminds/squirrel"
|
2024-03-11 01:04:47 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-20 19:00:59 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateProject(project db.Project) (newProject db.Project, err error) {
|
2024-07-22 08:14:06 +02:00
|
|
|
project.Created = time.Now().UTC()
|
2020-12-20 19:00:59 +01:00
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
insertId, err := d.insert(
|
|
|
|
"id",
|
2024-03-11 01:04:47 +01:00
|
|
|
"insert into project(name, created, type) values (?, ?, ?)",
|
|
|
|
project.Name, project.Created, project.Type)
|
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").
|
2024-04-22 10:22:23 +02:00
|
|
|
Limit(200).
|
2023-09-17 16:15:44 +02:00
|
|
|
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").
|
2024-04-22 10:15:32 +02:00
|
|
|
Limit(200).
|
2020-12-20 19:00:59 +01:00
|
|
|
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
|
|
|
|
}
|