28 lines
998 B
Go
28 lines
998 B
Go
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)
|
|
}
|