<commit_before>package main

import (
	"database/sql"
	"io"
	"math/rand"
	"net/http"
	"net/mail"
	"time"

	"errors"

	"github.com/NAExpire/API/src/util"
	"golang.org/x/crypto/bcrypt"
)

const senderEmail = "fill me in"
const senderPassword = "<PASSWORD>"
const smtpHost = "smtp.gmail.com"
const smtpPort = 587

type BusinessRegistrationHandler struct {
	DB *sql.DB
}

type ConsumerRegistrationHandler struct {
	DB *sql.DB
}

type businessRegistrationCredentials struct {
	FirstName           string `json:"firstName"`
	LastName            string `json:"lastName"`
	Email               string `json:"email"`
	Password            string `json:"password"`
	PersonalPhoneNumber string `json:"personalPhoneNumber"`
	RestaurantName      string `json:"restaurantName"`
	AddressLine1        string `json:"addressLine1"`
	AddressLine2        string `json:"addressLine2"`
	City                string `json:"city"`
	State               string `json:"state"`
	Zip                 string `json:"zip"`
	BusinessPhoneNumber string `json:"businessPhoneNumber"`
	Description         string `json:"description"`
}

type consumerRegistrationCredentials struct {
	FirstName           string `json:"firstName"`
	LastName            string `json:"lastName"`
	Email               string `json:"email"`
	Password            string `json:"password"`
	PersonalPhoneNumber string `json:"personalPhoneNumber"`
}

func (handler ConsumerRegistrationHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	x := &consumerRegistrationCredentials{}
	err := util.DecodeJSON(request.Body, x)
	if err != nil {
		writer.WriteHeader(http.StatusBadRequest)
		util.WriteErrorJSON(writer, "Malformed request syntax")
		return
	}

	responseCode, err := validateCredentials(handler.DB, x.Email, x.Password, x.PersonalPhoneNumber)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	confirmationCode := randomConfirmationCode()
	_, responseCode, err = registerUser(handler.DB, x.Email, x.Password, x.FirstName, x.LastName, "customer", confirmationCode)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	writer.WriteHeader(http.StatusCreated)
	io.WriteString(writer, "{\"ok\": true}")
}

func (handler BusinessRegistrationHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	x := &businessRegistrationCredentials{}
	err := util.DecodeJSON(request.Body, x)
	if err != nil {
		writer.WriteHeader(http.StatusBadRequest)
		util.WriteErrorJSON(writer, "Malformed request syntax")
		return
	}

	responseCode, err := validateCredentials(handler.DB, x.Email, x.Password, x.PersonalPhoneNumber)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	confirmationCode := randomConfirmationCode()
	insertedID, responseCode, err := registerUser(handler.DB, x.Email, x.Password, x.FirstName, x.LastName, "restaurant", confirmationCode)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	_, err = handler.DB.Exec("INSERT INTO `restaurants` (`ownerid`, `name`, `description`, `address`, `city`, `state`, `zip`, `registration-date`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", insertedID, x.RestaurantName, x.Description, x.AddressLine1+"\n"+x.AddressLine2, x.City, x.State, x.Zip, time.Now())
	if err != nil {
		writer.WriteHeader(http.StatusInternalServerError)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	writer.WriteHeader(http.StatusCreated)
	io.WriteString(writer, "{\"ok\": true}")
}

func validateCredentials(db *sql.DB, email string, password string, phoneNumber string) (int, error) {
	emailUsed, err := userWithEmailExists(db, email)
	if err != nil {
		return http.StatusInternalServerError, err
	} else if emailUsed {
		return http.StatusConflict, errors.New("Email is already used by another user")
	} else if !validatePassword(password) {
		return http.StatusUnprocessableEntity, errors.New("Password does not pass validation")
	} else if !validatePhoneNumber(phoneNumber) {
		return http.StatusUnprocessableEntity, errors.New("Phone number does not pass validation")
	} else if !validateEmail(email) {
		return http.StatusUnprocessableEntity, errors.New("Email does not pass valdiation")
	}<commit_msg>Add phone number to restaurants<commit_after>package main

import (
	"database/sql"
	"io"
	"math/rand"
	"net/http"
	"net/mail"
	"time"

	"errors"

	"github.com/NAExpire/API/src/util"
	"golang.org/x/crypto/bcrypt"
)

const senderEmail = "fill me in"
const senderPassword = "<PASSWORD>"
const smtpHost = "smtp.gmail.com"
const smtpPort = 587

type BusinessRegistrationHandler struct {
	DB *sql.DB
}

type ConsumerRegistrationHandler struct {
	DB *sql.DB
}

type businessRegistrationCredentials struct {
	FirstName           string `json:"firstName"`
	LastName            string `json:"lastName"`
	Email               string `json:"email"`
	Password            string `json:"password"`
	PersonalPhoneNumber string `json:"personalPhoneNumber"`
	RestaurantName      string `json:"restaurantName"`
	AddressLine1        string `json:"addressLine1"`
	AddressLine2        string `json:"addressLine2"`
	City                string `json:"city"`
	State               string `json:"state"`
	Zip                 string `json:"zip"`
	BusinessPhoneNumber string `json:"businessPhoneNumber"`
	Description         string `json:"description"`
}

type consumerRegistrationCredentials struct {
	FirstName           string `json:"firstName"`
	LastName            string `json:"lastName"`
	Email               string `json:"email"`
	Password            string `json:"password"`
	PersonalPhoneNumber string `json:"personalPhoneNumber"`
}

func (handler ConsumerRegistrationHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	x := &consumerRegistrationCredentials{}
	err := util.DecodeJSON(request.Body, x)
	if err != nil {
		writer.WriteHeader(http.StatusBadRequest)
		util.WriteErrorJSON(writer, "Malformed request syntax")
		return
	}

	responseCode, err := validateCredentials(handler.DB, x.Email, x.Password, x.PersonalPhoneNumber)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	confirmationCode := randomConfirmationCode()
	_, responseCode, err = registerUser(handler.DB, x.Email, x.Password, x.FirstName, x.LastName, "customer", confirmationCode)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	writer.WriteHeader(http.StatusCreated)
	io.WriteString(writer, "{\"ok\": true}")
}

func (handler BusinessRegistrationHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	x := &businessRegistrationCredentials{}
	err := util.DecodeJSON(request.Body, x)
	if err != nil {
		writer.WriteHeader(http.StatusBadRequest)
		util.WriteErrorJSON(writer, "Malformed request syntax")
		return
	}

	responseCode, err := validateCredentials(handler.DB, x.Email, x.Password, x.PersonalPhoneNumber)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	confirmationCode := randomConfirmationCode()
	insertedID, responseCode, err := registerUser(handler.DB, x.Email, x.Password, x.FirstName, x.LastName, "restaurant", confirmationCode)
	if err != nil {
		writer.WriteHeader(responseCode)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	_, err = handler.DB.Exec("INSERT INTO `restaurants` (`ownerid`, `name`, `description`, `address`, `city`, `state`, `zip`, `phone-number`, `registration-date`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", insertedID, x.RestaurantName, x.Description, x.AddressLine1+"\n"+x.AddressLine2, x.City, x.State, x.Zip, x.BusinessPhoneNumber, time.Now())
	if err != nil {
		writer.WriteHeader(http.StatusInternalServerError)
		util.WriteErrorJSON(writer, err.Error())
		return
	}

	writer.WriteHeader(http.StatusCreated)
	io.WriteString(writer, "{\"ok\": true}")
}

func validateCredentials(db *sql.DB, email string, password string, phoneNumber string) (int, error) {
	emailUsed, err := userWithEmailExists(db, email)
	if err != nil {
		return http.StatusInternalServerError, err
	} else if emailUsed {
		return http.StatusConflict, errors.New("Email is already used by another user")
	} else if !validatePassword(password) {
		return http.StatusUnprocessableEntity, errors.New("Password does not pass validation")
	} else if !validatePhoneNumber(phoneNumber) {
		return http.StatusUnprocessableEntity, errors.New("Phone number does not pass validation")
	} else if !validateEmail(email) {
		return http.StatusUnprocessableEntity, errors.New("Email does not pass valdiation")
	}
{'fraction_non_alphanumeric': 0.12543073742246727, 'fraction_numerical': 0.002067539627842867, 'mean_word_length': 4.624677002583979, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5783114', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2610, 'n_words': 608}