github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/appengine/login/gitkit/storage.go (about)

     1  package gitkit
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	aelog "google.golang.org/appengine/log"
     8  
     9  	"google.golang.org/appengine"
    10  
    11  	aeOrig "appengine"
    12  
    13  	"appengine/datastore"
    14  )
    15  
    16  var (
    17  	weekdays = []time.Weekday{
    18  		time.Sunday,
    19  		time.Monday,
    20  		time.Tuesday,
    21  		time.Wednesday,
    22  		time.Thursday,
    23  		time.Friday,
    24  		time.Saturday,
    25  	}
    26  )
    27  
    28  type FavWeekday struct {
    29  	// User ID. Serves as primary key in datastore.
    30  	ID string
    31  	// 0 is Sunday.
    32  	Weekday time.Weekday
    33  }
    34  
    35  // weekdayForUser fetches the favorite weekday for the user from the datastore.
    36  // Sunday is returned if no such data is found.
    37  func weekdayForUser(r *http.Request, u *User) time.Weekday {
    38  	c := aeOrig.NewContext(r)
    39  	c2 := appengine.NewContext(r)
    40  	k := datastore.NewKey(c, "FavWeekday", u.ID, 0, nil)
    41  	d := FavWeekday{}
    42  	err := datastore.Get(c, k, &d)
    43  	if err != nil {
    44  		if err != datastore.ErrNoSuchEntity {
    45  			aelog.Errorf(c2, "Failed to fetch the favorite weekday for user %+v: %s", *u, err)
    46  		}
    47  		return time.Sunday
    48  	}
    49  	return d.Weekday
    50  }
    51  
    52  // updateWeekdayForUser updates the favorite weekday for the user.
    53  func updateWeekdayForUser(r *http.Request, u *User, d time.Weekday) {
    54  	c := aeOrig.NewContext(r)
    55  	c2 := appengine.NewContext(r)
    56  	k := datastore.NewKey(c, "FavWeekday", u.ID, 0, nil)
    57  	_, err := datastore.Put(c, k, &FavWeekday{u.ID, d})
    58  	if err != nil {
    59  		aelog.Errorf(c2, "Failed to update the favorite weekday for user %+v: %s", *u, err)
    60  	}
    61  }