示例#1
0
func (r *CQLOinkRepo) FindByID(id string) (model.Oink, bool, error) {
	r.lock.RLock()
	defer r.lock.RUnlock()
	if !r.initialized {
		return model.Oink{}, false, errors.New("Uninitialized repo")
	}

	iter := r.session.Query("SELECT content, created_at, handle FROM oinker.oinks WHERE id = ?", id).Iter()
	var (
		content      string
		creationTime gocql.UUID
		handle       string
	)
	oink := model.Oink{
		ID: id,
	}
	if iter.Scan(&content, &creationTime, &handle) {
		oink.Content = content
		oink.CreationTime = creationTime.Time()
		oink.Handle = handle
	}
	if err := iter.Close(); err != nil {
		return model.Oink{}, false, fmt.Errorf("Selecting oink (id: %s): %s", id, err)
	}

	return oink, false, nil
}
示例#2
0
func (r *MockOinkRepo) Create(o model.Oink) (model.Oink, error) {
	r.lock.Lock()
	defer r.lock.Unlock()
	o.ID = strconv.Itoa(len(r.oinks))
	o.CreationTime = time.Now()
	r.oinks = append(r.oinks, o)
	return o, nil
}
示例#3
0
func (r *CQLOinkRepo) Create(o model.Oink) (model.Oink, error) {
	r.lock.RLock()
	defer r.lock.RUnlock()
	if !r.initialized {
		return model.Oink{}, errors.New("Uninitialized repo")
	}

	o.ID = uuid.NewV4().String()
	o.CreationTime = time.Now()

	err := r.session.Query(
		"INSERT INTO oinker.oinks (kind, id, content, created_at, handle) "+
			"VALUES (?, ?, ?, ?, ?)",
		"oink", o.ID, o.Content, gocql.UUIDFromTime(o.CreationTime), o.Handle,
	).Exec()
	if err != nil {
		return model.Oink{}, fmt.Errorf("Inserting oink (%+v): %s", o, err)
	}

	return o, nil
}