package service import ( "crypto/rand" "cybertron/internal/client/aws" "cybertron/models/db" "cybertron/pkg/kafka/producer" "cybertron/pkg/log" "encoding/hex" "errors" "math/big" "net/http" "strings" "github.com/gin-gonic/gin" "github.com/google/uuid" "gorm.io/gorm" ) type ProjectCreator struct { logger *log.Logger dbClient *gorm.DB s3Client *aws.Actions kafkaProducer producer.KProducer } type ProjectBody struct { Name string `json:"name" binding:"required"` Team string `json:"team" binding:"required"` Icon string `json:"icon"` GithubUrl string `json:"githubUrl" binding:"required"` Account string `json:"account" binding:"required"` } type UpdateProjectBody struct { Icon string `json:"logoUrl" binding:"required"` GithubUrl string `json:"githubUrl" binding:"required"` IgnorePatterns []string `json:"ignorePatterns" binding:""` Account string `json:"account" binding:"omitempty"` } func NewProjectCreator(logger *log.Logger, dbClient *gorm.DB, s3Client *aws.Actions, kafkaProducer producer.KProducer) *ProjectCreator { return &ProjectCreator{ logger: logger, dbClient: dbClient, s3Client: s3Client, kafkaProducer: kafkaProducer, } } func (pc *ProjectCreator) CreateProject(ctx *gin.Context) { var projectBody ProjectBody if err := ctx.BindJSON(&projectBody); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{ "message": "Invalid Request", }) return } var uuidGenerated = uuid.New() secret := make([]byte, 16) _, err := rand.Read(secret) if err != nil { // handle error here return } //converting uuid to all numbers since sentry only supports //all digit project uuid var i big.Int i.SetString(strings.Replace(uuidGenerated.String(), "-", "", 4), 16) // Write to database result := pc.dbClient.Create(&db.Project{ ProjectReferenceId: i.String(), Name: projectBody.Name, Team: projectBody.Team, Icon: projectBody.Icon, GithubUrl: projectBody.GithubUrl, Secret: hex.EncodeToString(secret), Account: projectBody.Account, }) if result.Error != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error(), "message": "Failed to create project"}) return } ctx.JSON(http.StatusOK, gin.H{ "message": "Project created", }) } func (pc *ProjectCreator) ProjectGet(c *gin.Context) { var projectId = c.Param("id") var projects []db.Project // If projectId is provided, find the project by ID if projectId != "" { var project db.Project if result := pc.dbClient.Where("project_reference_id = ?", projectId).First(&project); result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"}) } else { c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) } return } // Return the specific project c.JSON(http.StatusOK, project) return } // If no projectId is provided, return all projects if result := pc.dbClient.Find(&projects); result.Error != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) return } c.JSON(http.StatusOK, projects) } func (pc *ProjectCreator) ProjectUpdate(c *gin.Context) { var projectId = c.Param("id") var project db.Project var updateProjectBody UpdateProjectBody // Find the project by ID if result := pc.dbClient.Where("project_reference_id = ?", projectId).First(&project); result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"}) } else { c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) } return } // Bind the request body to the project object if err := c.ShouldBindJSON(&updateProjectBody); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Update the project in the database if result := pc.dbClient.Where("project_reference_id = ?", projectId).Updates(&db.Project{ GithubUrl: updateProjectBody.GithubUrl, Icon: updateProjectBody.Icon, IgnorePatterns: updateProjectBody.IgnorePatterns, Account: updateProjectBody.Account, }); result.Error != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) return } // Return the updated project c.JSON(http.StatusOK, "Project updated") }