init: Initial commit
This commit is contained in:
44
app/services/application.go
Normal file
44
app/services/application.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/mgmt-api/models"
|
||||
"git.secnex.io/secnex/mgmt-api/repositories"
|
||||
"git.secnex.io/secnex/mgmt-api/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func CreateNewApplication(name string, tenantID string, expiresAt *time.Time) *utils.HTTPResponse {
|
||||
tenant, err := repositories.GetTenantByID(tenantID)
|
||||
if err != nil {
|
||||
masterlog.Error("Failed to get tenant", map[string]interface{}{"error": err.Error()})
|
||||
return utils.NewHTTPResponse(http.StatusInternalServerError, &fiber.Map{"message": "Failed to get tenant"}, "", nil, nil)
|
||||
}
|
||||
if tenant == nil {
|
||||
return utils.NewHTTPResponse(http.StatusNotFound, &fiber.Map{"message": "Tenant not found"}, "", nil, nil)
|
||||
}
|
||||
|
||||
applicationID := uuid.New()
|
||||
applicationSecret := utils.GenerateRandomString(32)
|
||||
|
||||
application := &models.Application{
|
||||
Name: name,
|
||||
TenantID: tenant.ID,
|
||||
ExpiresAt: expiresAt,
|
||||
Secret: applicationSecret,
|
||||
ID: applicationID,
|
||||
}
|
||||
if err := repositories.CreateApplication(application); err != nil {
|
||||
masterlog.Error("Failed to create application", map[string]interface{}{"error": err.Error()})
|
||||
if utils.IsDuplicateKeyError(err) {
|
||||
return utils.NewHTTPResponse(http.StatusBadRequest, &fiber.Map{"message": "Application already exists"}, "", nil, nil)
|
||||
}
|
||||
return utils.NewHTTPResponse(http.StatusInternalServerError, &fiber.Map{"message": "Failed to create application"}, "", nil, nil)
|
||||
}
|
||||
|
||||
return utils.NewHTTPResponse(http.StatusOK, &fiber.Map{"message": "Application created", "id": applicationID, "secret": applicationSecret}, "", nil, nil)
|
||||
}
|
||||
27
app/services/tenant.go
Normal file
27
app/services/tenant.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.secnex.io/secnex/mgmt-api/models"
|
||||
"git.secnex.io/secnex/mgmt-api/repositories"
|
||||
"git.secnex.io/secnex/mgmt-api/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func CreateTenant(name string) *utils.HTTPResponse {
|
||||
tenant := &models.Tenant{
|
||||
Name: name,
|
||||
}
|
||||
createdOrFoundTenant, err := repositories.UpsertTenant(tenant)
|
||||
if err != nil {
|
||||
if utils.IsDuplicateKeyError(err) {
|
||||
return utils.NewHTTPResponse(http.StatusBadRequest, &fiber.Map{"message": "Tenant already exists"}, "", nil, nil)
|
||||
}
|
||||
return utils.NewHTTPResponse(http.StatusInternalServerError, &fiber.Map{"message": "Failed to create tenant"}, "", nil, nil)
|
||||
}
|
||||
if createdOrFoundTenant == nil {
|
||||
return utils.NewHTTPResponse(http.StatusInternalServerError, &fiber.Map{"message": "Failed to create tenant"}, "", nil, nil)
|
||||
}
|
||||
return utils.NewHTTPResponse(http.StatusOK, &fiber.Map{"message": "Tenant created", "id": createdOrFoundTenant.ID, "name": createdOrFoundTenant.Name}, "", nil, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user