示例#1
0
// WithIDInClass returns the Student with a specific ID in a single class, if one exists.
func WithIDInClass(c appengine.Context, id string, class *classes.Class, asOf time.Time) (*Student, error) {
	key := datastore.NewKey(c, "Student", id, 0, class.Key(c))
	student := &Student{}
	switch err := datastore.Get(c, key, student); err {
	case nil:
		if student.DropIn && student.Date.Before(asOf) {
			return nil, ErrStudentNotFound
		}
		student.ID = key.StringID()
		return student, nil
	case datastore.ErrNoSuchEntity:
		return nil, ErrStudentNotFound
	default:
		return nil, err
	}
}
示例#2
0
// In returns a list of all Students registered for a class. The list
// will include only those drop-in Students whose date is not in the
// past.
func In(c appengine.Context, class *classes.Class, now time.Time) []*Student {
	q := datastore.NewQuery("Student").
		Ancestor(class.Key(c))
	students := []*Student{}
	_, err := q.GetAll(c, &students)
	if err != nil {
		c.Errorf("Failed to look up students for %d: %s", class.ID, err)
		return nil
	}
	filtered := []*Student{}
	for _, student := range students {
		if student.DropIn && student.Date.Before(now) {
			continue
		}
		filtered = append(filtered, student)
	}
	return filtered
}