Skip to content

Commit

Permalink
Merge branch 'feature/#5' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed May 19, 2013
2 parents e651312 + 0904919 commit 39e0a27
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 70 deletions.
48 changes: 35 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,41 @@
<body>
<div ng-controller="MainController">
<h2>Hello AngularJS !!</h2>
<form ng-submit="submit()">
<select ng-model="entryForm.categoryId" ng-options="c.id as c.name for c in categories" ng-change="changeCategory()">
<option value="">-- chose category --</option>
</select>
<br />
<select ng-model="entryForm.itemId" ng-options="i.id as i.name for i in items">
<option value="">-- chose item --</option>
</select>
<br />
<input type="text" ng-model="entryForm.name"></input>
<br />
<button type="submit" class="btn">submit</button>
</form>
<div>
<table class="table">
<thead>
<tr>
<td>No</td>
<td>CategoryId</td>
<td>ItemId</td>
<td>Name</td>
</tr>
</thead>
<tbody ng-repeat="s in stores">
<tr>
<td>{{$index}}</td>
<td>{{s.CategoryId}}</td>
<td>{{s.ItemId}}</td>
<td>{{s.Name}}</td>
</tr>
</tbody>
</table>
</div>
<div>
<form ng-submit="submit()">
<select ng-model="entryForm.categoryId" ng-options="c.id as c.name for c in categories" ng-change="changeCategory()">
<option value="">-- chose category --</option>
</select>
<br />
<select ng-model="entryForm.itemId" ng-options="i.id as i.name for i in items">
<option value="">-- chose item --</option>
</select>
<br />
<input type="text" ng-model="entryForm.name"></input>
<br />
<button type="submit" class="btn">submit</button>
</form>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//code.angularjs.org/1.0.6/angular.min.js"></script>
Expand Down
14 changes: 12 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
app.controller("MainController", function($scope, $resource){
$scope.categories = [{"id" : "1", "name" : "野菜"}];
var List = $resource("/item/list");
var Entry = $resource("/store");
var Store = $resource("/store");

$scope.stores = Store.query(function() {
console.log("success store query");
console.log($scope.stores);
console.log($scope.stores[0].CategoryId);
}, function(){
console.log("error store query");
});

$scope.changeCategory = function() {
$scope.items = List.query({id : $scope.entryForm.categoryid}, function(){
console.log("success list");
}, function(){
console.log("error list");
});
};

$scope.submit = function($event) {
console.log($scope.entryForm);
Entry.save($scope.entryForm, function(){
Store.save($scope.entryForm, function(){
console.log("success entry");
}, function(){
console.log("error entry");
Expand Down
78 changes: 78 additions & 0 deletions process/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package store

import (
"appengine"
"appengine/datastore"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

func init() {
http.HandleFunc("/store", handler)
}

type Store struct {
CategoryId int `json:",string"`
ItemId int `json:",string"`
Name string
}

func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
post(w, r)
case "GET":
get(w, r)
default:
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Not Found")
}
}

func post(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)

body, _ := ioutil.ReadAll(r.Body)
var s Store
if err := json.Unmarshal(body, &s); err != nil {
c.Errorf("Error unmarshal Store: %s", err)
return
}
c.Infof("CategoryId=%s", s.CategoryId)
c.Infof("ItemId=%s", s.ItemId)
c.Infof("Name=%s", s.Name)

name := fmt.Sprintf("%d-_-%d-_-%s", s.CategoryId, s.ItemId, s.Name)
key := datastore.NewKey(c, "Store", name, 0, nil)
if _, err := datastore.Put(c, key, &s); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

fmt.Fprintf(w, "OK")
}

func get(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)

var stores []*Store

q := datastore.NewQuery("Store")
_, err := q.GetAll(c, &stores)
if err != nil {
c.Errorf("query error : %v", err)
return
}

j, err := json.Marshal(stores)
if err != nil {
c.Errorf("json marshal error : %v", err)
return
}

c.Infof("%s", j)
fmt.Fprintf(w, "%s", j)
}
55 changes: 0 additions & 55 deletions store/entry.go

This file was deleted.

0 comments on commit 39e0a27

Please sign in to comment.