igar/storage/entry.go

72 lines
1.4 KiB
Go

package storage
import (
"fmt"
"strings"
"time"
)
type (
JSTime time.Time
Media struct {
Type string `json:"type"`
File string `json:"file"`
Height int `json:"height"`
Width int `json:"width"`
}
Entry struct {
Date JSTime `json:"date"`
Description string `json:"description"`
Post string `json:"post"`
Likes int `json:"likes"`
Tags []string `json:"tags"`
Media []Media `json:"media"`
Prev *Entry `json:"-"`
Next *Entry `json:"-"`
// Users []string
}
)
const (
jsTimeLite = "2006-01-02"
jsTimeLayout = "2006-01-02 15:04:05"
adminUsername = "danolo"
adminPassword = "bd4cad796950f50352225de3c773d8f3c39622bc17f34ad661eabe615cdf6d32751c5751e0648dc17d890f40330018334a2ae899878f200f6dc80121ddb70cc9"
)
// JSTime
func (ct *JSTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), `"`)
nt, err := time.Parse(jsTimeLayout, s)
*ct = JSTime(nt)
return
}
func (ct JSTime) MarshalJSON() ([]byte, error) {
return []byte(ct.String()), nil
}
func (ct JSTime) String() string {
t := time.Time(ct)
return fmt.Sprintf("%q", t.Format(jsTimeLayout))
}
func (ct JSTime) Unix() int64 {
return time.Time(ct).Unix()
}
// Entry
func (entry Entry) Count() map[string]int {
count := make(map[string]int)
for _, media := range entry.Media {
count[media.Type] += 1
}
return count
}
func (entry Entry) Len() int { return len(entry.Media) }