Commit e5ec2661 authored by Liang Ding's avatar Liang Ding

🚧 #12256 填充侧边栏分类模型

parent 183ba177
...@@ -20,7 +20,7 @@ package org.b3log.solo.model; ...@@ -20,7 +20,7 @@ package org.b3log.solo.model;
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="mailto:dongxu.wang@acm.org">Dongxu Wang</a> * @author <a href="mailto:dongxu.wang@acm.org">Dongxu Wang</a>
* @version 1.4.5.1, Nov 1, 2015 * @version 1.5.5.1, Apr 8, 2017
* @since 0.3.1 * @since 0.3.1
*/ */
public final class Common { public final class Common {
...@@ -35,6 +35,11 @@ public final class Common { ...@@ -35,6 +35,11 @@ public final class Common {
*/ */
public static final String MOST_USED_TAGS = "mostUsedTags"; public static final String MOST_USED_TAGS = "mostUsedTags";
/**
* Most used categories.
*/
public static final String MOST_USED_CATEGORIES = "mostUsedCategories";
/** /**
* Most comment count articles. * Most comment count articles.
*/ */
...@@ -174,7 +179,7 @@ public final class Common { ...@@ -174,7 +179,7 @@ public final class Common {
* Key of current user. * Key of current user.
*/ */
public static final String CURRENT_USER = "currentUser"; public static final String CURRENT_USER = "currentUser";
/** /**
* Key of admin user. * Key of admin user.
*/ */
......
...@@ -19,11 +19,13 @@ import org.b3log.latke.repository.Repository; ...@@ -19,11 +19,13 @@ import org.b3log.latke.repository.Repository;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List;
/** /**
* Category repository. * Category repository.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.1, Apr 1, 2017 * @version 1.2.0.1, Apr 8, 2017
* @since 2.0.0 * @since 2.0.0
*/ */
public interface CategoryRepository extends Repository { public interface CategoryRepository extends Repository {
...@@ -80,4 +82,13 @@ public interface CategoryRepository extends Repository { ...@@ -80,4 +82,13 @@ public interface CategoryRepository extends Repository {
* @throws RepositoryException repository exception * @throws RepositoryException repository exception
*/ */
JSONObject getByOrder(final int order) throws RepositoryException; JSONObject getByOrder(final int order) throws RepositoryException;
/**
* Gets most used categories (contains the most tags) with the specified number.
*
* @param num the specified number
* @return a list of most used categories, returns an empty list if not found
* @throws RepositoryException repository exception
*/
List<JSONObject> getMostUsedCategories(final int num) throws RepositoryException;
} }
...@@ -18,16 +18,23 @@ package org.b3log.solo.repository.impl; ...@@ -18,16 +18,23 @@ package org.b3log.solo.repository.impl;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.repository.*; import org.b3log.latke.repository.*;
import org.b3log.latke.repository.annotation.Repository; import org.b3log.latke.repository.annotation.Repository;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.model.Category; import org.b3log.solo.model.Category;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.CategoryRepository; import org.b3log.solo.repository.CategoryRepository;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/** /**
* Category repository. * Category repository.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.0, Apr 1, 2017 * @version 1.1.0.1, Apr 8, 2017
* @since 2.0.0 * @since 2.0.0
*/ */
@Repository @Repository
...@@ -104,6 +111,20 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego ...@@ -104,6 +111,20 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego
return array.optJSONObject(0); return array.optJSONObject(0);
} }
@Override
public List<JSONObject> getMostUsedCategories(final int num) throws RepositoryException {
final Query query = new Query().addSort(Category.CATEGORY_TAG_CNT, SortDirection.DESCENDING).
setCurrentPageNum(1).setPageSize(num).setPageCount(1);
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
final List<JSONObject> ret = CollectionUtils.jsonArrayToList(array);
sortJSONCategoryList(ret);
return ret;
}
@Override @Override
public JSONObject getUpper(final String id) throws RepositoryException { public JSONObject getUpper(final String id) throws RepositoryException {
final JSONObject category = get(id); final JSONObject category = get(id);
...@@ -153,4 +174,14 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego ...@@ -153,4 +174,14 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego
return array.optJSONObject(0); return array.optJSONObject(0);
} }
private void sortJSONCategoryList(final List<JSONObject> tagJoList) {
Collections.sort(tagJoList, new Comparator<JSONObject>() {
@Override
public int compare(final JSONObject o1, final JSONObject o2) {
return Collator.getInstance(java.util.Locale.CHINA).
compare(o1.optString(Tag.TAG_TITLE), o2.optString(Tag.TAG_TITLE));
}
});
}
} }
...@@ -142,9 +142,8 @@ public class ArticleQueryService { ...@@ -142,9 +142,8 @@ public class ArticleQueryService {
pagination.put(Pagination.PAGINATION_PAGE_NUMS, (Object) Collections.emptyList()); pagination.put(Pagination.PAGINATION_PAGE_NUMS, (Object) Collections.emptyList());
try { try {
final JSONArray categoryTags = final JSONArray categoryTags = categoryTagRepository.getByCategoryId(
categoryTagRepository.getByCategoryId(categoryId, 1, Integer.MAX_VALUE) categoryId, 1, Integer.MAX_VALUE).optJSONArray(Keys.RESULTS);
.optJSONArray(Keys.RESULTS);
if (categoryTags.length() <= 0) { if (categoryTags.length() <= 0) {
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment