Search in sources :

Example 1 with Tag

use of com.myblog.model.Tag in project newblogback by Zephery.

the class TagServiceImpl method updatetag.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Integer updatetag(Integer tId) {
    // 标签
    List<Tag> tags = tagMapper.getAllTags();
    JsonArray jsonArray = new JsonArray();
    for (Tag tag : tags) {
        List<Blog> blogs = tagMapper.getblogbytagid(tag.gettId());
        String str = tag.gettName() + " " + "(" + String.valueOf(blogs.size()) + ")";
        KeyAndValue keyAndValue = new KeyAndValue();
        keyAndValue.setKey(tag.gettName());
        keyAndValue.setValue(str);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("key", tag.gettId());
        jsonObject.addProperty("value", str);
        jsonArray.add(jsonObject);
    }
    boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {

        @Override
        public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            connection.set(serializer.serialize("biaoqian"), serializer.serialize(jsonArray.toString()));
            return true;
        }
    });
    return tId;
}
Also used : KeyAndValue(com.myblog.model.KeyAndValue) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) Tag(com.myblog.model.Tag) Blog(com.myblog.model.Blog) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 2 with Tag

use of com.myblog.model.Tag in project newblogback by Zephery.

the class BlogServiceImpl method updateByPrimaryKeySelective.

@Override
public Integer updateByPrimaryKeySelective(Blog blog) {
    blog.setSummary(blog.getContent().length() > 120 ? blog.getContent().substring(0, 120) : blog.getContent());
    Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(blog.getContent());
    String image_url = null;
    while (matcher.find()) {
        image_url = matcher.group();
    }
    blog.setImageurl(getImageURl(image_url));
    String[] array = blog.getTagforsplit().split(",");
    List<Tag> tags = new ArrayList<>();
    for (String string : array) {
        if (tagMapper.selectByName(string) == null) {
            Tag tag = new Tag();
            tag.settName(string);
            // TODO 为了下面的relation增加标签id
            tagMapper.insert(tag);
            Tag newtag = tagMapper.selectByName(string);
            tags.add(newtag);
        } else {
            tags.add(tagMapper.selectByName(string));
        }
    }
    blog.setTags(tags.size() == 0 ? null : tags);
    blog.setContent(StringEscapeUtils.escapeSql(blog.getContent()));
    blogMapper.updateByPrimaryKeySelective(blog);
    relationMapper.deletebyblogid(blog.getBlogid());
    if (tags.size() > 0) {
        for (Tag tag : tags) {
            Relation relation = new Relation();
            relation.setBlogid(blog.getBlogid());
            relation.settId(tag.gettId());
            relationMapper.insert(relation);
        }
    }
    return blog.getBlogid();
}
Also used : Relation(com.myblog.model.Relation) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Tag(com.myblog.model.Tag)

Example 3 with Tag

use of com.myblog.model.Tag in project newblogback by Zephery.

the class CategoryController method index.

@GetMapping(value = "")
public ModelAndView index() {
    List<Category> categories = categoryService.getAllCategory();
    List<Tag> tags = tagService.getAllTags();
    ModelAndView mv = new ModelAndView();
    mv.addObject("categories", categories);
    mv.addObject("tags", tags);
    mv.setViewName("/admin/category");
    return mv;
}
Also used : Category(com.myblog.model.Category) ModelAndView(org.springframework.web.servlet.ModelAndView) Tag(com.myblog.model.Tag)

Example 4 with Tag

use of com.myblog.model.Tag in project newblogback by Zephery.

the class BlogServiceImpl method getBlogDetail.

@Override
public Blog getBlogDetail(Integer blogid) {
    Blog blog = blogMapper.selectByPrimaryKey(blogid);
    if (blog == null) {
        return null;
    }
    Category category = categoryMapper.selectByPrimaryKey(blog.getCategoryid());
    blog.setCategory(category);
    List<Tag> tags = tagMapper.getTagByBlogId(blog.getBlogid());
    List<String> list = new ArrayList<>();
    for (Tag tag : tags) {
        list.add(tag.gettName());
    }
    blog.setTagforsplit(StringUtils.join(list, ","));
    blog.setTags(tags.size() > 0 ? tags : null);
    if (blogMapper.updatehits(blogid)) {
        logger.info("read count success");
    } else {
        logger.info("read count failure");
    }
    return blog;
}
Also used : Category(com.myblog.model.Category) ArrayList(java.util.ArrayList) Tag(com.myblog.model.Tag) Blog(com.myblog.model.Blog)

Example 5 with Tag

use of com.myblog.model.Tag in project newblogback by Zephery.

the class BlogServiceImpl method insertblog.

@Override
public synchronized Integer insertblog(Blog blog) {
    blog.setSummary(blog.getContent().length() > 120 ? blog.getContent().substring(0, 120) : blog.getContent());
    blog.setCreateAt(DateTime.now().toString("yyyy-MM-dd"));
    Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(blog.getContent());
    String image_url = null;
    while (matcher.find()) {
        image_url = matcher.group();
    }
    // http://image.wenzhihuai.com/images/20171018051437.png?imageInfo
    // {"size":14736,"format":"png","width":667,"height":387,"colorModel":"rgba"}
    blog.setImageurl(getImageURl(image_url));
    String[] array = blog.getTagforsplit().split(",");
    List<Tag> tags = new ArrayList<>();
    for (String string : array) {
        if (tagMapper.selectByName(string) == null) {
            Tag tag = new Tag();
            tag.settName(string);
            tagMapper.insertSelective(tag);
            Tag newtag = tagMapper.selectByName(string);
            tags.add(newtag);
        // tag.settId(tId);//TODO 返回主键长不起作用
        // tags.add(tag);
        } else {
            tags.add(tagMapper.selectByName(string));
        }
    }
    blog.setTags(tags.size() == 0 ? null : tags);
    blog.setHits(0);
    blog.setContent(StringEscapeUtils.escapeSql(blog.getContent()));
    blogMapper.insert(blog);
    Blog newblog = blogMapper.selectByTitle(blog.getTitle());
    if (tags.size() > 0) {
        for (Tag tag : tags) {
            Relation relation = new Relation();
            relation.setBlogid(newblog.getBlogid());
            relation.settId(tag.gettId());
            relationMapper.insert(relation);
        }
    }
    return blogMapper.selectMaxId();
}
Also used : Relation(com.myblog.model.Relation) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Tag(com.myblog.model.Tag) Blog(com.myblog.model.Blog)

Aggregations

Tag (com.myblog.model.Tag)5 Blog (com.myblog.model.Blog)3 ArrayList (java.util.ArrayList)3 Category (com.myblog.model.Category)2 Relation (com.myblog.model.Relation)2 Matcher (java.util.regex.Matcher)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 KeyAndValue (com.myblog.model.KeyAndValue)1 DataAccessException (org.springframework.dao.DataAccessException)1 RedisConnection (org.springframework.data.redis.connection.RedisConnection)1 RedisSerializer (org.springframework.data.redis.serializer.RedisSerializer)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1