Semaphore/db/sql/project.go

120 lines
2.4 KiB
Go
Raw Normal View History

package sql
import (
"github.com/Masterminds/squirrel"
2024-03-11 01:04:47 +01:00
"github.com/ansible-semaphore/semaphore/db"
"time"
)
func (d *SqlDb) CreateProject(project db.Project) (newProject db.Project, err error) {
project.Created = time.Now()
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)
if err != nil {
return
}
newProject = project
newProject.ID = insertId
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
}
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).
ToSql()
if err != nil {
return
}
_, err = d.selectAll(&projects, query, args...)
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...)
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
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)
if err != nil {
2023-09-17 21:55:14 +02:00
_ = tx.Rollback()
return err
}
}
return tx.Commit()
}
func (d *SqlDb) UpdateProject(project db.Project) error {
2021-08-24 17:20:34 +02:00
_, err := d.exec(
"update project set name=?, alert=?, alert_chat=?, max_parallel_tasks=? where id=?",
project.Name,
project.Alert,
project.AlertChat,
project.MaxParallelTasks,
project.ID)
return err
}