From ddf0d3b689ded472986d2a9dd21f6b676630c313 Mon Sep 17 00:00:00 2001 From: Mathieu Nayrolles Date: Wed, 20 Jan 2016 14:54:45 -0500 Subject: [PATCH 1/2] Watches are now editable from the dashboard #101 --- application/config/constants.php | 3 +- application/controllers/Measures.php | 49 +++++++++++++++++++ application/models/Watch.php | 35 +++++++++++++ .../tests/controllers/Measures_test.php | 30 ++++++++++++ application/tests/models/Watch_test.php | 42 ++++++++++++++++ application/views/measure/dashboard.php | 1 + .../views/measure/dashboard/edit-watch.php | 5 ++ application/views/measure/edit-watch.php | 48 ++++++++++++++++++ assets/js/application.js | 21 ++++++++ 9 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 application/views/measure/dashboard/edit-watch.php create mode 100644 application/views/measure/edit-watch.php diff --git a/application/config/constants.php b/application/config/constants.php index 4f6051d7..73e7412b 100644 --- a/application/config/constants.php +++ b/application/config/constants.php @@ -108,6 +108,7 @@ define('ADD_WATCH', 'ADD_WATCH'); define('ADD_WATCH_LOAD', 'ADD_WATCH_LOAD'); define('DELETE_WATCH', 'DELETE_WATCH'); +define('UPDATE_WATCH', 'UPDATE_WATCH'); define('NEW_MEASURE', 'NEW_MEASURE'); define('DELETE_MEASURE', 'DELETE_MEASURE'); define('DELETE_ALL_MEASURES', 'DELETE_ALL_MEASURES'); @@ -122,4 +123,4 @@ define('HOME_PAGE_0', 'HOME_PAGE_0'); define('HOME_PAGE_1', 'HOME_PAGE_1'); define('HOME_PAGE_2', 'HOME_PAGE_2'); -define('HOME_PAGE_3', 'HOME_PAGE_3'); \ No newline at end of file +define('HOME_PAGE_3', 'HOME_PAGE_3'); diff --git a/application/controllers/Measures.php b/application/controllers/Measures.php index f74cb47e..5e2c1599 100644 --- a/application/controllers/Measures.php +++ b/application/controllers/Measures.php @@ -146,6 +146,55 @@ public function new_watch() { $this->load->view('footer'); } + /** + * Serves the edit watch page + * TODO: Is there a clean way to separate serving page + * functions and processing inputs functions ? + * TODO: A watch controller start to makes sense + * to separate things. + * @return Views + */ + public function edit_watch_p(){ + + if($this->expectsPost(array('watchId'))){ + + $watch = $this->watch->getWatch($this->watchId); + + if($watch){ + + $this->_headerData['headerClass'] = 'blue'; + $this->load->view('header', $this->_headerData); + $this->load->view('measure/edit-watch', $watch); + $this->load->view('footer'); + } + } + } + + /** + * Receive an edited watch post form + * @return body messages + */ + public function edit_watch(){ + if($this->expectsPost(array('watchId','brand', 'name', 'yearOfBuy', + 'serial', 'caliber'))){ + + if ($this->watch->editWatch($this->session->userdata('userId'), + $this->watchId, + $this->brand, $this->name, + $this->yearOfBuy, $this->serial, + $this->caliber)) { + + $this->_bodyData['success'] = 'Watch successfully updated!'; + + } else { + $this->_bodyData['error'] = 'An error occured while updating your watch.'; + } + + $this->constructMeasurePage(); + } + echo "not all posts"; + } + /** * Serves the new measure form (1/2) */ diff --git a/application/models/Watch.php b/application/models/Watch.php index 3a099f03..bba1cd0e 100644 --- a/application/models/Watch.php +++ b/application/models/Watch.php @@ -51,6 +51,41 @@ function addWatch($userId, $brand, $name, $yearOfBuy, $serial, $caliber) { return $res; } + /** + * Edit a watch given an userId and a watchId + * + * @param int $userId id of the user adding the watch + * @param int $watchId id of the watch + * @param String $brand Brand of the watch + * @param String $name Given name of the watch + * @param String $yearOfBuy Year of buy for the watch + * @param String $serial Serial number of the watch + * @param String $caliber Caliber of the watch + * @return boolean Update results + */ + function editWatch($userId, $watchId, $brand, $name, $yearOfBuy, $serial, $caliber){ + $res = false; + + $data = array( + 'brand' => $brand, + 'name' => $name, + 'yearOfBuy' => $yearOfBuy, + 'serial' => $serial, + 'caliber' => $caliber + ); + + $where = array( + 'userId' => $userId, + 'watchId' => $watchId + ); + + $res = $this->update($where, $data); + + $this->notify(UPDATE_WATCH, arrayToObject($data)); + + return $res === true && $this->affected_rows() === 1; + } + /** * Get all the watches of $userId * @param int $userId user id diff --git a/application/tests/controllers/Measures_test.php b/application/tests/controllers/Measures_test.php index 605919e0..27bb8378 100644 --- a/application/tests/controllers/Measures_test.php +++ b/application/tests/controllers/Measures_test.php @@ -72,6 +72,36 @@ public function test_indexDeleteMeasures(){ $this->assertContains('Measures successfully deleted!', $output); } + public function test_indexEditWatch(){ + $CI = &get_instance(); + $CI->load->model('Watch'); + $watch = $CI->Watch->find_by('userId', self::$userId); + + $output = $this->request( + 'POST', + ['Measures', 'edit_watch'], + [ + 'watchId' => $watch->watchId, + 'brand' => 'branda', + 'name' => 'nama', + 'yearOfBuy' => 2014, + 'serial' => 2013, + 'caliber' => 2012 + ] + ); + + $this->assertContains('Watch successfully updated!', $output); + + $watch = $CI->Watch->find_by('watchId', $watch->watchId); + + $this->assertEquals($watch->brand, "branda"); + $this->assertEquals($watch->name, "nama"); + $this->assertEquals($watch->yearOfBuy, 2014); + $this->assertEquals($watch->serial, 2013); + $this->assertEquals($watch->caliber, 2012); + + } + public function test_indexDeleteWatch(){ $CI = &get_instance(); $CI->load->model('Watch'); diff --git a/application/tests/models/Watch_test.php b/application/tests/models/Watch_test.php index 6d232e09..52a94145 100644 --- a/application/tests/models/Watch_test.php +++ b/application/tests/models/Watch_test.php @@ -94,6 +94,48 @@ public function test_getWatch() { $this->assertEquals('1', $watch->status); } + public function test_editWatch(){ + $watch = $this->obj->getWatch(self::$watchId); + + $result = $this->obj->editWatch( + self::$userId, + self::$watchId, + "branda", + "nama", + 2014, + 2013, + 2012 + ); + + $watch = $this->obj->getWatch(self::$watchId); + + $this->assertEquals($result, true); + + $this->assertEquals($watch->brand, "branda"); + $this->assertEquals($watch->name, "nama"); + $this->assertEquals($watch->yearOfBuy, 2014); + $this->assertEquals($watch->serial, 2013); + $this->assertEquals($watch->caliber, 2012); + } + + public function test_editWatchWrongUserId(){ + $watch = $this->obj->getWatch(self::$watchId); + + $result = $this->obj->editWatch( + 999, + self::$watchId, + "branda", + "nama", + 2014, + 2013, + 2012 + ); + + $watch = $this->obj->getWatch(self::$watchId); + + $this->assertEquals($result, false); + } + public function test_getWatchWrongId() { $this->assertEquals(false, $this->obj->getWatch('42')); } diff --git a/application/views/measure/dashboard.php b/application/views/measure/dashboard.php index 34141bdc..213fdaaf 100644 --- a/application/views/measure/dashboard.php +++ b/application/views/measure/dashboard.php @@ -55,6 +55,7 @@ $this->load->view("measure/dashboard/start-new-measure", $measure); + $this->load->view("measure/dashboard/edit-watch", $measure); $this->load->view("measure/dashboard/delete-watch", $measure); $this->load->view("measure/dashboard/end-action-button"); diff --git a/application/views/measure/dashboard/edit-watch.php b/application/views/measure/dashboard/edit-watch.php new file mode 100644 index 00000000..62c37501 --- /dev/null +++ b/application/views/measure/dashboard/edit-watch.php @@ -0,0 +1,5 @@ +
  • + Edit watch +
    +
    +
  • diff --git a/application/views/measure/edit-watch.php b/application/views/measure/edit-watch.php new file mode 100644 index 00000000..f65f9a88 --- /dev/null +++ b/application/views/measure/edit-watch.php @@ -0,0 +1,48 @@ +
    +
    +

    Add a new watch

    +
    +
    +
    +
    +
    + +
    + + This field is required. +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    diff --git a/assets/js/application.js b/assets/js/application.js index b9e3b633..83835f36 100644 --- a/assets/js/application.js +++ b/assets/js/application.js @@ -272,6 +272,20 @@ $(document).ready(function() }); + $('body').on('submit', 'form[name="editWatch"]', function(e) + { + + $('.watch-error').hide(); + + var brand = $('input[name="brand"]').val(); + + if(brand == ""){ + $('.brand-error').show(); + e.preventDefault(); + } + + }); + $('body').on('submit', 'form[name="newAccuracy"]', function(e) { @@ -468,6 +482,13 @@ $(document).ready(function() } }); + $('body').on('click', '.submitEditWatch', function(e) + { + e.preventDefault(); + var watchId = $(this).attr('data-watch'); + $('form[name="edit-watch-'+watchId+'"]').submit(); + }); + $('body').on('click', '.submitNewMeasure', function(e) { e.preventDefault(); From 6aeff70583c7085f8ed01a8d86ae21eff5ad0e18 Mon Sep 17 00:00:00 2001 From: Mathieu Nayrolles Date: Wed, 20 Jan 2016 15:13:59 -0500 Subject: [PATCH 2/2] Commit spaces to trigger heroku build --- app.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app.json b/app.json index e52b4eef..f4d43dbd 100644 --- a/app.json +++ b/app.json @@ -19,16 +19,16 @@ "NEW_RELIC_CONFIG_FILE": { "required": true }, - "NEW_RELIC_LICENSE_KEY":{ + "NEW_RELIC_LICENSE_KEY": { "required":true }, - "NEW_RELIC_LOG":{ + "NEW_RELIC_LOG": { "required":true }, - "MANDRILL_APIKEY":{ + "MANDRILL_APIKEY": { "required":true }, - "MC_APIKEY":{ + "MC_APIKEY": { "required":true } },