Skip to content

Commit

Permalink
Farm Editing Implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Williams committed Aug 26, 2015
1 parent c8953b4 commit ec822e4
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 60 deletions.
46 changes: 44 additions & 2 deletions public/javascripts/controllers/farmer-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ angular.module('jasmic.controllers')
*/
.controller('FarmerProfileCtrl', ['$scope', '$location', '$routeParams', '$mdDialog', 'OpenTransactionsFactory',
'TransactionsFactory', 'FarmerFactory', 'ParishesFactory', 'FarmerFarmFactory', 'CropsFactory',
'UnitsFactory', 'CommodityFactory','CommoditiesFactory', 'DistrictsFactory', 'FarmerMembershipsFactory','CommodityEditFactory',
'UnitsFactory', 'CommodityFactory','CommoditiesFactory', 'DistrictsFactory', 'FarmerMembershipsFactory','CommodityEditFactory', 'FarmEditFactory',
function ($scope, $location, $routeParams, $mdDialog, OpenTransactionsFactory, TransactionsFactory,
FarmerFactory, ParishesFactory, FarmerFarmFactory, CropsFactory, UnitsFactory,
CommodityFactory, CommoditiesFactory, DistrictsFactory, FarmerMembershipsFactory, CommodityEditFactory) {
CommodityFactory, CommoditiesFactory, DistrictsFactory, FarmerMembershipsFactory, CommodityEditFactory, FarmEditFactory) {
/**
* First query for the farmer based on the id supplied in the parameters,
* then query for the transactions this farmer has been involved in.
Expand Down Expand Up @@ -118,6 +118,22 @@ angular.module('jasmic.controllers')
$scope.newFarm = !$scope.newFarm;
};

/*.updateFarm = function() {
$scope.farm.di_district = selectedDistrict;
FarmerFarmFactory.create({id:$scope.farmer._id}, $scope.farm, function(success) {
$scope.newFarm = !$scope.newFarm;
loadAll();
}, function(fail) {
console.log(fail);
console.log($scope.farm);
showDialog($mdDialog, fail, true);
});
};*/

$scope.cancelEdit = function() {
$scope.editFarm = !$scope.editFarm;
};

/**
* Necessary to load all parishes in the necessary forms
*/
Expand All @@ -137,6 +153,14 @@ angular.module('jasmic.controllers')
$scope.newFarm = !$scope.newFarm;
$scope.farm = {};
};

$scope.editFarmLocation = function(arrayindex) {

$scope.editFarm = !$scope.editFarm;
$scope.farm = $scope.farmer.fr_farms[arrayindex];

};

$scope.newCommodityItem = function() {
$scope.newCommodity = !$scope.newCommodity;
$scope.commodity = {};
Expand Down Expand Up @@ -169,6 +193,24 @@ angular.module('jasmic.controllers')

}


$scope.updateFarm = function(arrayindex) {

FarmEditFactory.update({farm_id:$scope.farm._id}, $scope.farm, function(success) {

console.log("Success");

This comment has been minimized.

Copy link
@tremainebuchanan

tremainebuchanan Aug 26, 2015

Collaborator

@nick22891 we can use dialogs here to tell the user the outcome of the update/edit operations.


$scope.editFarm = false;

}, function (error) {

console.log("Error");

});

}


$scope.newCommodity = false;
$scope.newFarm = false;
$scope.editCommodity = false;
Expand Down
10 changes: 10 additions & 0 deletions public/javascripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ services.factory('CommodityEditFactory', function($resource){
});
});

/**
* Updates a farm
*/

services.factory('FarmEditFactory', function($resource){
return $resource('farm/:farm_id', {}, {
update: { method: 'PUT', params: {farm_id: '@farm_id'} },
});
});

/**
* Adds a new Demand and associates it with a buyer
*/
Expand Down
56 changes: 56 additions & 0 deletions public/partials/farm_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<md-content layout-padding layout="column">
<div layout="column">
<md-autocomplete flex
ng-required="true"
ng-disabled="false"
md-no-cache="true"
md-selected-item="selectedItem"
md-search-text-change="searchTextChange(searchText)"
md-search-text="searchText"
md-selected-item-change="selectedDistrictChange(item)"
md-items="item in queryDistrictSearch(searchText)"
md-item-text="item.di_extension_name+(item.di_district_name == ''? '' : ' - ')+item.di_district_name"
md-min-length="3"
placeholder="Please Enter the District or Extension Name">
<md-item-template>
<span md-highlight-text="searchText" md-highlight-flags="^i">{{item.di_extension_name}}
{{item.di_district_name == ""? "" : " - " + item.di_district_name}}</span>
</md-item-template>
<md-not-found>
No matches found for "{{searchText}}".
</md-not-found>
</md-autocomplete>
<md-input-container flex>
<label>Farm Name</label>
<input type="text" ng-model="farm.fr_name">
</md-input-container>
</div>


<md-input-container>
<label>Address 1</label>
<input type="text" ng-model="farm.ad_address1">
</md-input-container>


<div layout="row">
<md-input-container flex>
<label>Address 2</label>
<input type="text" ng-model="farm.ad_address2">
</md-input-container>
<md-input-container flex>
<label>City</label>
<input type="text" ng-model="farm.ad_city">
</md-input-container>
</div>

