96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package service
|
|
|
|
import (
|
|
"cybertron/internal/client/aws"
|
|
"cybertron/models/db"
|
|
"cybertron/pkg/kafka/producer"
|
|
"cybertron/pkg/log"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
"net/http"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// Write to database
|
|
result := pc.dbClient.Create(&db.Project{
|
|
ProjectReferenceId: uuid.New(),
|
|
Name: projectBody.Name,
|
|
Team: projectBody.Team,
|
|
})
|
|
|
|
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) {
|
|
//s3 processing
|
|
//buckets, err := pc.s3Client.S3Client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
|
|
//key := "async js.pdf"
|
|
//bucket := "navi-cd955a63c4476df0f00c1cea0e4a40d1"
|
|
//request, err := pc.s3Client.S3PresignClient.PresignGetObject(context.TODO(), &s3.GetObjectInput{
|
|
// Bucket: &bucket,
|
|
// Key: &key,
|
|
//}, func(opts *s3.PresignOptions) {
|
|
// opts.Expires = time.Duration(7 * 24 * time.Hour)
|
|
//})
|
|
//pc.logger.Info(request.URL)
|
|
//if err != nil {
|
|
// log.Log.Fatal(err.Error())
|
|
// pc.logger.Error("S3 List Buckets Failed")
|
|
//}
|
|
|
|
//kafka producer testing
|
|
|
|
//generate Random numbers in go
|
|
|
|
//err := pc.kafkaProducer.PublishEvent(rand.Int(), "kafka-stream", "", nil, encoder.JsonEncoderInstance)
|
|
//if err != nil {
|
|
// pc.logger.Info(err.Error())
|
|
//}
|
|
var projects []db.Project
|
|
pc.dbClient.Find(&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)
|
|
}
|