Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 90 additions & 97 deletions _mocks/opencsg.com/csghub-server/component/mock_TagComponent.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion api/handler/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ func (h *DatasetHandler) Update(ctx *gin.Context) {

dataset, err := h.dataset.Update(ctx.Request.Context(), req)
if err != nil {
slog.Error("Failed to update dataset", slog.Any("error", err))
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
return
}
slog.Error("Failed to update dataset", slog.Any("error", err))
httpbase.ServerError(ctx, err)
return
}
Expand Down
37 changes: 16 additions & 21 deletions api/handler/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type TagsHandler struct {
// @Param category query string false "category name"
// @Param scope query string false "scope name" Enums(model, dataset, code, space, prompt)
// @Param built_in query bool false "built_in"
// @Param search query string false "search on name and show_name fields"
// @Success 200 {object} types.ResponseWithTotal{data=[]types.RepoTag} "tags"
// @Failure 400 {object} types.APIBadRequest "Bad request"
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
Expand All @@ -61,7 +62,7 @@ func (t *TagsHandler) AllTags(ctx *gin.Context) {
// CreateTag godoc
// @Security ApiKey
// @Summary Create new tag
// @Description Create new tag
// @Description Create new tag, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -71,14 +72,13 @@ func (t *TagsHandler) AllTags(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags [post]
func (t *TagsHandler) CreateTag(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)
var req types.CreateTag
if err := ctx.ShouldBindJSON(&req); err != nil {
slog.Error("Bad request format", slog.Any("error", err))
httpbase.BadRequest(ctx, err.Error())
return
}
tag, err := t.tag.CreateTag(ctx.Request.Context(), userName, req)
tag, err := t.tag.CreateTag(ctx.Request.Context(), req)
if err != nil {
slog.Error("Failed to create tag", slog.Any("req", req), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand All @@ -90,7 +90,7 @@ func (t *TagsHandler) CreateTag(ctx *gin.Context) {
// GetTag godoc
// @Security ApiKey
// @Summary Get a tag by id
// @Description Get a tag by id
// @Description Get a tag by id, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -100,14 +100,13 @@ func (t *TagsHandler) CreateTag(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags/{id} [get]
func (t *TagsHandler) GetTagByID(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
slog.Error("Bad request format", slog.Any("error", err))
httpbase.BadRequest(ctx, err.Error())
return
}
tag, err := t.tag.GetTagByID(ctx.Request.Context(), userName, id)
tag, err := t.tag.GetTagByID(ctx.Request.Context(), id)
if err != nil {
slog.Error("Failed to get tag", slog.Int64("id", id), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand All @@ -119,7 +118,7 @@ func (t *TagsHandler) GetTagByID(ctx *gin.Context) {
// UpdateTag godoc
// @Security ApiKey
// @Summary Update a tag by id
// @Description Update a tag by id
// @Description Update a tag by id, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -130,7 +129,6 @@ func (t *TagsHandler) GetTagByID(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags/{id} [put]
func (t *TagsHandler) UpdateTag(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
slog.Error("Bad request format", slog.Any("error", err))
Expand All @@ -143,7 +141,7 @@ func (t *TagsHandler) UpdateTag(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
tag, err := t.tag.UpdateTag(ctx.Request.Context(), userName, id, req)
tag, err := t.tag.UpdateTag(ctx.Request.Context(), id, req)
if err != nil {
slog.Error("Failed to update tag", slog.Int64("id", id), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand All @@ -155,7 +153,7 @@ func (t *TagsHandler) UpdateTag(ctx *gin.Context) {
// DeleteTag godoc
// @Security ApiKey
// @Summary Delete a tag by id
// @Description Delete a tag by id
// @Description Delete a tag by id, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -165,14 +163,13 @@ func (t *TagsHandler) UpdateTag(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags/{id} [delete]
func (t *TagsHandler) DeleteTag(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
slog.Error("Bad request format", slog.Any("error", err))
httpbase.BadRequest(ctx, err.Error())
return
}
err = t.tag.DeleteTag(ctx.Request.Context(), userName, id)
err = t.tag.DeleteTag(ctx.Request.Context(), id)
if err != nil {
slog.Error("Failed to delete tag", slog.Int64("id", id), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -205,7 +202,7 @@ func (t *TagsHandler) AllCategories(ctx *gin.Context) {
// CreateCategory godoc
// @Security ApiKey
// @Summary Create new category
// @Description Create new category
// @Description Create new category, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -215,14 +212,13 @@ func (t *TagsHandler) AllCategories(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags/categories [post]
func (t *TagsHandler) CreateCategory(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)
var req types.CreateCategory
if err := ctx.ShouldBindJSON(&req); err != nil {
slog.Error("Bad request format", slog.Any("error", err))
httpbase.BadRequest(ctx, err.Error())
return
}
category, err := t.tag.CreateCategory(ctx.Request.Context(), userName, req)
category, err := t.tag.CreateCategory(ctx.Request.Context(), req)
if err != nil {
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
Expand All @@ -238,7 +234,7 @@ func (t *TagsHandler) CreateCategory(ctx *gin.Context) {
// UpdateCategory godoc
// @Security ApiKey
// @Summary Create new category
// @Description Create new category
// @Description Create new category, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -248,7 +244,6 @@ func (t *TagsHandler) CreateCategory(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags/categories/id [put]
func (t *TagsHandler) UpdateCategory(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)
var req types.UpdateCategory
if err := ctx.ShouldBindJSON(&req); err != nil {
slog.Error("Bad request format", slog.Any("error", err))
Expand All @@ -261,7 +256,7 @@ func (t *TagsHandler) UpdateCategory(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
category, err := t.tag.UpdateCategory(ctx.Request.Context(), userName, req, id)
category, err := t.tag.UpdateCategory(ctx.Request.Context(), req, id)
if err != nil {
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
Expand All @@ -277,7 +272,7 @@ func (t *TagsHandler) UpdateCategory(ctx *gin.Context) {
// DeleteCategory godoc
// @Security ApiKey
// @Summary Delete a category by id
// @Description Delete a category by id
// @Description Delete a category by id, used for admin
// @Tags Tag
// @Accept json
// @Produce json
Expand All @@ -286,14 +281,14 @@ func (t *TagsHandler) UpdateCategory(ctx *gin.Context) {
// @Failure 500 {object} types.APIInternalServerError "Internal server error"
// @Router /tags/categories/id [delete]
func (t *TagsHandler) DeleteCategory(ctx *gin.Context) {
userName := httpbase.GetCurrentUser(ctx)

id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
slog.Error("Bad request format", slog.Any("error", err))
httpbase.BadRequest(ctx, err.Error())
return
}
err = t.tag.DeleteCategory(ctx.Request.Context(), userName, id)
err = t.tag.DeleteCategory(ctx.Request.Context(), id)
if err != nil {
if errors.Is(err, errorx.ErrForbidden) {
httpbase.ForbiddenError(ctx, err)
Expand Down
33 changes: 8 additions & 25 deletions api/handler/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestTagHandler_AllTags(t *testing.T) {

t.Run("with builtin", func(t *testing.T) {
var tags []*types.RepoTag
tags = append(tags, &types.RepoTag{ID: 1, Name: "test1"})
tags = append(tags, &types.RepoTag{Name: "test1"})

values := url.Values{}
values.Add("category", "task")
Expand Down Expand Up @@ -105,7 +105,6 @@ func TestTagHandler_AllTags(t *testing.T) {
}

func TestTagHandler_CreateTag(t *testing.T) {
username := "testuser"
data := types.CreateTag{
Name: "testtag",
Scope: "testscope",
Expand All @@ -118,11 +117,10 @@ func TestTagHandler_CreateTag(t *testing.T) {

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().CreateTag(ginContext.Request.Context(), username, mock.Anything).Return(&database.Tag{ID: 1, Name: "testtag"}, nil)
tagComp.EXPECT().CreateTag(ginContext.Request.Context(), mock.Anything).Return(&database.Tag{ID: 1, Name: "testtag"}, nil)

tagHandler, err := NewTestTagHandler(tagComp)
require.Nil(t, err)
Expand All @@ -142,18 +140,15 @@ func TestTagHandler_CreateTag(t *testing.T) {
}

func TestTagHandler_GetTagByID(t *testing.T) {
username := "testuser"

req := httptest.NewRequest("get", "/api/v1/tags/1", nil)

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.AddParam("id", "1")
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().GetTagByID(ginContext.Request.Context(), username, int64(1)).Return(&database.Tag{ID: 1, Name: "test1"}, nil)
tagComp.EXPECT().GetTagByID(ginContext.Request.Context(), int64(1)).Return(&database.Tag{ID: 1, Name: "test1"}, nil)

tagHandler, err := NewTestTagHandler(tagComp)
require.Nil(t, err)
Expand All @@ -173,7 +168,6 @@ func TestTagHandler_GetTagByID(t *testing.T) {
}

func TestTagHandler_UpdateTag(t *testing.T) {
username := "testuser"
data := types.UpdateTag{
Name: "testtag",
Scope: "testscope",
Expand All @@ -186,12 +180,11 @@ func TestTagHandler_UpdateTag(t *testing.T) {

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.AddParam("id", "1")
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().UpdateTag(ginContext.Request.Context(), username, int64(1), mock.Anything).Return(&database.Tag{ID: 1, Name: "testtag"}, nil)
tagComp.EXPECT().UpdateTag(ginContext.Request.Context(), int64(1), mock.Anything).Return(&database.Tag{ID: 1, Name: "testtag"}, nil)

tagHandler, err := NewTestTagHandler(tagComp)
require.Nil(t, err)
Expand All @@ -211,18 +204,15 @@ func TestTagHandler_UpdateTag(t *testing.T) {
}

func TestTagHandler_DeleteTag(t *testing.T) {
username := "testuser"

req := httptest.NewRequest("delete", "/api/v1/tags/1", nil)

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.AddParam("id", "1")
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().DeleteTag(ginContext.Request.Context(), username, int64(1)).Return(nil)
tagComp.EXPECT().DeleteTag(ginContext.Request.Context(), int64(1)).Return(nil)

tagHandler, err := NewTestTagHandler(tagComp)
require.Nil(t, err)
Expand Down Expand Up @@ -272,7 +262,6 @@ func TestTagHandler_AllCategories(t *testing.T) {
}

func TestTagHandler_CreateCategory(t *testing.T) {
username := "testuser"
data := types.CreateCategory{
Name: "testcate",
Scope: "testscope",
Expand All @@ -284,11 +273,10 @@ func TestTagHandler_CreateCategory(t *testing.T) {

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().CreateCategory(ginContext.Request.Context(), username, data).Return(
tagComp.EXPECT().CreateCategory(ginContext.Request.Context(), data).Return(
&database.TagCategory{ID: 1, Name: "testcate", Scope: types.TagScope("testscope")},
nil,
)
Expand All @@ -311,7 +299,6 @@ func TestTagHandler_CreateCategory(t *testing.T) {
}

func TestTagHandler_UpdateCategory(t *testing.T) {
username := "testuser"
data := types.UpdateCategory{
Name: "testcate",
Scope: "testscope",
Expand All @@ -323,12 +310,11 @@ func TestTagHandler_UpdateCategory(t *testing.T) {

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.AddParam("id", "1")
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().UpdateCategory(ginContext.Request.Context(), username, data, int64(1)).Return(
tagComp.EXPECT().UpdateCategory(ginContext.Request.Context(), data, int64(1)).Return(
&database.TagCategory{ID: 1, Name: "testcate", Scope: types.TagScope("testscope")},
nil,
)
Expand All @@ -351,18 +337,15 @@ func TestTagHandler_UpdateCategory(t *testing.T) {
}

func TestTagHandler_DeleteCategory(t *testing.T) {
username := "testuser"

req := httptest.NewRequest("delete", "/tags/categories/1", nil)

hr := httptest.NewRecorder()
ginContext, _ := gin.CreateTestContext(hr)
ginContext.Set("currentUser", username)
ginContext.AddParam("id", "1")
ginContext.Request = req

tagComp := mockcom.NewMockTagComponent(t)
tagComp.EXPECT().DeleteCategory(ginContext.Request.Context(), username, int64(1)).Return(nil)
tagComp.EXPECT().DeleteCategory(ginContext.Request.Context(), int64(1)).Return(nil)

tagHandler, err := NewTestTagHandler(tagComp)
require.Nil(t, err)
Expand Down
11 changes: 9 additions & 2 deletions builder/store/database/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (ts *tagStoreImpl) AllTags(ctx context.Context, filter *types.TagFilter) ([
if filter.BuiltIn != nil {
q = q.Where("built_in = ?", *filter.BuiltIn)
}
if filter.Search != "" {
searchName := "%" + filter.Search + "%"
q = q.Where("name like ? OR show_name like ?", searchName, searchName)
}
}

err := q.Scan(ctx, &tags)
Expand Down Expand Up @@ -188,7 +192,7 @@ func (ts *tagStoreImpl) AllCategories(ctx context.Context, scope types.TagScope)
}

func (ts *tagStoreImpl) CreateTag(ctx context.Context, tag Tag) (*Tag, error) {
_, err := ts.db.Operator.Core.NewInsert().Model(&tag).Exec(ctx)
err := ts.db.Operator.Core.NewInsert().Model(&tag).Scan(ctx, &tag)
return &tag, err
}

Expand All @@ -205,6 +209,8 @@ func (ts *tagStoreImpl) SaveTags(ctx context.Context, tags []*Tag) error {
}

// SetMetaTags will delete existing tags and create new ones
//
// return updated meta tags, exclude exCategories
func (ts *tagStoreImpl) SetMetaTags(ctx context.Context, repoType types.RepositoryType, namespace, name string, tags []*Tag) (repoTags []*RepositoryTag, err error) {
repo := new(Repository)
err = ts.db.Operator.Core.NewSelect().Model(repo).
Expand All @@ -221,6 +227,7 @@ func (ts *tagStoreImpl) SetMetaTags(ctx context.Context, repoType types.Reposito
"framework": true,
"runtime_framework": true,
"evaluation": true,
"industry": true,
}
for _, tag := range repo.Tags {
if !exCategories[tag.Category] {
Expand Down Expand Up @@ -373,7 +380,7 @@ func (ts *tagStoreImpl) FindOrCreate(ctx context.Context, tag Tag) (*Tag, error)
var resTag Tag
err := ts.db.Operator.Core.NewSelect().
Model(&resTag).
Where("name = ? and category = ? and built_in = ? and scope = ?", tag.Name, tag.Category, tag.BuiltIn, tag.Scope).
Where("name = ? and category = ? and scope = ?", tag.Name, tag.Category, tag.Scope).
Scan(ctx)
if err == nil {
return &resTag, nil
Expand Down
Loading
Loading