You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"person-home/internal/db"
|
|
"person-home/internal/models"
|
|
)
|
|
|
|
type ProjectHandler struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
func (h *ProjectHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
tag := r.URL.Query().Get("tag")
|
|
featuredOnly := r.URL.Query().Get("featured") == "true"
|
|
limit := 0
|
|
if l := r.URL.Query().Get("limit"); l != "" {
|
|
limit, _ = strconv.Atoi(l)
|
|
}
|
|
|
|
projects, err := db.ListProjects(h.DB, tag, featuredOnly, limit)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to list projects")
|
|
return
|
|
}
|
|
if projects == nil {
|
|
projects = []models.Project{}
|
|
}
|
|
writeJSON(w, http.StatusOK, projects)
|
|
}
|
|
|
|
func (h *ProjectHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
idStr := r.PathValue("id")
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
|
|
project, err := db.GetProject(h.DB, id)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to get project")
|
|
return
|
|
}
|
|
if project == nil {
|
|
writeError(w, http.StatusNotFound, "project not found")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, project)
|
|
}
|
|
|
|
func (h *ProjectHandler) Tags(w http.ResponseWriter, r *http.Request) {
|
|
tags, err := db.AllTags(h.DB)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to get tags")
|
|
return
|
|
}
|
|
if tags == nil {
|
|
tags = []string{}
|
|
}
|
|
writeJSON(w, http.StatusOK, tags)
|
|
}
|
|
|