loop save ok

This commit is contained in:
Baptiste Lemoine 2018-08-27 15:20:01 +02:00
parent f40ae1e1f5
commit 3fa2457cbe
5 changed files with 50 additions and 43 deletions

View File

@ -30,11 +30,11 @@
<p class="padded"> <p class="padded">
Euros disponibles au départ: Euros disponibles au départ:
<input type="number" ng-model="config.disponibility"> <input type="number" ng-model="config.disponibility" ng-change="save()">
<br> <br>
Gains moyen par mois: Gains moyen par mois:
<input type="number" ng-model="config.averageMonthlyEarnings"> <input type="number" ng-model="config.averageMonthlyEarnings" ng-disabled="config.isSaving">
</p> </p>
<p> <p>
<p> <p>

View File

@ -294,36 +294,38 @@ angular
$scope.config.loading = true; $scope.config.loading = true;
$http.get('get-my-expenses').then((rep) => { $http.get('get-my-expenses').then((rep) => {
console.log('get-my-expenses', rep.data.expenses); console.log('get-my-expenses', rep.data.expenses);
$scope.config.loading = false; $scope.config.loading = false;
$scope.config.initialLoadingDone = true; $scope.config.initialLoadingDone = true;
$scope.config.disponibility = rep.data.disponibility;
$scope.config.averageMonthlyEarnings = rep.data.averageMonthlyEarnings;
// default data when user has nothing saved // default data when user has nothing saved
console.log('rep.data.expenses.length',rep.data.expenses.length) console.log('rep.data.expenses.length', rep.data.expenses.length)
if(!rep.data.expenses.length){ if (!rep.data.expenses.length) {
$scope.expenses = exampleExpenses; $scope.expenses = exampleExpenses;
}else{ } else {
$scope.expenses = rep.data.expenses; $scope.expenses = rep.data.expenses;
} }
}, },
$scope.manageError) $scope.manageError)
}; };
// save TODO // save TODO
$scope.save = () => { $scope.save = () => {
if($scope.config.loading ){ if ($scope.config.loading) {
console.log('already saving'); console.log('already saving');
return; return;
} }
console.log('update expenses...'); console.log('update expenses...');
$scope.config.loading = true; $scope.config.loading = true;
$http.post('save-my-expenses', { $http.post('save-my-expenses', {
expenses: $scope.expenses, expenses: $scope.expenses,
config: $scope.config config: $scope.config
}) })
.then((rep) => { .then((rep) => {
console.log('save-my-expenses', rep); console.log('save-my-expenses', rep);
$scope.config.loading = false; $scope.config.loading = false;
}, },
$scope.manageError) $scope.manageError)
}; };
$scope.addExpense = () => { $scope.addExpense = () => {
@ -334,10 +336,10 @@ angular
amount: 0, amount: 0,
}) })
}; };
$scope.init = ()=>{ $scope.init = () => {
$scope.fetchExpenses(); $scope.fetchExpenses();
}; };
$scope.manageError = (error)=>{ $scope.manageError = (error) => {
console.error(error); console.error(error);
$scope.config.loading = false; $scope.config.loading = false;

View File

@ -162,6 +162,8 @@ class DefaultController extends Controller
return new JsonResponse([ return new JsonResponse([
'expenses' => $expensesOfUser, 'expenses' => $expensesOfUser,
'disponibility' => $currentUser->getDisponibility(),
'averageMonthlyEarnings' => $currentUser->getAverageMonthlyEarnings(),
]); ]);
} }
@ -170,7 +172,7 @@ class DefaultController extends Controller
* @param Request $request * @param Request $request
* @return JsonResponse the list of expanses * @return JsonResponse the list of expanses
*/ */
public function saveExpensesAction(Request $request) public function saveMyExpensesAction(Request $request)
{ {
$json = json_decode($request->getContent(), true); $json = json_decode($request->getContent(), true);
@ -186,33 +188,34 @@ class DefaultController extends Controller
// save the user configuration // save the user configuration
foreach ($json['expenses'] as $expens) { foreach ($json['expenses'] as $expens) {
if($expens['id']){ if(isset($expens['id'])){
$foundExpense = $categoriesByID[$expens['id']]; $foundExpense = $categoriesByID[$expens['id']];
if($foundExpense){ if($foundExpense){
// update existing expenses of logged in user
$foundExpense->setName($expens['name']); $foundExpense->setName($expens['name']);
$foundExpense->setAmount($expens['amount']); $foundExpense->setAmount($expens['amount']);
$foundExpense->setDelay($expens['delay']);
$foundExpense->setRepeatitions($expens['repeat']); $foundExpense->setRepeatitions($expens['repeat']);
$foundExpense->setEnabled($expens['enabled']); $foundExpense->setEnabled($expens['enabled']);
$m->persist($foundExpense); $m->persist($foundExpense);
}else{
// create new expense for user
$newExpense = new ExpenseKind();
$newExpense->setUser($currentUser);
$newExpense->setName($expens['name']);
$newExpense->setAmount($expens['amount']);
$newExpense->setRepeatitions($expens['repeat']);
$newExpense->setEnabled($expens['enabled']);
$m->persist($newExpense);
} }
}else{
// create new expense for user
$newExpense = new ExpenseKind();
$newExpense->setUser($currentUser);
$newExpense->setName($expens['name']);
$newExpense->setDelay($expens['delay']);
$newExpense->setAmount($expens['amount']);
$newExpense->setRepeatitions($expens['repeat']);
$newExpense->setEnabled($expens['enabled']);
$m->persist($newExpense);
} }
} }
foreach ($json['config'] as $conf) {
} $currentUser->setDisponibility($json['config']['disponibility']);
$nexExpense = new ExpenseKind(); $currentUser->setAverageMonthlyEarnings($json['config']['averageMonthlyEarnings']);
$m->persist($nexExpense);
$m->persist($currentUser); $m->persist($currentUser);
$m->flush(); $m->flush();

View File

@ -71,9 +71,10 @@ class User extends BaseUser {
//expenses previsionnel configs //expenses previsionnel configs
/** /**
* @ORM\Column(name="averageSpend", type="float", nullable=true) * @ORM\Column(name="averageMonthlyEarnings", type="float", nullable=true)
*/ */
private $averageSpend; private $averageMonthlyEarnings;
/** /**
* @ORM\Column(name="disponibility", type="float", nullable=true) * @ORM\Column(name="disponibility", type="float", nullable=true)
*/ */
@ -84,21 +85,20 @@ class User extends BaseUser {
*/ */
private $expenses; private $expenses;
/** /**
* @return mixed * @return mixed
*/ */
public function getAverageSpend() public function getAverageMonthlyEarnings()
{ {
return $this->averageSpend; return $this->averageMonthlyEarnings;
} }
/** /**
* @param mixed $averageSpend * @param mixed $averageMonthlyEarnings
*/ */
public function setAverageSpend($averageSpend) public function setAverageMonthlyEarnings($averageMonthlyEarnings)
{ {
$this->averageSpend = $averageSpend; $this->averageMonthlyEarnings = $averageMonthlyEarnings;
} }
/** /**
* @return mixed * @return mixed

View File

@ -132,10 +132,12 @@ class OwnerService {
foreach ( $expenses as $exp ) { foreach ( $expenses as $exp ) {
$serialized[] = $serialized[] =
[ [
'amount' => $exp->getAmount(),
'id' => $exp->getId(), 'id' => $exp->getId(),
'name' => $exp->getName(), 'name' => $exp->getName(),
'delay' => $exp->getDelay(), 'delay' => $exp->getDelay(),
'repeat' => $exp->getRepeat(), 'repeat' => $exp->getRepeatitions(),
'enabled' => $exp->getEnabled(),
]; ];
} }