TP-55555 | Project handler

This commit is contained in:
Lokesh Dugar
2024-07-24 12:30:35 +05:30
parent 80fe136a42
commit 0952887606
14 changed files with 104 additions and 74 deletions

2
go.mod
View File

@@ -6,6 +6,7 @@ require (
github.com/gin-contrib/zap v0.2.0
github.com/gin-gonic/gin v1.9.1
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/google/uuid v1.4.0
github.com/prometheus/client_golang v1.19.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
@@ -28,7 +29,6 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect

View File

@@ -1,11 +0,0 @@
package database
import (
"cybertron/models/db"
"gorm.io/gorm"
)
func InitProductRepository(dbClient *gorm.DB) *gorm.DB {
dbClient.AutoMigrate(&db.Product{})
return dbClient
}

View File

@@ -0,0 +1,11 @@
package database
import (
"cybertron/models/db"
"gorm.io/gorm"
)
func InitProjectRepository(dbClient *gorm.DB) *gorm.DB {
dbClient.AutoMigrate(&db.Project{})
return dbClient
}

View File

@@ -22,11 +22,11 @@ type Service struct {
}
type Handler struct {
ProductHandler *handler.ProductHandler
ProjectHandler *handler.ProjectHandler
}
type Repositories struct {
ProductRepository *gorm.DB
ProjectRepository *gorm.DB
}
func InitDependencies() *Dependencies {
@@ -51,13 +51,13 @@ func initServices() *Service {
func initRepositories(dbClient *gorm.DB) *Repositories {
return &Repositories{
ProductRepository: database.InitProductRepository(dbClient),
ProjectRepository: database.InitProjectRepository(dbClient),
}
}
func initHandlers(dbClient *gorm.DB) *Handler {
productHandler := handler.NewProductHandler(dbClient)
projectHandler := handler.NewProjectHandler(dbClient)
return &Handler{
ProductHandler: productHandler,
ProjectHandler: projectHandler,
}
}

View File

@@ -1,32 +0,0 @@
package handler
import (
"cybertron/models/db"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http"
)
type ProductHandler struct {
dbClient *gorm.DB
}
func (h *ProductHandler) ProductCreate(c *gin.Context) {
h.dbClient.Create(&db.Product{ProductReferenceId: "1", Name: "name"})
c.JSON(http.StatusOK, gin.H{
"message": "create product",
})
}
func (h *ProductHandler) ProductGet(c *gin.Context) {
var product db.Product
h.dbClient.First(&product, 1)
c.JSON(http.StatusOK, gin.H{
"message": product,
})
}
func NewProductHandler(dbClient *gorm.DB) *ProductHandler {
return &ProductHandler{
dbClient: dbClient,
}
}

View File

@@ -0,0 +1,53 @@
package handler
import (
"cybertron/models/db"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"gorm.io/gorm"
"net/http"
)
type ProjectHandler struct {
dbClient *gorm.DB
}
type ProjectBody struct {
Name string `json:"name" binding:"required"`
Team string `json:"team" binding:"required"`
}
func (h *ProjectHandler) 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
h.dbClient.Create(&db.Project{
ProjectReferenceId: uuid.New(),
Name: projectBody.Name,
Team: projectBody.Team,
})
c.JSON(http.StatusOK, gin.H{
"message": "Project created",
})
}
func (h *ProjectHandler) ProjectGet(c *gin.Context) {
var project db.Project
h.dbClient.First(&project, 1)
c.JSON(http.StatusOK, gin.H{
"message": project,
})
}
func NewProjectHandler(dbClient *gorm.DB) *ProjectHandler {
return &ProjectHandler{
dbClient: dbClient,
}
}

View File

@@ -6,7 +6,8 @@ import (
"github.com/gin-gonic/gin"
)
type HealthCheckHandler struct{}
type HealthCheckHandler struct {
}
func (h *HealthCheckHandler) Readiness(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{

View File

@@ -1,11 +0,0 @@
package router
import (
"cybertron/internal/dependencies"
"github.com/gin-gonic/gin"
)
func ProductRouter(r *gin.Engine, dep *dependencies.Dependencies) {
r.GET("/product", dep.Handler.ProductHandler.ProductCreate)
r.GET("/productGet", dep.Handler.ProductHandler.ProductGet)
}

View File

@@ -0,0 +1,16 @@
package router
import (
"cybertron/internal/dependencies"
"cybertron/internal/transport/handler"
"github.com/gin-gonic/gin"
)
func ProjectRouter(r *gin.Engine, dep *dependencies.Dependencies) {
projectHandler := handler.NewProjectHandler(dep.DBClient)
projectRouterGroup := r.Group("/api/v1")
{
projectRouterGroup.POST("/project", projectHandler.ProjectCreate)
projectRouterGroup.GET("/project", projectHandler.ProjectGet) // TODO: Can make this paginated in future
}
}

View File

@@ -30,7 +30,7 @@ func NewServer(dep *dependencies.Dependencies) *Server {
func (s *Server) router() {
router.ReadinessRouter(s.gin)
router.ProductRouter(s.gin, s.dependencies)
router.ProjectRouter(s.gin, s.dependencies)
}
func (s *Server) Start() {

View File

@@ -4,6 +4,6 @@ import "gorm.io/gorm"
type Release struct {
gorm.Model
ProductReferenceId string `gorm:"primaryKey"`
ProjectReferenceId string `gorm:"primaryKey"`
ReleaseVersion string `gorm:"column:name"`
}

View File

@@ -1,9 +0,0 @@
package db
import "gorm.io/gorm"
type Product struct {
gorm.Model
ProductReferenceId string `gorm:"primaryKey"`
Name string `gorm:"column:name"`
}

13
models/db/project.go Normal file
View File

@@ -0,0 +1,13 @@
package db
import (
"github.com/google/uuid"
"gorm.io/gorm"
)
type Project struct {
gorm.Model
ProjectReferenceId uuid.UUID `gorm:"primaryKey"`
Name string `gorm:"column:name;unique"`
Team string
}

View File

@@ -4,7 +4,6 @@ import "gorm.io/gorm"
type Roles struct {
gorm.Model
ProductReferenceId string `gorm:"primaryKey"`
ProjectReferenceId string `gorm:"primaryKey"`
Role string `gorm:"column:name"`
}