Added post deleting

This commit is contained in:
Marcin Kurczewski 2014-09-23 20:18:12 +02:00
parent 9a191c8237
commit a140e04ca3
8 changed files with 58 additions and 2 deletions

1
TODO
View file

@ -7,7 +7,6 @@ everything related to posts:
- time of last edit
- time of last feature
- how many times the post was featured
- delete
- feature
- fav
- score (see notes about scoring)

View file

@ -41,6 +41,7 @@ listSketchyPosts = anonymous, regularUser, powerUser, moderator, administ
listUnsafePosts = anonymous, regularUser, powerUser, moderator, administrator
uploadPosts = regularUser, powerUser, moderator, administrator
uploadPostsAnonymously = regularUser, powerUser, moderator, administrator
deletePosts = moderator, administrator
listTags = anonymous, regularUser, powerUser, moderator, administrator

View file

@ -23,6 +23,7 @@ App.Auth = function(_, jQuery, util, api, appState, promise) {
listUnsafePosts: 'listUnsafePosts',
uploadPosts: 'uploadPosts',
uploadPostsAnonymously: 'uploadPostsAnonymously',
deletePosts: 'deletePosts',
listTags: 'listTags',
};

View file

@ -7,6 +7,8 @@ App.Presenters.PostPresenter = function(
util,
promise,
api,
auth,
router,
topNavigationPresenter,
messagePresenter) {
@ -16,11 +18,14 @@ App.Presenters.PostPresenter = function(
var postContentTemplate;
var post;
var postNameOrId;
var privileges = {};
function init(args, loaded) {
postNameOrId = args.postNameOrId;
topNavigationPresenter.select('posts');
privileges.canDeletePosts = auth.hasPrivilege(auth.privileges.deletePosts);
promise.waitAll(
util.promiseTemplate('post'),
util.promiseTemplate('post-content'),
@ -50,7 +55,25 @@ App.Presenters.PostPresenter = function(
formatRelativeTime: util.formatRelativeTime,
formatFileSize: util.formatFileSize,
postContentTemplate: postContentTemplate,
privileges: privileges,
}));
$el.find('.delete').click(deleteButtonClicked);
}
function deleteButtonClicked(e) {
e.preventDefault();
if (window.confirm('Do you really want to delete this post?')) {
deletePost();
}
}
function deletePost() {
api.delete('/posts/' + post.id).then(function(response) {
router.navigate('#/posts');
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
return {
@ -60,4 +83,4 @@ App.Presenters.PostPresenter = function(
};
App.DI.register('postPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostPresenter);
App.DI.register('postPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'router', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.PostPresenter);

View file

@ -79,6 +79,21 @@
<% } %>
</ul>
<% if (_.any(privileges)) { %>
<h1>Options</h1>
<ul class="operations">
<% if (privileges.canDeletePosts) { %>
<li>
<a href="#" class="delete">
Delete
</a>
</li>
<% } %>
</ul>
<% } %>
</div>
<div id="post-view">

View file

@ -25,6 +25,7 @@ final class PostController extends AbstractController
$router->post('/api/posts', [$this, 'createPost']);
$router->get('/api/posts', [$this, 'getFiltered']);
$router->get('/api/posts/:postNameOrId', [$this, 'getByNameOrId']);
$router->delete('/api/posts/:postNameOrId', [$this, 'deletePost']);
}
public function getByNameOrId($postNameOrId)
@ -57,4 +58,10 @@ final class PostController extends AbstractController
$post = $this->postService->createPost($formData);
return $this->postViewProxy->fromEntity($post);
}
public function deletePost($postNameOrId)
{
$post = $this->postService->getByNameOrId($postNameOrId);
$this->postService->deletePost($post);
}
}

View file

@ -23,6 +23,7 @@ class Privilege
const LIST_UNSAFE_POSTS = 'listUnsafePosts';
const UPLOAD_POSTS = 'uploadPosts';
const UPLOAD_POSTS_ANONYMOUSLY = 'uploadPostsAnonymously';
const DELETE_POSTS = 'deletePosts';
const LIST_TAGS = 'listTags';
}

View file

@ -190,6 +190,15 @@ class PostService
$post->setTags($tags);
}
public function deletePost(\Szurubooru\Entities\Post $post)
{
$transactionFunc = function() use ($post)
{
$this->postDao->deleteById($post->getId());
};
$this->transactionManager->commit($transactionFunc);
}
private function assertNoPostWithThisContentChecksum(\Szurubooru\Entities\Post $parent)
{
$checksumToCheck = $parent->getContentChecksum();