Files
nl-blogs/server/handlers/services.go
2026-01-16 08:29:57 +08:00

145 lines
3.9 KiB
Go

package handlers
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/niangaodev/art-code/models"
"github.com/niangaodev/art-code/repositories"
)
// GetTestimonials 获取客户评价
func GetTestimonials(c *gin.Context) {
testimonials, err := repositories.GetTestimonials()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch testimonials"})
return
}
c.JSON(http.StatusOK, testimonials)
}
// GetPartners 获取合作伙伴
func GetPartners(c *gin.Context) {
partners, err := repositories.GetPartners()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch partners"})
return
}
c.JSON(http.StatusOK, partners)
}
// AdminCreateTestimonial 创建客户评价
func AdminCreateTestimonial(c *gin.Context) {
var t models.Testimonial
if err := c.ShouldBindJSON(&t); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
if err := repositories.CreateTestimonial(&t); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create testimonial"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Testimonial created successfully"})
}
// AdminUpdateTestimonial 更新客户评价
func AdminUpdateTestimonial(c *gin.Context) {
idStr := c.Param("id")
var id uint
if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
return
}
var t models.Testimonial
if err := c.ShouldBindJSON(&t); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
t.ID = id
if err := repositories.UpdateTestimonial(&t); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update testimonial"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Testimonial updated successfully"})
}
// AdminDeleteTestimonial 删除客户评价
func AdminDeleteTestimonial(c *gin.Context) {
idStr := c.Param("id")
var id uint
if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
return
}
if err := repositories.DeleteTestimonial(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete testimonial"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Testimonial deleted successfully"})
}
// AdminCreatePartner 创建合作伙伴
func AdminCreatePartner(c *gin.Context) {
var p models.Partner
if err := c.ShouldBindJSON(&p); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
if err := repositories.CreatePartner(&p); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create partner"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Partner created successfully"})
}
// AdminUpdatePartner 更新合作伙伴
func AdminUpdatePartner(c *gin.Context) {
idStr := c.Param("id")
var id uint
if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
return
}
var p models.Partner
if err := c.ShouldBindJSON(&p); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
p.ID = id
if err := repositories.UpdatePartner(&p); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update partner"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Partner updated successfully"})
}
// AdminDeletePartner 删除合作伙伴
func AdminDeletePartner(c *gin.Context) {
idStr := c.Param("id")
var id uint
if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
return
}
if err := repositories.DeletePartner(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete partner"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Partner deleted successfully"})
}