-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-75, handlers and service layers created, bugs fixed, new SQL sc…
…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
- Loading branch information
Showing
18 changed files
with
583 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("I'm identity management service") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
|
||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/omc-college/management-system/pkg/ims/validation" | ||
"github.com/omc-college/management-system/pkg/ims/models" | ||
"github.com/omc-college/management-system/pkg/ims/repository/postgresql" | ||
"github.com/omc-college/management-system/pkg/ims/service" | ||
) | ||
|
||
type SignUpHandler struct { | ||
SignUpService *service.SignUpService | ||
} | ||
|
||
func NewSignUpHandler(service *service.SignUpService) *SignUpHandler { | ||
return &SignUpHandler{ | ||
SignUpService: service, | ||
} | ||
} | ||
|
||
// handleError handles existing error in handlers | ||
func handleError(err error, w http.ResponseWriter) { | ||
var error models.Error | ||
var queryErr *postgresql.QueryError | ||
var scanErr *postgresql.ScanError | ||
|
||
if errors.As(err, &queryErr) { | ||
error = models.Error{http.StatusInternalServerError, queryErr.Message} | ||
} else if errors.As(err, &scanErr) { | ||
error = models.Error{http.StatusInternalServerError, scanErr.Message} | ||
} else if errors.Is(err, validation.ErrNoSymbols) || errors.Is(err, validation.ErrToMuchSymbols) { | ||
error = models.Error{http.StatusBadRequest, err.Error()} | ||
} else if errors.Is(err, validation.ErrEmailExists) || errors.Is(err, validation.ErrInvalidEmail){ | ||
error = models.Error{http.StatusBadRequest, err.Error()} | ||
} else { | ||
error = models.Error{http.StatusInternalServerError, err.Error()} | ||
} | ||
|
||
logrus.Errorf(error.Message) | ||
w.WriteHeader(error.Code) | ||
json.NewEncoder(w).Encode(error) | ||
} | ||
|
||
func (h *SignUpHandler)SignUp(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
var request models.SignupRequest | ||
var err error | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
err = json.Unmarshal(body, &request) | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
err = r.Body.Close() | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
err = validation.Data(&request) | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
err = h.SignUpService.SignUp(&request) | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
} | ||
|
||
func (h *SignUpHandler)EmailAvailable(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
var err error | ||
|
||
params := mux.Vars(r) | ||
|
||
if params["email"] == "" { | ||
err = validation.ErrNoSymbols | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
result, err := h.SignUpService.EmailAvailable(params["email"]) | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} else if result == true { | ||
err = validation.ErrEmailExists | ||
handleError(err, w) | ||
return | ||
} else { | ||
fmt.Fprintf(w, "email is not occupied") | ||
} | ||
} | ||
|
||
func (h *SignUpHandler) CheckEmailVerificationToken(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
var tok models.EmailVerificationTokens | ||
var err error | ||
|
||
params := mux.Vars(r) | ||
|
||
if params["verification_token"] == "" { | ||
err = validation.ErrNoSymbols | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
tok.VerificationToken = params["verification_token"] | ||
|
||
err = h.SignUpService.EmailVerificationToken(&tok) | ||
if err != nil { | ||
handleError(err, w) | ||
return | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package routers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/omc-college/management-system/pkg/ims/service" | ||
"github.com/omc-college/management-system/pkg/ims/api/handlers" | ||
) | ||
|
||
//NewSignUpRouter inits Sign Up router | ||
func NewSignUpRouter (service *service.SignUpService) *mux.Router { | ||
|
||
signUpHandler := handlers.NewSignUpHandler(service) | ||
|
||
router := mux.NewRouter() | ||
|
||
router.HandleFunc("/signup", signUpHandler.SignUp).Methods(http.MethodPost) | ||
router.HandleFunc("/email/available/{email}", signUpHandler.EmailAvailable).Methods(http.MethodGet) | ||
router.HandleFunc("/users/emailVerificationToken/verify/{verification_token}", signUpHandler.CheckEmailVerificationToken).Methods(http.MethodGet) | ||
|
||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
pkg/ims/repository/postgresql/migrations/000005_re-creation_role.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE users | ||
DROP COLUMN IF EXISTS roles, | ||
DROP COLUMN IF EXISTS roles; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ ALTER TABLE users | |
DROP COLUMN role; | ||
|
||
ALTER TABLE users | ||
ADD COLUMN roles []text; | ||
|
||
ADD COLUMN roles text[]; | ||
|
||
COMMIT; |
16 changes: 16 additions & 0 deletions
16
pkg/ims/repository/postgresql/migrations/000006_change_id.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
BEGIN; | ||
|
||
SELECT MAX(id)+1 FROM credentials; | ||
CREATE SEQUENCE cred_id_seq MINVALUE 1; | ||
ALTER TABLE credentials ALTER id SET DEFAULT nextval('cred_id_seq'); | ||
ALTER SEQUENCE cred_id_seq OWNED BY credentials.id; | ||
|
||
SELECT MAX(id)+1 FROM email_verification_tokens; | ||
CREATE SEQUENCE token_id_seq MINVALUE 1; | ||
ALTER TABLE email_verification_tokens ALTER id SET DEFAULT nextval('token_id_seq'); | ||
ALTER SEQUENCE token_id_seq OWNED BY email_verification_tokens.id; | ||
|
||
UPDATE users SET mobile_phone = NULL WHERE mobile_phone = ''; | ||
CREATE UNIQUE INDEX mobile_phone ON users (mobile_phone); | ||
|
||
COMMIT; |
Oops, something went wrong.