mirror of https://github.com/go-gitea/gitea
Never use /api/v1 from Gitea UI Pages (#19318)
Reusing `/api/v1` from Gitea UI Pages have pros and cons. Pros: 1) Less code copy Cons: 1) API/v1 have to support shared session with page requests. 2) You need to consider for each other when you want to change something about api/v1 or page. This PR moves all dependencies to API/v1 from UI Pages. Partially replace #16052pull/19244/head
parent
bb7e0619c3
commit
783a021889
@ -0,0 +1,46 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package integrations |
||||
|
||||
import ( |
||||
"net/http" |
||||
"net/url" |
||||
"testing" |
||||
|
||||
api "code.gitea.io/gitea/modules/structs" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestTopicSearch(t *testing.T) { |
||||
defer prepareTestEnv(t)() |
||||
searchURL, _ := url.Parse("/explore/topics/search") |
||||
var topics struct { |
||||
TopicNames []*api.TopicResponse `json:"topics"` |
||||
} |
||||
|
||||
query := url.Values{"page": []string{"1"}, "limit": []string{"4"}} |
||||
|
||||
searchURL.RawQuery = query.Encode() |
||||
res := MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) |
||||
DecodeJSON(t, res, &topics) |
||||
assert.Len(t, topics.TopicNames, 4) |
||||
assert.EqualValues(t, "6", res.Header().Get("x-total-count")) |
||||
|
||||
query.Add("q", "topic") |
||||
searchURL.RawQuery = query.Encode() |
||||
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) |
||||
DecodeJSON(t, res, &topics) |
||||
assert.Len(t, topics.TopicNames, 2) |
||||
|
||||
query.Set("q", "database") |
||||
searchURL.RawQuery = query.Encode() |
||||
res = MakeRequest(t, NewRequest(t, "GET", searchURL.String()), http.StatusOK) |
||||
DecodeJSON(t, res, &topics) |
||||
if assert.Len(t, topics.TopicNames, 1) { |
||||
assert.EqualValues(t, 2, topics.TopicNames[0].ID) |
||||
assert.EqualValues(t, "database", topics.TopicNames[0].Name) |
||||
assert.EqualValues(t, 1, topics.TopicNames[0].RepoCount) |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils |
||||
|
||||
import ( |
||||
"code.gitea.io/gitea/models/db" |
||||
"code.gitea.io/gitea/modules/context" |
||||
"code.gitea.io/gitea/modules/convert" |
||||
) |
||||
|
||||
// GetListOptions returns list options using the page and limit parameters
|
||||
func GetListOptions(ctx *context.APIContext) db.ListOptions { |
||||
return db.ListOptions{ |
||||
Page: ctx.FormInt("page"), |
||||
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")), |
||||
} |
||||
} |
@ -0,0 +1,42 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package explore |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
repo_model "code.gitea.io/gitea/models/repo" |
||||
"code.gitea.io/gitea/modules/context" |
||||
"code.gitea.io/gitea/modules/convert" |
||||
api "code.gitea.io/gitea/modules/structs" |
||||
) |
||||
|
||||
// TopicSearch search for creating topic
|
||||
func TopicSearch(ctx *context.Context) { |
||||
opts := &repo_model.FindTopicOptions{ |
||||
Keyword: ctx.FormString("q"), |
||||
ListOptions: db.ListOptions{ |
||||
Page: ctx.FormInt("page"), |
||||
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")), |
||||
}, |
||||
} |
||||
|
||||
topics, total, err := repo_model.FindTopics(opts) |
||||
if err != nil { |
||||
ctx.Error(http.StatusInternalServerError) |
||||
return |
||||
} |
||||
|
||||
topicResponses := make([]*api.TopicResponse, len(topics)) |
||||
for i, topic := range topics { |
||||
topicResponses[i] = convert.ToTopicResponse(topic) |
||||
} |
||||
|
||||
ctx.SetTotalCountHeader(total) |
||||
ctx.JSON(http.StatusOK, map[string]interface{}{ |
||||
"topics": topicResponses, |
||||
}) |
||||
} |
@ -0,0 +1,44 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
user_model "code.gitea.io/gitea/models/user" |
||||
"code.gitea.io/gitea/modules/context" |
||||
"code.gitea.io/gitea/modules/convert" |
||||
) |
||||
|
||||
// Search search users
|
||||
func Search(ctx *context.Context) { |
||||
listOptions := db.ListOptions{ |
||||
Page: ctx.FormInt("page"), |
||||
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")), |
||||
} |
||||
|
||||
users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ |
||||
Actor: ctx.Doer, |
||||
Keyword: ctx.FormTrim("q"), |
||||
UID: ctx.FormInt64("uid"), |
||||
Type: user_model.UserTypeIndividual, |
||||
ListOptions: listOptions, |
||||
}) |
||||
if err != nil { |
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ |
||||
"ok": false, |
||||
"error": err.Error(), |
||||
}) |
||||
return |
||||
} |
||||
|
||||
ctx.SetTotalCountHeader(maxResults) |
||||
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{ |
||||
"ok": true, |
||||
"data": convert.ToUsers(ctx.Doer, users), |
||||
}) |
||||
} |
@ -0,0 +1,41 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/models/db" |
||||
"code.gitea.io/gitea/modules/context" |
||||
"code.gitea.io/gitea/modules/convert" |
||||
) |
||||
|
||||
// GetStopwatches get all stopwatches
|
||||
func GetStopwatches(ctx *context.Context) { |
||||
sws, err := models.GetUserStopwatches(ctx.Doer.ID, db.ListOptions{ |
||||
Page: ctx.FormInt("page"), |
||||
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")), |
||||
}) |
||||
if err != nil { |
||||
ctx.Error(http.StatusInternalServerError, err.Error()) |
||||
return |
||||
} |
||||
|
||||
count, err := models.CountUserStopwatches(ctx.Doer.ID) |
||||
if err != nil { |
||||
ctx.Error(http.StatusInternalServerError, err.Error()) |
||||
return |
||||
} |
||||
|
||||
apiSWs, err := convert.ToStopWatches(sws) |
||||
if err != nil { |
||||
ctx.Error(http.StatusInternalServerError, err.Error()) |
||||
return |
||||
} |
||||
|
||||
ctx.SetTotalCountHeader(count) |
||||
ctx.JSON(http.StatusOK, apiSWs) |
||||
} |
Loading…
Reference in new issue