text.go 1.4 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
package serverService

import (
	"github.com/easysoft/zendata/src/model"
	"github.com/easysoft/zendata/src/server/repo"
	logUtils "github.com/easysoft/zendata/src/utils/log"
	stringUtils "github.com/easysoft/zendata/src/utils/string"
)

type TextService struct {
	textRepo *serverRepo.TextRepo
	resService *ResService
}

func (s *TextService) List() (list []*model.ZdText) {
	text := s.resService.LoadRes("text")
	list, _ = s.textRepo.List()

	s.saveResToDB(text, list)
	list, _ = s.textRepo.List()

	return
}

func (s *TextService) Get(id int) (text model.ZdText) {
	text, _ = s.textRepo.Get(uint(id))

	return
}

func (s *TextService) Save(text *model.ZdText) (err error) {
	err = s.textRepo.Save(text)

	return
}

func (s *TextService) Remove(id int) (err error) {
	err = s.textRepo.Remove(uint(id))
	if err != nil {
		return
	}

	text, _ := s.textRepo.Get(uint(id))
	logUtils.PrintTo(text.Path)
	//fileUtils.RemoveExist(text.Path)

	return
}

func (s *TextService) saveResToDB(text []model.ResFile, list []*model.ZdText) (err error) {
	names := make([]string, 0)
	for _, item := range list {
		names = append(names, item.Path)
	}

	for _, item := range text {
		if !stringUtils.FindInArrBool(item.Path, names) {
			text := model.ZdText{Path: item.Path, Name: item.Name, Title: item.Title}
			s.textRepo.Save(&text)
		}
	}

	return
}

func NewTextService(textRepo *serverRepo.TextRepo) *TextService {
	return &TextService{textRepo: textRepo}
}