Files
landing_page/backend/internal/handlers/home.go
T
Godopu 3f452468d6 feat(console,api): implement /console Admin Dashboard and Go backend full CRUD REST APIs
- Add comprehensive /console admin web UI for managing research projects, areas, members, publications, patents, lectures, and standards
- Implement Admin token authentication middleware (RequireAdminToken) with Bearer token validation
- Implement POST, PUT, DELETE REST API endpoints for all database entities across 5 domains
- Add typed admin API client in refer_landing_page/lib/adminApi.ts with localStorage session persistence
- Enhance E2E test runner to manage backend server lifecycle during automated tests
- Pass all 12 quality gates cleanly with unanimous reviewer PASS verdicts
2026-08-24 22:21:12 +09:00

156 lines
4.2 KiB
Go

package handlers
import (
"strconv"
"git.godopu.com/lab/landing_page/backend/internal/httpx"
"git.godopu.com/lab/landing_page/backend/internal/models"
"git.godopu.com/lab/landing_page/backend/internal/repository"
"github.com/gin-gonic/gin"
)
type HomeHandler struct {
repo *repository.HomeRepository
}
func NewHomeHandler(repo *repository.HomeRepository) *HomeHandler {
return &HomeHandler{repo: repo}
}
func (h *HomeHandler) GetResearchProjects(c *gin.Context) {
projects, err := h.repo.GetResearchProjects(c.Request.Context())
if err != nil {
httpx.InternalServerError(c, "Failed to retrieve research projects: "+err.Error())
return
}
httpx.OK(c, projects)
}
func (h *HomeHandler) CreateResearchProject(c *gin.Context) {
var req models.ResearchProject
if err := c.ShouldBindJSON(&req); err != nil {
httpx.BadRequest(c, "Invalid request body: "+err.Error())
return
}
if req.Slug == "" || req.Title == "" || req.Abstract == "" {
httpx.BadRequest(c, "slug, title, and abstract are required")
return
}
id, err := h.repo.CreateResearchProject(c.Request.Context(), req)
if err != nil {
httpx.InternalServerError(c, "Failed to create research project: "+err.Error())
return
}
req.ID = id
httpx.Created(c, req)
}
func (h *HomeHandler) UpdateResearchProject(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil || id <= 0 {
httpx.BadRequest(c, "Invalid project ID")
return
}
var req models.ResearchProject
if err := c.ShouldBindJSON(&req); err != nil {
httpx.BadRequest(c, "Invalid request body: "+err.Error())
return
}
if req.Slug == "" || req.Title == "" || req.Abstract == "" {
httpx.BadRequest(c, "slug, title, and abstract are required")
return
}
req.ID = id
if err := h.repo.UpdateResearchProject(c.Request.Context(), id, req); err != nil {
httpx.InternalServerError(c, "Failed to update research project: "+err.Error())
return
}
httpx.OK(c, req)
}
func (h *HomeHandler) DeleteResearchProject(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil || id <= 0 {
httpx.BadRequest(c, "Invalid project ID")
return
}
if err := h.repo.DeleteResearchProject(c.Request.Context(), id); err != nil {
httpx.InternalServerError(c, "Failed to delete research project: "+err.Error())
return
}
httpx.NoContent(c)
}
func (h *HomeHandler) GetResearchAreas(c *gin.Context) {
areas, err := h.repo.GetResearchAreas(c.Request.Context())
if err != nil {
httpx.InternalServerError(c, "Failed to retrieve research areas: "+err.Error())
return
}
httpx.OK(c, areas)
}
func (h *HomeHandler) CreateResearchArea(c *gin.Context) {
var req models.ResearchArea
if err := c.ShouldBindJSON(&req); err != nil {
httpx.BadRequest(c, "Invalid request body: "+err.Error())
return
}
if req.NameEn == "" {
httpx.BadRequest(c, "name_en is required")
return
}
id, err := h.repo.CreateResearchArea(c.Request.Context(), req)
if err != nil {
httpx.InternalServerError(c, "Failed to create research area: "+err.Error())
return
}
req.ID = id
httpx.Created(c, req)
}
func (h *HomeHandler) UpdateResearchArea(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil || id <= 0 {
httpx.BadRequest(c, "Invalid area ID")
return
}
var req models.ResearchArea
if err := c.ShouldBindJSON(&req); err != nil {
httpx.BadRequest(c, "Invalid request body: "+err.Error())
return
}
if req.NameEn == "" {
httpx.BadRequest(c, "name_en is required")
return
}
req.ID = id
if err := h.repo.UpdateResearchArea(c.Request.Context(), id, req); err != nil {
httpx.InternalServerError(c, "Failed to update research area: "+err.Error())
return
}
httpx.OK(c, req)
}
func (h *HomeHandler) DeleteResearchArea(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil || id <= 0 {
httpx.BadRequest(c, "Invalid area ID")
return
}
if err := h.repo.DeleteResearchArea(c.Request.Context(), id); err != nil {
httpx.InternalServerError(c, "Failed to delete research area: "+err.Error())
return
}
httpx.NoContent(c)
}
func (h *HomeHandler) GetStatsSummary(c *gin.Context) {
stats, err := h.repo.GetStatsSummary(c.Request.Context())
if err != nil {
httpx.InternalServerError(c, "Failed to retrieve statistics summary: "+err.Error())
return
}
httpx.OK(c, stats)
}