<commit_before>package bolt

import (
	"github.com/boltdb/bolt"
	"github.com/golang/protobuf/proto"
	"github.com/satori/go.uuid"

	"github.com/octavore/press/db"
	"github.com/octavore/press/proto/press/models"
)

const PAGE_BUCKET = "pages"

var _ db.AddressableProto = &models.Page{}
var _ db.TimestampedProto = &models.Page{}

func (m *Module) GetPage(uuid string) (*models.Page, error) {
	page := &models.Page{}
	err := m.Get(PAGE_BUCKET, uuid, page)
	if err != nil {
		return nil, err
	}
	return page, nil
}

func (m *Module) DeletePage(page *models.Page) error {
	err := m.delete(PAGE_BUCKET, page)
	if err != nil {
		return err
	}
	routes, err := m.ListRoutes()
	if err != nil {
		return err
	}
	for _, route := range routes {
		if route.GetPageUuid() == page.GetUuid() {
			err := m.DeleteRoute(route)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (m *Module) UpdatePage(page *models.Page) error {
	if page.GetUuid() == "" {
		page.Uuid = proto.String(uuid.NewV4().String())
	}
	return m.Update(PAGE_BUCKET, page)
}

func (m *Module) ListPages() ([]*models.Page, error) {
	lst := []*models.Page{}
	err := m.Bolt.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(PAGE_BUCKET))
		return b.ForEach(func(_, v []byte) error {
			pb := &models.Page{}
			err := proto.Unmarshal(v, pb)
			if err != nil {
				return err
			}
			lst = append(lst, pb)
			return nil
		})
	})
	if err != nil {
		return nil, err
	}
	return lst, nil
}<commit_msg>bolt: Set UUID on page contents.<commit_after>package bolt

import (
	"github.com/boltdb/bolt"
	"github.com/golang/protobuf/proto"
	"github.com/satori/go.uuid"

	"github.com/octavore/press/db"
	"github.com/octavore/press/proto/press/models"
)

const PAGE_BUCKET = "pages"

var _ db.AddressableProto = &models.Page{}
var _ db.TimestampedProto = &models.Page{}

func (m *Module) GetPage(uuid string) (*models.Page, error) {
	page := &models.Page{}
	err := m.Get(PAGE_BUCKET, uuid, page)
	if err != nil {
		return nil, err
	}
	return page, nil
}

func (m *Module) DeletePage(page *models.Page) error {
	err := m.delete(PAGE_BUCKET, page)
	if err != nil {
		return err
	}
	routes, err := m.ListRoutes()
	if err != nil {
		return err
	}
	for _, route := range routes {
		if route.GetPageUuid() == page.GetUuid() {
			err := m.DeleteRoute(route)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (m *Module) UpdatePage(page *models.Page) error {
	if page.GetUuid() == "" {
		page.Uuid = proto.String(uuid.NewV4().String())
	}

	for _, c := range page.Contents {
		if c.GetUuid() == "" {
			c.Uuid = proto.String(uuid.NewV4().String())
		}
	}
	return m.Update(PAGE_BUCKET, page)
}

func (m *Module) ListPages() ([]*models.Page, error) {
	lst := []*models.Page{}
	err := m.Bolt.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(PAGE_BUCKET))
		return b.ForEach(func(_, v []byte) error {
			pb := &models.Page{}
			err := proto.Unmarshal(v, pb)
			if err != nil {
				return err
			}
			lst = append(lst, pb)
			return nil
		})
	})
	if err != nil {
		return nil, err
	}
	return lst, nil
}
{'fraction_non_alphanumeric': 0.1703099510603589, 'fraction_numerical': 0.0009787928221859706, 'mean_word_length': 3.3427762039660056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2798544', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1218, 'n_words': 279}