Commit 1d0d8f43 authored by nemoNoboru's avatar nemoNoboru

done

parent aedaf069
...@@ -14,7 +14,6 @@ app.use(bodyParser.urlencoded({ extended: false })) ...@@ -14,7 +14,6 @@ app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(cors()) app.use(cors())
var people_path = '/people'
function end(res,err) { function end(res,err) {
if (err) { if (err) {
...@@ -24,6 +23,8 @@ function end(res,err) { ...@@ -24,6 +23,8 @@ function end(res,err) {
} }
} }
var people_path = '/people'
app.get(people_path,function (req,res) { app.get(people_path,function (req,res) {
people.index(function (err,people) { people.index(function (err,people) {
res.send(people) res.send(people)
...@@ -43,6 +44,12 @@ app.put(people_path,function (req,res) { ...@@ -43,6 +44,12 @@ app.put(people_path,function (req,res) {
}) })
}) })
app.delete(people_path+"/:person_id/pets/:petname",function (req,res) {
people.removePet(req.params.person_id,req.params.petname, function (err) {
end(res,err)
})
})
app.delete(people_path+"/:person_id",function (req,res) { app.delete(people_path+"/:person_id",function (req,res) {
people.remove(req.params.person_id, function (err) { people.remove(req.params.person_id, function (err) {
end(res,err) end(res,err)
...@@ -55,10 +62,5 @@ app.post(people_path+"/pets",function (req,res) { ...@@ -55,10 +62,5 @@ app.post(people_path+"/pets",function (req,res) {
}) })
}) })
app.delete(people_path+"/pets",function (req,res) {
people.removePet(req.body.person_id,req.body.petname, function (err) {
end(res,err)
})
})
app.listen(port) app.listen(port)
...@@ -7,7 +7,7 @@ db.once('open',function () { ...@@ -7,7 +7,7 @@ db.once('open',function () {
people.model = mongoose.model('People',{ name: String, pets: Array }) people.model = mongoose.model('People',{ name: String, pets: Array })
people.index = function (callback) { people.index = function (callback) {
people.model.find({},callback) people.model.find({}, callback)
} }
people.create = function (personName, callback) { people.create = function (personName, callback) {
...@@ -15,27 +15,27 @@ db.once('open',function () { ...@@ -15,27 +15,27 @@ db.once('open',function () {
newPeople.save(callback) newPeople.save(callback)
} }
people.modify = function (id,name,callback) { people.modify = function (id, name, callback) {
people.model.findById(id).then(function (person) { people.model.findById(id).then(function (person) {
person.name = name person.name = name
person.save(callback) person.save(callback)
}) })
} }
people.remove = function (id,callback) { people.remove = function (id, callback) {
people.model.findById(id).remove(callback) people.model.findById(id).remove(callback)
} }
people.addPet = function (id,pet_name,callback) { people.addPet = function (id, pet_name, callback) {
people.model.findById(id).then(function (person) { people.model.findById(id).then(function (person) {
person.pets.push(pet_name) person.pets.push(pet_name)
person.save(callback) person.save(callback)
}) })
} }
people.removePet = function (id, pet_name , callback) { people.removePet = function (id, pet_name, callback) {
people.model.findById(id).then(function (person) { people.model.findById(id).then(function (person) {
person.pets.remove(pet_name) person.pets.splice(person.pets.indexOf(pet_name),1)
person.save(callback) person.save(callback)
}) })
} }
......
.cfield{
margin-bottom: 30px;
}
.margin {
margin-bottom: 20px;
}
.name {
margin: 30px;
}
...@@ -15,6 +15,16 @@ app.service('people', function ($http) { ...@@ -15,6 +15,16 @@ app.service('people', function ($http) {
$http.put('http://127.0.0.1:8000/people',{person_id: item._id, name: item.name}) $http.put('http://127.0.0.1:8000/people',{person_id: item._id, name: item.name})
.then(callback) .then(callback)
} }
this.addPet = function (item,pet_name,callback) {
$http.post('http://127.0.0.1:8000/people/pets',{person_id: item._id, petname: pet_name})
.then(callback)
}
this.removePet = function (item, pet_name, callback) {
$http.delete('http://127.0.0.1:8000/people/'+item._id+'/pets/'+pet_name)
.then(callback)
}
}) })
app.controller('peopleCtrl',function ($scope,people) { app.controller('peopleCtrl',function ($scope,people) {
...@@ -54,4 +64,17 @@ app.controller('peopleCtrl',function ($scope,people) { ...@@ -54,4 +64,17 @@ app.controller('peopleCtrl',function ($scope,people) {
}) })
} }
$scope.addPet = function (person,pet) {
people.addPet(person, pet, function () {
self.load()
$scope.pet = ''
})
}
$scope.removePet = function (person,pet) {
people.removePet(person, pet, function () {
self.load()
})
}
}) })
<html> <html>
<head> <head>
<title>Flatten!</title> <title>People!</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="assets/css/main.css" /> <link rel="stylesheet" href="assets/css/main.css" />
<link rel="stylesheet" href="assets/css/flatten.css" /> <link rel="stylesheet" href="assets/css/people.css" />
</head> </head>
<body ng-app='daa'> <body ng-app='daa'>
<div ng-controller='peopleCtrl as $ctrl'> <div ng-controller='peopleCtrl as $ctrl'>
...@@ -24,13 +24,31 @@ ...@@ -24,13 +24,31 @@
</div> </div>
<hr/> <hr/>
<div class="box" ng-repeat='i in people_list'> <div class="box" ng-repeat='i in people_list'>
<span ng-hide='editing'>{{i.name}}</span> <!-- just show name -->
<div class="row">
<div class="12u margin">
<span class='name' ng-hide='editing'>{{i.name}}</span>
<button ng-hide='editing' ng-click='edit(i.name)'>edit</button> <button ng-hide='editing' ng-click='edit(i.name)'>edit</button>
<button ng-click='remove(i)'>Trash</button>
<!-- hide name and show input text -->
<input ng-show='editing' class="text" ng-model='e'/> <input ng-show='editing' class="text" ng-model='e'/>
<button ng-click='save(i,e)' ng-show='editing'>save</button> <button ng-click='save(i,e)' ng-show='editing'>save</button>
<section class='align-right'> </div>
<button class='align-right' ng-click='remove(i)'>Trash</button> <div class="12u margin">
</section> <!-- pets -->
<button ng-click='removePet(i,p)' ng-repeat='p in i.pets'>
{{p}} X
</button>
</div>
<div class="10u">
<input type="text" ng-model='pet'>
</div>
<div class="2u margin">
<button ng-click='addPet(i,pet)'>add</button>
</div>
</div>
</div> </div>
</div> </div>
</section> </section>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment