refactor site config repo to general config repo

This commit is contained in:
Niko Abeler 2023-07-19 20:25:42 +02:00
parent 1bf817465c
commit b488f9b032
20 changed files with 199 additions and 158 deletions

View File

@ -4,6 +4,7 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"strings" "strings"
@ -12,10 +13,10 @@ import (
type AuthorService struct { type AuthorService struct {
repo repository.AuthorRepository repo repository.AuthorRepository
siteConfigRepo repository.SiteConfigRepository siteConfigRepo repository.ConfigRepository
} }
func NewAuthorService(repo repository.AuthorRepository, siteConfigRepo repository.SiteConfigRepository) *AuthorService { func NewAuthorService(repo repository.AuthorRepository, siteConfigRepo repository.ConfigRepository) *AuthorService {
return &AuthorService{repo: repo, siteConfigRepo: siteConfigRepo} return &AuthorService{repo: repo, siteConfigRepo: siteConfigRepo}
} }
@ -49,18 +50,19 @@ func (s *AuthorService) Authenticate(name string, password string) bool {
} }
func (s *AuthorService) getSecretKey() string { func (s *AuthorService) getSecretKey() string {
config, err := s.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := s.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if config.Secret == "" { if siteConfig.Secret == "" {
config.Secret = RandStringRunes(64) siteConfig.Secret = RandStringRunes(64)
err = s.siteConfigRepo.Update(config) err = s.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
return config.Secret return siteConfig.Secret
} }
func (s *AuthorService) CreateToken(name string) (string, error) { func (s *AuthorService) CreateToken(name string) (string, error) {

View File

@ -16,13 +16,14 @@ type testConfigRepo struct {
} }
// Get implements repository.SiteConfigRepository. // Get implements repository.SiteConfigRepository.
func (c *testConfigRepo) Get() (model.SiteConfig, error) { func (c *testConfigRepo) Get(name string, result interface{}) error {
return c.config, nil *result.(*model.SiteConfig) = c.config
return nil
} }
// Update implements repository.SiteConfigRepository. // Update implements repository.SiteConfigRepository.
func (c *testConfigRepo) Update(siteConfig model.SiteConfig) error { func (c *testConfigRepo) Update(name string, result interface{}) error {
c.config = siteConfig c.config = result.(model.SiteConfig)
return nil return nil
} }

View File

@ -30,7 +30,7 @@ type AuthorRepository interface {
FindByName(name string) (*model.Author, error) FindByName(name string) (*model.Author, error)
} }
type SiteConfigRepository interface { type ConfigRepository interface {
Get() (model.SiteConfig, error) Get(name string, config interface{}) error
Update(siteConfig model.SiteConfig) error Update(name string, siteConfig interface{}) error
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
entrytypes "owl-blogs/entry_types" entrytypes "owl-blogs/entry_types"
"owl-blogs/importer" "owl-blogs/importer"
@ -84,7 +85,11 @@ var importCmd = &cobra.Command{
}) })
} }
v2Config, _ := app.SiteConfigRepo.Get() v2Config := &model.SiteConfig{}
err = app.SiteConfigRepo.Get(config.SITE_CONFIG, v2Config)
if err != nil {
panic(err)
}
v2Config.Title = v1Config.Title v2Config.Title = v1Config.Title
v2Config.SubTitle = v1Config.SubTitle v2Config.SubTitle = v1Config.SubTitle
v2Config.HeaderColor = v1Config.HeaderColor v2Config.HeaderColor = v1Config.HeaderColor
@ -95,7 +100,7 @@ var importCmd = &cobra.Command{
v2Config.HeaderMenu = headerMenu v2Config.HeaderMenu = headerMenu
v2Config.FooterMenu = footerMenu v2Config.FooterMenu = footerMenu
err = app.SiteConfigRepo.Update(v2Config) err = app.SiteConfigRepo.Update(config.SITE_CONFIG, v2Config)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -38,7 +38,7 @@ func App(db infra.Database) *web.WebApp {
entryRepo := infra.NewEntryRepository(db, registry) entryRepo := infra.NewEntryRepository(db, registry)
binRepo := infra.NewBinaryFileRepo(db) binRepo := infra.NewBinaryFileRepo(db)
authorRepo := infra.NewDefaultAuthorRepo(db) authorRepo := infra.NewDefaultAuthorRepo(db)
siteConfigRepo := infra.NewSiteConfigRepo(db) siteConfigRepo := infra.NewConfigRepo(db)
entryService := app.NewEntryService(entryRepo) entryService := app.NewEntryService(entryRepo)
binaryService := app.NewBinaryFileService(binRepo) binaryService := app.NewBinaryFileService(binRepo)

View File

@ -2,6 +2,10 @@ package config
import "os" import "os"
const (
SITE_CONFIG = "site_config"
)
type Config interface { type Config interface {
} }

View File

@ -0,0 +1,60 @@
package infra
import (
"encoding/json"
"owl-blogs/app/repository"
"github.com/jmoiron/sqlx"
)
type DefaultConfigRepo struct {
db *sqlx.DB
}
func NewConfigRepo(db Database) repository.ConfigRepository {
sqlxdb := db.Get()
sqlxdb.MustExec(`
CREATE TABLE IF NOT EXISTS site_config (
name TEXT PRIMARY KEY,
config TEXT
);
`)
return &DefaultConfigRepo{
db: sqlxdb,
}
}
// Get implements repository.SiteConfigRepository.
func (r *DefaultConfigRepo) Get(name string, result interface{}) error {
data := []byte{}
err := r.db.Get(&data, "SELECT config FROM site_config WHERE name = ?", name)
if err != nil {
if err.Error() == "sql: no rows in result set" {
return nil
}
return err
}
if len(data) == 0 {
return nil
}
return json.Unmarshal(data, result)
}
// Update implements repository.SiteConfigRepository.
func (r *DefaultConfigRepo) Update(name string, siteConfig interface{}) error {
jsonData, err := json.Marshal(siteConfig)
if err != nil {
return err
}
res, err := r.db.Exec("UPDATE site_config SET config = ? WHERE name = ?", jsonData, name)
if err != nil {
return err
}
rows, err := res.RowsAffected()
if rows == 0 {
_, err = r.db.Exec("INSERT INTO site_config (name, config) VALUES (?, ?)", name, jsonData)
}
return err
}

View File

@ -2,6 +2,7 @@ package infra_test
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/infra" "owl-blogs/infra"
"owl-blogs/test" "owl-blogs/test"
"testing" "testing"
@ -9,16 +10,17 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func setupSiteConfigRepo() repository.SiteConfigRepository { func setupSiteConfigRepo() repository.ConfigRepository {
db := test.NewMockDb() db := test.NewMockDb()
repo := infra.NewSiteConfigRepo(db) repo := infra.NewConfigRepo(db)
return repo return repo
} }
func TestSiteConfigRepo(t *testing.T) { func TestSiteConfigRepo(t *testing.T) {
repo := setupSiteConfigRepo() repo := setupSiteConfigRepo()
config, err := repo.Get() config := model.SiteConfig{}
err := repo.Get("test", &config)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "", config.Title) require.Equal(t, "", config.Title)
require.Equal(t, "", config.SubTitle) require.Equal(t, "", config.SubTitle)
@ -26,10 +28,11 @@ func TestSiteConfigRepo(t *testing.T) {
config.Title = "title" config.Title = "title"
config.SubTitle = "SubTitle" config.SubTitle = "SubTitle"
err = repo.Update(config) err = repo.Update("test", config)
require.NoError(t, err) require.NoError(t, err)
config2, err := repo.Get() config2 := model.SiteConfig{}
err = repo.Get("test", &config2)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "title", config2.Title) require.Equal(t, "title", config2.Title)
require.Equal(t, "SubTitle", config2.SubTitle) require.Equal(t, "SubTitle", config2.SubTitle)
@ -37,8 +40,8 @@ func TestSiteConfigRepo(t *testing.T) {
func TestSiteConfigUpdates(t *testing.T) { func TestSiteConfigUpdates(t *testing.T) {
repo := setupSiteConfigRepo() repo := setupSiteConfigRepo()
config := model.SiteConfig{}
config, err := repo.Get() err := repo.Get("test", &config)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "", config.Title) require.Equal(t, "", config.Title)
require.Equal(t, "", config.SubTitle) require.Equal(t, "", config.SubTitle)
@ -46,10 +49,10 @@ func TestSiteConfigUpdates(t *testing.T) {
config.Title = "title" config.Title = "title"
config.SubTitle = "SubTitle" config.SubTitle = "SubTitle"
err = repo.Update(config) err = repo.Update("test", config)
require.NoError(t, err) require.NoError(t, err)
config2 := model.SiteConfig{}
config2, err := repo.Get() err = repo.Get("test", &config2)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "title", config2.Title) require.Equal(t, "title", config2.Title)
require.Equal(t, "SubTitle", config2.SubTitle) require.Equal(t, "SubTitle", config2.SubTitle)
@ -57,10 +60,10 @@ func TestSiteConfigUpdates(t *testing.T) {
config2.Title = "title2" config2.Title = "title2"
config2.SubTitle = "SubTitle2" config2.SubTitle = "SubTitle2"
err = repo.Update(config2) err = repo.Update("test", config2)
require.NoError(t, err) require.NoError(t, err)
config3 := model.SiteConfig{}
config3, err := repo.Get() err = repo.Get("test", &config3)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "title2", config3.Title) require.Equal(t, "title2", config3.Title)
require.Equal(t, "SubTitle2", config3.SubTitle) require.Equal(t, "SubTitle2", config3.SubTitle)

View File

@ -1,62 +0,0 @@
package infra
import (
"encoding/json"
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"github.com/jmoiron/sqlx"
)
type DefaultSiteConfigRepo struct {
db *sqlx.DB
}
func NewSiteConfigRepo(db Database) repository.SiteConfigRepository {
sqlxdb := db.Get()
sqlxdb.MustExec(`
CREATE TABLE IF NOT EXISTS site_config (
config TEXT
);
`)
return &DefaultSiteConfigRepo{
db: sqlxdb,
}
}
// Get implements repository.SiteConfigRepository.
func (r *DefaultSiteConfigRepo) Get() (model.SiteConfig, error) {
data := []byte{}
err := r.db.Get(&data, "SELECT config FROM site_config LIMIT 1")
if err != nil {
if err.Error() == "sql: no rows in result set" {
return model.SiteConfig{}, nil
}
return model.SiteConfig{}, err
}
if len(data) == 0 {
return model.SiteConfig{}, nil
}
config := model.SiteConfig{}
err = json.Unmarshal(data, &config)
return config, err
}
// Update implements repository.SiteConfigRepository.
func (r *DefaultSiteConfigRepo) Update(siteConfig model.SiteConfig) error {
jsonData, err := json.Marshal(siteConfig)
if err != nil {
return err
}
res, err := r.db.Exec("UPDATE site_config SET config = ?", jsonData)
if err != nil {
return err
}
rows, err := res.RowsAffected()
if rows == 0 {
_, err = r.db.Exec("INSERT INTO site_config (config) VALUES (?)", jsonData)
}
return err
}

View File

@ -20,7 +20,7 @@ type WebApp struct {
BinaryService *app.BinaryService BinaryService *app.BinaryService
Registry *app.EntryTypeRegistry Registry *app.EntryTypeRegistry
AuthorService *app.AuthorService AuthorService *app.AuthorService
SiteConfigRepo repository.SiteConfigRepository SiteConfigRepo repository.ConfigRepository
} }
func NewWebApp( func NewWebApp(
@ -28,7 +28,7 @@ func NewWebApp(
typeRegistry *app.EntryTypeRegistry, typeRegistry *app.EntryTypeRegistry,
binService *app.BinaryService, binService *app.BinaryService,
authorService *app.AuthorService, authorService *app.AuthorService,
siteConfigRepo repository.SiteConfigRepository, siteConfigRepo repository.ConfigRepository,
) *WebApp { ) *WebApp {
app := fiber.New() app := fiber.New()

View File

@ -12,7 +12,7 @@ import (
) )
type EditorHandler struct { type EditorHandler struct {
configRepo repository.SiteConfigRepository configRepo repository.ConfigRepository
entrySvc *app.EntryService entrySvc *app.EntryService
binSvc *app.BinaryService binSvc *app.BinaryService
registry *app.EntryTypeRegistry registry *app.EntryTypeRegistry
@ -22,7 +22,7 @@ func NewEditorHandler(
entryService *app.EntryService, entryService *app.EntryService,
registry *app.EntryTypeRegistry, registry *app.EntryTypeRegistry,
binService *app.BinaryService, binService *app.BinaryService,
configRepo repository.SiteConfigRepository, configRepo repository.ConfigRepository,
) *EditorHandler { ) *EditorHandler {
return &EditorHandler{ return &EditorHandler{
entrySvc: entryService, entrySvc: entryService,
@ -54,7 +54,7 @@ func (h *EditorHandler) HandleGet(c *fiber.Ctx) error {
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/editor", htmlForm) return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm)
} }
func (h *EditorHandler) HandlePost(c *fiber.Ctx) error { func (h *EditorHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -9,7 +9,7 @@ import (
) )
type EditorListHandler struct { type EditorListHandler struct {
configRepo repository.SiteConfigRepository configRepo repository.ConfigRepository
registry *app.EntryTypeRegistry registry *app.EntryTypeRegistry
} }
@ -18,7 +18,7 @@ type EditorListContext struct {
} }
func NewEditorListHandler(registry *app.EntryTypeRegistry, func NewEditorListHandler(registry *app.EntryTypeRegistry,
configRepo repository.SiteConfigRepository) *EditorListHandler { configRepo repository.ConfigRepository) *EditorListHandler {
return &EditorListHandler{ return &EditorListHandler{
registry: registry, registry: registry,
configRepo: configRepo, configRepo: configRepo,
@ -37,5 +37,5 @@ func (h *EditorListHandler) Handle(c *fiber.Ctx) error {
typeNames = append(typeNames, name) typeNames = append(typeNames, name)
} }
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/editor_list", &EditorListContext{Types: typeNames}) return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor_list", &EditorListContext{Types: typeNames})
} }

View File

@ -10,7 +10,7 @@ import (
) )
type EntryHandler struct { type EntryHandler struct {
configRepo repository.SiteConfigRepository configRepo repository.ConfigRepository
entrySvc *app.EntryService entrySvc *app.EntryService
authorSvc *app.AuthorService authorSvc *app.AuthorService
registry *app.EntryTypeRegistry registry *app.EntryTypeRegistry
@ -25,7 +25,7 @@ func NewEntryHandler(
entryService *app.EntryService, entryService *app.EntryService,
registry *app.EntryTypeRegistry, registry *app.EntryTypeRegistry,
authorService *app.AuthorService, authorService *app.AuthorService,
configRepo repository.SiteConfigRepository, configRepo repository.ConfigRepository,
) *EntryHandler { ) *EntryHandler {
return &EntryHandler{ return &EntryHandler{
entrySvc: entryService, entrySvc: entryService,
@ -49,5 +49,5 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
author = &model.Author{} author = &model.Author{}
} }
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/entry", entryData{Entry: entry, Author: author}) return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/entry", entryData{Entry: entry, Author: author})
} }

View File

@ -12,13 +12,13 @@ import (
) )
type IndexHandler struct { type IndexHandler struct {
configRepo repository.SiteConfigRepository configRepo repository.ConfigRepository
entrySvc *app.EntryService entrySvc *app.EntryService
} }
func NewIndexHandler( func NewIndexHandler(
entryService *app.EntryService, entryService *app.EntryService,
configRepo repository.SiteConfigRepository, configRepo repository.ConfigRepository,
) *IndexHandler { ) *IndexHandler {
return &IndexHandler{ return &IndexHandler{
entrySvc: entryService, entrySvc: entryService,
@ -73,7 +73,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return err return err
} }
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/index", indexRenderData{ return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/index", indexRenderData{
Entries: entries, Entries: entries,
Page: pageNum, Page: pageNum,
NextPage: pageNum + 1, NextPage: pageNum + 1,

View File

@ -10,13 +10,13 @@ import (
) )
type LoginHandler struct { type LoginHandler struct {
configRepo repository.SiteConfigRepository configRepo repository.ConfigRepository
authorService *app.AuthorService authorService *app.AuthorService
} }
func NewLoginHandler( func NewLoginHandler(
authorService *app.AuthorService, authorService *app.AuthorService,
configRepo repository.SiteConfigRepository, configRepo repository.ConfigRepository,
) *LoginHandler { ) *LoginHandler {
return &LoginHandler{ return &LoginHandler{
authorService: authorService, authorService: authorService,
@ -26,7 +26,7 @@ func NewLoginHandler(
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error { func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/login", nil) return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/login", nil)
} }
func (h *LoginHandler) HandlePost(c *fiber.Ctx) error { func (h *LoginHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -2,16 +2,18 @@ package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
type SiteConfigHandler struct { type SiteConfigHandler struct {
siteConfigRepo repository.SiteConfigRepository siteConfigRepo repository.ConfigRepository
} }
func NewSiteConfigHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigHandler { func NewSiteConfigHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigHandler {
return &SiteConfigHandler{ return &SiteConfigHandler{
siteConfigRepo: siteConfigRepo, siteConfigRepo: siteConfigRepo,
} }
@ -20,29 +22,32 @@ func NewSiteConfigHandler(siteConfigRepo repository.SiteConfigRepository) *SiteC
func (h *SiteConfigHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase(c, getConfig(h.siteConfigRepo), "views/site_config", config) return render.RenderTemplateWithBase(c, getSiteConfig(h.siteConfigRepo), "views/site_config", siteConfig)
} }
func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error { func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
config.Title = c.FormValue("Title") siteConfig.Title = c.FormValue("Title")
config.SubTitle = c.FormValue("SubTitle") siteConfig.SubTitle = c.FormValue("SubTitle")
config.HeaderColor = c.FormValue("HeaderColor") siteConfig.HeaderColor = c.FormValue("HeaderColor")
config.AuthorName = c.FormValue("AuthorName") siteConfig.AuthorName = c.FormValue("AuthorName")
config.AvatarUrl = c.FormValue("AvatarUrl") siteConfig.AvatarUrl = c.FormValue("AvatarUrl")
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,6 +3,7 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"strconv" "strconv"
@ -11,7 +12,7 @@ import (
) )
type SiteConfigListHandler struct { type SiteConfigListHandler struct {
siteConfigRepo repository.SiteConfigRepository siteConfigRepo repository.ConfigRepository
typeRegistry *app.EntryTypeRegistry typeRegistry *app.EntryTypeRegistry
} }
@ -21,7 +22,7 @@ type siteConfigListTemplateData struct {
} }
func NewSiteConfigListHandler( func NewSiteConfigListHandler(
siteConfigRepo repository.SiteConfigRepository, siteConfigRepo repository.ConfigRepository,
typeRegistry *app.EntryTypeRegistry, typeRegistry *app.EntryTypeRegistry,
) *SiteConfigListHandler { ) *SiteConfigListHandler {
return &SiteConfigListHandler{ return &SiteConfigListHandler{
@ -33,7 +34,9 @@ func NewSiteConfigListHandler(
func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -48,8 +51,8 @@ func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
} }
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, getConfig(h.siteConfigRepo), "views/site_config_list", siteConfigListTemplateData{ c, getSiteConfig(h.siteConfigRepo), "views/site_config_list", siteConfigListTemplateData{
Lists: config.Lists, Lists: siteConfig.Lists,
Types: types, Types: types,
}) })
} }
@ -57,7 +60,9 @@ func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error { func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -67,14 +72,14 @@ func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
return err return err
} }
config.Lists = append(config.Lists, model.EntryList{ siteConfig.Lists = append(siteConfig.Lists, model.EntryList{
Id: c.FormValue("Id"), Id: c.FormValue("Id"),
Title: c.FormValue("Title"), Title: c.FormValue("Title"),
Include: form.Value["Include"], Include: form.Value["Include"],
ListType: c.FormValue("ListType"), ListType: c.FormValue("ListType"),
}) })
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -85,7 +90,9 @@ func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error { func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -95,9 +102,9 @@ func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error {
return err return err
} }
config.Lists = append(config.Lists[:id], config.Lists[id+1:]...) siteConfig.Lists = append(siteConfig.Lists[:id], siteConfig.Lists[id+1:]...)
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"strconv" "strconv"
@ -10,10 +11,10 @@ import (
) )
type SiteConfigMeHandler struct { type SiteConfigMeHandler struct {
siteConfigRepo repository.SiteConfigRepository siteConfigRepo repository.ConfigRepository
} }
func NewSiteConfigMeHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigMeHandler { func NewSiteConfigMeHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMeHandler {
return &SiteConfigMeHandler{ return &SiteConfigMeHandler{
siteConfigRepo: siteConfigRepo, siteConfigRepo: siteConfigRepo,
} }
@ -22,29 +23,33 @@ func NewSiteConfigMeHandler(siteConfigRepo repository.SiteConfigRepository) *Sit
func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, getConfig(h.siteConfigRepo), "views/site_config_me", config.Me) c, getSiteConfig(h.siteConfigRepo), "views/site_config_me", siteConfig.Me)
} }
func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error { func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
config.Me = append(config.Me, model.MeLinks{ siteConfig.Me = append(siteConfig.Me, model.MeLinks{
Name: c.FormValue("Name"), Name: c.FormValue("Name"),
Url: c.FormValue("Url"), Url: c.FormValue("Url"),
}) })
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -55,7 +60,9 @@ func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error { func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -64,9 +71,9 @@ func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
if err != nil { if err != nil {
return err return err
} }
config.Me = append(config.Me[:idx], config.Me[idx+1:]...) siteConfig.Me = append(siteConfig.Me[:idx], siteConfig.Me[idx+1:]...)
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"strconv" "strconv"
@ -10,7 +11,7 @@ import (
) )
type SiteConfigMenusHandler struct { type SiteConfigMenusHandler struct {
siteConfigRepo repository.SiteConfigRepository siteConfigRepo repository.ConfigRepository
} }
type siteConfigMenusTemplateData struct { type siteConfigMenusTemplateData struct {
@ -18,7 +19,7 @@ type siteConfigMenusTemplateData struct {
FooterMenu []model.MenuItem FooterMenu []model.MenuItem
} }
func NewSiteConfigMenusHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigMenusHandler { func NewSiteConfigMenusHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMenusHandler {
return &SiteConfigMenusHandler{ return &SiteConfigMenusHandler{
siteConfigRepo: siteConfigRepo, siteConfigRepo: siteConfigRepo,
} }
@ -27,22 +28,26 @@ func NewSiteConfigMenusHandler(siteConfigRepo repository.SiteConfigRepository) *
func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, getConfig(h.siteConfigRepo), "views/site_config_menus", siteConfigMenusTemplateData{ c, getSiteConfig(h.siteConfigRepo), "views/site_config_menus", siteConfigMenusTemplateData{
HeaderMenu: config.HeaderMenu, HeaderMenu: siteConfig.HeaderMenu,
FooterMenu: config.FooterMenu, FooterMenu: siteConfig.FooterMenu,
}) })
} }
func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error { func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -55,12 +60,12 @@ func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
} }
if c.FormValue("menu") == "header" { if c.FormValue("menu") == "header" {
config.HeaderMenu = append(config.HeaderMenu, menuItem) siteConfig.HeaderMenu = append(siteConfig.HeaderMenu, menuItem)
} else if c.FormValue("menu") == "footer" { } else if c.FormValue("menu") == "footer" {
config.FooterMenu = append(config.FooterMenu, menuItem) siteConfig.FooterMenu = append(siteConfig.FooterMenu, menuItem)
} }
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -71,7 +76,9 @@ func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error { func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get() siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -83,12 +90,12 @@ func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error {
} }
if menu == "header" { if menu == "header" {
config.HeaderMenu = append(config.HeaderMenu[:idx], config.HeaderMenu[idx+1:]...) siteConfig.HeaderMenu = append(siteConfig.HeaderMenu[:idx], siteConfig.HeaderMenu[idx+1:]...)
} else if menu == "footer" { } else if menu == "footer" {
config.FooterMenu = append(config.FooterMenu[:idx], config.FooterMenu[idx+1:]...) siteConfig.FooterMenu = append(siteConfig.FooterMenu[:idx], siteConfig.FooterMenu[idx+1:]...)
} }
err = h.siteConfigRepo.Update(config) err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,13 +2,15 @@ package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
) )
func getConfig(repo repository.SiteConfigRepository) model.SiteConfig { func getSiteConfig(repo repository.ConfigRepository) model.SiteConfig {
config, err := repo.Get() siteConfig := model.SiteConfig{}
err := repo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return config return siteConfig
} }