diff --git a/internal/handler/template.go b/internal/handler/template.go index e361761..8784d70 100644 --- a/internal/handler/template.go +++ b/internal/handler/template.go @@ -1,6 +1,7 @@ package handler import ( + "errors" "strconv" "gcy_hpc_server/internal/model" @@ -9,6 +10,7 @@ import ( "github.com/gin-gonic/gin" "go.uber.org/zap" + "gorm.io/gorm" ) type TemplateHandler struct { @@ -111,8 +113,13 @@ func (h *TemplateHandler) UpdateTemplate(c *gin.Context) { } if err := h.store.Update(c.Request.Context(), id, &req); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + h.logger.Warn("template not found for update", zap.Int64("id", id)) + server.NotFound(c, "template not found") + return + } h.logger.Error("failed to update template", zap.Int64("id", id), zap.Error(err)) - server.InternalError(c, err.Error()) + server.InternalError(c, "failed to update template") return } diff --git a/internal/store/template_store.go b/internal/store/template_store.go index 8d196ff..c5f0fa6 100644 --- a/internal/store/template_store.go +++ b/internal/store/template_store.go @@ -100,7 +100,13 @@ func (s *TemplateStore) Update(ctx context.Context, id int64, req *model.UpdateT } result := s.db.WithContext(ctx).Model(&model.JobTemplate{}).Where("id = ?", id).Updates(updates) - return result.Error + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return gorm.ErrRecordNotFound + } + return nil } // Delete removes a job template by ID. Idempotent — returns nil even if the row doesn't exist.