cpolis/cmd/data/articles.go

199 lines
3.6 KiB
Go
Raw Normal View History

package data
2024-03-01 11:30:31 +01:00
import (
2024-03-03 13:56:49 +01:00
"encoding/gob"
"fmt"
"os"
"sync"
2024-03-01 11:30:31 +01:00
"time"
"github.com/google/uuid"
)
type Article struct {
2024-03-03 09:16:49 +01:00
Title string
Author string
Created time.Time
Desc string
Content string
Tags []string
UUID uuid.UUID
AuthorID int64
}
2024-03-05 18:20:34 +01:00
// TODO: setCh
type ArticleList struct {
2024-03-03 13:56:49 +01:00
addCh chan *Article
delCh chan uuid.UUID
getCh chan []Article
2024-03-05 18:20:34 +01:00
retCh chan *Article
2024-03-03 13:56:49 +01:00
articles []*Article
wg sync.WaitGroup
}
2024-03-05 18:20:34 +01:00
// TODO: setCh
2024-03-03 13:56:49 +01:00
type TagList struct {
addCh chan string
getCh chan []string
tags []string
wg sync.WaitGroup
}
2024-03-05 16:38:18 +01:00
func initArticleList() *ArticleList {
return &ArticleList{
addCh: make(chan *Article),
delCh: make(chan uuid.UUID),
getCh: make(chan []Article),
2024-03-05 18:20:34 +01:00
retCh: make(chan *Article),
}
}
2024-03-05 16:38:18 +01:00
func initTagList() *TagList {
2024-03-03 13:56:49 +01:00
return &TagList{
addCh: make(chan string),
getCh: make(chan []string),
}
}
func (al *ArticleList) start() {
al.wg.Done()
2024-03-01 21:01:38 +01:00
for {
select {
2024-03-03 13:56:49 +01:00
case article := <-al.addCh:
al.articles = append(al.articles, article)
case uuid := <-al.delCh:
for i, article := range al.articles {
2024-03-01 21:01:38 +01:00
if article.UUID == uuid {
2024-03-03 13:56:49 +01:00
al.articles = append(al.articles[:i], al.articles[i+1:]...)
al.retCh <- article
2024-03-01 21:01:38 +01:00
}
}
2024-03-03 13:56:49 +01:00
case al.getCh <- func() []Article {
2024-03-01 21:01:38 +01:00
var list []Article
2024-03-03 13:56:49 +01:00
for _, article := range al.articles {
2024-03-01 21:01:38 +01:00
list = append(list, *article)
}
return list
}():
}
}
}
2024-03-03 13:56:49 +01:00
func (tl *TagList) start() {
tl.wg.Done()
for {
select {
case tag := <-tl.addCh:
tl.tags = append(tl.tags, tag)
case tl.getCh <- tl.tags:
}
}
}
2024-03-01 21:01:38 +01:00
func NewArticleList() *ArticleList {
2024-03-05 16:38:18 +01:00
list := initArticleList()
2024-03-05 18:20:34 +01:00
list.articles = make([]*Article, 0)
list.wg.Add(1)
2024-03-01 21:01:38 +01:00
go list.start()
list.wg.Wait()
2024-03-01 21:01:38 +01:00
return list
}
2024-03-03 13:56:49 +01:00
func (al *ArticleList) Add(a *Article) {
al.addCh <- a
2024-03-01 21:01:38 +01:00
}
2024-03-03 13:56:49 +01:00
func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
al.delCh <- uuid
article := <-al.retCh
2024-03-05 16:38:18 +01:00
2024-03-01 21:01:38 +01:00
if article == nil {
return nil, false
}
2024-03-01 21:01:38 +01:00
return article, true
}
2024-03-03 13:56:49 +01:00
func (al *ArticleList) Get() []Article {
return <-al.getCh
}
func (al *ArticleList) Save(filename string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating key file: %v", err)
}
defer file.Close()
articles := al.Get()
2024-03-05 16:38:18 +01:00
if err = gob.NewEncoder(file).Encode(articles); err != nil {
2024-03-03 13:56:49 +01:00
return fmt.Errorf("error ecoding key: %v", err)
}
return nil
}
func LoadArticleList(filename string) (*ArticleList, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening key file: %v", err)
}
articleList := NewArticleList()
2024-03-05 16:38:18 +01:00
if err = gob.NewDecoder(file).Decode(&articleList.articles); err != nil {
2024-03-03 13:56:49 +01:00
return nil, fmt.Errorf("error decoding key: %v", err)
}
return articleList, nil
}
func NewTagList() *TagList {
2024-03-05 16:38:18 +01:00
list := initTagList()
2024-03-05 18:20:34 +01:00
list.tags = make([]string, 0)
2024-03-03 13:56:49 +01:00
list.wg.Add(1)
go list.start()
list.wg.Wait()
return list
}
func (tl *TagList) Add(tag string) {
tl.addCh <- tag
}
func (tl *TagList) Get() []string {
return <-tl.getCh
}
func (tl *TagList) Save(filename string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating key file: %v", err)
}
defer file.Close()
tags := tl.Get()
2024-03-05 16:38:18 +01:00
if err = gob.NewEncoder(file).Encode(tags); err != nil {
2024-03-03 13:56:49 +01:00
return fmt.Errorf("error ecoding key: %v", err)
}
return nil
}
func LoadTagList(filename string) (*TagList, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening key file: %v", err)
}
2024-03-05 16:38:18 +01:00
defer file.Close()
2024-03-03 13:56:49 +01:00
tagList := NewTagList()
2024-03-05 16:38:18 +01:00
if err = gob.NewDecoder(file).Decode(&tagList.tags); err != nil {
2024-03-03 13:56:49 +01:00
return nil, fmt.Errorf("error decoding key: %v", err)
}
return tagList, nil
}