Ejemplo n.º 1
0
func (d *PostDriver) beforeSave(p *blog.Post) *ds.Key {
	if p.Id == "" {
		p.Id, _ = wcg.UUID()
		p.CreatedAt = time.Now()
	}
	p.UpdatedAt = time.Now()
	return d.NewKey(p.Id, 0, nil)
}
Ejemplo n.º 2
0
func NewApiToken() *ApiToken {
	token, err := wcg.UUID()
	if err != nil {
		panic(err)
	}
	return &ApiToken{
		Token:       token,
		Description: "",
		CreatedAt:   time.Now(),
	}
}
Ejemplo n.º 3
0
func genTestIEpg() *tv.IEpg {
	id, _ := wcg.UUID()
	start := time.Now()
	end := start.Add(50 * time.Minute)
	return &tv.IEpg{
		Id:        id,
		StationId: "stationId1", StationName: "stationName1",
		ProgramTitle: "program1", ProgramId: 1, Body: util.ByteString("content"),
		StartAt: start, EndAt: end, Category: "category",
		Cid: "cid1", Sid: "sid1",
		Optout: false, CreatedAt: time.Now(), UpdatedAt: time.Now(),
	}
}
Ejemplo n.º 4
0
func (d *EventDriver) Save(t *event.Event) (*event.Event, error) {
	_now := now()
	if t.Id == "" {
		uid, _ := wcg.UUID()
		t.Id = uid
		t.CreatedAt = _now
		t.UpdatedAt = _now
	} else {
		t.UpdatedAt = _now
	}
	key := d.NewKey(t.Id, 0, nil)
	_, err := d.Put(key, t)
	return t, err
}
Ejemplo n.º 5
0
func (d *ShowDriver) Save(s *event.Show) (*event.Show, error) {
	_now := now()
	if s.Id == "" {
		uid, _ := wcg.UUID()
		s.Id = uid
		s.CreatedAt = _now
		s.UpdatedAt = _now
	} else {
		s.UpdatedAt = _now
	}
	tkey := d.eventd.NewKey(s.EventId, 0, nil)
	key := d.NewKey(s.Id, 0, tkey)
	_, err := d.Put(key, s)
	return s, err
}
Ejemplo n.º 6
0
func (iepg *IEpg) ToTvRecord() *TvRecord {
	return &TvRecord{
		Id:        wcg.Must(wcg.UUID()).(string),
		Title:     strings.Replace(iepg.ProgramTitle, "/", "/", -1),
		Category:  iepg.Category,
		StartAt:   iepg.StartAt,
		EndAt:     iepg.EndAt,
		Cid:       iepg.Cid,
		Sid:       iepg.Sid,
		Uid:       "", // for future use.
		IEpgId:    iepg.Id,
		CreatedAt: iepg.CreatedAt,
		UpdatedAt: iepg.UpdatedAt,
	}
}
Ejemplo n.º 7
0
func NewPost(ownerId, blogId string) *Post {
	now := time.Now()
	id, _ := wcg.UUID()
	return &Post{
		Id:        id,
		Tags:      make([]string, 0),
		CreatedAt: now,
		UpdatedAt: now,
		PostDate:  time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()),
		OwnerId:   ownerId,
		BlogId:    blogId,
		IsDraft:   true,
		IsNew:     true,
	}
}
Ejemplo n.º 8
0
func NewTvRecord(title string, category string, start time.Time, end time.Time,
	cid string, sid string, uid string) *TvRecord {
	r := &TvRecord{
		Id:       wcg.Must(wcg.UUID()).(string),
		Title:    title,
		Category: category,
		StartAt:  start,
		EndAt:    end,
		Cid:      cid,
		Sid:      sid,
		Uid:      uid,
	}
	now := time.Now()
	r.CreatedAt = now
	r.UpdatedAt = now
	return r
}
Ejemplo n.º 9
0
func updateContents(appCtx *lib.AppContext, entries []*ameblo.AmebloEntry, memberList []*ameblo.Member) error {
	var d = NewAmebloEntryDriver(appCtx)
	// Analyze the references
	refs := make([]*ameblo.AmebloRef, 0)
	for _, ent := range entries {
		for _, m := range memberList {
			if ent.Owner != m.Name && m.IsMentionedIn(ent.Content) {
				refs = append(refs, &ameblo.AmebloRef{ent.Url, ent.Owner, m.Name})
			}
		}
	}

	// Then update content and refs.
	refd := NewAmebloRefDriver(appCtx)
	irefkeys := make([]*ds.Key, len(refs))
	drefkeys := make([]*ds.Key, 0)
	keys := make([]*ds.Key, len(entries))
	// preparing related keys
	for i, ent := range entries {
		ent.CrawledAt = time.Now()
		keys[i] = d.NewKey(ent.Url, 0, nil)
		if _drefkeys, err := refd.NewQuery().Ancestor(keys[i]).KeysOnly().GetAll(nil); datastore.IsDatastoreError(err) {
			return fmt.Errorf("Could not retrieve deletion keys for AmebloRef entities: %v", err)
		} else {
			drefkeys = append(drefkeys, _drefkeys...)
		}
	}
	for i, ref := range refs {
		uid, _ := wcg.UUID()
		irefkeys[i] = refd.NewKey(uid, 0, d.NewKey(ref.Url, 0, nil))
	}

	err := d.RunInTransaction(func(d *datastore.Driver) error {
		// remove all reference data under entries
		if len(drefkeys) > 0 {
			d.Logger.Debug("Deleting old %d AmebloRef entities...", len(drefkeys))
			if err := refd.DeleteMulti(drefkeys); err != nil {
				return fmt.Errorf("Could not delete old AmebloRef entities: %v", err)
			}
		}
		// insert refs
		if len(irefkeys) > 0 {
			d.Logger.Debug("Inserting new %d AmebloRef entities...", len(irefkeys))
			if _, err := refd.PutMulti(irefkeys, refs); datastore.IsDatastoreError(err) {
				return fmt.Errorf("Could not insert new AmebloRef entities: %v", err)
			}
		}
		return nil
	}, nil)
	if err != nil {
		return err
	}

	// update all entries
	d.Logger.Debug("Updating %d AmebloEntry entities...", len(keys))
	if _, err := d.PutMulti(keys, entries); datastore.IsDatastoreError(err) {
		return fmt.Errorf("Could not update AmebloEntry entities: %v", err)
	} else {
		return nil
	}
}