2023-02-05 04:41:24 +04:00
|
|
|
package article
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2023-05-23 04:10:50 +04:00
|
|
|
"strconv"
|
2023-02-05 04:41:24 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Article struct {
|
2023-05-23 04:10:50 +04:00
|
|
|
ID int64
|
2023-02-05 04:41:24 +04:00
|
|
|
Date time.Time
|
2023-09-24 19:03:29 +04:00
|
|
|
Title string
|
2023-02-05 04:41:24 +04:00
|
|
|
Description string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:02:43 +04:00
|
|
|
func New(body, meta []byte) (article *Article, err error) {
|
|
|
|
article = &Article{Body: body}
|
|
|
|
err = article.ParseMetadata(meta)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-05 04:41:24 +04:00
|
|
|
func (artcl *Article) ParseMetadata(data []byte) error {
|
|
|
|
lines := bytes.Split(data, []byte{'\n'})
|
2023-05-23 04:10:50 +04:00
|
|
|
if len(lines) != 4 {
|
|
|
|
return errors.New("metadata file is not complete. 4 lines (id, title, date(DD MM YYYY), descr) should present")
|
2023-02-05 04:41:24 +04:00
|
|
|
}
|
|
|
|
|
2023-05-23 04:10:50 +04:00
|
|
|
artcl.ID, _ = strconv.ParseInt(string(lines[0]), 10, 64)
|
|
|
|
artcl.Title = string(lines[1])
|
|
|
|
date, err := time.Parse("02 01 2006", string(lines[2]))
|
2023-02-05 04:41:24 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
artcl.Date = date
|
|
|
|
|
2023-05-23 04:10:50 +04:00
|
|
|
artcl.Description = string(lines[3])
|
2023-02-05 04:41:24 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|