Skip to content

Commit

Permalink
Favoriteが登録できるように実装 refs #15
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed Jun 11, 2013
1 parent af51351 commit 69106ec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions html/guestbook/entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ <h4>好きなポケモンをおしえて</h4>
<option value="">-- chose element type --</option>
</select>
<br />
<select ng-model="entryForm.pokemonId" ng-options="p.id as p.name for p in pokemons">
<select ng-model="entryForm.pokemon" ng-options="p.name for p in pokemons">
<option value="">-- chose pokemon --</option>
</select>
<br />
<input type="text" ng-model="entryForm.name"></input>
<input type="text" ng-model="entryForm.nickname"></input>
<br />
<input type="email" ng-model="entryForm.email"></input>
<button type="submit" class="btn btn-primary" ng-disabled="form.$invalid">Save</button>
Expand Down
16 changes: 10 additions & 6 deletions process/favorite/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import (
)

type Favorite struct {
Id string `json:"id"`
Id string `json:"id" datastore:"-"`
PokemonName string `json:"pokemonName"`
Nickname string `json:nickname`
Email string `json:email`
Created time.Time `json:"created"`
}

func (f *Favorite) key(c appengine.Context) *datastore.Key {
return datastore.NewKey(c, "Favorite", fmt.Sprintf("%s-_-%s"), 0, nil)
return datastore.NewKey(c, "Favorite", fmt.Sprintf("%s-_-%s", f.Email, f.Nickname), 0, nil)
}

func (t *Favorite) save(c appengine.Context) (*Favorite, error) {
k, err := datastore.Put(c, t.key(c), t)
func (f *Favorite) save(c appengine.Context) (*Favorite, error) {
f.Created = time.Now()
k, err := datastore.Put(c, f.key(c), f)
if err != nil {
return nil, err
}
t.Id = k.StringID()
return t, nil
f.Id = k.StringID()
return f, nil
}

func decodeFavorite(r io.ReadCloser) (*Favorite, error) {
Expand Down Expand Up @@ -75,6 +76,9 @@ func handleFavorites(c appengine.Context, r *http.Request) (interface{}, error)
return nil, err
}
return favorite.save(c)
case "GET":
return getAllFavorites(c)
}

return nil, fmt.Errorf("method not implemented")
}
18 changes: 12 additions & 6 deletions script/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
}]);

app.controller('GuestBookListController', ['$scope', '$resource', function($scope, $resource) {
var Store = $resource("/store");
$scope.stores = Store.query(function() {
console.log("success store query");
var Favorite = $resource("/favorite");
$scope.stores = Favorite.query(function() {
console.log("success favorite query");
}, function(){
console.log("error store query");
console.log("error favorite query");
});
}]);

Expand All @@ -51,7 +51,7 @@
{"id" : "2", "name" : "ほのお"},
{"id" : "3", "name" : "みず"}];
var Pokemon = $resource("/pokemon");
var Store = $resource("/store");
var Favorite = $resource("/favorite");

$scope.changeElementType = function() {
console.log($scope.entryForm.elementTypeId);
Expand All @@ -65,7 +65,13 @@

$scope.submit = function($event) {
console.log($scope.entryForm);
Store.save($scope.entryForm, function(){
var value = {
pokemonName : $scope.entryForm.pokemon.name,
nickname : $scope.entryForm.nickname,
email : $scope.entryForm.email
}
console.log(value);
Favorite.save(value, function(){
console.log("success entry");
$location.path('/#/guestbook/');
}, function(){
Expand Down

0 comments on commit 69106ec

Please sign in to comment.