package service import ( "cybertron/internal/client/aws" "cybertron/models/db" "cybertron/pkg/kafka/producer" "cybertron/pkg/log" "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"` } 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() //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, }) 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 projects []db.Project 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) }