Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsongyan committed Nov 5, 2024
1 parent 29cf366 commit 7a980b5
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 32 deletions.
3 changes: 3 additions & 0 deletions controllers/archive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"github.com/cihub/seelog"
"math"
"net/http"
"strconv"
Expand Down Expand Up @@ -33,11 +34,13 @@ func ArchiveGet(c *gin.Context) {
}
posts, err = models.ListPostByArchive(year, month, pageIndex, pageSize)
if err != nil {
seelog.Errorf("models.ListPostByArchive err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByArchive(year, month)
if err != nil {
seelog.Errorf("models.CountPostByArchive err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func CommentPost(c *gin.Context) {
res["message"] = err.Error()
return
}
post, err = models.GetPostById(uint(pid))
post, err = models.GetPostById(pid)
if err != nil {
res["message"] = err.Error()
return
}
comment := &models.Comment{
PostID: uint(pid),
PostID: pid,
Content: content,
UserID: userId,
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"github.com/cihub/seelog"
"math"
"net/http"
"strconv"
Expand Down Expand Up @@ -29,11 +30,13 @@ func IndexGet(c *gin.Context) {
}
posts, err = models.ListPublishedPost("", pageIndex, pageSize)
if err != nil {
seelog.Errorf("models.ListPublishedPost err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByTag("")
if err != nil {
seelog.Errorf("models.CountPostByTag err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
Expand Down
2 changes: 2 additions & 0 deletions controllers/link.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"github.com/cihub/seelog"
"github.com/wangsongyan/wblog/system"
"net/http"
"strconv"
Expand Down Expand Up @@ -88,6 +89,7 @@ func LinkGet(c *gin.Context) {
}
link, err := models.GetLinkById(id)
if err != nil {
seelog.Errorf("models.GetLinkById err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
Expand Down
5 changes: 4 additions & 1 deletion controllers/page.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"github.com/cihub/seelog"
"github.com/gin-gonic/gin"
"github.com/wangsongyan/wblog/models"
"github.com/wangsongyan/wblog/system"
Expand Down Expand Up @@ -68,6 +69,7 @@ func PageEdit(c *gin.Context) {
page, err := models.GetPageById(id)
if err != nil {
Handle404(c)
return
}
c.HTML(http.StatusOK, "page/modify.html", gin.H{
"page": page,
Expand All @@ -83,13 +85,14 @@ func PageUpdate(c *gin.Context) {
published := "on" == isPublished
id, err := ParamUint(c, "id")
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
HandleMessage(c, err.Error())
return
}
page := &models.Page{Title: title, Body: body, IsPublished: published}
page.ID = id
err = page.Update()
if err != nil {
seelog.Errorf("page.Update err: %v", err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
Expand Down
7 changes: 5 additions & 2 deletions controllers/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"fmt"
"net/http"

"github.com/cihub/seelog"
"github.com/gin-gonic/gin"
Expand All @@ -26,7 +27,8 @@ func RssGet(c *gin.Context) {
feed.Items = make([]*feeds.Item, 0)
posts, err := models.ListPublishedPost("", 0, 0)
if err != nil {
seelog.Error(err)
seelog.Errorf("models.ListPublishedPost err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

Expand All @@ -42,7 +44,8 @@ func RssGet(c *gin.Context) {
}
rss, err := feed.ToRss()
if err != nil {
seelog.Error(err)
seelog.Errorf("feed.ToRss err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Writer.WriteString(rss)
Expand Down
4 changes: 2 additions & 2 deletions controllers/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ func UnSubscribe(c *gin.Context) {
HandleMessage(c, "Unscribe Succeessful!")
}

func GetUnSubcribeUrl(subscriber *models.Subscriber) (string, error) {
/*func GetUnSubcribeUrl(subscriber *models.Subscriber) (string, error) {
uuid := helpers.UUID()
signature := helpers.Md5(subscriber.Email + uuid)
subscriber.SecretKey = uuid
subscriber.Signature = signature
err := subscriber.Update()
return fmt.Sprintf("%s/unsubscribe?sid=%s", system.GetConfiguration().Domain, signature), err
}
}*/

func sendEmailToSubscribers(subject, body string) (err error) {
var (
Expand Down
3 changes: 3 additions & 0 deletions controllers/tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"github.com/cihub/seelog"
"math"
"net/http"
"strconv"
Expand Down Expand Up @@ -48,11 +49,13 @@ func TagGet(c *gin.Context) {
}
posts, err = models.ListPublishedPost(tagName, pageIndex, pageSize)
if err != nil {
seelog.Errorf("models.ListPublishedPost err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
total, err = models.CountPostByTag(tagName)
if err != nil {
seelog.Errorf("models.CountPostByTag err: %v", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
Expand Down
33 changes: 8 additions & 25 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,23 @@ func Oauth2Callback(c *gin.Context) {
// exchange accesstoken by code
token, err := exchangeTokenByCode(code)
if err != nil {
seelog.Error(err)
seelog.Errorf("exchangeTokenByCode err: %v", err)
c.Redirect(http.StatusMovedPermanently, "/signin")
return
}

//get github userinfo by accesstoken
userInfo, err = getGithubUserInfoByAccessToken(token)
if err != nil {
seelog.Error(err)
seelog.Errorf("getGithubUserInfoByAccessToken err: %v", err)
c.Redirect(http.StatusMovedPermanently, "/signin")
return
}

sessionUser, exists := c.Get(ContextUserKey)
if exists { // 已登录
user, _ = sessionUser.(*models.User)
_, err1 := models.IsGithubIdExists(userInfo.Login, user.ID)
if err1 != nil { // 未绑定
if _, e := models.IsGithubIdExists(userInfo.Login, user.ID); e != nil { // 未绑定
if user.IsAdmin {
user.GithubLoginId = userInfo.Login
}
Expand Down Expand Up @@ -231,7 +230,7 @@ func exchangeTokenByCode(code string) (accessToken string, err error) {
// cache token
tokenCache := oauth.CacheFile("./request.token")
if err := tokenCache.PutToken(token); err != nil {
seelog.Error(err)
seelog.Errorf("tokenCache.PutToken err: %v", err)
}
return
}
Expand Down Expand Up @@ -280,11 +279,7 @@ func ProfileUpdate(c *gin.Context) {
avatarUrl := c.PostForm("avatarUrl")
nickName := c.PostForm("nickName")
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
return
}
user, _ := sessionUser.(*models.User)
err = user.UpdateProfile(avatarUrl, nickName)
if err != nil {
res["message"] = err.Error()
Expand All @@ -302,11 +297,7 @@ func BindEmail(c *gin.Context) {
defer writeJSON(c, res)
email := c.PostForm("email")
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
return
}
user, _ := sessionUser.(*models.User)
if len(user.Email) > 0 {
res["message"] = "email have bound"
return
Expand All @@ -331,11 +322,7 @@ func UnbindEmail(c *gin.Context) {
)
defer writeJSON(c, res)
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
return
}
user, _ := sessionUser.(*models.User)
if user.Email == "" {
res["message"] = "email haven't bound"
return
Expand All @@ -355,11 +342,7 @@ func UnbindGithub(c *gin.Context) {
)
defer writeJSON(c, res)
sessionUser, _ := c.Get(ContextUserKey)
user, ok := sessionUser.(*models.User)
if !ok {
res["message"] = "server interval error"
return
}
user, _ := sessionUser.(*models.User)
if user.GithubLoginId == "" {
res["message"] = "github haven't bound"
return
Expand Down

0 comments on commit 7a980b5

Please sign in to comment.