forked from jason/cpolis
Added partial support for tags
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -19,11 +22,18 @@ type Article struct {
|
||||
}
|
||||
|
||||
type ArticleList struct {
|
||||
addCh chan *Article
|
||||
delCh chan uuid.UUID
|
||||
retCh chan *Article
|
||||
getCh chan []Article
|
||||
list []*Article
|
||||
addCh chan *Article
|
||||
delCh chan uuid.UUID
|
||||
retCh chan *Article
|
||||
getCh chan []Article
|
||||
articles []*Article
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
type TagList struct {
|
||||
addCh chan string
|
||||
getCh chan []string
|
||||
tags []string
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
@ -36,22 +46,29 @@ func minArticleList() *ArticleList {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ArticleList) start() {
|
||||
l.wg.Done()
|
||||
func minTagList() *TagList {
|
||||
return &TagList{
|
||||
addCh: make(chan string),
|
||||
getCh: make(chan []string),
|
||||
}
|
||||
}
|
||||
|
||||
func (al *ArticleList) start() {
|
||||
al.wg.Done()
|
||||
for {
|
||||
select {
|
||||
case article := <-l.addCh:
|
||||
l.list = append(l.list, article)
|
||||
case uuid := <-l.delCh:
|
||||
for i, article := range l.list {
|
||||
case article := <-al.addCh:
|
||||
al.articles = append(al.articles, article)
|
||||
case uuid := <-al.delCh:
|
||||
for i, article := range al.articles {
|
||||
if article.UUID == uuid {
|
||||
l.list = append(l.list[:i], l.list[i+1:]...)
|
||||
l.retCh <- article
|
||||
al.articles = append(al.articles[:i], al.articles[i+1:]...)
|
||||
al.retCh <- article
|
||||
}
|
||||
}
|
||||
case l.getCh <- func() []Article {
|
||||
case al.getCh <- func() []Article {
|
||||
var list []Article
|
||||
for _, article := range l.list {
|
||||
for _, article := range al.articles {
|
||||
list = append(list, *article)
|
||||
}
|
||||
return list
|
||||
@ -60,9 +77,20 @@ func (l *ArticleList) start() {
|
||||
}
|
||||
}
|
||||
|
||||
func (tl *TagList) start() {
|
||||
tl.wg.Done()
|
||||
for {
|
||||
select {
|
||||
case tag := <-tl.addCh:
|
||||
tl.tags = append(tl.tags, tag)
|
||||
case tl.getCh <- tl.tags:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewArticleList() *ArticleList {
|
||||
list := minArticleList()
|
||||
list.list = []*Article{}
|
||||
list.articles = []*Article{}
|
||||
|
||||
list.wg.Add(1)
|
||||
go list.start()
|
||||
@ -71,19 +99,104 @@ func NewArticleList() *ArticleList {
|
||||
return list
|
||||
}
|
||||
|
||||
func (l *ArticleList) Add(a *Article) {
|
||||
l.addCh <- a
|
||||
func (al *ArticleList) Add(a *Article) {
|
||||
al.addCh <- a
|
||||
}
|
||||
|
||||
func (l *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
|
||||
l.delCh <- uuid
|
||||
article := <-l.retCh
|
||||
func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
|
||||
al.delCh <- uuid
|
||||
article := <-al.retCh
|
||||
if article == nil {
|
||||
return nil, false
|
||||
}
|
||||
return article, true
|
||||
}
|
||||
|
||||
func (l *ArticleList) Get() []Article {
|
||||
return <-l.getCh
|
||||
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()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
articles := al.Get()
|
||||
err = encoder.Encode(articles)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
articleList := NewArticleList()
|
||||
err = decoder.Decode(&articleList.articles)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||
}
|
||||
|
||||
return articleList, nil
|
||||
}
|
||||
|
||||
func NewTagList() *TagList {
|
||||
list := minTagList()
|
||||
list.tags = []string{}
|
||||
|
||||
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()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
tags := tl.Get()
|
||||
err = encoder.Encode(tags)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
tagList := NewTagList()
|
||||
err = decoder.Decode(&tagList.tags)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||
}
|
||||
|
||||
return tagList, nil
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func OpenDB(dbName string) (*DB, error) {
|
||||
return &db, nil
|
||||
}
|
||||
|
||||
func (db *DB) AddUser(user User, pass string) error {
|
||||
func (db *DB) AddUser(user *User, pass string) error {
|
||||
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating password hash: %v", err)
|
||||
|
Reference in New Issue
Block a user