forked from Archives/Athou_commafeed
added rest methods for updating positions
This commit is contained in:
@@ -51,6 +51,13 @@ public class FeedCategoryDAO extends GenericDAO<FeedCategory> {
|
||||
return category;
|
||||
}
|
||||
|
||||
public List<FeedCategory> findByParent(User user, FeedCategory parent) {
|
||||
EasyCriteria<FeedCategory> criteria = createCriteria();
|
||||
criteria.andEquals(FeedCategory_.user.getName(), user);
|
||||
criteria.andEquals(FeedCategory_.parent.getName(), parent);
|
||||
return criteria.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedCategory> findAllChildrenCategories(User user,
|
||||
FeedCategory parent) {
|
||||
List<FeedCategory> list = Lists.newArrayList();
|
||||
@@ -77,4 +84,5 @@ public class FeedCategoryDAO extends GenericDAO<FeedCategory> {
|
||||
}
|
||||
return isChild;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ public class CategoryModificationRequest implements Serializable {
|
||||
@ApiProperty(value = "new parent category id")
|
||||
private String parentId;
|
||||
|
||||
@ApiProperty(value = "new display position, null if not changed")
|
||||
private Integer position;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -48,4 +51,12 @@ public class CategoryModificationRequest implements Serializable {
|
||||
this.parentId = parentId;
|
||||
}
|
||||
|
||||
public Integer getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(Integer position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ public class FeedModificationRequest implements Serializable {
|
||||
@ApiProperty(value = "new parent category id")
|
||||
private String categoryId;
|
||||
|
||||
@ApiProperty(value = "new display position, null if not changed")
|
||||
private Integer position;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -48,4 +51,12 @@ public class FeedModificationRequest implements Serializable {
|
||||
this.categoryId = categoryId;
|
||||
}
|
||||
|
||||
public Integer getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(Integer position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -266,6 +266,31 @@ public class CategoryREST extends AbstractResourceREST {
|
||||
Long.valueOf(req.getParentId()));
|
||||
}
|
||||
category.setParent(parent);
|
||||
|
||||
if (req.getPosition() != null) {
|
||||
List<FeedCategory> categories = feedCategoryDAO.findByParent(
|
||||
getUser(), parent);
|
||||
int existingIndex = -1;
|
||||
for (int i = 0; i < categories.size(); i++) {
|
||||
if (ObjectUtils.equals(categories.get(i).getId(),
|
||||
category.getId())) {
|
||||
existingIndex = i;
|
||||
}
|
||||
}
|
||||
if (existingIndex != -1) {
|
||||
categories.remove(existingIndex);
|
||||
}
|
||||
|
||||
categories.add(Math.min(req.getPosition(), categories.size()),
|
||||
category);
|
||||
for (int i = 0; i < categories.size(); i++) {
|
||||
categories.get(i).setPosition(i);
|
||||
}
|
||||
feedCategoryDAO.update(categories);
|
||||
} else {
|
||||
feedCategoryDAO.update(category);
|
||||
}
|
||||
|
||||
feedCategoryDAO.update(category);
|
||||
|
||||
return Response.ok(Status.OK).build();
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.apache.commons.fileupload.FileItemFactory;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -327,7 +328,29 @@ public class FeedREST extends AbstractResourceREST {
|
||||
Long.valueOf(req.getCategoryId()));
|
||||
}
|
||||
subscription.setCategory(parent);
|
||||
feedSubscriptionDAO.update(subscription);
|
||||
|
||||
if (req.getPosition() != null) {
|
||||
List<FeedSubscription> subs = feedSubscriptionDAO.findByCategory(
|
||||
getUser(), parent);
|
||||
int existingIndex = -1;
|
||||
for (int i = 0; i < subs.size(); i++) {
|
||||
if (ObjectUtils.equals(subs.get(i).getId(),
|
||||
subscription.getId())) {
|
||||
existingIndex = i;
|
||||
}
|
||||
}
|
||||
if (existingIndex != -1) {
|
||||
subs.remove(existingIndex);
|
||||
}
|
||||
|
||||
subs.add(Math.min(req.getPosition(), subs.size()), subscription);
|
||||
for (int i = 0; i < subs.size(); i++) {
|
||||
subs.get(i).setPosition(i);
|
||||
}
|
||||
feedSubscriptionDAO.update(subs);
|
||||
} else {
|
||||
feedSubscriptionDAO.update(subscription);
|
||||
}
|
||||
|
||||
return Response.ok(Status.OK).build();
|
||||
}
|
||||
|
||||
@@ -364,13 +364,13 @@ module.directive('draggable', function() {
|
||||
restrict : 'A',
|
||||
link : function(scope, element, attrs) {
|
||||
element.draggable({
|
||||
revert: true
|
||||
axis: 'y'
|
||||
}).data('source', scope.$eval(attrs.draggable));
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
module.directive('droppable', function($compile) {
|
||||
module.directive('droppable', [ 'CategoryService', 'FeedService', function(CategoryService, FeedService) {
|
||||
return {
|
||||
restrict : 'A',
|
||||
link : function(scope, element, attrs) {
|
||||
@@ -382,15 +382,50 @@ module.directive('droppable', function($compile) {
|
||||
drop : function(event, ui) {
|
||||
var draggable = angular.element(ui.draggable);
|
||||
|
||||
|
||||
var index = draggable.data('index');
|
||||
var source = draggable.data('source');
|
||||
var target = scope.$eval(attrs.droppable);
|
||||
|
||||
console.log(source)
|
||||
var data = {
|
||||
id: source.id,
|
||||
name: source.name
|
||||
};
|
||||
|
||||
if (source.children) {
|
||||
// source is a category
|
||||
|
||||
if (target.children) {
|
||||
// target is a category
|
||||
data.parentId = target.id;
|
||||
data.position = 0;
|
||||
} else {
|
||||
// target is a feed
|
||||
data.parentId = target.categoryId;
|
||||
}
|
||||
|
||||
CategoryService.modify(data, function() {
|
||||
CategoryService.init();
|
||||
});
|
||||
} else {
|
||||
// source is a feed
|
||||
|
||||
if (target.children) {
|
||||
// target is a category
|
||||
data.categoryId = target.id;
|
||||
data.position = 0;
|
||||
} else {
|
||||
// target is a feed
|
||||
data.categoryId = target.categoryId;
|
||||
data.position = target.position;
|
||||
}
|
||||
|
||||
FeedService.modify(data, function() {
|
||||
CategoryService.init();
|
||||
});
|
||||
}
|
||||
|
||||
scope.$apply();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}]);
|
||||
Reference in New Issue
Block a user