Skip to content

Commit b53fbce

Browse files
authored
Merge pull request #130 from carryall/release/2.6.0
Release 2.6.0
2 parents 04f8b9e + 2fa5c10 commit b53fbce

File tree

25 files changed

+766
-432
lines changed

25 files changed

+766
-432
lines changed

constants/session.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package constants
22

33
const (
4-
SignInFail = "Incorrect email or password"
5-
SignInSuccess = "Successfully signed in"
6-
SignOutFail = "Failed to sign out"
7-
SignOutSuccess = "Successfully signed out"
4+
ContextCurrentUser = "CurrentUser"
5+
SignInFail = "Incorrect email or password"
6+
SignInSuccess = "Successfully signed in"
7+
SignOutFail = "Failed to sign out"
8+
SignOutSuccess = "Successfully signed out"
89
)

helpers/web/response.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/api/v1/controllers/base.go

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,19 @@
11
package controllers
22

33
import (
4-
"strconv"
5-
6-
"go-google-scraper-challenge/errors"
7-
. "go-google-scraper-challenge/helpers/api"
4+
"go-google-scraper-challenge/constants"
85
"go-google-scraper-challenge/lib/models"
9-
"go-google-scraper-challenge/lib/services/oauth"
106

117
"github.com/gin-gonic/gin"
128
)
139

14-
type BaseController struct {
15-
CurrentUser *models.User
16-
}
17-
18-
func (b *BaseController) EnsureAuthenticatedUser(ctx *gin.Context) (err error) {
19-
currentUser, err := b.GetCurrentUser(ctx)
20-
if err != nil {
21-
RenderJSONError(ctx, errors.ErrUnauthorizedUser, err.Error())
22-
23-
return err
24-
}
25-
26-
b.CurrentUser = currentUser
27-
28-
return nil
29-
}
30-
31-
func (b *BaseController) GetCurrentUser(ctx *gin.Context) (user *models.User, err error) {
32-
tokenInfo, err := oauth.ValidateToken(ctx.Request)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
userID, err := strconv.Atoi(tokenInfo.GetUserID())
38-
if err != nil {
39-
return nil, err
40-
}
10+
type BaseController struct{}
4111

42-
user, err = models.GetUserByID(int64(userID))
43-
if err != nil {
44-
return nil, err
12+
func (c *BaseController) GetCurrentUser(ctx *gin.Context) *models.User {
13+
currentUser := ctx.MustGet(constants.ContextCurrentUser)
14+
if currentUser == nil {
15+
return nil
4516
}
4617

47-
return user, nil
18+
return currentUser.(*models.User)
4819
}

lib/api/v1/controllers/results.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ type ResultsController struct {
1818
}
1919

2020
func (c *ResultsController) List(ctx *gin.Context) {
21-
if c.EnsureAuthenticatedUser(ctx) != nil {
22-
return
23-
}
24-
2521
keyword := ctx.Query("keyword")
22+
user := c.GetCurrentUser(ctx)
2623

27-
results, err := models.GetUserResults(c.CurrentUser.ID, []string{"User", "AdLinks", "Links"}, keyword)
24+
results, err := models.GetUserResults(user.ID, []string{"User", "AdLinks", "Links"}, keyword)
2825
if err != nil {
2926
RenderJSONError(ctx, errors.ErrServerError, err.Error())
3027

@@ -40,9 +37,7 @@ func (c *ResultsController) List(ctx *gin.Context) {
4037
}
4138

4239
func (c *ResultsController) Show(ctx *gin.Context) {
43-
if c.EnsureAuthenticatedUser(ctx) != nil {
44-
return
45-
}
40+
user := c.GetCurrentUser(ctx)
4641

4742
resultID, err := strconv.Atoi(ctx.Param("id"))
4843
if err != nil {
@@ -51,7 +46,7 @@ func (c *ResultsController) Show(ctx *gin.Context) {
5146
return
5247
}
5348

54-
result, err := models.GetResultByID(int64(resultID), c.CurrentUser, []string{"User", "AdLinks", "Links"})
49+
result, err := models.GetResultByID(int64(resultID), user, []string{"User", "AdLinks", "Links"})
5550
if err != nil {
5651
RenderJSONError(ctx, errors.ErrNotFound, err.Error())
5752

@@ -63,9 +58,7 @@ func (c *ResultsController) Show(ctx *gin.Context) {
6358
}
6459

6560
func (c *ResultsController) Create(ctx *gin.Context) {
66-
if c.EnsureAuthenticatedUser(ctx) != nil {
67-
return
68-
}
61+
user := c.GetCurrentUser(ctx)
6962

7063
file, fileHeader, err := ctx.Request.FormFile("file")
7164
if err != nil {
@@ -77,7 +70,7 @@ func (c *ResultsController) Create(ctx *gin.Context) {
7770
uploadForm := &forms.UploadForm{
7871
File: file,
7972
FileHeader: fileHeader,
80-
User: c.CurrentUser,
73+
User: user,
8174
}
8275

8376
resultIDs, err := uploadForm.Save()

0 commit comments

Comments
 (0)