178 lines
4.5 KiB
Go
178 lines
4.5 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"next-terminal/server/common"
|
|
"next-terminal/server/common/maps"
|
|
"next-terminal/server/model"
|
|
"next-terminal/server/repository"
|
|
"next-terminal/server/utils"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type DatabaseAssetApi struct{}
|
|
|
|
func (api DatabaseAssetApi) AllEndpoint(c echo.Context) error {
|
|
dbType := c.QueryParam("type")
|
|
items, err := repository.DatabaseAssetRepository.FindByType(context.TODO(), dbType)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, items)
|
|
}
|
|
|
|
func (api DatabaseAssetApi) PagingEndpoint(c echo.Context) error {
|
|
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
|
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
|
name := c.QueryParam("keyword")
|
|
dbType := c.QueryParam("type")
|
|
order := c.QueryParam("order")
|
|
field := c.QueryParam("field")
|
|
|
|
items, total, err := repository.DatabaseAssetRepository.Find(context.TODO(), pageIndex, pageSize, name, dbType, order, field)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return Success(c, maps.Map{
|
|
"items": items,
|
|
"total": total,
|
|
})
|
|
}
|
|
|
|
func (api DatabaseAssetApi) CreateEndpoint(c echo.Context) error {
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
Database string `json:"database"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Description string `json:"description"`
|
|
GatewayType string `json:"gatewayType"`
|
|
GatewayId string `json:"gatewayId"`
|
|
Tags []string `json:"tags"`
|
|
}
|
|
if err := c.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
account, _ := GetCurrentAccount(c)
|
|
|
|
item := &model.DatabaseAsset{
|
|
ID: utils.UUID(),
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Host: req.Host,
|
|
Port: req.Port,
|
|
Database: req.Database,
|
|
Username: req.Username,
|
|
Password: req.Password,
|
|
Description: req.Description,
|
|
GatewayType: req.GatewayType,
|
|
GatewayId: req.GatewayId,
|
|
Tags: strings.Join(req.Tags, ","),
|
|
Owner: account.ID,
|
|
Created: common.NowJsonTime(),
|
|
Updated: common.NowJsonTime(),
|
|
}
|
|
|
|
if item.Port == 0 {
|
|
item.Port = repository.DatabaseAssetRepository.GetDBPort(item.Type)
|
|
}
|
|
|
|
if err := repository.DatabaseAssetRepository.Create(context.TODO(), item); err != nil {
|
|
return err
|
|
}
|
|
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api DatabaseAssetApi) UpdateEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
Database string `json:"database"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Description string `json:"description"`
|
|
GatewayType string `json:"gatewayType"`
|
|
GatewayId string `json:"gatewayId"`
|
|
Tags []string `json:"tags"`
|
|
}
|
|
if err := c.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
existing, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
existing.Name = req.Name
|
|
existing.Type = req.Type
|
|
existing.Host = req.Host
|
|
existing.Port = req.Port
|
|
existing.Database = req.Database
|
|
existing.Username = req.Username
|
|
if req.Password != "" {
|
|
existing.Password = req.Password
|
|
}
|
|
existing.Description = req.Description
|
|
existing.GatewayType = req.GatewayType
|
|
existing.GatewayId = req.GatewayId
|
|
existing.Tags = strings.Join(req.Tags, ",")
|
|
existing.Updated = common.NowJsonTime()
|
|
|
|
if err := repository.DatabaseAssetRepository.UpdateById(context.TODO(), &existing); err != nil {
|
|
return err
|
|
}
|
|
|
|
return Success(c, existing)
|
|
}
|
|
|
|
func (api DatabaseAssetApi) DeleteEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
if err := repository.DatabaseAssetRepository.DeleteById(context.TODO(), id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api DatabaseAssetApi) GetEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
item, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api DatabaseAssetApi) DecryptEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
item, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, maps.Map{
|
|
"id": id,
|
|
"password": item.Password,
|
|
})
|
|
}
|
|
|
|
func (api DatabaseAssetApi) TagsEndpoint(c echo.Context) error {
|
|
tags, err := repository.DatabaseAssetRepository.FindAllTags(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, tags)
|
|
}
|