grafana/pkg/services/comments/sql_storage.go
Kristin Laemmert 05709ce411
chore: remove sqlstore & mockstore dependencies from (most) packages (#57087)
* chore: add alias for InitTestDB and Session

Adds an alias for the sqlstore InitTestDB and Session, and updates tests using these to reduce dependencies on the sqlstore.Store.

* next pass of removing sqlstore imports
* last little bit
* remove mockstore where possible
2022-10-19 09:02:15 -04:00

118 lines
2.6 KiB
Go

package comments
import (
"context"
"time"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/comments/commentmodel"
)
type sqlStorage struct {
sql db.DB
}
func checkObjectType(contentType string) bool {
_, ok := commentmodel.RegisteredObjectTypes[contentType]
return ok
}
func checkObjectID(objectID string) bool {
return objectID != ""
}
func (s *sqlStorage) Create(ctx context.Context, orgID int64, objectType string, objectID string, userID int64, content string) (*commentmodel.Comment, error) {
if !checkObjectType(objectType) {
return nil, errUnknownObjectType
}
if !checkObjectID(objectID) {
return nil, errEmptyObjectID
}
if content == "" {
return nil, errEmptyContent
}
var result *commentmodel.Comment
return result, s.sql.WithTransactionalDbSession(ctx, func(dbSession *db.Session) error {
var group commentmodel.CommentGroup
has, err := dbSession.NoAutoCondition().Where(
"org_id=? AND object_type=? AND object_id=?",
orgID, objectType, objectID,
).Get(&group)
if err != nil {
return err
}
nowUnix := time.Now().Unix()
groupID := group.Id
if !has {
group.OrgId = orgID
group.ObjectType = objectType
group.ObjectId = objectID
group.Created = nowUnix
group.Updated = nowUnix
group.Settings = commentmodel.Settings{}
_, err = dbSession.Insert(&group)
if err != nil {
return err
}
groupID = group.Id
}
message := commentmodel.Comment{
GroupId: groupID,
UserId: userID,
Content: content,
Created: nowUnix,
Updated: nowUnix,
}
_, err = dbSession.Insert(&message)
if err != nil {
return err
}
result = &message
return nil
})
}
const maxLimit = 300
func (s *sqlStorage) Get(ctx context.Context, orgID int64, objectType string, objectID string, filter GetFilter) ([]*commentmodel.Comment, error) {
if !checkObjectType(objectType) {
return nil, errUnknownObjectType
}
if !checkObjectID(objectID) {
return nil, errEmptyObjectID
}
var result []*commentmodel.Comment
limit := 100
if filter.Limit > 0 {
limit = int(filter.Limit)
if limit > maxLimit {
limit = maxLimit
}
}
return result, s.sql.WithTransactionalDbSession(ctx, func(dbSession *db.Session) error {
var group commentmodel.CommentGroup
has, err := dbSession.NoAutoCondition().Where(
"org_id=? AND object_type=? AND object_id=?",
orgID, objectType, objectID,
).Get(&group)
if err != nil {
return err
}
if !has {
return nil
}
clause := dbSession.Where("group_id=?", group.Id)
if filter.BeforeID > 0 {
clause.Where("id < ?", filter.BeforeID)
}
return clause.OrderBy("id desc").Limit(limit).Find(&result)
})
}