github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section5/gopherfacedb/handlers/signup.go (about)

     1  package handlers
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/EngineerKamesh/gofullstack/volume2/section5/gopherfacedb/common"
     9  	"github.com/EngineerKamesh/gofullstack/volume2/section5/gopherfacedb/models"
    10  
    11  	"github.com/EngineerKamesh/gofullstack/volume2/section5/gopherfacedb/validationkit"
    12  )
    13  
    14  type SignUpForm struct {
    15  	FieldNames []string
    16  	Fields     map[string]string
    17  	Errors     map[string]string
    18  }
    19  
    20  // DisplaySignUpForm displays the Sign Up form
    21  func DisplaySignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm) {
    22  	RenderTemplate(w, "./templates/signupform.html", s)
    23  }
    24  
    25  func DisplayConfirmation(w http.ResponseWriter, r *http.Request, s *SignUpForm) {
    26  	RenderUnsafeTemplate(w, "./templates/signupconfirmation.html", s)
    27  }
    28  
    29  func PopulateFormFields(r *http.Request, s *SignUpForm) {
    30  
    31  	for _, fieldName := range s.FieldNames {
    32  		s.Fields[fieldName] = r.FormValue(fieldName)
    33  	}
    34  
    35  }
    36  
    37  // ValidateSignUpForm validates the Sign Up form's fields
    38  func ValidateSignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm, e *common.Env) {
    39  
    40  	PopulateFormFields(r, s)
    41  	// Check if username was filled out
    42  	if r.FormValue("username") == "" {
    43  		s.Errors["usernameError"] = "The username field is required."
    44  	}
    45  
    46  	// Check if first name was filled out
    47  	if r.FormValue("firstName") == "" {
    48  		s.Errors["firstNameError"] = "The first name field is required."
    49  	}
    50  
    51  	// Check if last name was filled out
    52  	if r.FormValue("lastName") == "" {
    53  		s.Errors["lastNameError"] = "The last name field is required."
    54  	}
    55  
    56  	// Check if e-mail address was filled out
    57  	if r.FormValue("email") == "" {
    58  		s.Errors["emailError"] = "The e-mail address field is required."
    59  	}
    60  
    61  	// Check if e-mail address was filled out
    62  	if r.FormValue("password") == "" {
    63  		s.Errors["passwordError"] = "The password field is required."
    64  	}
    65  
    66  	// Check if e-mail address was filled out
    67  	if r.FormValue("confirmPassword") == "" {
    68  		s.Errors["confirmPasswordError"] = "The confirm password field is required."
    69  	}
    70  
    71  	// Check username syntax
    72  	if validationkit.CheckUsernameSyntax(r.FormValue("username")) == false {
    73  
    74  		usernameErrorMessage := "The username entered has an improper syntax."
    75  		if _, ok := s.Errors["usernameError"]; ok {
    76  			s.Errors["usernameError"] += " " + usernameErrorMessage
    77  		} else {
    78  			s.Errors["usernameError"] = usernameErrorMessage
    79  		}
    80  	}
    81  
    82  	// Check e-mail address syntax
    83  	if validationkit.CheckEmailSyntax(r.FormValue("email")) == false {
    84  		emailErrorMessage := "The e-mail address entered has an improper syntax."
    85  		if _, ok := s.Errors["usernameError"]; ok {
    86  			s.Errors["emailError"] += " " + emailErrorMessage
    87  		} else {
    88  			s.Errors["emailError"] = emailErrorMessage
    89  		}
    90  	}
    91  
    92  	// Check if password and confirm password field values match
    93  	if r.FormValue("password") != r.FormValue("confirmPassword") {
    94  		s.Errors["confirmPasswordError"] = "The password and confirm pasword fields do not match."
    95  	}
    96  
    97  	if len(s.Errors) > 0 {
    98  		DisplaySignUpForm(w, r, s)
    99  	} else {
   100  		ProcessSignUpForm(w, r, s, e)
   101  	}
   102  
   103  }
   104  
   105  // ProcessSignUpForm
   106  func ProcessSignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm, e *common.Env) {
   107  
   108  	u := models.NewUser(r.FormValue("username"), r.FormValue("firstName"), r.FormValue("lastName"), r.FormValue("email"), r.FormValue("password"))
   109  	//fmt.Println("user: ", u)
   110  	err := e.DB.CreateUser(u)
   111  
   112  	if err != nil {
   113  		log.Print(err)
   114  	}
   115  
   116  	user, err := e.DB.GetUser("EngineerKamesh")
   117  	if err != nil {
   118  		log.Print(err)
   119  	} else {
   120  		fmt.Printf("Fetch User Result: %+v\n", user)
   121  	}
   122  
   123  	// Display form confirmation message
   124  	DisplayConfirmation(w, r, s)
   125  
   126  }
   127  
   128  func SignUpHandler(e *common.Env) http.Handler {
   129  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   130  
   131  		s := SignUpForm{}
   132  		s.FieldNames = []string{"username", "firstName", "lastName", "email"}
   133  		s.Fields = make(map[string]string)
   134  		s.Errors = make(map[string]string)
   135  
   136  		switch r.Method {
   137  
   138  		case "GET":
   139  			DisplaySignUpForm(w, r, &s)
   140  		case "POST":
   141  			ValidateSignUpForm(w, r, &s, e)
   142  		default:
   143  			DisplaySignUpForm(w, r, &s)
   144  		}
   145  
   146  	})
   147  }