未发布和不存在的文章跳转至404页面
This commit is contained in:
parent
4aabb57ff9
commit
c64f52ed19
|
@ -66,7 +66,7 @@ func (ctrl *ArticleController) GetLatest() {
|
||||||
func (ctrl *ArticleController) ViewArticle() {
|
func (ctrl *ArticleController) ViewArticle() {
|
||||||
articleId := ctrl.Ctx.Params().Get("id")
|
articleId := ctrl.Ctx.Params().Get("id")
|
||||||
article := service.ArticleService.GetArticle(articleId)
|
article := service.ArticleService.GetArticle(articleId)
|
||||||
if article.Id == "" {
|
if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH {
|
||||||
ctrl.Ctx.View("404.html")
|
ctrl.Ctx.View("404.html")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ func (repository *baseRep[T]) GetById(id string) (ret *T) {
|
||||||
logrus.Debug("执行的SQL:", sql)
|
logrus.Debug("执行的SQL:", sql)
|
||||||
err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error
|
err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Info(err)
|
logrus.Error(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,9 @@ func (*articleService) GetArticle(id string) *model.BlogArticle {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Infoln(id, "文章的缓存不存在,读取数据库")
|
logrus.Infoln(id, "文章的缓存不存在,读取数据库")
|
||||||
ret = repository.ArticleRepository.GetById(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
|
return ret
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue