diff --git a/src/main/java/com/rymcu/vertical/core/service/AbstractService.java b/src/main/java/com/rymcu/vertical/core/service/AbstractService.java index ebcab81..6b4ea6f 100644 --- a/src/main/java/com/rymcu/vertical/core/service/AbstractService.java +++ b/src/main/java/com/rymcu/vertical/core/service/AbstractService.java @@ -26,26 +26,32 @@ public abstract class AbstractService implements Service { modelClass = (Class) pt.getActualTypeArguments()[0]; } + @Override public void save(T model) { mapper.insertSelective(model); } + @Override public void save(List models) { mapper.insertList(models); } + @Override public void deleteById(String id) { mapper.deleteByPrimaryKey(id); } + @Override public void deleteByIds(String ids) { mapper.deleteByIds(ids); } + @Override public void update(T model) { mapper.updateByPrimaryKeySelective(model); } + @Override public T findById(String id) { return mapper.selectByPrimaryKey(id); } @@ -63,14 +69,17 @@ public abstract class AbstractService implements Service { } } + @Override public List findByIds(String ids) { return mapper.selectByIds(ids); } + @Override public List findByCondition(Condition condition) { return mapper.selectByCondition(condition); } + @Override public List findAll() { return mapper.selectAll(); } diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java index 6e6b919..ce315ea 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java @@ -3,6 +3,7 @@ package com.rymcu.vertical.dto; import lombok.Data; import java.util.Date; +import java.util.List; /** * @author ronger @@ -44,4 +45,6 @@ public class ArticleDTO { private Date updatedTime; private Author articleAuthor; + + private List tags; } diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java new file mode 100644 index 0000000..7eb15f7 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java @@ -0,0 +1,21 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class ArticleTagDTO { + private Integer idTag; + + private String tagTitle; + + private String tagUri; + + private String tagDescription; + + private String tagIconPath; + + private Integer tagAuthorId; +} diff --git a/src/main/java/com/rymcu/vertical/entity/Tag.java b/src/main/java/com/rymcu/vertical/entity/Tag.java index 2b6f113..24dba8f 100644 --- a/src/main/java/com/rymcu/vertical/entity/Tag.java +++ b/src/main/java/com/rymcu/vertical/entity/Tag.java @@ -36,6 +36,8 @@ public class Tag implements Serializable,Cloneable { private String tagAd; /** 是否显示全站侧边栏广告 */ private String tagShowSideAd; + /** 标签状态 */ + private String tagStatus; /** 创建时间 */ private Date createdTime; /** 更新时间 */ diff --git a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java index 8bf3682..f365dd5 100644 --- a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java @@ -2,6 +2,7 @@ package com.rymcu.vertical.mapper; import com.rymcu.vertical.core.mapper.Mapper; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.ArticleTagDTO; import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.ArticleContent; @@ -27,4 +28,8 @@ public interface ArticleMapper extends Mapper
{ List selectArticlesByTagName(@Param("tagName") String tagName); List selectUserArticles(@Param("idUser") Integer idUser); + + Integer deleteTagArticle(@Param("id") Integer id); + + List selectTags(@Param("idArticle") Integer idArticle); } diff --git a/src/main/java/com/rymcu/vertical/mapper/TagMapper.java b/src/main/java/com/rymcu/vertical/mapper/TagMapper.java index 6c3543b..64c7b14 100644 --- a/src/main/java/com/rymcu/vertical/mapper/TagMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/TagMapper.java @@ -12,4 +12,8 @@ public interface TagMapper extends Mapper { Integer selectCountUserTagById(@Param("idUser") Integer idUser, @Param("idTag") Integer idTag); Integer insertUserTag(@Param("idTag") Integer idTag, @Param("idUser") Integer idUser, @Param("type") Integer type); + + Integer deleteUnusedTag(); + + Integer update(@Param("idTag") Integer idTag, @Param("tagUri") String tagUri, @Param("tagIconPath") String tagIconPath, @Param("tagStatus") String tagStatus, @Param("tagDescription") String tagDescription); } diff --git a/src/main/java/com/rymcu/vertical/service/ArticleService.java b/src/main/java/com/rymcu/vertical/service/ArticleService.java index 6f217fc..871c8d9 100644 --- a/src/main/java/com/rymcu/vertical/service/ArticleService.java +++ b/src/main/java/com/rymcu/vertical/service/ArticleService.java @@ -61,4 +61,11 @@ public interface ArticleService extends Service
{ * @return * */ Map postArticle(ArticleDTO article, HttpServletRequest request) throws UnsupportedEncodingException, BaseApiException; + + /** + * 删除文章 + * @param id + * @return + * */ + Map delete(Integer id); } diff --git a/src/main/java/com/rymcu/vertical/service/TagService.java b/src/main/java/com/rymcu/vertical/service/TagService.java index b83a5cf..7c0f4c4 100644 --- a/src/main/java/com/rymcu/vertical/service/TagService.java +++ b/src/main/java/com/rymcu/vertical/service/TagService.java @@ -6,6 +6,7 @@ import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.web.api.exception.BaseApiException; import java.io.UnsupportedEncodingException; +import java.util.Map; /** * @author ronger @@ -20,4 +21,17 @@ public interface TagService extends Service { * @return * */ Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException; + + /** + * 清除未使用标签 + * @return + * */ + Map cleanUnusedTag(); + + /** + * 添加/更新标签 + * @param tag + * @return + */ + Map saveTag(Tag tag); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 8dc7c6e..eeb22b1 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -2,6 +2,7 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.ArticleTagDTO; import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.ArticleContent; @@ -135,10 +136,30 @@ public class ArticleServiceImpl extends AbstractService
implements Arti return map; } + @Override + @Transactional(rollbackFor = Exception.class) + public Map delete(Integer id) { + Map map = new HashMap(1); + Integer result; + // 删除引用标签记录 + result = articleMapper.deleteTagArticle(id); + if (result > 0){ + result = articleMapper.deleteByPrimaryKey(id); + if (result < 1){ + map.put("message", "删除失败!"); + } + } else { + map.put("message", "删除失败!"); + } + return map; + } + private ArticleDTO genArticle(ArticleDTO article,Integer type) { Author author = articleMapper.selectAuthor(article.getArticleAuthorId()); article.setArticleAuthor(author); article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime())); + List tags = articleMapper.selectTags(article.getIdArticle()); + article.setTags(tags); if(type == 1){ ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); article.setArticleContent(articleContent.getArticleContentHtml()); diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 345e813..d78b366 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -16,6 +16,8 @@ import javax.annotation.Resource; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Date; +import java.util.HashMap; +import java.util.Map; /** * @author ronger @@ -45,12 +47,15 @@ public class TagServiceImpl extends AbstractService implements TagService { tag.setTagUri(URLEncoder.encode(tag.getTagTitle(),"UTF-8")); tag.setCreatedTime(new Date()); tag.setUpdatedTime(tag.getCreatedTime()); + tag.setTagArticleCount(1); tagMapper.insertSelective(tag); addTagArticle = true; addUserTag = true; } else { Integer count = tagMapper.selectCountTagArticleById(tag.getIdTag(),article.getIdArticle()); if(count == 0){ + tag.setTagArticleCount(tag.getTagArticleCount() + 1); + tagMapper.updateByPrimaryKeySelective(tag); addTagArticle = true; } Integer countUserTag = tagMapper.selectCountUserTagById(user.getIdUser(),tag.getIdTag()); @@ -69,4 +74,33 @@ public class TagServiceImpl extends AbstractService implements TagService { } return 0; } + + @Override + @Transactional(rollbackFor = Exception.class) + public Map cleanUnusedTag() { + Map map = new HashMap(1); + tagMapper.deleteUnusedTag(); + return map; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public Map saveTag(Tag tag) { + Integer result = 0; + if (tag.getIdTag() == null) { + tag.setCreatedTime(new Date()); + tag.setUpdatedTime(tag.getCreatedTime()); + result = tagMapper.insertSelective(tag); + } else { + tag.setUpdatedTime(new Date()); + result = tagMapper.update(tag.getIdTag(),tag.getTagUri(),tag.getTagIconPath(),tag.getTagStatus(),tag.getTagDescription()); + } + Map map = new HashMap(1); + if (result == 0) { + map.put("message","操作失败!"); + } else { + map.put("tag", tag); + } + return map; + } } diff --git a/src/main/java/com/rymcu/vertical/service/impl/TopicServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TopicServiceImpl.java index 598fbde..333c159 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TopicServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TopicServiceImpl.java @@ -9,6 +9,7 @@ import com.rymcu.vertical.entity.Topic; import com.rymcu.vertical.mapper.TopicMapper; import com.rymcu.vertical.service.TopicService; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Date; @@ -52,6 +53,7 @@ public class TopicServiceImpl extends AbstractService implements TopicSer } @Override + @Transactional(rollbackFor = Exception.class) public Map saveTopic(Topic topic) { Integer result = 0; if (topic.getIdTopic() == null) { diff --git a/src/main/java/com/rymcu/vertical/util/Utils.java b/src/main/java/com/rymcu/vertical/util/Utils.java index 37e4409..db4060c 100644 --- a/src/main/java/com/rymcu/vertical/util/Utils.java +++ b/src/main/java/com/rymcu/vertical/util/Utils.java @@ -136,7 +136,7 @@ public class Utils { public static Map getArticlesGlobalResult(PageInfo pageInfo) { Map map = new HashMap(2); map.put("articles", pageInfo.getList()); - Map pagination = new HashMap(3); + Map pagination = new HashMap(4); pagination.put("pageSize",pageInfo.getPageSize()); pagination.put("total",pageInfo.getTotal()); pagination.put("currentPage",pageInfo.getPageNum()); diff --git a/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java b/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java index 68c2cf1..7b87193 100644 --- a/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java +++ b/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java @@ -6,9 +6,11 @@ import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.dto.admin.UserRoleDTO; import com.rymcu.vertical.entity.Role; +import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.Topic; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.service.RoleService; +import com.rymcu.vertical.service.TagService; import com.rymcu.vertical.service.TopicService; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.Utils; @@ -33,6 +35,8 @@ public class AdminController { private RoleService roleService; @Resource private TopicService topicService; + @Resource + private TagService tagService; @GetMapping("/users") public GlobalResult> users(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){ @@ -136,4 +140,40 @@ public class AdminController { return GlobalResultGenerator.genSuccessResult(map); } + @GetMapping("/tags") + public GlobalResult> tags(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){ + PageHelper.startPage(page, rows); + List list = tagService.findAll(); + PageInfo pageInfo = new PageInfo<>(list); + Map map = new HashMap<>(2); + map.put("tags", pageInfo.getList()); + Map pagination = Utils.getPagination(pageInfo); + map.put("pagination", pagination); + return GlobalResultGenerator.genSuccessResult(map); + } + + @DeleteMapping("/tag/clean-unused") + public GlobalResult> cleanUnusedTag(){ + Map map = tagService.cleanUnusedTag(); + return GlobalResultGenerator.genSuccessResult(map); + } + + @GetMapping("/tag/detail/{idTag}") + public GlobalResult tagDetail(@PathVariable Integer idTag){ + Tag tag = tagService.findById(idTag.toString()); + return GlobalResultGenerator.genSuccessResult(tag); + } + + @PostMapping("/tag/post") + public GlobalResult addTag(@RequestBody Tag tag){ + Map map = tagService.saveTag(tag); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PutMapping("/tag/post") + public GlobalResult updateTag(@RequestBody Tag tag){ + Map map = tagService.saveTag(tag); + return GlobalResultGenerator.genSuccessResult(map); + } + }