Created
March 5, 2015 02:44
-
-
Save astemborskim/edc5d4a80dfe6e154c99 to your computer and use it in GitHub Desktop.
Client data to server side controller?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.controller('listController', ['$scope', '$resource', | |
function ($scope, $resource){ | |
$scope.prof={}; | |
$scope.prof.check=false; | |
//$scope.prof.newName = {}; | |
//Rest API Route on server | |
var Listing = $resource('/api/listing/:id', {}, { update : {method : 'PUT'}}); | |
Listing.query(function (results){ | |
$scope.listings=results; | |
//console.log('Query:\n' + JSON.stringify(results)); | |
}) | |
$scope.postListing = function(){ | |
var listing = new Listing(); | |
//console.log('listing:' + JSON.stringify(listing)); | |
listing.name = $scope.prof.petName; | |
listing.desc = $scope.prof.petDesc; | |
//console.log(JSON.stringify(listing)); | |
if(listing.name != null && listing.desc != null){ | |
listing.$save(function (results){ | |
$scope.listings.push(results); | |
$scope.prof.petName=null; | |
$scope.prof.petDesc=null; | |
var postid = results._id; | |
$scope.prof.post_id = postid; | |
//toggle ng-show/ng-hide forms | |
//$scope.prof.check=true; | |
}); | |
}//end if | |
else{console.log('NULL VALUE');} | |
} | |
$scope.updateListing = function (object, idx){ | |
//console.log('Update: ' + JSON.stringify(object) + " @ " + idx); | |
var listing = new Listing(); | |
listing.name = $scope.prof.newName[idx]; | |
if(listing.name != null){ | |
listing.$update({id : object._id}, listing.name); | |
$scope.listings[idx].name=listing.name; | |
$scope.prof.newName=null; | |
}; | |
} | |
$scope.removeListing = function (object, idx){ | |
var listing = new Listing(); | |
//console.log('list-Cntr: ' + object._id + "\n object.index: " + idx); | |
//console.log('Listing: ' + JSON.stringify(listing)); | |
listing.$remove({id : object._id}); | |
$scope.listings.splice(idx, 1); | |
} | |
$scope.getId = function (object, idx){ | |
console.log($scope.post_id); | |
} | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends layout | |
block navigation | |
div(class="navbar navbar-default") | |
ul(class="nav nav-pills") | |
li | |
a(href='/') Home | |
li | |
a(href='/cats') Cats | |
li | |
a(href='/dogs') Dogs | |
li | |
a(href='/fish') Fish | |
li | |
a(href='/reptiles') Reptiles | |
li | |
a(href='/small_animals') Small Animals | |
li | |
a(href='/forum') Forums | |
li.active | |
a(href='/list') List a Pet | |
block heading | |
span(class="panel panel-primary") | |
h2(class="panel panel-heading") | |
| List a Pet for Adoption | |
block content | |
div(ng-controller="listController") | |
div(id="wrap-form") | |
form(id="petInfo" ng-hide="prof.check==true") | |
div | |
fieldset(class="well") | |
legend(class="the-legend") | |
| Step 1: Basic Pet Info: | |
input(type="text" placeholder="Pet's name" ng-model="prof.petName") | |
div | |
input(type="text", placeholder="Pet Description" ng-model="prof.petDesc") | |
div | |
button(type="button" ng-click="postListing()") | |
| Post | |
form(id="petPhoto" ng-hide="prof.check==true" enctype="multipart/form-data" action = "/api/image" method = "post") | |
div | |
fieldset(class="well") | |
legend(class="the-legend") | |
| Step 2: Upload Photo: | |
input( type="file" name="image" ) | |
div | |
input( type="submit" name="submit" value="Upload Image") | |
p Recently listed: | |
div | |
li(ng-repeat= "list in listings") {{list.name}} | |
input(type="text" ng-model="prof.newName[$index]" ng-show="checked" placeholder="Edit Name Here...") | |
button(type="button" ng-click="updateListing(list, $index); checked = !checked" ng-show="checked") | |
| Update | |
button(type="button" ng-click="removeListing(list, $index)" ng-show="checked") | |
| Delete | |
input(type="checkbox" ng-model="checked" ) | |
div(id="catProfiles" ng-controller="testController" class="panel panel-info") | |
h3(class="panel panel-heading") | |
| Cat Profiles: | |
div(class="container-fluid") | |
div | |
div(id="profile-container" class="thumbnail") | |
div(id="image-container") | |
img(id="pet-image" ng-src="{{prof.image}}" alt="{{prof.name}}") | |
div | |
div(id="pet-desc" class="caption") | |
h3 Name goes here: {{prof.name}} | |
p Description goes here: {{prof.desc}} | |
a(id="button-adopt" href="#" class="btn btn-primary" role="button") | |
span(class="glyphicon glyphicon-heart") | |
| Adopt | |
a(id="button-info" href="#" class="btn btn-default" role="button") | |
span(class="glyphicon glyphicon-info-sign") | |
| Info | |
block footer | |
p © AniDopt Pet Services 2015 | |
script( type="text/javascript" src="/js/controllers/list-controller.js") | |
script( type="text/javascript" src="/js/controllers/test-controller.js") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PetModel = require('../models/pet-profiles'); | |
var mongoose = require('mongoose'); | |
//var formidable = require('formidable'); | |
var util = require('util'); | |
var fs = require('fs'); | |
module.exports.uploadList = function(req, res){ | |
//console.log('Output: ' + JSON.stringify(req.body)); | |
var petmodel = new PetModel(req.body); | |
petmodel.save(function (err, result){ | |
res.json(result); | |
}); | |
} | |
module.exports.downloadList = function(req, res){ | |
PetModel.find({}, function (err, results){ | |
res.json(results); | |
}); | |
} | |
module.exports.getOne = function(req, res){ | |
PetModel.find({ _id : req.params.id }, function (err, results){ | |
res.json(results); | |
}); | |
} | |
module.exports.updateList = function(req, res){ | |
//var query = {_id: req.param.id}; | |
PetModel.update({ _id : req.params.id }, req.body, function (err, results){ | |
if(err){console.log(err);} | |
res.json(results); | |
}); | |
} | |
module.exports.deleteList = function(req, res){ | |
//console.log('req.params.id: ' + JSON.stringify(req.params.id)); | |
PetModel.remove({ _id : req.params.id }, function (err, results){ | |
if(err){console.log(err);} | |
res.json(results); | |
}); | |
} | |
module.exports.uploadImage = function(req, res){ | |
//NEED the id from mongo for the first submission here so i can update with image path | |
console.log('Body: ' + JSON.stringify(req.body)); | |
console.log('File: ' + JSON.stringify(req.files)) | |
res.json(req.body); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
app = express(), | |
mongodb = require('mongodb'), | |
mongoose = require('mongoose'), | |
bodyparser = require('body-parser'), | |
stylus = require('stylus'), | |
multer = require('multer'), | |
serverController = require('./server/controllers/server-controller'); | |
//DB connection | |
mongoose.connect('mongodb://localhost:27017/anidopt'); | |
//routes defined | |
var routes = require('./client/js/routes/index'); | |
var cats = require('./client/js/routes/cats'); | |
var dogs = require('./client/js/routes/dogs'); | |
var fish = require('./client/js/routes/fish'); | |
var birds = require('./client/js/routes/birds'); | |
var reptiles = require('./client/js/routes/reptiles'); | |
var small_animals = require('./client/js/routes/small_animals'); | |
var forum = require('./client/js/routes/forum'); | |
var list = require('./client/js/routes/list'); | |
//View engine config | |
app.set('views', __dirname + '/client/views'); | |
app.set('view engine', 'jade'); | |
app.use(bodyparser.json()); | |
//app.use(express.bodyparser()); | |
app.use(express.static(__dirname + '/client/')); | |
app.use(stylus.middleware({ | |
src: __dirname + '/css', | |
dest: __dirname + '/css', | |
debug: true, | |
force: true | |
})); | |
//get route | |
app.get('/', routes); | |
app.get('/cats', cats); | |
app.get('/dogs', dogs); | |
app.get('/fish', fish); | |
app.get('/birds', birds); | |
app.get('/reptiles', reptiles); | |
app.get('/small_animals', small_animals); | |
app.get('/forum', forum); | |
app.get('/list', list); | |
//REST API Routes | |
app.post('/api/listing', serverController.uploadList); | |
app.get('/api/listing', serverController.downloadList); | |
app.get('/api/listing/:id', serverController.getOne); | |
app.delete('/api/listing/:id', serverController.deleteList); | |
app.put('/api/listing/:id', serverController.updateList); | |
app.post('/api/image/', [multer({ | |
dest: './server/uploads/', | |
rename: function (fieldname, filename) { | |
return filename+Date.now(); | |
} | |
}), serverController.uploadImage | |
]); | |
//Start server on port 3000 | |
app.listen(3000, function () { | |
console.log("Server is listening..."); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment