Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Ferraguti committed Aug 23, 2023
1 parent 253b02f commit 62e62f9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Codec struct{}

type CodecProvider interface {

// From Requests to Entities or Models
// From Requests to Models
FromSignupRequestToUserModel(request entities.SignupRequest, hashedPassword string) models.User
FromLoginRequestToUserModel(request entities.LoginRequest) models.User
FromGetUserRequestToUserModel(request entities.GetUserRequest) models.User
Expand Down
12 changes: 10 additions & 2 deletions pkg/models/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package models

import "time"
import (
"time"

"github.com/gilperopiola/go-rest-example/pkg/utils"
)

type User struct {
ID int
Expand All @@ -13,11 +17,15 @@ type User struct {
UpdatedAt time.Time
}

func (user *User) Fill(username, email string) {
func (user *User) OverwriteFields(username, email string) {
if username != "" {
user.Username = username
}
if email != "" {
user.Email = email
}
}

func (user *User) PasswordMatches(password string) bool {
return user.Password == utils.Hash(user.Email, password)
}
6 changes: 1 addition & 5 deletions pkg/service/service_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Service) Login(loginRequest entities.LoginRequest) (entities.LoginRespo
}

// Check if passwords match, if not return error
if !passwordsMatch(userToLogin.Password, loginRequest.Password, userToLogin.Email) {
if !userToLogin.PasswordMatches(loginRequest.Password) {
return entities.LoginResponse{}, s.ErrorsMapper.Map(entities.ErrWrongPassword)
}

Expand All @@ -52,7 +52,3 @@ func (s *Service) Login(loginRequest entities.LoginRequest) (entities.LoginRespo
// Return generated token on the response
return entities.LoginResponse{Token: s.Auth.GenerateToken(userEntity)}, nil
}

func passwordsMatch(password, otherPassword, email string) bool {
return password == utils.Hash(email, otherPassword)
}
2 changes: 1 addition & 1 deletion pkg/service/service_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *Service) UpdateUser(updateUserRequest entities.UpdateUserRequest) (enti
}

// Replace fields
userToUpdate.Fill(updateUserRequest.Username, updateUserRequest.Email)
userToUpdate.OverwriteFields(updateUserRequest.Username, updateUserRequest.Email)

// Update user on the DB
if userToUpdate, err = s.Repository.UpdateUser(userToUpdate); err != nil {
Expand Down
28 changes: 22 additions & 6 deletions pkg/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transport

import (
"github.com/gilperopiola/go-rest-example/pkg/codec"
"github.com/gilperopiola/go-rest-example/pkg/entities"
"github.com/gilperopiola/go-rest-example/pkg/service"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -37,14 +38,29 @@ func NewTransport(service service.ServiceProvider, codec codec.CodecProvider, er

/* ----------------- */

type RequestProvider interface {
Validate() error
type Request interface {
entities.SignupRequest |
entities.LoginRequest |
entities.GetUserRequest |
entities.UpdateUserRequest |
entities.DeleteUserRequest
}
type Response interface {
entities.SignupResponse |
entities.LoginResponse |
entities.GetUserResponse |
entities.UpdateUserResponse |
entities.DeleteUserResponse
}

type Request RequestProvider
type Response interface{}

func HandleRequest[T Request, R Response](t Transport, c *gin.Context, makeRequest func(*gin.Context) (T, error), serviceCall func(T) (R, error)) {
// HandleRequest takes:
//
// - a transport and a gin context
// - a function that makes a request from the gin context
// - a function that calls the service with that request
//
// It returns a response with the result of the service call.
func HandleRequest[req Request, resp Response](t Transport, c *gin.Context, makeRequest func(*gin.Context) (req, error), serviceCall func(req) (resp, error)) {

// Make, validate and get request
request, err := makeRequest(c)
Expand Down
8 changes: 4 additions & 4 deletions pkg/transport/transport_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func makeLoginRequest(c *gin.Context) (entities.LoginRequest, error) {
func makeGetUserRequest(c *gin.Context) (entities.GetUserRequest, error) {

// Get info from context and URL, check if user IDs match
userToGetID, err := getAndValidateRequestUserIDs(c)
userToGetID, err := getUserIDFromContext(c)
if err != nil {
return entities.GetUserRequest{}, err
}
Expand All @@ -62,7 +62,7 @@ func makeGetUserRequest(c *gin.Context) (entities.GetUserRequest, error) {
func makeUpdateUserRequest(c *gin.Context) (entities.UpdateUserRequest, error) {

// Validate and get User ID
userToUpdateID, err := getAndValidateRequestUserIDs(c)
userToUpdateID, err := getUserIDFromContext(c)
if err != nil {
return entities.UpdateUserRequest{}, err
}
Expand All @@ -88,7 +88,7 @@ func makeUpdateUserRequest(c *gin.Context) (entities.UpdateUserRequest, error) {
func makeDeleteUserRequest(c *gin.Context) (entities.DeleteUserRequest, error) {

// Get info from context and URL, check if user IDs match
userToDeleteID, err := getAndValidateRequestUserIDs(c)
userToDeleteID, err := getUserIDFromContext(c)
if err != nil {
return entities.DeleteUserRequest{}, err
}
Expand All @@ -106,7 +106,7 @@ func makeDeleteUserRequest(c *gin.Context) (entities.DeleteUserRequest, error) {

/* ----------------- */

func getAndValidateRequestUserIDs(c *gin.Context) (int, error) {
func getUserIDFromContext(c *gin.Context) (int, error) {

// Get logged user ID
loggedUserID, err := utils.GetIntFromContext(c, "ID")
Expand Down

0 comments on commit 62e62f9

Please sign in to comment.