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

package organization
import (
"net/http"
"strconv"
"task-track-backend/model"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func (h *OrganizationHandler) UpdateOrganization(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "Invalid organization ID",
})
return
}
var updateData model.Organization
if err := c.ShouldBindJSON(&updateData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "Invalid request data",
"error": err.Error(),
})
return
}
// 检查机构是否存在
var organization model.Organization
if err := h.db.First(&organization, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"message": "Organization not found",
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": "Failed to find organization",
"error": err.Error(),
})
return
}
// 更新机构信息
if err := h.db.Model(&organization).Updates(updateData).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"message": "Failed to update organization",
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "Organization updated successfully",
"data": organization,
})
}