v2 #43

Merged
h4kor merged 41 commits from v2 into main 2023-07-19 19:09:19 +00:00
20 changed files with 199 additions and 158 deletions
Showing only changes of commit b488f9b032 - Show all commits

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"owl-blogs/config"
"owl-blogs/domain/model"
entrytypes "owl-blogs/entry_types"
"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.SubTitle = v1Config.SubTitle
v2Config.HeaderColor = v1Config.HeaderColor
@ -95,7 +100,7 @@ var importCmd = &cobra.Command{
v2Config.HeaderMenu = headerMenu
v2Config.FooterMenu = footerMenu
err = app.SiteConfigRepo.Update(v2Config)
err = app.SiteConfigRepo.Update(config.SITE_CONFIG, v2Config)
if err != nil {
panic(err)
}

View File

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

View File

@ -2,6 +2,10 @@ package config
import "os"
const (
SITE_CONFIG = "site_config"
)
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 (
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/infra"
"owl-blogs/test"
"testing"
@ -9,16 +10,17 @@ import (
"github.com/stretchr/testify/require"
)
func setupSiteConfigRepo() repository.SiteConfigRepository {
func setupSiteConfigRepo() repository.ConfigRepository {
db := test.NewMockDb()
repo := infra.NewSiteConfigRepo(db)
repo := infra.NewConfigRepo(db)
return repo
}
func TestSiteConfigRepo(t *testing.T) {
repo := setupSiteConfigRepo()
config, err := repo.Get()
config := model.SiteConfig{}
err := repo.Get("test", &config)
require.NoError(t, err)
require.Equal(t, "", config.Title)
require.Equal(t, "", config.SubTitle)
@ -26,10 +28,11 @@ func TestSiteConfigRepo(t *testing.T) {
config.Title = "title"
config.SubTitle = "SubTitle"
err = repo.Update(config)
err = repo.Update("test", config)
require.NoError(t, err)
config2, err := repo.Get()
config2 := model.SiteConfig{}
err = repo.Get("test", &config2)
require.NoError(t, err)
require.Equal(t, "title", config2.Title)
require.Equal(t, "SubTitle", config2.SubTitle)
@ -37,8 +40,8 @@ func TestSiteConfigRepo(t *testing.T) {
func TestSiteConfigUpdates(t *testing.T) {
repo := setupSiteConfigRepo()
config, err := repo.Get()
config := model.SiteConfig{}
err := repo.Get("test", &config)
require.NoError(t, err)
require.Equal(t, "", config.Title)
require.Equal(t, "", config.SubTitle)
@ -46,10 +49,10 @@ func TestSiteConfigUpdates(t *testing.T) {
config.Title = "title"
config.SubTitle = "SubTitle"
err = repo.Update(config)
err = repo.Update("test", config)
require.NoError(t, err)
config2, err := repo.Get()
config2 := model.SiteConfig{}
err = repo.Get("test", &config2)
require.NoError(t, err)
require.Equal(t, "title", config2.Title)
require.Equal(t, "SubTitle", config2.SubTitle)
@ -57,10 +60,10 @@ func TestSiteConfigUpdates(t *testing.T) {
config2.Title = "title2"
config2.SubTitle = "SubTitle2"
err = repo.Update(config2)
err = repo.Update("test", config2)
require.NoError(t, err)
config3, err := repo.Get()
config3 := model.SiteConfig{}
err = repo.Get("test", &config3)
require.NoError(t, err)
require.Equal(t, "title2", config3.Title)
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
Registry *app.EntryTypeRegistry
AuthorService *app.AuthorService
SiteConfigRepo repository.SiteConfigRepository
SiteConfigRepo repository.ConfigRepository
}
func NewWebApp(
@ -28,7 +28,7 @@ func NewWebApp(
typeRegistry *app.EntryTypeRegistry,
binService *app.BinaryService,
authorService *app.AuthorService,
siteConfigRepo repository.SiteConfigRepository,
siteConfigRepo repository.ConfigRepository,
) *WebApp {
app := fiber.New()

View File

@ -12,7 +12,7 @@ import (
)
type EditorHandler struct {
configRepo repository.SiteConfigRepository
configRepo repository.ConfigRepository
entrySvc *app.EntryService
binSvc *app.BinaryService
registry *app.EntryTypeRegistry
@ -22,7 +22,7 @@ func NewEditorHandler(
entryService *app.EntryService,
registry *app.EntryTypeRegistry,
binService *app.BinaryService,
configRepo repository.SiteConfigRepository,
configRepo repository.ConfigRepository,
) *EditorHandler {
return &EditorHandler{
entrySvc: entryService,
@ -54,7 +54,7 @@ func (h *EditorHandler) HandleGet(c *fiber.Ctx) error {
if err != nil {
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 {

View File

@ -9,7 +9,7 @@ import (
)
type EditorListHandler struct {
configRepo repository.SiteConfigRepository
configRepo repository.ConfigRepository
registry *app.EntryTypeRegistry
}
@ -18,7 +18,7 @@ type EditorListContext struct {
}
func NewEditorListHandler(registry *app.EntryTypeRegistry,
configRepo repository.SiteConfigRepository) *EditorListHandler {
configRepo repository.ConfigRepository) *EditorListHandler {
return &EditorListHandler{
registry: registry,
configRepo: configRepo,
@ -37,5 +37,5 @@ func (h *EditorListHandler) Handle(c *fiber.Ctx) error {
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 {
configRepo repository.SiteConfigRepository
configRepo repository.ConfigRepository
entrySvc *app.EntryService
authorSvc *app.AuthorService
registry *app.EntryTypeRegistry
@ -25,7 +25,7 @@ func NewEntryHandler(
entryService *app.EntryService,
registry *app.EntryTypeRegistry,
authorService *app.AuthorService,
configRepo repository.SiteConfigRepository,
configRepo repository.ConfigRepository,
) *EntryHandler {
return &EntryHandler{
entrySvc: entryService,
@ -49,5 +49,5 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
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 {
configRepo repository.SiteConfigRepository
configRepo repository.ConfigRepository
entrySvc *app.EntryService
}
func NewIndexHandler(
entryService *app.EntryService,
configRepo repository.SiteConfigRepository,
configRepo repository.ConfigRepository,
) *IndexHandler {
return &IndexHandler{
entrySvc: entryService,
@ -73,7 +73,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return err
}
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/index", indexRenderData{
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/index", indexRenderData{
Entries: entries,
Page: pageNum,
NextPage: pageNum + 1,

View File

@ -10,13 +10,13 @@ import (
)
type LoginHandler struct {
configRepo repository.SiteConfigRepository
configRepo repository.ConfigRepository
authorService *app.AuthorService
}
func NewLoginHandler(
authorService *app.AuthorService,
configRepo repository.SiteConfigRepository,
configRepo repository.ConfigRepository,
) *LoginHandler {
return &LoginHandler{
authorService: authorService,
@ -26,7 +26,7 @@ func NewLoginHandler(
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
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 {

View File

@ -2,16 +2,18 @@ package web
import (
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
type SiteConfigHandler struct {
siteConfigRepo repository.SiteConfigRepository
siteConfigRepo repository.ConfigRepository
}
func NewSiteConfigHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigHandler {
func NewSiteConfigHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigHandler {
return &SiteConfigHandler{
siteConfigRepo: siteConfigRepo,
}
@ -20,29 +22,32 @@ func NewSiteConfigHandler(siteConfigRepo repository.SiteConfigRepository) *SiteC
func (h *SiteConfigHandler) HandleGet(c *fiber.Ctx) error {
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 {
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 {
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 {
return err
}
config.Title = c.FormValue("Title")
config.SubTitle = c.FormValue("SubTitle")
config.HeaderColor = c.FormValue("HeaderColor")
config.AuthorName = c.FormValue("AuthorName")
config.AvatarUrl = c.FormValue("AvatarUrl")
siteConfig.Title = c.FormValue("Title")
siteConfig.SubTitle = c.FormValue("SubTitle")
siteConfig.HeaderColor = c.FormValue("HeaderColor")
siteConfig.AuthorName = c.FormValue("AuthorName")
siteConfig.AvatarUrl = c.FormValue("AvatarUrl")
err = h.siteConfigRepo.Update(config)
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil {
return err
}

View File

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

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/render"
"strconv"
@ -10,10 +11,10 @@ import (
)
type SiteConfigMeHandler struct {
siteConfigRepo repository.SiteConfigRepository
siteConfigRepo repository.ConfigRepository
}
func NewSiteConfigMeHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigMeHandler {
func NewSiteConfigMeHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMeHandler {
return &SiteConfigMeHandler{
siteConfigRepo: siteConfigRepo,
}
@ -22,29 +23,33 @@ func NewSiteConfigMeHandler(siteConfigRepo repository.SiteConfigRepository) *Sit
func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error {
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 {
return err
}
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 {
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 {
return err
}
config.Me = append(config.Me, model.MeLinks{
siteConfig.Me = append(siteConfig.Me, model.MeLinks{
Name: c.FormValue("Name"),
Url: c.FormValue("Url"),
})
err = h.siteConfigRepo.Update(config)
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
if err != nil {
return err
}
@ -55,7 +60,9 @@ func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
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 {
return err
}
@ -64,9 +71,9 @@ func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
if err != nil {
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 {
return err
}

View File

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

View File

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