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 }