// createUser handles http request to create a user
createUser handles http request to create a user
// createUser handles http request to create a user
func (s *Srv) createUser(w http.ResponseWriter, r *http.Request) {
	var u user.User
	err := u.ParseUser(r)
	if err != nil {
		log.Fatal(err)
	}
	u.Password = utils.EncryptPassword([]byte(u.Password))
	var result user.User
	err = s.db.Database("podoloff").Collection("users").FindOne(context.TODO(), bson.D{primitive.E{Key: "email", Value: u.Email}}).Decode(&result)
	if err != nil {
		if err.Error() == "mongo: no documents in result" {
			_, err = s.db.Database("podoloff").Collection("users").InsertOne(context.TODO(), u)
			if err != nil {
				w.Header().Set("Content-Type", "application/json")
				json.NewEncoder(w).Encode("Unable to add user. Please try again.")
				return
			}
			w.Header().Set("Content-Type", "application/json")
			json.NewEncoder(w).Encode("User successfully added.")
			return
		}
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode("User email already used. Please try again")
	return
}
func ( s * Srv ) createUser ( w http . ResponseWriter , r * http . Request ) { var u user . User err := u . ParseUser ( r ) if err != nil { log . Fatal ( err ) } u . Password = utils . EncryptPassword ( [ ] byte ( u . Password ) ) var result user . User err = s . db . Database ( " " ) . Collection ( " " ) . FindOne ( context . TODO ( ) , bson . D { primitive . E { Key : " " , Value : u . Email } } ) . Decode ( & result ) if err != nil { if err . Error ( ) == " " { _ , err = s . db . Database ( " " ) . Collection ( " " ) . InsertOne ( context . TODO ( ) , u ) if err != nil { w . Header ( ) . Set ( " " , " " ) json . NewEncoder ( w ) . Encode ( " " ) return } w . Header ( ) . Set ( " " , " " ) json . NewEncoder ( w ) . Encode ( " " ) return } } w . Header ( ) . Set ( " " , " " ) json . NewEncoder ( w ) . Encode ( " " ) return }
createUser handles http request to create a user
createUser handles http request to create a user