package models

import (
  "path"
  "io/ioutil"
  "os"
  "encoding/json"
  "ghighlighter/utils"
)

type GhHighlights struct {
  items []GhHighlight
}

func (m *GhHighlights) GetAll() []GhHighlight {
  m.Read()
  return m.items
}

func highlightsFilePath() string {
  return path.Join(utils.DataDir(), "highlights.json")
}

func (m *GhHighlights) Read() {
  data, error := ioutil.ReadFile(highlightsFilePath())
  if error != nil && !os.IsExist(error) {
    m.Write()
    m.Read()
  }

  var tmpItems []GhHighlight
  json.Unmarshal(data, &tmpItems)

  m.items = make([]GhHighlight, len(tmpItems))
  for _, highlight := range tmpItems {
    m.items = append(m.items, highlight)
  }
}

func (m *GhHighlights) Write() {
  if m.items == nil {
    m.items = make([]GhHighlight, 0)
  }

  data, error := json.Marshal(m.items)
  if error != nil { return }

  error = ioutil.WriteFile(highlightsFilePath(), data, 0755)
}

func Highlights() *GhHighlights {
  highlights := &GhHighlights{}
  highlights.Read()
  return highlights
}


package models

import (
  "path"
  "io/ioutil"
  "os"
  "encoding/json"
  "ghighlighter/utils"
)

type GhHighlights struct {
  Items []GhHighlight
}

func highlightsFilePath() string {
  return path.Join(utils.DataDir(), "highlights.json")
}

func (m *GhHighlights) Read() {
  data, error := ioutil.ReadFile(highlightsFilePath())
  if error != nil && !os.IsExist(error) {
    m.Write()
    m.Read()
  }

  var tmpItems []GhHighlight
  json.Unmarshal(data, &tmpItems)

  for _, highlight := range tmpItems {
    m.Items = append(m.Items, highlight)
  }
}

func (m *GhHighlights) Write() {
  if m.Items == nil {
    m.Items = make([]GhHighlight, 0)
  }

  data, error := json.Marshal(m.Items)
  if error != nil { return }

  error = ioutil.WriteFile(highlightsFilePath(), data, 0755)
}

func Highlights() *GhHighlights {
  highlights := &GhHighlights{}
  highlights.Read()
  return highlights
}


Remove Highlights.GetAll and make Items public
Remove Highlights.GetAll and make Items public
