first commit
This commit is contained in:
146
service/HoustonService.go
Normal file
146
service/HoustonService.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"cybertron/pkg/houstonClient"
|
||||
"cybertron/pkg/kafka/producer"
|
||||
"cybertron/pkg/log"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type HoustonService struct {
|
||||
logger *log.Logger
|
||||
dbClient *gorm.DB
|
||||
kafkaProducer producer.KProducer
|
||||
houstonClient houstonClient.HoustonClientInterface
|
||||
}
|
||||
|
||||
type CreateHoustonRequest struct {
|
||||
Title string `json:"title"`
|
||||
SeverityId int `json:"severityId"`
|
||||
Description string `json:"description"`
|
||||
ReportingTeamId int `json:"reportingTeamId"`
|
||||
ResponderTeamId int `json:"responderTeamId"`
|
||||
ProductIds []int `json:"productIds"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
}
|
||||
|
||||
func NewHoustonService(logger *log.Logger, dbClient *gorm.DB, kafkaProducer producer.KProducer, houstonClient houstonClient.HoustonClientInterface) *HoustonService {
|
||||
return &HoustonService{
|
||||
logger: logger,
|
||||
dbClient: dbClient,
|
||||
kafkaProducer: kafkaProducer,
|
||||
houstonClient: houstonClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (houstonService *HoustonService) CreateHouston(c *gin.Context) {
|
||||
var request CreateHoustonRequest
|
||||
if err := c.BindJSON(&request); err != nil {
|
||||
fmt.Println("Error binding JSON:", err)
|
||||
createErrorResponse(c, http.StatusBadRequest, "Invalid request payload")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Received request payload:", request)
|
||||
|
||||
if missingFields := validateCreateHoustonRequest(request); len(missingFields) > 0 {
|
||||
fmt.Println("Missing required fields:", missingFields)
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Missing required fields",
|
||||
"fields": missingFields,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Make the POST request using houstonClient
|
||||
response, err := houstonService.houstonClient.CreateIncident(houstonClient.CreateHoustonRequest(request))
|
||||
if err != nil {
|
||||
fmt.Println("Error creating incident:", err)
|
||||
createErrorResponse(c, http.StatusInternalServerError, "Failed to create incident")
|
||||
return
|
||||
}
|
||||
|
||||
// Handle the response
|
||||
if response.StatusCode != http.StatusOK {
|
||||
fmt.Println("Failed to create incident, status code:", response.StatusCode)
|
||||
createErrorResponse(c, response.StatusCode, "Failed to create incident")
|
||||
return
|
||||
}
|
||||
|
||||
// Send the response body back to the client
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Incident created successfully",
|
||||
"data": response.Data,
|
||||
})
|
||||
}
|
||||
|
||||
func (houstonService *HoustonService) GetProducts(c *gin.Context) {
|
||||
// Get the products using the houstonClient
|
||||
products, err := houstonService.houstonClient.GetAllProducts()
|
||||
if err != nil {
|
||||
fmt.Println("Error getting products:", err)
|
||||
createErrorResponse(c, http.StatusInternalServerError, "Failed to get products")
|
||||
return
|
||||
}
|
||||
|
||||
// Send the response back to the client
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Products fetched successfully",
|
||||
"data": products.Data,
|
||||
})
|
||||
}
|
||||
|
||||
func (houstonService *HoustonService) GetResponderTeam(c *gin.Context) {
|
||||
// read query params
|
||||
productID := c.QueryArray("productID")
|
||||
|
||||
// Get the responder team using the houstonClient
|
||||
responderTeam, err := houstonService.houstonClient.GetReportingAndResponder(productID)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting responder team:", err)
|
||||
createErrorResponse(c, http.StatusInternalServerError, "Failed to get responder team")
|
||||
return
|
||||
}
|
||||
|
||||
// Send the response back to the client
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Responder team fetched successfully",
|
||||
"data": responderTeam.Data,
|
||||
})
|
||||
}
|
||||
|
||||
func validateCreateHoustonRequest(request CreateHoustonRequest) []string {
|
||||
missingFields := []string{}
|
||||
if request.Title == "" {
|
||||
missingFields = append(missingFields, "title")
|
||||
}
|
||||
if request.SeverityId == 0 {
|
||||
missingFields = append(missingFields, "severityId")
|
||||
}
|
||||
if request.Description == "" {
|
||||
missingFields = append(missingFields, "description")
|
||||
}
|
||||
if request.ReportingTeamId == 0 {
|
||||
missingFields = append(missingFields, "reportingTeamId")
|
||||
}
|
||||
if request.ResponderTeamId == 0 {
|
||||
missingFields = append(missingFields, "responderTeamId")
|
||||
}
|
||||
if len(request.ProductIds) == 0 {
|
||||
missingFields = append(missingFields, "productIds")
|
||||
}
|
||||
if request.CreatedBy == "" {
|
||||
missingFields = append(missingFields, "createdBy")
|
||||
}
|
||||
return missingFields
|
||||
}
|
||||
|
||||
func createErrorResponse(c *gin.Context, statusCode int, message string) {
|
||||
c.JSON(statusCode, gin.H{
|
||||
"message": message,
|
||||
})
|
||||
}
|
||||
@@ -5,12 +5,13 @@ import (
|
||||
"cybertron/models/db"
|
||||
"cybertron/pkg/kafka/producer"
|
||||
"cybertron/pkg/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProjectCreator struct {
|
||||
@@ -21,9 +22,10 @@ type ProjectCreator struct {
|
||||
}
|
||||
|
||||
type ProjectBody struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Team string `json:"team" binding:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Team string `json:"team" binding:"required"`
|
||||
Icon string `json:"icon"`
|
||||
GithubUrl string `json:"githubUrl" binding:"required"`
|
||||
}
|
||||
|
||||
func NewProjectCreator(logger *log.Logger, dbClient *gorm.DB, s3Client *aws.Actions, kafkaProducer producer.KProducer) *ProjectCreator {
|
||||
|
||||
Reference in New Issue
Block a user