diff --git a/kube_cache/model/model.go b/kube_cache/model/model.go index 0efdfef..f53da92 100644 --- a/kube_cache/model/model.go +++ b/kube_cache/model/model.go @@ -69,6 +69,11 @@ func New(metrics *metrics.Metrics, ai *aiStuff.KubeAi, url string) *Database { return database } +func (db *Database) SetKubeImage(kube Kube, image []byte) error { + result := db.Db.Model(&kube).Update("image", image) + return result.Error +} + func (db *Database) GetKubeImage(id string) ([]byte, error) { var kube Kube result := db.Db.First(&kube, "id = ?", id) diff --git a/kube_cache/server/cache.go b/kube_cache/server/cache.go index 29f2896..ed67cb4 100644 --- a/kube_cache/server/cache.go +++ b/kube_cache/server/cache.go @@ -26,6 +26,7 @@ func (s *Server) Use(engine *gin.Engine) { engine.GET("/kubeRecipes", s.GetAllKubeRecipes) engine.GET("/kubeById/:id", s.GetKube) engine.GET("/kubeImageById/:id", s.GetKubeImage) + engine.GET("/kubeImageByIdNew/:id", s.RegenerateImage) engine.GET("/kubeRecipeByIds/:id1/:id2", s.GetKubeRecipe) } @@ -33,6 +34,30 @@ func (s *Server) CacheMetrics(c *gin.Context) { c.Data(200, "text/plain", []byte(s.Metrics.String())) } +func (s *Server) RegenerateImage(c *gin.Context) { + id := c.Param("id") + kube, err := s.Database.GetKube(id) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + image, err := s.Ai.GenerateDalleForKube(kube.Name) + if err != nil { + log.Printf("Error generating Dalle for kube: %s", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + err = s.Database.SetKubeImage(kube, image) + if err != nil { + log.Printf("Cannot save kube image: %s", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } + c.JSON(200, gin.H{"message": "Image regenerated"}) +} + func (s *Server) GetAllKubeRecipes(c *gin.Context) { recipes, err := s.Database.GetAllKubeRecipes() if err != nil { diff --git a/kube_cache/server/index.go b/kube_cache/server/index.go index ee67236..a0fa114 100644 --- a/kube_cache/server/index.go +++ b/kube_cache/server/index.go @@ -17,8 +17,8 @@ func (s *Server) Index(c *gin.Context) { for _, kube := range kubes { kubesHtml += fmt.Sprintf(`