phase 3 bug repositories fixes
This commit is contained in:
@@ -15,6 +15,24 @@ import (
|
||||
"github.com/forgeo/forgebucket/internal/models"
|
||||
)
|
||||
|
||||
// repoResponse enriches a Repository with derived fields the frontend needs.
|
||||
type repoResponse struct {
|
||||
models.Repository
|
||||
OwnerName string `json:"ownerName"`
|
||||
IsEmpty bool `json:"isEmpty"`
|
||||
}
|
||||
|
||||
func (h *RepoHandler) withOwnerName(repo *models.Repository) repoResponse {
|
||||
var owner models.User
|
||||
h.db.ID(repo.OwnerID).Get(&owner)
|
||||
gitdomain.SetRepoRoot(h.cfg.RepoRoot)
|
||||
return repoResponse{
|
||||
Repository: *repo,
|
||||
OwnerName: owner.Username,
|
||||
IsEmpty: gitdomain.IsEmpty(repo.DiskPath),
|
||||
}
|
||||
}
|
||||
|
||||
type RepoHandler struct {
|
||||
db *xorm.Engine
|
||||
cfg *config.Config
|
||||
@@ -32,10 +50,16 @@ func (h *RepoHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
jsonError(w, "could not list repositories", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if repos == nil {
|
||||
repos = []models.Repository{}
|
||||
|
||||
// Fetch owner username once (all repos belong to the same user in this query)
|
||||
var owner models.User
|
||||
h.db.ID(userID).Get(&owner)
|
||||
|
||||
result := make([]repoResponse, len(repos))
|
||||
for i, repo := range repos {
|
||||
result[i] = repoResponse{Repository: repo, OwnerName: owner.Username}
|
||||
}
|
||||
jsonOK(w, repos)
|
||||
jsonOK(w, result)
|
||||
}
|
||||
|
||||
func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -80,7 +104,7 @@ func (h *RepoHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(repo)
|
||||
json.NewEncoder(w).Encode(h.withOwnerName(repo))
|
||||
}
|
||||
|
||||
func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -88,7 +112,7 @@ func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
jsonOK(w, repo)
|
||||
jsonOK(w, h.withOwnerName(repo))
|
||||
}
|
||||
|
||||
func (h *RepoHandler) Tree(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user