v2 #43

Merged
h4kor merged 41 commits from v2 into main 2023-07-19 19:09:19 +00:00
4 changed files with 168 additions and 0 deletions
Showing only changes of commit 3a1559584c - Show all commits

View File

@ -29,3 +29,8 @@ type AuthorRepository interface {
// It returns an error if the author is not found
FindByName(name string) (*model.Author, error)
}
type SiteConfigRepository interface {
Get() (model.SiteConfig, error)
Update(siteConfig model.SiteConfig) error
}

View File

@ -0,0 +1,33 @@
package model
type MeLinks struct {
Name string
Url string
}
type EntryList struct {
Id string
Title string
Include []string
ListType string
}
type MenuItem struct {
Title string
List string
Url string
Post string
}
type SiteConfig struct {
Title string
SubTitle string
HeaderColor string
AuthorName string
Me []MeLinks
Lists []EntryList
PrimaryListInclude []string
HeaderMenu []MenuItem
FooterMenu []MenuItem
Secret string
}

View File

@ -0,0 +1,62 @@
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

@ -0,0 +1,68 @@
package infra_test
import (
"owl-blogs/app/repository"
"owl-blogs/infra"
"owl-blogs/test"
"testing"
"github.com/stretchr/testify/require"
)
func setupSiteConfigRepo() repository.SiteConfigRepository {
db := test.NewMockDb()
repo := infra.NewSiteConfigRepo(db)
return repo
}
func TestSiteConfigRepo(t *testing.T) {
repo := setupSiteConfigRepo()
config, err := repo.Get()
require.NoError(t, err)
require.Equal(t, "", config.Title)
require.Equal(t, "", config.SubTitle)
config.Title = "title"
config.SubTitle = "SubTitle"
err = repo.Update(config)
require.NoError(t, err)
config2, err := repo.Get()
require.NoError(t, err)
require.Equal(t, "title", config2.Title)
require.Equal(t, "SubTitle", config2.SubTitle)
}
func TestSiteConfigUpdates(t *testing.T) {
repo := setupSiteConfigRepo()
config, err := repo.Get()
require.NoError(t, err)
require.Equal(t, "", config.Title)
require.Equal(t, "", config.SubTitle)
config.Title = "title"
config.SubTitle = "SubTitle"
err = repo.Update(config)
require.NoError(t, err)
config2, err := repo.Get()
require.NoError(t, err)
require.Equal(t, "title", config2.Title)
require.Equal(t, "SubTitle", config2.SubTitle)
config2.Title = "title2"
config2.SubTitle = "SubTitle2"
err = repo.Update(config2)
require.NoError(t, err)
config3, err := repo.Get()
require.NoError(t, err)
require.Equal(t, "title2", config3.Title)
require.Equal(t, "SubTitle2", config3.SubTitle)
}