github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/initial/local_migrations/m_template.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package local_migrations
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  	"strings"
    28  
    29  	"github.com/e154/smart-home/adaptors"
    30  	"github.com/e154/smart-home/common"
    31  	m "github.com/e154/smart-home/models"
    32  	. "github.com/e154/smart-home/system/initial/assertions"
    33  )
    34  
    35  type MigrationTemplates struct {
    36  	adaptors *adaptors.Adaptors
    37  }
    38  
    39  func NewMigrationTemplates(adaptors *adaptors.Adaptors) *MigrationTemplates {
    40  	return &MigrationTemplates{
    41  		adaptors: adaptors,
    42  	}
    43  }
    44  
    45  func (t *MigrationTemplates) Up(ctx context.Context, adaptors *adaptors.Adaptors) (err error) {
    46  
    47  	if adaptors != nil {
    48  		t.adaptors = adaptors
    49  	}
    50  	dataDir := filepath.Join("data", "templates")
    51  
    52  	var files []os.DirEntry
    53  	files, err = os.ReadDir(dataDir)
    54  	So(err, ShouldBeNil)
    55  
    56  	for _, file := range files {
    57  		if file.IsDir() {
    58  			continue
    59  		}
    60  		name := strings.Replace(file.Name(), ".html", "", -1)
    61  
    62  		switch name {
    63  		case "main", "message", "body", "callout", "footer", "contacts", "social", "facebook", "google", "header", "password_reset", "privacy", "register_admin_created", "title", "twitter", "vk":
    64  			continue
    65  		default:
    66  			//fmt.Printf("unknown file %v", file.Name())
    67  			return
    68  		}
    69  	}
    70  
    71  	fileNames := []string{"main", "message", "body", "callout", "footer", "contacts", "social", "facebook", "google", "header", "password_reset", "privacy", "register_admin_created", "title", "twitter", "vk"}
    72  
    73  	for _, name := range fileNames {
    74  
    75  		templateType := m.TemplateTypeItem
    76  		var parent *string
    77  
    78  		switch name {
    79  		case "header":
    80  			parent = common.String("main")
    81  		case "main":
    82  		case "body":
    83  			parent = common.String("message")
    84  		case "google":
    85  			parent = common.String("social")
    86  		case "message":
    87  			parent = common.String("main")
    88  		case "callout":
    89  			parent = common.String("main")
    90  		case "contacts":
    91  			parent = common.String("footer")
    92  		case "footer":
    93  			parent = common.String("main")
    94  		case "social":
    95  			parent = common.String("footer")
    96  		case "facebook":
    97  			parent = common.String("social")
    98  		case "privacy":
    99  			parent = common.String("main")
   100  		case "title":
   101  			parent = common.String("message")
   102  		case "twitter":
   103  			parent = common.String("social")
   104  		case "vk":
   105  			parent = common.String("social")
   106  		case "password_reset":
   107  			templateType = m.TemplateTypeTemplate
   108  		case "register_admin_created":
   109  			templateType = m.TemplateTypeTemplate
   110  		}
   111  
   112  		var tpl *m.Template
   113  		if templateType == m.TemplateTypeTemplate {
   114  			tpl, err = t.adaptors.Template.GetByName(ctx, name)
   115  		} else {
   116  			tpl, err = t.adaptors.Template.GetItemByName(ctx, name)
   117  		}
   118  
   119  		if err == nil || tpl != nil {
   120  			continue
   121  		}
   122  
   123  		var b []byte
   124  		b, err = ioutil.ReadFile(filepath.Join(dataDir, fmt.Sprintf("%s.html", name)))
   125  		So(err, ShouldBeNil)
   126  
   127  		template := &m.Template{
   128  			Name:       name,
   129  			Content:    string(b),
   130  			Status:     m.TemplateStatusActive,
   131  			Type:       templateType,
   132  			ParentName: parent,
   133  		}
   134  
   135  		err = t.adaptors.Template.Create(ctx, template)
   136  		So(err, ShouldBeNil)
   137  	}
   138  
   139  	return
   140  }