Make updating of anime list work

This commit is contained in:
Timothy Warren 2015-11-13 11:33:27 -05:00
parent 1891aafef5
commit 3c4ba096c6
8 changed files with 125 additions and 34 deletions

View File

@ -7,10 +7,10 @@
<h2><?= $escape->html($name) ?></h2>
<section class="media-wrap">
<?php foreach($items as $item): ?>
<article class="media" id="a-<?= $item['id'] ?>">
<?php /*if (is_logged_in()): ?>
<article class="media" id="<?= $item['anime']['slug'] ?>">
<?php if ($auth->is_authenticated()): ?>
<button class="plus_one" hidden>+1 Episode</button>
<?php endif*/ ?>
<?php endif ?>
<?= $helper->img($item['anime']['image']); ?>
<div class="name">
<a href="<?= $escape->attr($item['anime']['url']) ?>">
@ -39,6 +39,6 @@
<?php endforeach ?>
<?php endif ?>
</main>
<?php /*if (is_logged_in()): ?>
<script src="<?= $config->asset_url('js.php?g=edit') ?>"></script>
<?php endif*/ ?>
<?php if ($auth->is_authenticated()): ?>
<script src="<?= $urlGenerator->asset_url('js.php?g=edit') ?>"></script>
<?php endif ?>

View File

@ -1,7 +1,7 @@
<main>
<?= $message ?>
<aside>
<form method="post" action="<?= $urlGenerator->full_url('/login', $url_type) ?>">
<form method="post" action="<?= $urlGenerator->full_url($urlGenerator->path(), $url_type) ?>">
<dl>
<?php /*<dt><label for="username">Username: </label></dt>
<dd><input type="text" id="username" name="username" required="required" /></dd>*/ ?>

17
public/js/anime_edit.js Normal file → Executable file
View File

