128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
package services
|
||
|
||
import (
|
||
"time"
|
||
|
||
"mengyaping-backend/models"
|
||
"mengyaping-backend/storage"
|
||
"mengyaping-backend/utils"
|
||
)
|
||
|
||
// WebsiteService 网站服务
|
||
type WebsiteService struct {
|
||
storage *storage.Storage
|
||
}
|
||
|
||
// NewWebsiteService 创建网站服务
|
||
func NewWebsiteService() *WebsiteService {
|
||
return &WebsiteService{
|
||
storage: storage.GetStorage(),
|
||
}
|
||
}
|
||
|
||
// CreateWebsite 创建网站
|
||
func (s *WebsiteService) CreateWebsite(req models.CreateWebsiteRequest) (*models.Website, error) {
|
||
website := models.Website{
|
||
ID: utils.GenerateID(),
|
||
Name: req.Name,
|
||
Group: req.Group,
|
||
URLs: make([]models.URLInfo, 0),
|
||
CreatedAt: time.Now(),
|
||
UpdatedAt: time.Now(),
|
||
}
|
||
|
||
for _, url := range req.URLs {
|
||
urlInfo := models.URLInfo{
|
||
ID: utils.GenerateShortID(),
|
||
URL: url,
|
||
}
|
||
website.URLs = append(website.URLs, urlInfo)
|
||
}
|
||
|
||
if err := s.storage.AddWebsite(website); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 立即检测该网站
|
||
go GetMonitorService().CheckWebsiteNow(website.ID)
|
||
|
||
return &website, nil
|
||
}
|
||
|
||
// GetWebsite 获取网站
|
||
func (s *WebsiteService) GetWebsite(id string) *models.Website {
|
||
return s.storage.GetWebsite(id)
|
||
}
|
||
|
||
// GetAllWebsites 获取所有网站
|
||
func (s *WebsiteService) GetAllWebsites() []models.Website {
|
||
return s.storage.GetWebsites()
|
||
}
|
||
|
||
// UpdateWebsite 更新网站
|
||
func (s *WebsiteService) UpdateWebsite(id string, req models.UpdateWebsiteRequest) (*models.Website, error) {
|
||
website := s.storage.GetWebsite(id)
|
||
if website == nil {
|
||
return nil, nil
|
||
}
|
||
|
||
if req.Name != "" {
|
||
website.Name = req.Name
|
||
}
|
||
if req.Group != "" {
|
||
website.Group = req.Group
|
||
}
|
||
if len(req.URLs) > 0 {
|
||
// 保留已有URL的ID,添加新URL
|
||
existingURLs := make(map[string]models.URLInfo)
|
||
for _, u := range website.URLs {
|
||
existingURLs[u.URL] = u
|
||
}
|
||
|
||
newURLs := make([]models.URLInfo, 0)
|
||
for _, url := range req.URLs {
|
||
if existing, ok := existingURLs[url]; ok {
|
||
newURLs = append(newURLs, existing)
|
||
} else {
|
||
newURLs = append(newURLs, models.URLInfo{
|
||
ID: utils.GenerateShortID(),
|
||
URL: url,
|
||
})
|
||
}
|
||
}
|
||
website.URLs = newURLs
|
||
}
|
||
|
||
website.UpdatedAt = time.Now()
|
||
|
||
if err := s.storage.UpdateWebsite(*website); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return website, nil
|
||
}
|
||
|
||
// DeleteWebsite 删除网站
|
||
func (s *WebsiteService) DeleteWebsite(id string) error {
|
||
return s.storage.DeleteWebsite(id)
|
||
}
|
||
|
||
// GetGroups 获取所有分组
|
||
func (s *WebsiteService) GetGroups() []models.Group {
|
||
return s.storage.GetGroups()
|
||
}
|
||
|
||
// AddGroup 添加分组
|
||
func (s *WebsiteService) AddGroup(name string) (*models.Group, error) {
|
||
group := models.Group{
|
||
ID: utils.GenerateShortID(),
|
||
Name: name,
|
||
}
|
||
|
||
if err := s.storage.AddGroup(group); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &group, nil
|
||
}
|