当前位置: 首页 > article >正文

Spring boot+Vue3博客平台:文章搜索与推荐和文章阅读统计模块

一、文章搜索与推荐功能

 

1.前端搜索功能实现

在文章列表组件中添加搜索框,用户可以输入关键字进行文章搜索。同时,在搜索结果下方展示相关文章推荐。

<template>
  <div class="article-list">
    <div class="search">
      <input type="text" v-model="searchKeyword" @input="searchArticles" placeholder="Search articles" />
    </div>
    <!-- ... -->
    <div class="related-articles" v-if="relatedArticles.length">
      <h3>Related articles</h3>
      <ul>
        <li v-for="article in relatedArticles" :key="article.id">
          <a :href="'/article/' + article.id">{{ article.title }}</a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // ...
      searchKeyword: "",
      relatedArticles: [],
    };
  },
  methods: {
    // ...
    async searchArticles() {
      const response = await this.$http.get("/api/articles/search", {
        params: { keyword: this.searchKeyword },
      });
      this.articles = response.data;
      this.fetchRelatedArticles();
    },
    async fetchRelatedArticles() {
      // 实现根据搜索关键字获取相关文章的逻辑
    },
  },
};
</script>

2.后端搜索功能实现

ArticleController中添加搜索文章的接口。

@GetMapping("/search")
public ResponseEntity<?> searchArticles(@RequestParam String keyword) {
    List<Article> articles = articleRepository.searchByKeyword(keyword);
    return ResponseEntity.ok(articles);
}

ArticleRepository中添加根据关键字搜索文章的方法。

public interface ArticleRepository extends JpaRepository<Article, Long> {
    @Query("SELECT a FROM Article a WHERE a.title LIKE %:keyword% OR a.content LIKE %:keyword%")
    List<Article> searchByKeyword(@Param("keyword") String keyword);
}

二、文章阅读统计模块

1.前端阅读次数显示

在文章详情组件中,展示文章阅读次数。

<template>
  <div class="article-detail">
    <h1>{{ article.title }}</h1>
    <p class="meta">Views: {{ article.views }}</p>
    <!-- ... -->
  </div>
</template>

2.后端阅读次数更新

ArticleController中添加更新阅读次数的接口。

@PutMapping("/{articleId}/views")
public ResponseEntity<?> updateArticleViews(@PathVariable Long articleId) {
    Optional<Article> optionalArticle = articleRepository.findById(articleId);
    if (optionalArticle.isPresent()) {
        Article article = optionalArticle.get();
        article.setViews(article.getViews() + 1);
        articleRepository.save(article);
        return ResponseEntity.ok("Article views updated successfully.");
    } else {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Article not found.");
    }
}

3.更新阅读次数

在文章详情页加载完成后,调用更新阅读次数接口。

export default {
  // ...
  async mounted() {
    await this.fetchArticle();
    await this.incrementArticleViews();
  },
  methods: {
    // ...
    async incrementArticleViews() {
      await this.$http.put(`/api/articles/${this.articleId}/increment-views`);
    },
  },
};

通过上述代码,我们优化了更新阅读次数的功能。在文章详情页加载完成后,调用更新阅读次数接口,使阅读次数增加。在后端,我们更改了接口名称以更好地反映其功能。

在这里,我们将补充文章详情页面中文章推荐的功能。为了给用户提供更好的阅读体验,我们可以在文章详情页面底部展示相关文章推荐。

  1. 前端相关文章推荐

在文章详情组件中添加相关文章推荐部分。

<template>
  <div class="article-detail">
    <!-- ... -->
    <div class="related-articles" v-if="relatedArticles.length">
      <h3>Related articles</h3>
      <ul>
        <li v-for="article in relatedArticles" :key="article.id">
          <a :href="'/article/' + article.id">{{ article.title }}</a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // ...
      relatedArticles: [],
    };
  },
  async mounted() {
    await this.fetchArticle();
    await this.incrementArticleViews();
    await this.fetchRelatedArticles();
  },
  methods: {
    // ...
    async fetchRelatedArticles() {
      const response = await this.$http.get(`/api/articles/${this.articleId}/related`);
      this.relatedArticles = response.data;
    },
  },
};
</script>

2.后端相关文章推荐

ArticleController中添加获取相关文章的接口。

@GetMapping("/{articleId}/related")
public ResponseEntity<?> getRelatedArticles(@PathVariable Long articleId) {
    Optional<Article> optionalArticle = articleRepository.findById(articleId);
    if (optionalArticle.isPresent()) {
        Article article = optionalArticle.get();
        List<Article> relatedArticles = articleRepository.findRelatedArticles(article.getCategoryId(), articleId);
        return ResponseEntity.ok(relatedArticles);
    } else {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Article not found.");
    }
}

ArticleRepository中添加根据分类获取相关文章的方法。

public interface ArticleRepository extends JpaRepository<Article, Long> {
    // ...
    @Query("SELECT a FROM Article a WHERE a.category.id = :categoryId AND a.id <> :articleId")
    List<Article> findRelatedArticles(@Param("categoryId") Long categoryId, @Param("articleId") Long articleId);
}

通过上述代码,我们实现了文章详情页面中相关文章推荐的功能。在前端,我们根据文章的分类展示相关文章。在后端,我们创建了一个根据分类获取相关文章的接口,忽略当前正在查看的文章。这样,用户在阅读文章时可以方便地找到其他相关文章。


http://www.kler.cn/news/10079.html

相关文章:

  • HTML - 实现IE浏览器访问网址自动跳转至谷歌浏览器打开
  • 电脑重装了系统开不了机怎么办?
  • 嵌入式入门基础知识有哪些?
  • pytorch张量及其使用——初学者入门必看
  • kruskal重构树
  • 商品列表API接入文档和参数说明
  • Python if 语句、Python3 os.rmdir() 方法
  • angular/react/vue/un-app
  • 全国青少年信息素养大赛2023年python·选做题模拟三卷
  • Android NDK 开发Demo
  • mysql数据库审计(1)
  • Cadence Allegro 导出Unassigned Functions Report报告详解
  • 有符号加法运算
  • 【C++】结构体嵌套结构体
  • 包装类,String,String的方法
  • ZooKeeper+Kafka+ELK+Filebeat集群搭建实现大批量日志收集和展示
  • 激活函数(Activation Function)
  • Android 新版 Logcat 操作小技巧
  • 【软件安装】图数据库_Neo4j下载与安装配置详解
  • 人人拥有ChatGPT的时代来临了,这次微软很大方!
  • java 面试消息题1-13
  • 手游盒子平台搭建流程
  • React styled-components(二)—— props、attrs属性
  • Swagger教程
  • x86中断基础
  • 【React全家桶】React-Redux
  • tpm2-tools源码分析之tpm2_createprimary.c(1)
  • 或许能用 ChatGPT 插件实现财富自由
  • 第31天-贪心-第八章 ● 122.买卖股票的最佳时机II ● 55. 跳跃游戏 ● 45.跳跃游戏II
  • 分享(五):免费可用的多种类 API 大全集合整理