@ -1,7 +1,9 @@
/**
* Javascript for editing anime, if logged in
*/
(function($, undefined){
(function($){
"use strict";
if (CONTROLLER !== "anime") return;
@ -12,7 +14,6 @@
var self = this;
var this_sel = $(this);
var parent_sel = $(this).closest("article");
var self = this;
var watched_count = parseInt(parent_sel.find('.completed_number').text(), 10);
var total_count = parseInt(parent_sel.find('.total_number').text(), 10);
@ -20,7 +21,7 @@
// Setup the update data
var data = {
id: this_sel.parent('article').attr('id').replace('a-', ''),
id: this_sel.parent('article').attr('id'),
increment_episodes: true
};
@ -39,7 +40,13 @@
}
// okay, lets actually make some changes!
$.post(BASE_URL + 'update', data, function(res) {
$.ajax({
data: data,
dataType: 'json',
method: 'POST',
mimeType: 'application/json',
url: BASE_URL + CONTROLLER + '/update'
}).done(function(res) {
if (res.status === 'completed')
{
$(self).closest('article').hide();
@ -47,6 +54,8 @@
add_message('success', "Sucessfully updated " + title);
parent_sel.find('.completed_number').text(++watched_count);
}).fail(function() {
add_message('error', "Failed to updated " + title);
});
});

View File

@ -76,7 +76,7 @@ class HummingbirdAuth {
*
* @return void
*/
public function log_out()
public function logout()
{
$this->segment->clear();
}

View File

@ -176,6 +176,49 @@ class Controller {
], $view);
}
/**
* Attempt login authentication
*
* @return bool
*/
public function login_action()
{
$auth = $this->container->get('auth');
if ($auth->authenticate($this->request->post->get('password')))
{
$this->response->redirect->afterPost(
$this->urlGenerator->full_url('', $this->base_data['url_type'])
);
}
$this->login("Invalid username or password.");
}
/**
* Deauthorize the current user
*
* @return void
*/
public function logout()
{
$auth = $this->container->get('auth');
$auth->logout();
$this->redirect_to_default();
}
/**
* 404 action
*
* @return void
*/
public function not_found()
{
$this->outputHTML('404', [
'title' => 'Sorry, page not found'
]);
}
/**
* Add a message box to the page
*

View File

@ -54,26 +54,48 @@ class Dispatcher extends RoutingBase {
*/
protected function generate_convention_routes()
{
$this->output_routes[] = $this->router->add('index_redirect', '/')
->setValues([
'controller' => 'Aviat\\AnimeClient\\Controller',
'action' => 'redirect_to_default'
]);
$default_controller = $this->routes['convention']['default_controller'];
$this->output_routes[] = $this->router->add('login', '/{controller}/login')
$this->output_routes[] = $this->router->addGet('login', '/{controller}/login')
->setValues([
'controller' => $this->routes['convention']['default_controller'],
'controller' => $default_controller,
'action' => 'login'
]);
$this->output_routes[] = $this->router->add('list', '/{controller}/{type}{/view}')
$this->output_routes[] = $this->router->addPost('login_post', '/{controller}/login')
->setValues([
'controller' => $this->routes['convention']['default_controller'],
'controller' => $default_controller,
'action' => 'login_action'
]);
$this->output_routes[] = $this->router->addGet('logout', '/{controller}/logout')
->setValues([
'controller' => $default_controller,
'action' => 'logout'
]);
$this->output_routes[] = $this->router->addPost('update', '/{controller}/update')
->setValues([
'controller' => $default_controller,
'action' => 'update'
])->setTokens([
'controller' => '[a-z_]+'
]);
$this->output_routes[] = $this->router->addGet('list', '/{controller}/{type}{/view}')
->setValues([
'controller' => $default_controller,
'action' => $this->routes['convention']['default_method'],
])->setTokens([
'type' => '[a-z_]+',
'view' => '[a-z_]+'
]);
$this->output_routes[] = $this->router->addGet('index_redirect', '/')
->setValues([
'controller' => 'Aviat\\AnimeClient\\Controller',
'action' => 'redirect_to_default'
]);
}
/**
@ -115,6 +137,9 @@ class Dispatcher extends RoutingBase {
public function __invoke($route = NULL)
{
$error_handler = $this->container->get('error-handler');
$controller_name = $this->routes['convention']['default_controller'];
$action_method = $this->routes['convention']['404_method'];
$params = [];
if (is_null($route))
{
@ -129,8 +154,15 @@ class Dispatcher extends RoutingBase {
}
else
{
$controller_name = $route->params['controller'];
$action_method = $route->params['action'];
if (isset($route->params['controller']))
{
$controller_name = $route->params['controller'];
}
if (isset($route->params['action']))
{
$action_method = $route->params['action'];
}
if (is_null($controller_name))
{
@ -242,7 +274,14 @@ class Dispatcher extends RoutingBase {
unset($route['path']);
$controller_map = $this->get_controller_list();
$controller_class = $controller_map[$route_type];
if (array_key_exists($route_type, $controller_map))
{
$controller_class = $controller_map[$route_type];
}
else
{
$controller_class = $this->routes['convention']['default_controller'];
}
// Prepend the controller to the route parameters
$route['controller'] = $controller_class;

View File

@ -55,7 +55,7 @@ class Anime extends API {
$data['auth_token'] = $auth->get_auth_token();
$response = $this->client->post("libraries/{$data['id']}", [
'body' => $data
'form_params' => $data
]);
return json_decode($response->getBody(), TRUE);

View File

@ -9,15 +9,15 @@ use Aviat\Ion\Friend;
use Aviat\AnimeClient\Auth\HummingbirdAuth;
class HummingbirdAuthTest extends AnimeClient_TestCase {
static $session;
static $sessionHandler;
public static function setUpBeforeClass()
{
self::$session = (new SessionFactory)->newInstance([]);
}
public function setUp()
{
parent::setUp();
@ -26,11 +26,11 @@ class HummingbirdAuthTest extends AnimeClient_TestCase {
$this->auth = $friend;
$this->container->set('session', self::$session);
}
public function dataAuthenticate()
{
$testToken = 'notReallyAValidTokenButThisIsATest';
return [
'successful auth call' => [
'username' => 'timw4mailtest',
@ -54,7 +54,7 @@ class HummingbirdAuthTest extends AnimeClient_TestCase {
]
];
}
/**
* @dataProvider dataAuthenticate
*/
@ -73,17 +73,17 @@ class HummingbirdAuthTest extends AnimeClient_TestCase {
]);
$model->__set('client', $client);
$this->auth->__set('model', $model);
$actual = $this->auth->authenticate($password);
$this->assertEquals($expected, $actual);
}
public function testIsAuthenticated()
{
$data = $this->dataAuthenticate();
call_user_func_array([$this, 'testAuthenticate'], $data['successful auth call']);
$this->assertTrue($this->auth->is_authenticated());
$this->auth->log_out();
$this->auth->logout();
$this->assertFalse($this->auth->is_authenticated());
}
}