Create ContentTagController

pull/137/head
ruibaby 2019-03-21 20:48:42 +08:00
parent d81837e3c9
commit fc4beb2447
11 changed files with 157 additions and 5 deletions

View File

@ -4,6 +4,8 @@ import cc.ryanc.halo.model.entity.Category;
import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.lang.NonNull;
import java.util.Optional;
/**
* Category repository.
*
@ -26,4 +28,12 @@ public interface CategoryRepository extends BaseRepository<Category, Integer> {
* @return the count
*/
long countById(@NonNull Integer id);
/**
* Get category by slug name
*
* @param slugName slug name
* @return Optional of Category
*/
Optional<Category> getBySlugName(@NonNull String slugName);
}

View File

@ -4,6 +4,8 @@ import cc.ryanc.halo.model.entity.Tag;
import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.lang.NonNull;
import java.util.Optional;
/**
* Tag repository.
*
@ -19,4 +21,12 @@ public interface TagRepository extends BaseRepository<Tag, Integer> {
* @return tag count
*/
long countByNameOrSlugName(@NonNull String name, @NonNull String slugName);
/**
* Get tag by slug name
*
* @param slugName slug name
* @return Tag
*/
Optional<Tag> getBySlugName(@NonNull String slugName);
}

View File

@ -31,4 +31,12 @@ public interface CategoryService extends CrudService<Category, Integer> {
*/
@NonNull
List<CategoryVO> listAsTree(@NonNull Sort sort);
/**
* Get category by slug name
*
* @param slugName slug name
* @return Category
*/
Category getBySlugName(@NonNull String slugName);
}

View File

@ -1,7 +1,6 @@
package cc.ryanc.halo.service;
import cc.ryanc.halo.model.dto.GalleryOutputDTO;
import cc.ryanc.halo.model.dto.LinkOutputDTO;
import cc.ryanc.halo.model.entity.Gallery;
import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Sort;

View File

@ -32,4 +32,11 @@ public interface TagService extends CrudService<Tag, Integer> {
@NonNull
List<TagOutputDTO> listDtos(@NonNull Sort sort);
/**
* Get tag by slug name
*
* @param slugName slug name
* @return Tag
*/
Tag getBySlugName(@NonNull String slugName);
}

View File

@ -61,6 +61,7 @@ public interface UserService extends CrudService<User, Integer> {
* @return user info
* @throws NotFoundException throws when the username does not exist
*/
@NonNull
User getByEmailOfNonNull(@NonNull String email);
/**

View File

@ -147,4 +147,15 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
return topCategory;
}
/**
* Get category by slug name
*
* @param slugName slug name
* @return Category
*/
@Override
public Category getBySlugName(String slugName) {
return categoryRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The Category does not exist").setErrorData(slugName));
}
}

View File

@ -8,16 +8,13 @@ import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.entity.*;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.params.PostParam;
import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.*;
import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.MarkdownUtils;
import cc.ryanc.halo.utils.ServiceUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.exception.AlreadyExistsException;
import cc.ryanc.halo.exception.NotFoundException;
import cc.ryanc.halo.model.dto.TagOutputDTO;
import cc.ryanc.halo.model.entity.Tag;
import cc.ryanc.halo.repository.TagRepository;
@ -63,4 +64,15 @@ public class TagServiceImpl extends AbstractCrudService<Tag, Integer> implements
// Get tag name
return super.create(tag);
}
/**
* Get tag by slug name
*
* @param slugName slug name
* @return Tag
*/
@Override
public Tag getBySlugName(String slugName) {
return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName));
}
}

View File

@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
/**
* Tag controller.

View File

@ -0,0 +1,98 @@
package cc.ryanc.halo.web.controller.content;
import cc.ryanc.halo.model.entity.Tag;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.vo.PostListVO;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.web.controller.content.base.BaseContentController;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Tag Controller
*
* @author : RYAN0UP
* @date : 2019-03-21
*/
@Controller
@RequestMapping(value = "/tags")
public class ContentTagController extends BaseContentController {
private TagService tagService;
private PostService postService;
public ContentTagController(TagService tagService, PostService postService) {
this.tagService = tagService;
this.postService = postService;
}
/**
* All of tags
*
* @return template path: themes/{theme}/tags
*/
@GetMapping
public String tags() {
return this.render("tags");
}
/**
* List tags by tag slug
*
* @param model model
* @param slugName slug name
* @return template path: themes/{theme}/tag
*/
@GetMapping(value = "{slugName}")
public String tags(Model model,
@PathVariable("slugName") String slugName) {
return this.tags(model, slugName, 1, Sort.by(DESC, "postDate"));
}
/**
* List tags by tag slug
*
* @param model model
* @param slugName slug name
* @param page current page
* @return template path: themes/{theme}/tag
*/
@GetMapping(value = "{slugName}/page/{page}")
public String tags(Model model,
@PathVariable("slugName") String slugName,
@PathVariable("page") Integer page,
@SortDefault(sort = "postDate", direction = DESC) Sort sort) {
final Tag tag = tagService.getBySlugName(slugName);
if (null == tag) {
return this.renderNotFound();
}
int size = 10;
if (StrUtil.isNotBlank(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()))) {
size = Integer.parseInt(OPTIONS.get(BlogProperties.INDEX_POSTS.getValue()));
}
final Pageable pageable = PageRequest.of(page - 1, size, sort);
// TODO get posts by tag
final Page<PostListVO> posts;
//final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
// model.addAttribute("is_tags", true);
// model.addAttribute("posts", posts);
// model.addAttribute("rainbow", rainbow);
// model.addAttribute("tag", tag);
return this.render("tag");
}
}