* TP-55555 | document client and kafka integration * TP-55555 | introduce service concept refactor code
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"cybertron/models/db"
|
|
"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
|
|
}
|
|
|
|
type ProjectBody struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Team string `json:"team" binding:"required"`
|
|
}
|
|
|
|
func NewProjectCreator(logger *log.Logger, dbClient *gorm.DB) *ProjectCreator {
|
|
return &ProjectCreator{
|
|
logger: logger,
|
|
dbClient: dbClient,
|
|
}
|
|
}
|
|
|
|
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
|
|
pc.dbClient.Create(&db.Project{
|
|
ProjectReferenceId: uuid.New(),
|
|
Name: projectBody.Name,
|
|
Team: projectBody.Team,
|
|
})
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"message": "Project created",
|
|
})
|
|
}
|
|
|
|
func (pc *ProjectCreator) GetProject(ctx *gin.Context) {
|
|
var project db.Project
|
|
pc.dbClient.First(&project, 1)
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"message": project,
|
|
})
|
|
}
|