-
Notifications
You must be signed in to change notification settings - Fork 16
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if result is not 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 | ||
} | ||
|
||
} |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,19 @@ package models | |
|
||
import ( | ||
"github.com/dgrijalva/jwt-go" | ||
"time" | ||
) | ||
|
||
type Users struct { | ||
ID int `json:"id"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Email string `json:"email"` | ||
MobilePhone string `json:"mobile_phone"` | ||
CreatedAt string `json:"created_at"` | ||
ModifiedAt string `json:"modified_at"` | ||
Roles []string `json:"roles"` | ||
Verified bool `json:"verified"` | ||
type User struct { | ||
ID int `json:"id" db:"id"` | ||
FirstName string `json:"first_name" db:"first_name"` | ||
LastName string `json:"last_name" db:"last_name"` | ||
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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make an issue to make this field pointer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue created: #110 |
||
Roles []string `json:"roles" db:"roles"` | ||
Verified bool `json:"verified" db:"verified"` | ||
} | ||
|
||
type Claims struct { | ||
|
@@ -24,19 +25,27 @@ type Claims struct { | |
} | ||
|
||
type Credentials struct { | ||
ID string `json:"id"` | ||
PasswordHash string `json:"password_hash"` | ||
Salt string `json:"salt"` | ||
UpdatedAt string `json:"updated_at"` | ||
ID int `json:"id" db:"id"` | ||
PasswordHash string `json:"password_hash" db:"password_hash"` | ||
Salt string `json:"salt" db:"salt"` | ||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"` | ||
} | ||
|
||
type EmailVerificationTokens struct { | ||
ID string `json:"id"` | ||
VerificationToken string `json:"verification_token"` | ||
GeneretedAt string `json:"generated_at"` | ||
ID int `json:"id" db:"id"` | ||
VerificationToken string `json:"verification_token" db:"verification_token"` | ||
GeneretedAt string `json:"generated_at" db:"generated_at"` | ||
} | ||
|
||
type Error struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type SignupRequest struct { | ||
FirstName string `json:"first_name" db:"first_name"` | ||
LastName string `json:"last_name" db:"last_name"` | ||
Email string `json:"email" db:"email"` | ||
Password string `json:"password" db:"password"` | ||
} | ||
|
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; |
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; |
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue created: #100