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-03 11:57:39 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-20 19:00:59 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *SqlDb) getEvents(q squirrel.SelectBuilder, params db.RetrieveQueryParams) (events []db.Event, err error) {
|
|
|
|
|
|
|
|
if params.Count > 0 {
|
|
|
|
q = q.Limit(uint64(params.Count))
|
|
|
|
}
|
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
_, err = d.selectAll(&events, query, args...)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:27:16 +02:00
|
|
|
err = db.FillEvents(d, events)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateEvent(evt db.Event) (newEvent db.Event, err error) {
|
|
|
|
var created = time.Now()
|
|
|
|
|
2021-08-24 17:20:34 +02:00
|
|
|
_, err = d.exec(
|
2021-08-20 08:28:50 +02:00
|
|
|
"insert into event(user_id, project_id, object_id, object_type, description, created) values (?, ?, ?, ?, ?, ?)",
|
|
|
|
evt.UserID,
|
2020-12-20 19:00:59 +01:00
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
return d.getEvents(q, params)
|
|
|
|
}
|