From c64f52ed195b327f3fef0f3375f16faebb0d2839 Mon Sep 17 00:00:00 2001 From: sysnix <535420543@qq.com> Date: Thu, 23 Nov 2023 05:38:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E5=8F=91=E5=B8=83=E5=92=8C=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E6=96=87=E7=AB=A0=E8=B7=B3=E8=BD=AC?= =?UTF-8?q?=E8=87=B3404=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/controller/cli_controller/article_controller.go | 2 +- internal/repository/base_repository.go | 2 +- internal/service/article_service.go | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/controller/cli_controller/article_controller.go b/internal/controller/cli_controller/article_controller.go index 7c96c12..98611ff 100644 --- a/internal/controller/cli_controller/article_controller.go +++ b/internal/controller/cli_controller/article_controller.go @@ -66,7 +66,7 @@ func (ctrl *ArticleController) GetLatest() { func (ctrl *ArticleController) ViewArticle() { articleId := ctrl.Ctx.Params().Get("id") article := service.ArticleService.GetArticle(articleId) - if article.Id == "" { + if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH { ctrl.Ctx.View("404.html") return } diff --git a/internal/repository/base_repository.go b/internal/repository/base_repository.go index d758e84..d0efca6 100644 --- a/internal/repository/base_repository.go +++ b/internal/repository/base_repository.go @@ -28,7 +28,7 @@ func (repository *baseRep[T]) GetById(id string) (ret *T) { logrus.Debug("执行的SQL:", sql) err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error if err != nil { - logrus.Info(err) + logrus.Error(err) return nil } diff --git a/internal/service/article_service.go b/internal/service/article_service.go index d1cc219..92ddd28 100644 --- a/internal/service/article_service.go +++ b/internal/service/article_service.go @@ -49,7 +49,9 @@ func (*articleService) GetArticle(id string) *model.BlogArticle { if err != nil { logrus.Infoln(id, "文章的缓存不存在,读取数据库") ret = repository.ArticleRepository.GetById(id) - client.RedisClient.Set(context.Background(), key, ret, time.Duration(0)) + if ret != nil { + client.RedisClient.Set(context.Background(), key, ret, time.Duration(0)) + } } return ret }