Semaphore/db/sql/event.go

73 lines
1.6 KiB
Go
Raw Normal View History

package sql
import (
2023-07-03 01:41:13 +02:00
"github.com/ansible-semaphore/semaphore/db"
"github.com/masterminds/squirrel"
"time"
)
func (d *SqlDb) getEvents(q squirrel.SelectBuilder, params db.RetrieveQueryParams) (events []db.Event, err error) {
2023-07-03 01:41:13 +02:00
if params.Count > 0 {
q = q.Limit(uint64(params.Count))
}
2023-07-03 01:41:13 +02:00
query, args, err := q.ToSql()
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
_, err = d.selectAll(&events, query, args...)
2023-07-03 01:41:13 +02:00
if err != nil {
return
}
2023-07-03 01:41:13 +02:00
err = db.FillEvents(d, events)
2023-07-03 01:41:13 +02:00
return
}
func (d *SqlDb) CreateEvent(evt db.Event) (newEvent db.Event, err error) {
2023-07-03 01:41:13 +02:00
var created = time.Now()
_, err = d.exec(
"insert into event(user_id, project_id, object_id, object_type, description, created) values (?, ?, ?, ?, ?, ?)",
evt.UserID,
evt.ProjectID,
evt.ObjectID,
evt.ObjectType,
evt.Description,
created)
if err != nil {
return
}
newEvent = evt
newEvent.Created = created
return
}
func (d *SqlDb) GetUserEvents(userID int, params db.RetrieveQueryParams) ([]db.Event, error) {
2023-07-03 01:41:13 +02:00
q := squirrel.Select("event.*, p.name as project_name").
From("event").
LeftJoin("project as p on event.project_id=p.id").
OrderBy("created desc").
LeftJoin("project__user as pu on pu.project_id=p.id").
Where("p.id IS NULL or pu.user_id=?", userID)
return d.getEvents(q, params)
}
func (d *SqlDb) GetEvents(projectID int, params db.RetrieveQueryParams) ([]db.Event, error) {
2023-07-03 01:41:13 +02:00
q := squirrel.Select("event.*, p.name as project_name").
From("event").
LeftJoin("project as p on event.project_id=p.id").
OrderBy("created desc").
Where("event.project_id=?", projectID)
2023-07-03 01:41:13 +02:00
return d.getEvents(q, params)
}