66 lines
1.4 KiB
Go
66 lines
1.4 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) ProjectCreate(c *gin.Context) {
|
|
var projectBody ProjectBody
|
|
if err := c.BindJSON(&projectBody); err != nil {
|
|
c.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 {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error(), "message": "Failed to create project"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Project created",
|
|
})
|
|
}
|
|
|
|
func (pc *ProjectCreator) ProjectGet(c *gin.Context) {
|
|
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)
|
|
}
|