Skip to content

Commit

Permalink
Store user's last IP
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Jun 17, 2020
1 parent b7ce689 commit 14d9e7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
33 changes: 26 additions & 7 deletions internal/users/firestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"google.golang.org/grpc/status"
)

type UserModel struct {
Balance int
DisplayName string
Role string
LastIP string
}

type db struct {
firestoreClient *firestore.Client
}
Expand All @@ -21,37 +28,37 @@ func (d db) UserDocumentRef(userID string) *firestore.DocumentRef {
return d.UsersCollection().Doc(userID)
}

func (d db) GetUser(ctx context.Context, userID string) (User, error) {
func (d db) GetUser(ctx context.Context, userID string) (UserModel, error) {
doc, err := d.UserDocumentRef(userID).Get(ctx)

if err != nil && status.Code(err) != codes.NotFound {
return User{}, err
return UserModel{}, err
}
if err != nil && status.Code(err) == codes.NotFound {
return User{
return UserModel{
Balance: 0,
}, nil
}

var user User
var user UserModel
err = doc.DataTo(&user)
if err != nil {
return User{}, err
return UserModel{}, err
}

return user, nil
}

func (d db) UpdateBalance(ctx context.Context, userID string, amountChange int) error {
return d.firestoreClient.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
var user User
var user UserModel

userDoc, err := tx.Get(d.UserDocumentRef(userID))
if err != nil && status.Code(err) != codes.NotFound {
return err
}
if err != nil && status.Code(err) == codes.NotFound {
user = User{
user = UserModel{
Balance: 0,
}
} else {
Expand All @@ -68,3 +75,15 @@ func (d db) UpdateBalance(ctx context.Context, userID string, amountChange int)
return tx.Set(userDoc.Ref, user)
})
}

func (d db) UpdateLastIP(ctx context.Context, userID string, lastIP string) error {
updates := []firestore.Update{
{
Path: "LastIP",
Value: lastIP,
},
}

_, err := d.UserDocumentRef(userID).Update(ctx, updates)
return err
}
20 changes: 17 additions & 3 deletions internal/users/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"net"
"net/http"

"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/auth"
Expand All @@ -19,13 +20,26 @@ func (h HttpServer) GetCurrentUser(w http.ResponseWriter, r *http.Request) {
return
}

host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
err = h.db.UpdateLastIP(r.Context(), authUser.UUID, host)
if err != nil {
httperr.InternalError("internal-server-error", err, w, r)
return
}
}

user, err := h.db.GetUser(r.Context(), authUser.UUID)
if err != nil {
httperr.InternalError("cannot-get-user", err, w, r)
return
}
user.Role = authUser.Role
user.DisplayName = authUser.DisplayName

render.Respond(w, r, user)
userResponse := User{
DisplayName: authUser.DisplayName,
Balance: user.Balance,
Role: authUser.Role,
}

render.Respond(w, r, userResponse)
}
6 changes: 0 additions & 6 deletions internal/users/openapi_types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 14d9e7b

Please sign in to comment.