Improved API responses

This commit is contained in:
rr- 2015-08-03 18:54:10 +02:00
parent 5a537ba168
commit b3def7fc21
49 changed files with 75 additions and 76 deletions

View file

@ -387,7 +387,7 @@ App.Controls.TagInput = function($underlyingInput) {
return promise.make(function(resolve, reject) {
promise.wait(api.get('/tags/' + tagName + '/siblings'))
.then(function(response) {
resolve(response.json.data);
resolve(response.json.tags);
}).fail(function() {
reject();
});

View file

@ -71,10 +71,7 @@ App.Pager = function(
var totalRecords = response.json.totalRecords;
totalPages = Math.ceil(totalRecords / pageSize);
resolve({
entities: response.json.data,
totalRecords: totalRecords,
totalPages: totalPages});
resolve(response);
}).fail(function(response) {
reject(response);

View file

@ -53,7 +53,7 @@ App.Presenters.CommentListPresenter = function(
if (comments.length === 0) {
promise.wait(api.get('/comments/' + params.post.id))
.then(function(response) {
comments = response.json.data;
comments = response.json.comments;
render();
}).fail(function() {
console.log(arguments);
@ -176,7 +176,7 @@ App.Presenters.CommentListPresenter = function(
p.then(function(response) {
$textarea.val('');
var comment = response.json;
var comment = response.json.comment;
if (commentToEdit) {
$form.slideUp(function() {

View file

@ -38,8 +38,8 @@ App.Presenters.GlobalCommentListPresenter = function(
baseUri: '#/comments',
backendUri: '/comments',
$target: $el.find('.pagination-target'),
updateCallback: function($page, data) {
renderComments($page, data.entities);
updateCallback: function($page, response) {
renderComments($page, response.json.comments);
},
},
function() {
@ -65,12 +65,11 @@ App.Presenters.GlobalCommentListPresenter = function(
$el.html(templates.list());
}
function renderComments($page, data) {
function renderComments($page, postComments) {
var $target = $page.find('.posts');
_.each(data, function(data) {
var post = data.post;
var comments = data.comments;
_.each(postComments, function(postComments) {
var post = postComments.post;
var comments = postComments.comments;
var $post = jQuery('<li>' + templates.listItem({
util: util,
post: post,

View file

@ -31,8 +31,8 @@ App.Presenters.HistoryPresenter = function(
baseUri: '#/history',
backendUri: '/history',
$target: $el.find('.pagination-target'),
updateCallback: function($page, data) {
renderHistory($page, data.entities);
updateCallback: function($page, response) {
renderHistory($page, response.json.history);
},
},
function() {

View file

@ -114,7 +114,14 @@ App.Presenters.PagerPresenter = function(
updateCallback($page, response);
refreshPageList();
if (!response.entities.length) {
var entities =
response.json.posts ||
response.json.users ||
response.json.comments ||
response.json.tags;
if (!entities.length) {
messagePresenter.showInfo($messages, 'No data to show');
if (pager.getVisiblePages().length === 1) {
hidePageList();
@ -125,7 +132,7 @@ App.Presenters.PagerPresenter = function(
showPageList();
}
if (pager.getPage() < response.totalPages) {
if (pager.getPage() < pager.getTotalPages()) {
attachNextPageLoader();
}

View file

@ -151,8 +151,9 @@ App.Presenters.PostEditPresenter = function(
promise.wait(api.post('/posts/' + post.id, formData))
.then(function(response) {
tagList.refreshTags();
post = response.json.post;
if (typeof(updateCallback) !== 'undefined') {
updateCallback(post = response.json);
updateCallback(post);
}
}).fail(function(response) {
showEditError(response);

View file

@ -45,8 +45,8 @@ App.Presenters.PostListPresenter = function(
baseUri: '#/posts',
backendUri: '/posts',
$target: $el.find('.pagination-target'),
updateCallback: function($page, data) {
renderPosts($page, data.entities);
updateCallback: function($page, response) {
renderPosts($page, response.json.posts);
},
},
function() {
@ -221,7 +221,7 @@ App.Presenters.PostListPresenter = function(
formData.tags = tags.join(' ');
promise.wait(api.post('/posts/' + post.id, formData))
.then(function(response) {
post = response.json;
post = response.json.post;
$post.data('post', post);
softRenderPost($post);
}).fail(function(response) {

View file

@ -120,7 +120,7 @@ App.Presenters.PostPresenter = function(
return promise.make(function(resolve, reject) {
promise.wait(api.get('/posts/' + postNameOrId))
.then(function(postResponse) {
post = postResponse.json;
post = postResponse.json.post;
resolve();
}).fail(function(response) {
showGenericError(response);

View file

@ -60,7 +60,7 @@ App.Presenters.RegistrationPresenter = function(
function registrationSuccess(apiResponse) {
$el.find('form').slideUp(function() {
var message = 'Registration complete! ';
if (!apiResponse.json.confirmed) {
if (!apiResponse.json.user.confirmed) {
message += '<br/>Check your inbox for activation e-mail.<br/>If e-mail doesn\'t show up, check your spam folder.';
} else {
message += '<a href="#/login">Click here</a> to login.';

View file

@ -36,8 +36,8 @@ App.Presenters.TagListPresenter = function(
baseUri: '#/tags',
backendUri: '/tags',
$target: $el.find('.pagination-target'),
updateCallback: function($page, data) {
renderTags($page, data.entities);
updateCallback: function($page, response) {
renderTags($page, response.json.tags);
},
},
function() {

View file

@ -66,9 +66,9 @@ App.Presenters.TagPresenter = function(
api.get('tags/' + tagName + '/siblings'),
api.get('posts', {query: tagName}))
.then(function(tagResponse, siblingsResponse, postsResponse) {
tag = tagResponse.json;
siblings = siblingsResponse.json.data;
posts = postsResponse.json.data;
tag = tagResponse.json.tag;
siblings = siblingsResponse.json.tags;
posts = postsResponse.json.posts;
posts = posts.slice(0, 8);
render();

View file

@ -133,7 +133,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
function editSuccess(apiResponse) {
var wasLoggedIn = auth.isLoggedIn(user.name);
user = apiResponse.json;
user = apiResponse.json.user;
if (wasLoggedIn) {
auth.updateCurrentUser(user);
}
@ -142,7 +142,7 @@ App.Presenters.UserAccountSettingsPresenter = function(
var $messages = jQuery(target).find('.messages');
var message = 'Account settings updated!';
if (!apiResponse.json.confirmed) {
if (!apiResponse.json.user.confirmed) {
message += '<br/>Check your inbox for activation e-mail.<br/>If e-mail doesn\'t show up, check your spam folder.';
}
messagePresenter.showInfo($messages, message);

View file

@ -35,8 +35,8 @@ App.Presenters.UserListPresenter = function(
baseUri: '#/users',
backendUri: '/users',
$target: $el.find('.pagination-target'),
updateCallback: function($page, data) {
renderUsers($page, data.entities);
updateCallback: function($page, response) {
renderUsers($page, response.json.users);
},
},
function() {

View file

@ -41,7 +41,7 @@ App.Presenters.UserPresenter = function(
promise.wait(api.get('/users/' + userName))
.then(function(response) {
user = response.json;
user = response.json.user;
var extendedContext = _.extend(params, {user: user});
presenterManager.initPresenters([

View file

@ -15,7 +15,7 @@ App.Services.PostsAroundCalculator = function(_, promise, util, pager) {
pager.setPage(query.page);
promise.wait(pager.retrieveCached())
.then(function(response) {
var postIds = _.pluck(response.entities, 'id');
var postIds = _.pluck(response.json.posts, 'id');
var position = _.indexOf(postIds, postId);
if (position === -1) {
@ -51,10 +51,10 @@ App.Services.PostsAroundCalculator = function(_, promise, util, pager) {
pager.setPage(page + direction);
promise.wait(pager.retrieveCached())
.then(function(response) {
if (response.entities.length) {
if (response.json.posts.length) {
var post = direction === - 1 ?
_.last(response.entities) :
_.first(response.entities);
_.last(response.json.posts) :
_.first(response.json.posts);
var url = util.appendComplexRouteParam(
'#/post/' + post.id,

View file

@ -49,6 +49,6 @@ class AddComment extends AbstractCommentRoute
$post = $this->postService->getByNameOrId($args['postNameOrId']);
$comment = $this->commentService->createComment($post, $this->inputReader->text);
return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig());
return ['comment' => $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig())];
}
}

View file

@ -52,6 +52,6 @@ class DeleteComment extends AbstractCommentRoute
? Privilege::DELETE_OWN_COMMENTS
: Privilege::DELETE_ALL_COMMENTS);
return $this->commentService->deleteComment($comment);
$this->commentService->deleteComment($comment);
}
}

View file

@ -53,6 +53,6 @@ class EditComment extends AbstractCommentRoute
: Privilege::EDIT_ALL_COMMENTS);
$comment = $this->commentService->updateComment($comment, $this->inputReader->text);
return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig());
return ['comment' => $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig())];
}
}

View file

@ -80,7 +80,7 @@ class GetComments extends AbstractCommentRoute
}
return [
'data' => $data,
'comments' => $data,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}

View file

@ -63,6 +63,6 @@ class GetPostComments extends AbstractCommentRoute
$result = $this->commentService->getFiltered($filter);
$entities = $this->commentViewProxy->fromArray($result->getEntities(), $this->getCommentsFetchConfig());
return ['data' => $entities];
return ['comments' => $entities];
}
}

View file

@ -47,6 +47,6 @@ class AddToFavorites extends AbstractRoute
$this->favoritesService->addFavorite($user, $post);
$users = $this->favoritesService->getFavoriteUsers($post);
return ['data' => $this->userViewProxy->fromArray($users)];
return ['users' => $this->userViewProxy->fromArray($users)];
}
}

View file

@ -43,6 +43,6 @@ class GetFavoriteUsers extends AbstractRoute
{
$post = $this->postService->getByNameOrId($args['postNameOrId']);
$users = $this->favoritesService->getFavoriteUsers($post);
return ['data' => $this->userViewProxy->fromArray($users)];
return ['users' => $this->userViewProxy->fromArray($users)];
}
}

View file

@ -47,6 +47,6 @@ class RemoveFromFavorites extends AbstractRoute
$this->favoritesService->deleteFavorite($user, $post);
$users = $this->favoritesService->getFavoriteUsers($post);
return ['data' => $this->userViewProxy->fromArray($users)];
return ['users' => $this->userViewProxy->fromArray($users)];
}
}

View file

@ -49,7 +49,7 @@ class GetHistory extends AbstractRoute
$result = $this->historyService->getFiltered($filter);
$entities = $this->snapshotViewProxy->fromArray($result->getEntities());
return [
'data' => $entities,
'history' => $entities,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}

View file

@ -78,8 +78,7 @@ class Login extends AbstractRoute
$user = $this->authService->getLoggedInUser();
}
return
[
return [
'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()),
'user' => $this->userViewProxy->fromEntity($user),
'privileges' => $this->privilegeService->getCurrentPrivileges(),

View file

@ -47,6 +47,6 @@ class CreatePost extends AbstractPostRoute
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS_ANONYMOUSLY);
$post = $this->postService->createPost($formData);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
return ['post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig())];
}
}

View file

@ -36,7 +36,6 @@ class GetFeaturedPost extends AbstractPostRoute
$user = $this->postFeatureService->getFeaturedPostUser();
return [
'user' => $this->userViewProxy->fromEntity($user),
'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()),
];
'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig())];
}
}

View file

@ -36,6 +36,6 @@ class GetPost extends AbstractPostRoute
$this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS);
$post = $this->postService->getByNameOrId($args['postNameOrId']);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
return ['post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig())];
}
}

View file

@ -54,7 +54,7 @@ class GetPosts extends AbstractPostRoute
$result = $this->postService->getFiltered($filter);
$entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig());
return [
'data' => $entities,
'posts' => $entities,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}

View file

@ -49,6 +49,6 @@ class AddPostNote extends AbstractPostRoute
$formData = new PostNoteFormData($this->inputReader);
$postNote = $this->postNotesService->createPostNote($post, $formData);
return $this->postNoteViewProxy->fromEntity($postNote);
return ['note' => $this->postNoteViewProxy->fromEntity($postNote)];
}
}

View file

@ -32,6 +32,6 @@ class DeletePostNote extends AbstractPostRoute
{
$postNote = $this->postNotesService->getById($args['postNoteId']);
$this->privilegeService->assertPrivilege(Privilege::DELETE_POST_NOTES);
return $this->postNotesService->deletePostNote($postNote);
$this->postNotesService->deletePostNote($postNote);
}
}

View file

@ -44,6 +44,6 @@ class GetPostNotes extends AbstractPostRoute
{
$post = $this->postService->getByNameOrId($args['postNameOrId']);
$postNotes = $this->postNotesService->getByPost($post);
return $this->postNoteViewProxy->fromArray($postNotes);
return ['notes' => $this->postNoteViewProxy->fromArray($postNotes)];
}
}

View file

@ -45,6 +45,6 @@ class UpdatePostNote extends AbstractPostRoute
$formData = new PostNoteFormData($this->inputReader);
$postNote = $this->postNotesService->updatePostNote($postNote, $formData);
return $this->postNoteViewProxy->fromEntity($postNote);
return ['note' => $this->postNoteViewProxy->fromEntity($postNote)];
}
}

View file

@ -59,6 +59,6 @@ class UpdatePost extends AbstractPostRoute
$this->postService->updatePost($post, $formData);
$post = $this->postService->getByNameOrId($postNameOrId);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
return ['post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig())];
}
}

View file

@ -42,7 +42,6 @@ abstract class AbstractScoreRoute extends AbstractRoute
$result = $this->scoreService->setUserScore($user, $entity, $score);
return [
'score' => $this->scoreService->getScoreValue($entity),
'ownScore' => $result->getScore(),
];
'ownScore' => $result->getScore()];
}
}

View file

@ -31,6 +31,6 @@ class DeleteTag extends AbstractTagRoute
{
$tag = $this->tagService->getByName($args['tagName']);
$this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS);
return $this->tagService->deleteTag($tag);
$this->tagService->deleteTag($tag);
}
}

View file

@ -36,6 +36,6 @@ class GetTag extends AbstractTagRoute
$this->privilegeService->assertPrivilege(Privilege::LIST_TAGS);
$tag = $this->tagService->getByName($args['tagName']);
return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig());
return ['tag' => $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig())];
}
}

View file

@ -38,8 +38,6 @@ class GetTagSiblings extends AbstractTagRoute
$tag = $this->tagService->getByName($tagName);
$result = $this->tagService->getSiblings($tagName);
$entities = $this->tagViewProxy->fromArray($result);
return [
'data' => $entities,
];
return ['tags' => $entities];
}
}

View file

@ -49,7 +49,7 @@ class GetTags extends AbstractTagRoute
$result = $this->tagService->getFiltered($filter);
$entities = $this->tagViewProxy->fromArray($result->getEntities(), $this->getFullFetchConfig());
return [
'data' => $entities,
'tags' => $entities,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}

View file

@ -38,6 +38,6 @@ class MergeTags extends AbstractTagRoute
$sourceTag = $this->tagService->getByName($tagName);
$targetTag = $this->tagService->getByName($targetTagName);
$this->privilegeService->assertPrivilege(Privilege::MERGE_TAGS);
return $this->tagService->mergeTag($sourceTag, $targetTag);
$this->tagService->mergeTag($sourceTag, $targetTag);
}
}

View file

@ -57,6 +57,6 @@ class UpdateTag extends AbstractTagRoute
$this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_SUGGESTIONS);
$tag = $this->tagService->updateTag($tag, $formData);
return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig());
return ['tag' => $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig())];
}
}

View file

@ -24,6 +24,6 @@ class ActivateAccount extends AbstractUserRoute
public function work($args)
{
$user = $this->userService->getByNameOrEmail($args['userNameOrEmail'], true);
return $this->userService->sendActivationEmail($user);
$this->userService->sendActivationEmail($user);
}
}

View file

@ -41,6 +41,6 @@ class CreateUser extends AbstractUserRoute
$this->privilegeService->assertPrivilege(Privilege::REGISTER);
$formData = new RegistrationFormData($this->inputReader);
$user = $this->userService->createUser($formData);
return $this->userViewProxy->fromEntity($user);
return ['user' => $this->userViewProxy->fromEntity($user)];
}
}

View file

@ -38,6 +38,6 @@ class DeleteUser extends AbstractUserRoute
: Privilege::DELETE_ALL_ACCOUNTS);
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
return $this->userService->deleteUser($user);
$this->userService->deleteUser($user);
}
}

View file

@ -41,6 +41,6 @@ class GetUser extends AbstractUserRoute
if (!$this->privilegeService->isLoggedIn($userNameOrEmail))
$this->privilegeService->assertPrivilege(Privilege::VIEW_USERS);
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
return $this->userViewProxy->fromEntity($user);
return ['user' => $this->userViewProxy->fromEntity($user)];
}
}

View file

@ -52,7 +52,7 @@ class GetUsers extends AbstractUserRoute
$result = $this->userService->getFiltered($filter);
$entities = $this->userViewProxy->fromArray($result->getEntities());
return [
'data' => $entities,
'users' => $entities,
'pageSize' => $result->getPageSize(),
'totalRecords' => $result->getTotalRecords()];
}

View file

@ -22,6 +22,6 @@ class PasswordReset extends AbstractUserRoute
public function work($args)
{
$user = $this->userService->getByNameOrEmail($args['userNameOrEmail']);
return $this->userService->sendPasswordResetEmail($user);
$this->userService->sendPasswordResetEmail($user);
}
}

View file

@ -91,6 +91,6 @@ class UpdateUser extends AbstractUserRoute
}
$user = $this->userService->updateUser($user, $formData);
return $this->userViewProxy->fromEntity($user);
return ['user' => $this->userViewProxy->fromEntity($user)];
}
}