<md-input-container>
<label>Farm Size (Acres)</label>
<input type="number" ng-model="farm.fr_size">
</md-input-container>

<md-input-container layout="row" layout-align="center center">
<md-button ng-click="cancelAdd()">Cancel</md-button>
<md-button ng-click="updateFarm(0)">Update</md-button>
</md-input-container>
</md-content>
61 changes: 4 additions & 57 deletions public/partials/farmer_profile_large.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,66 +98,13 @@ <H1 class="farmerSummary">{{farmer.fa_first_name}} {{farmer.fa_middle_name}} {{f
</md-card>

<md-card>
<md-card-content class="{{newFarm ? 'dark':'white'}}">
<div layout="row">
<h3 class="{{newFarm ? 'dark':'white'}}">Farms</h3>
<div flex class="md-actions edge" layout="row" layout-align="end center">
<md-button ng-click="newFarmLocation()" class="md-fab edge" aria-label="Add New Farm">
<md-icon class="edge" md-svg-icon="/images/ic_add_24px.svg"></md-icon>
</md-button>
</div>
</div>
<div ng-include="newFarm ? '/partials/farm_new.html':null">

</div>
</md-card-content>

<md-card-content>
<div class="farmerInfo" layout="column" layout-padding ng-repeat="farm in farmer.fr_farms">
<div layout-align="space-around start" layout="row" >
<md-icon flex="5" md-svg-icon="/images/ic_location_on_24px.svg"></md-icon>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.fr_name}}
<div class="label">Farm Name</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.fr_size}}
<div class="label">Farm Size (Acre)</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.ad_address1}}
<div class="label">Address 1</div>
</div>
</div>
</div>
<div layout-align="space-around start" layout-padding layout="row">
<span flex="5"></span>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.ad_city}}
<div class="label">City</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.di_district.di_parish_name}}
<div class="label">Parish</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.di_district.di_extension_name}} - {{farm.di_district.di_district_name}}
<div class="label">Extension - District</div>
</div>
</div>
</div>
</div>
<md-card-content ng-include="'/partials/farms_card.html'">


</md-card-content>

</md-card>


Expand Down
68 changes: 68 additions & 0 deletions public/partials/farms_card.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<md-card-content class="{{newFarm ? 'dark':'white'}}">
<div layout="row">
<h3 class="{{newFarm ? 'dark':'white'}}">Farms</h3>
<div flex class="md-actions edge" layout="row" layout-align="end center">
<md-button ng-click="newFarmLocation()" class="md-fab edge" aria-label="Add New Farm">
<md-icon class="edge" md-svg-icon="/images/ic_add_24px.svg"></md-icon>
</md-button>
</div>
</div>
<div ng-include="newFarm ? '/partials/farm_new.html':null">

</div>

<div ng-include="editFarm ? '/partials/farm_edit.html':null">

</div>

</md-card-content>

<md-card-content>
<div class="farmerInfo" layout="column" layout-padding ng-repeat="farm in farmer.fr_farms">
<div layout-align="space-around start" layout="row" >
<md-icon flex="5" md-svg-icon="/images/ic_location_on_24px.svg"></md-icon>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.fr_name}}
<div class="label">Farm Name</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.fr_size}}
<div class="label">Farm Size (Acre)</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.ad_address1}}
<div class="label">Address 1</div>
</div>
</div>
</div>
<div layout-align="space-around start" layout-padding layout="row">
<span flex="5"></span>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.ad_city}}
<div class="label">City</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.di_district.di_parish_name}}
<div class="label">Parish</div>
</div>
</div>
<div layout="column" flex>
<div layout-align="start center" layout="column">
{{farm.di_district.di_extension_name}} - {{farm.di_district.di_district_name}}
<div class="label">Extension - District</div>
</div>
</div>
</div>
</div>

<button ng-click="editFarmLocation(0)">Edit This Farm</button>

</md-card-content>
15 changes: 14 additions & 1 deletion routes/farmer/farmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var model = require('../../models/db');
var common = require('../common/common');
var Commodity = model.Commodity;
var Farm = model.Farm;
var Demand = model.Demand;
var Branch = model.Branch;
var Membership = model.Membership;
Expand Down Expand Up @@ -242,7 +243,7 @@ exports.getCommodities = function(req, res) {
*/
exports.updateFarmById = function(req, res) {
if(common.isAuthenticated(req, res)) {
model.Farm.update({_id: req.params.farm_id}, req.body, function (err, response) {
/*model.Farm.update({_id: req.params.farm_id}, req.body, function (err, response) {
if (err) {
handleDBError(err, res);
} else {
Expand All @@ -255,6 +256,18 @@ exports.updateFarmById = function(req, res) {
}
}
});
*/

Farm.findByIdAndUpdate({_id:req.params.farm_id}, req.body, function (err, changes) {
if (err) {
handleDBError(err, res);
} else {
res.send(changes);

This comment has been minimized.

Copy link
@tremainebuchanan

tremainebuchanan Aug 26, 2015

Collaborator

@nick22891 change the name of the variable "changes" to "result" and remove the console.log at the end of the function.

}
});

console.log(req.body);

}
};

Expand Down

1 comment on commit ec822e4

@matjames007
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A console.log( ) really should be a debugging statement and removed later.... @nick22891

Please sign in to comment.