Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watches are now editable from the dashboard #104

Merged
merged 3 commits into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"MANDRILL_APIKEY":{
"required":true
},
"MC_APIKEY":{
"MC_APIKEY": {
"required":true
}
},
Expand Down
3 changes: 2 additions & 1 deletion application/config/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
define('HOME_PAGE_3', 'HOME_PAGE_3');
49 changes: 49 additions & 0 deletions application/controllers/Measures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down
35 changes: 35 additions & 0 deletions application/models/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions application/tests/controllers/Measures_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
42 changes: 42 additions & 0 deletions application/tests/models/Watch_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
1 change: 1 addition & 0 deletions application/views/measure/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
5 changes: 5 additions & 0 deletions application/views/measure/dashboard/edit-watch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<li>
<a href="#" class="submitEditWatch" data-watch="<?php echo $watchId; ?>">Edit watch</a>
<form method="post" action="/measures/edit_watch_p" name="edit-watch-<?php echo $watchId; ?>" class="no-display">
<input type="hidden" name="watchId" value="<?php echo $watchId; ?>"></form>
</li>
48 changes: 48 additions & 0 deletions application/views/measure/edit-watch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<div class="container container-fluid content first">
<div class="row">
<div class="col-md-12"><center><h1>Add a new watch</h1></center></div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<form class="form-horizontal" action="/measures/edit_watch" method="post" name="editWatch">
<div class="form-group">
<label for="brand" class="col-sm-3 control-label">Brand<i>*</i></label>
<div class="col-sm-9">
<input value="<?php echo $brand;?>" type="text" class="form-control" name="brand" placeholder="Jaeger-LeCoultre">
<span class="watch-error brand-error">This field is required.</span>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-3 control-label">Model</label>
<div class="col-sm-9">
<input value="<?php echo $name;?>" type="text" class="form-control" name="name" placeholder="Duometre">
</div>
</div>
<div class="form-group">
<label for="caliber" class="col-sm-3 control-label">Caliber</label>
<div class="col-sm-9">
<input value="<?php echo $caliber;?>" type="text" class="form-control" name="caliber" placeholder="Calibre Jaeger-LeCoultre 381">
</div>
</div>
<div class="form-group">
<label for="yearOfBuy" class="col-sm-3 control-label">Year of buy</label>
<div class="col-sm-9">
<input value="<?php echo $yearOfBuy;?>" type="text" class="form-control" name="yearOfBuy" placeholder="1998">
</div>
</div>
<div class="form-group">
<label for="serial" class="col-sm-3 control-label">Serial number</label>
<div class="col-sm-9">
<input value="<?php echo $serial;?>" type="text" class="form-control" name="serial" placeholder="268/53">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<input type="submit" class="btn btn-primary" name="addWatch" value="Edit the watch">
</div>
</div>
<input type="hidden" name="watchId" value="<?php echo $watchId; ?>">
</form>
</div>
</div>
</div>
21 changes: 21 additions & 0 deletions assets/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down