2024-07-27 17:00:47 +05:30
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2024-07-29 15:46:17 +05:30
|
|
|
"cybertron/internal/client/aws"
|
2024-07-27 17:00:47 +05:30
|
|
|
"cybertron/models/db"
|
2024-07-29 15:46:17 +05:30
|
|
|
"cybertron/pkg/kafka/producer"
|
2024-07-27 17:00:47 +05:30
|
|
|
"cybertron/pkg/log"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"gorm.io/gorm"
|
2024-08-16 06:26:21 +05:30
|
|
|
"math/big"
|
2024-07-27 17:00:47 +05:30
|
|
|
"net/http"
|
2024-08-16 06:26:21 +05:30
|
|
|
"strings"
|
2024-07-27 17:00:47 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ProjectCreator struct {
|
2024-07-29 15:46:17 +05:30
|
|
|
logger *log.Logger
|
|
|
|
|
dbClient *gorm.DB
|
|
|
|
|
s3Client *aws.Actions
|
|
|
|
|
kafkaProducer producer.KProducer
|
2024-07-27 17:00:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ProjectBody struct {
|
|
|
|
|
Name string `json:"name" binding:"required"`
|
|
|
|
|
Team string `json:"team" binding:"required"`
|
2024-09-11 12:03:08 +05:30
|
|
|
Icon string `json:"icon"`
|
2024-07-27 17:00:47 +05:30
|
|
|
}
|
|
|
|
|
|
2024-07-29 15:46:17 +05:30
|
|
|
func NewProjectCreator(logger *log.Logger, dbClient *gorm.DB, s3Client *aws.Actions, kafkaProducer producer.KProducer) *ProjectCreator {
|
2024-07-27 17:00:47 +05:30
|
|
|
return &ProjectCreator{
|
2024-07-29 15:46:17 +05:30
|
|
|
logger: logger,
|
|
|
|
|
dbClient: dbClient,
|
|
|
|
|
s3Client: s3Client,
|
|
|
|
|
kafkaProducer: kafkaProducer,
|
2024-07-27 17:00:47 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2024-07-27 17:00:47 +05:30
|
|
|
|
|
|
|
|
// Write to database
|
2024-07-27 18:57:02 +05:30
|
|
|
result := pc.dbClient.Create(&db.Project{
|
2024-08-16 06:26:21 +05:30
|
|
|
ProjectReferenceId: i.String(),
|
2024-07-27 17:00:47 +05:30
|
|
|
Name: projectBody.Name,
|
|
|
|
|
Team: projectBody.Team,
|
2024-09-11 12:03:08 +05:30
|
|
|
Icon: projectBody.Icon,
|
2024-07-27 17:00:47 +05:30
|
|
|
})
|
|
|
|
|
|
2024-07-27 18:57:02 +05:30
|
|
|
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"})
|
2024-07-27 18:57:02 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 17:00:47 +05:30
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"message": "Project created",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 18:57:02 +05:30
|
|
|
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)
|
2024-07-27 17:00:47 +05:30
|
|
|
}
|