feat(be): sql implementation of views

This commit is contained in:
Denis Gukov 2021-10-27 17:15:36 +05:00
parent 339dfa52ce
commit 793d74dac6
4 changed files with 43 additions and 10 deletions

View File

@ -26,7 +26,7 @@ func (d *BoltDb) DeleteView(projectID int, viewID int) error {
} }
func (d *BoltDb) SetViewPositions(projectID int, positions map[int]int) error { func (d *BoltDb) SetViewPositions(projectID int, positions map[int]int) error {
for position, id := range positions { for id, position := range positions {
view, err := d.GetView(projectID, id) view, err := d.GetView(projectID, id)
if err != nil { if err != nil {
return err return err

View File

@ -111,8 +111,8 @@ func TestSetViewPositions(t *testing.T) {
} }
err = store.SetViewPositions(proj1.ID, map[int]int{ err = store.SetViewPositions(proj1.ID, map[int]int{
v1.ID: 1, v1.ID: 3,
v2.ID: 2, v2.ID: 6,
}) })
if err != nil { if err != nil {
@ -133,7 +133,7 @@ func TestSetViewPositions(t *testing.T) {
return found[i].Position < found[j].Position return found[i].Position < found[j].Position
}) })
if found[0].Position != 1 || found[1].Position != 2 { if found[0].Position != 3 || found[1].Position != 6 {
t.Fatal() t.Fatal()
} }
} }

View File

@ -11,8 +11,8 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
"id", "id",
"insert into project__template (project_id, inventory_id, repository_id, environment_id, " + "insert into project__template (project_id, inventory_id, repository_id, environment_id, " +
"alias, playbook, arguments, override_args, description, vault_key_id, `type`, start_version," + "alias, playbook, arguments, override_args, description, vault_key_id, `type`, start_version," +
"build_template_id)" + "build_template_id, view_id)" +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
template.ProjectID, template.ProjectID,
template.InventoryID, template.InventoryID,
template.RepositoryID, template.RepositoryID,
@ -25,7 +25,8 @@ func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, e
template.VaultKeyID, template.VaultKeyID,
template.Type, template.Type,
template.StartVersion, template.StartVersion,
template.BuildTemplateID) template.BuildTemplateID,
template.ViewID)
if err != nil { if err != nil {
return return
@ -57,6 +58,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
"`type`=?, " + "`type`=?, " +
"start_version=?," + "start_version=?," +
"build_template_id=? " + "build_template_id=? " +
"view_id=? " +
"where removed = false and id=? and project_id=?", "where removed = false and id=? and project_id=?",
template.InventoryID, template.InventoryID,
template.RepositoryID, template.RepositoryID,
@ -70,6 +72,7 @@ func (d *SqlDb) UpdateTemplate(template db.Template) error {
template.Type, template.Type,
template.StartVersion, template.StartVersion,
template.BuildTemplateID, template.BuildTemplateID,
template.ViewID,
template.ID, template.ID,
template.ProjectID, template.ProjectID,
) )

View File

@ -3,26 +3,56 @@ package sql
import "github.com/ansible-semaphore/semaphore/db" import "github.com/ansible-semaphore/semaphore/db"
func (d *SqlDb) GetView(projectID int, viewID int) (view db.View, err error) { func (d *SqlDb) GetView(projectID int, viewID int) (view db.View, err error) {
err = d.getObject(projectID, db.ViewProps, viewID, &view)
return return
} }
func (d *SqlDb) GetViews(projectID int) (views []db.View, err error) { func (d *SqlDb) GetViews(projectID int) (views []db.View, err error) {
views = make([]db.View, 0) err = d.getObjects(projectID, db.ViewProps, db.RetrieveQueryParams{}, &views)
return return
} }
func (d *SqlDb) UpdateView(view db.View) error { func (d *SqlDb) UpdateView(view db.View) error {
return nil _, err := d.exec(
"update project__view set title=?, position=?, project_id=? where id=?",
view.Title,
view.Position,
view.ProjectID,
view.ID)
return err
} }
func (d *SqlDb) CreateView(view db.View) (newView db.View, err error) { func (d *SqlDb) CreateView(view db.View) (newView db.View, err error) {
insertID, err := d.insert(
"id",
"insert into project__view (project_id, title, position) values (?, ?, ?)",
view.ProjectID,
view.Title,
view.Position)
if err != nil {
return
}
newView = view
newView.ID = insertID
return return
} }
func (d *SqlDb) DeleteView(projectID int, viewID int) error { func (d *SqlDb) DeleteView(projectID int, viewID int) error {
return nil return d.deleteObject(projectID, db.ViewProps, viewID)
} }
func (d *SqlDb) SetViewPositions(projectID int, positions map[int]int) error { func (d *SqlDb) SetViewPositions(projectID int, positions map[int]int) error {
for id, position := range positions {
_, err := d.exec("update project__view set position=? where project_id=? and id=?",
position,
projectID,
id)
if err != nil {
return err
}
}
return nil return nil
} }