Files
cybertron/service/ProjectCreator.go

82 lines
2.0 KiB
Go
Raw Normal View History

package service
import (
"cybertron/internal/client/aws"
"cybertron/models/db"
"cybertron/pkg/kafka/producer"
"cybertron/pkg/log"
2024-08-16 06:26:21 +05:30
"math/big"
"net/http"
2024-08-16 06:26:21 +05:30
"strings"
2024-09-12 02:59:54 +05:30
"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 {
2024-09-12 02:59:54 +05:30
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
}
2024-08-16 06:26:21 +05:30
var uuidGenerated = uuid.New()
2024-08-16 13:47:08 +05:30
//converting uuid to all numbers since sentry only supports
//all digit project uuid
2024-08-16 06:26:21 +05:30
var i big.Int
i.SetString(strings.Replace(uuidGenerated.String(), "-", "", 4), 16)
// Write to database
result := pc.dbClient.Create(&db.Project{
2024-08-16 06:26:21 +05:30
ProjectReferenceId: i.String(),
Name: projectBody.Name,
Team: projectBody.Team,
Icon: projectBody.Icon,
})
if result.Error != nil {
2024-07-29 15:59:13 +05:30
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)
}