Browse Source

update

tags/v1.22.6.1^2
liuzx 3 years ago
parent
commit
8380ef6c29
2 changed files with 126 additions and 1 deletions
  1. +123
    -0
      models/attachment.go
  2. +3
    -1
      routers/repo/dataset.go

+ 123
- 0
models/attachment.go View File

@@ -81,6 +81,7 @@ type AttachmentsOptions struct {
NeedRepoInfo bool NeedRepoInfo bool
Keyword string Keyword string
RecommendOnly bool RecommendOnly bool
UserId int64
} }


func (a *Attachment) AfterUpdate() { func (a *Attachment) AfterUpdate() {
@@ -653,3 +654,125 @@ func Attachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) {


return attachments, count, nil return attachments, count, nil
} }

func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) {
sess := x.NewSession()
defer sess.Close()

var cond = builder.NewCond()
if opts.NeedDatasetIDs {
cond = cond.And(
builder.In("attachment.dataset_id", opts.DatasetIDs),
)
}

if opts.UploaderID > 0 {
cond = cond.And(
builder.Eq{"attachment.uploader_id": opts.UploaderID},
)
}

if (opts.Type) >= 0 {
cond = cond.And(
builder.Eq{"attachment.type": opts.Type},
)
}

if opts.NeedIsPrivate {
cond = cond.And(
builder.Eq{"attachment.is_private": opts.IsPrivate},
)
for _, DatasetID := range opts.DatasetIDs {
dataset, err := GetDatasetByID(DatasetID)
if err != nil {
return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err)
}
repo, err := GetRepositoryByID(dataset.RepoID)
if err != nil {
return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err)
}
isCollaborator, _ := repo.IsCollaborator(opts.UserId)
if !isCollaborator {
cond = cond.And(
builder.Eq{"attachment.dataset_id": DatasetID},
builder.Eq{"attachment.is_private": false},
)
} else {
cond = cond.And(
builder.Eq{"attachment.dataset_id": DatasetID},
)
}
}
}
if opts.RecommendOnly {
cond = cond.And(builder.In("attachment.id", builder.Select("attachment.id").
From("attachment").
Join("INNER", "dataset", "attachment.dataset_id = dataset.id and dataset.recommend=true")))
}

if opts.JustNeedZipFile {
var DecompressState []int32
DecompressState = append(DecompressState, DecompressStateDone, DecompressStateIng, DecompressStateFailed)
cond = cond.And(
builder.In("attachment.decompress_state", DecompressState),
)
}

var count int64
var err error
if len(opts.Keyword) == 0 {
count, err = sess.Where(cond).Count(new(Attachment))
} else {
lowerKeyWord := strings.ToLower(opts.Keyword)

cond = cond.And(builder.Or(builder.Like{"LOWER(attachment.name)", lowerKeyWord}, builder.Like{"LOWER(attachment.description)", lowerKeyWord}))
count, err = sess.Table(&Attachment{}).Where(cond).Count(new(AttachmentInfo))

}

if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}

if opts.Page >= 0 && opts.PageSize > 0 {
var start int
if opts.Page == 0 {
start = 0
} else {
start = (opts.Page - 1) * opts.PageSize
}
sess.Limit(opts.PageSize, start)
}

sess.OrderBy("attachment.created_unix DESC")
attachments := make([]*AttachmentInfo, 0, setting.UI.DatasetPagingNum)
if err := sess.Table(&Attachment{}).Where(cond).
Find(&attachments); err != nil {
return nil, 0, fmt.Errorf("Find: %v", err)
}

if opts.NeedRepoInfo {
for _, attachment := range attachments {
dataset, err := GetDatasetByID(attachment.DatasetID)
if err != nil {
return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err)
}
attachment.Recommend = dataset.Recommend
repo, err := GetRepositoryByID(dataset.RepoID)
if err == nil {
attachment.Repo = repo
} else {
return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err)
}
user, err := GetUserByID(attachment.UploaderID)
if err == nil {
attachment.RelAvatarLink = user.RelAvatarLink()
attachment.UserName = user.Name
} else {
return nil, 0, fmt.Errorf("GetUserByID failed error: %v", err)
}
}
}

return attachments, count, nil
}

+ 3
- 1
routers/repo/dataset.go View File

@@ -448,6 +448,7 @@ func PublicDataset(ctx *context.Context) {
} }


func MyFavoriteDataset(ctx *context.Context) { func MyFavoriteDataset(ctx *context.Context) {
UserId := ctx.User.ID
page := ctx.QueryInt("page") page := ctx.QueryInt("page")
cloudbrainType := ctx.QueryInt("type") cloudbrainType := ctx.QueryInt("type")
keyword := strings.Trim(ctx.Query("q"), " ") keyword := strings.Trim(ctx.Query("q"), " ")
@@ -467,7 +468,7 @@ func MyFavoriteDataset(ctx *context.Context) {
datasetIDs = append(datasetIDs, datasetStars[i].DatasetID) datasetIDs = append(datasetIDs, datasetStars[i].DatasetID)
} }


datasets, count, err := models.Attachments(&models.AttachmentsOptions{
datasets, count, err := models.MyFvAttachments(&models.AttachmentsOptions{
ListOptions: models.ListOptions{ ListOptions: models.ListOptions{
Page: page, Page: page,
PageSize: setting.UI.DatasetPagingNum, PageSize: setting.UI.DatasetPagingNum,
@@ -481,6 +482,7 @@ func MyFavoriteDataset(ctx *context.Context) {
JustNeedZipFile: true, JustNeedZipFile: true,
NeedRepoInfo: true, NeedRepoInfo: true,
RecommendOnly: ctx.QueryBool("recommend"), RecommendOnly: ctx.QueryBool("recommend"),
UserId: UserId,
}) })
if err != nil { if err != nil {
ctx.ServerError("datasets", err) ctx.ServerError("datasets", err)


Loading…
Cancel
Save