Query history: Create API to delete query from query history (#44653)
* Query history: Add delete and refactor * Update docs/sources/http_api/query_history.md Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com> Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
This commit is contained in:
co-authored by
Piotr Jamróz
parent
1680e284e5
commit
0f362f8dfc
@@ -9,12 +9,12 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func (s QueryHistoryService) createQuery(ctx context.Context, user *models.SignedInUser, cmd CreateQueryInQueryHistoryCommand) error {
|
||||
func (s QueryHistoryService) createQuery(ctx context.Context, user *models.SignedInUser, cmd CreateQueryInQueryHistoryCommand) (QueryHistoryDTO, error) {
|
||||
queryHistory := QueryHistory{
|
||||
OrgId: user.OrgId,
|
||||
Uid: util.GenerateShortUID(),
|
||||
OrgID: user.OrgId,
|
||||
UID: util.GenerateShortUID(),
|
||||
Queries: cmd.Queries,
|
||||
DatasourceUid: cmd.DatasourceUid,
|
||||
DatasourceUID: cmd.DatasourceUID,
|
||||
CreatedBy: user.UserId,
|
||||
CreatedAt: time.Now().Unix(),
|
||||
Comment: "",
|
||||
@@ -25,8 +25,32 @@ func (s QueryHistoryService) createQuery(ctx context.Context, user *models.Signe
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return QueryHistoryDTO{}, err
|
||||
}
|
||||
|
||||
return nil
|
||||
dto := QueryHistoryDTO{
|
||||
UID: queryHistory.UID,
|
||||
DatasourceUID: queryHistory.DatasourceUID,
|
||||
CreatedBy: queryHistory.CreatedBy,
|
||||
CreatedAt: queryHistory.CreatedAt,
|
||||
Comment: queryHistory.Comment,
|
||||
Queries: queryHistory.Queries,
|
||||
Starred: false,
|
||||
}
|
||||
|
||||
return dto, nil
|
||||
}
|
||||
|
||||
func (s QueryHistoryService) deleteQuery(ctx context.Context, user *models.SignedInUser, UID string) (int64, error) {
|
||||
var queryID int64
|
||||
err := s.SQLStore.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
|
||||
id, err := session.Where("org_id = ? AND created_by = ? AND uid = ?", user.OrgId, user.UserId, UID).Delete(QueryHistory{})
|
||||
if id == 0 {
|
||||
return ErrQueryNotFound
|
||||
}
|
||||
queryID = id
|
||||
return err
|
||||
})
|
||||
|
||||
return queryID, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user