Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-75, handlers and service layers created, bugs fixed, new SQL sсripts created #97

Merged
merged 1 commit into from
Jun 4, 2020

Conversation

DubBro
Copy link
Contributor

@DubBro DubBro commented May 28, 2020

signup_handlers.go: handlers layer, handle errors function declared
models.go: added struct SignupRepository, corrected mistakes (ID type string -> int)
router.go: inits routers
credentials_repository.go: updated
signup_repository.go: updated, modified with TextArray
users_repository.go: updated
000005_re-creation_role.down.sql: fixed bugs
000005_re-creation_role.up.sql: fixed bugs
000006_change_id.up.sql: drops unique mobile_phone constraint, id
type in tables credentials and email_verification_tokens changed into integer
000006_change_id.down.sql
service.go: service layer
data.go: validates user's entered data
email.go: validates user's entered email
errors.go: contains validation errors
ims-api.yaml: updated

Added function that handles errors.
Added error handlers to signup_handlers.go, signup_repository.go, users_repository.go, credentials_repository.go, service.go.

Closes #75

request.Email = keyVal["email"]
request.Password = keyVal["password"]

err = r.Body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it is better to close the body right after ReadAll call?

}

keyVal := make(map[string]string)
err = json.Unmarshal(body, &keyVal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmarshal it directly in SignupRequest

return
}

keyVal := make(map[string]string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this map please it is not needed

err = postgresql.ErrNoSymbols
handleError(err, w)
return
} else if len(keyVal["password"]) > 256 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a separate package for all your validations functions.

}

if keyVal["password"] == "" {
err = postgresql.ErrNoSymbols
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is PostgreSQL error?

var str string

for _, val := range u.Roles {
str = str + val + ","
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use TextArray instead of this

@DubBro DubBro requested a review from taaraora May 29, 2020 02:47
@DubBro DubBro changed the title Issue-75, handlers and service layers created, bugs fixed, new SQL sсript created Issue-75, handlers and service layers created, bugs fixed, new SQL sсripts created May 29, 2020
return
}

err = validation.Data(&request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make an issue to refactor validate function to make it reusable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #100

if err != nil {
handleError(err, w)
return
} else if result == true {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if result is not true?

if err != nil {
return nil, err
}

return ToUser(u)
}

func ToUser(privatUser user) (*models.Users, error) {
// CheckEmail returns email if it exists in DB or empty string
func (ur *UsersRepository) CheckEmail (email string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not needed, just use GetUserByEmail

func (ur *UsersRepository) GetEmailByToken (token *models.EmailVerificationTokens) (string, error) {
var email string

err := ur.DB.Get(&email, "SELECT (SELECT email FROM users WHERE id = email_verification_tokens.id) FROM email_verification_tokens WHERE verification_token = $1", token.VerificationToken)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make an issue to simplify this query

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #101

return err
}

err = service.SignUpRepo.InsertUser(&user)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to start a transaction before making your first request to DB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand enough what do you mean

Copy link
Contributor Author

@DubBro DubBro May 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was solved at the zoom conference

@DubBro DubBro requested a review from taaraora June 3, 2020 00:45
return
}

// Send Email Verification Token function must be here!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function should be added in service layer

}
}

func (h *SignUpHandler)EmailVerificationToken(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe EmailVerificationToken should be named CheckEmailVerificationToken

Email string `json:"email" db:"email"`
MobilePhone string `json:"mobile_phone" db:"mobile_phone"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
ModifiedAt time.Time `json:"modified_at" db:"modified_at"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make an issue to make this field pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #110

@@ -7,26 +7,18 @@ import (
)

type CredRepository struct {
db *sqlx.DB
DB sqlx.Ext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can make it unexportable?

var exist bool
var err error

tx, err := service.db.Beginx()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explicit transaction is not needed here.

…ripts created

`signup_handlers.go`: handlers layer, handle errors function declared
`models.go`: added struct `SignupRepository`, corrected mistakes (`ID` type string -> int),
db tags added, columns `UpdatedAt`, `CreatedAt`, `GeneratedAt` were changed from type string to type time.Time
`type.go`: db tags added, columns `UpdatedAt`, `CreatedAt` were changed from type string to type time.Time
`router.go`: inits routers
`credentials_repository.go`: updated
`signup_repository.go`: updated, modified with TextArray, `InsertUser` was moved from `signup_repository.go` into `users_repository.go`
`users_repository.go`: updated, `InsertUser` was moved from `signup_repository.go` into `users_repository.go`
`000005_re-creation_role.down.sql`: fixed bugs
`000005_re-creation_role.up.sql`: fixed bugs
`000006_change_id.up.sql`: drops unique `mobile_phone` constraint, `id` type in tables `credentials` and `email_verification_tokens` changed into integer
`000006_change_id.down.sql`: rolls back
`service.go`: service layer, added transactions
`data.go`: validates user's entered data
`email.go`: validates user's entered email
`errors.go`: contains validation errors
`ims-api.yaml`: updated
@DubBro DubBro requested a review from taaraora June 4, 2020 21:02
@taaraora taaraora merged commit dd58889 into omc-college:master Jun 4, 2020
@DubBro DubBro deleted the issue-75 branch June 4, 2020 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handlers and service layers created, bugs fixed, new SQL sсripts created
3 participants