Files
at-container-registry/pkg/appview/handlers/user.go
T
2025-10-08 20:50:27 -05:00

79 lines
1.8 KiB
Go

package handlers
import (
"database/sql"
"html/template"
"net/http"
"atcr.io/pkg/appview/db"
"github.com/gorilla/mux"
)
// UserPageHandler handles the public user page showing all images for a user
type UserPageHandler struct {
DB *sql.DB
Templates *template.Template
RegistryURL string
}
func (h *UserPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
handle := vars["handle"]
// Look up user by handle
viewedUser, err := db.GetUserByHandle(h.DB, handle)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if viewedUser == nil {
http.Error(w, "User not found", http.StatusNotFound)
return
}
// Fetch repositories for this user
repos, err := db.GetUserRepositories(h.DB, viewedUser.DID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Convert to RepoCardData for template
cards := make([]db.RepoCardData, 0, len(repos))
for _, repo := range repos {
stats, err := db.GetRepositoryStats(h.DB, viewedUser.DID, repo.Name)
if err != nil {
// Continue with zero stats on error
stats = &db.RepositoryStats{
DID: viewedUser.DID,
Repository: repo.Name,
}
}
cards = append(cards, db.RepoCardData{
OwnerHandle: viewedUser.Handle,
Repository: repo.Name,
Title: repo.Title,
Description: repo.Description,
IconURL: repo.IconURL,
StarCount: stats.StarCount,
PullCount: stats.PullCount,
})
}
data := struct {
PageData
ViewedUser *db.User // User whose page we're viewing
Repositories []db.RepoCardData
}{
PageData: NewPageData(r, h.RegistryURL),
ViewedUser: viewedUser,
Repositories: cards,
}
if err := h.Templates.ExecuteTemplate(w, "user", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}