Skip to content

Instantly share code, notes, and snippets.

@justinpage
Created August 5, 2020 01:14
Show Gist options
  • Select an option

  • Save justinpage/909facc5648d4f704222afcf47165314 to your computer and use it in GitHub Desktop.

Select an option

Save justinpage/909facc5648d4f704222afcf47165314 to your computer and use it in GitHub Desktop.
package service
import (
"fmt"
"log"
"mocking/database"
)
type registerationPreChecker interface {
UserExists(string) bool
}
type regPreCheck struct {}
func (regPreCheck) UserExists(email string) bool {
return database.UserExists(email)
}
type User struct {
Name string `json:"name"`
Email string `json:"email"`
UserName string `json:"user_name"`
}
func NewRegistrationPreChecker() registerationPreChecker {
return regPreCheck{}
}
// Register a user if they don't exist
func RegisterUser(user User, regPreCond registerationPreChecker) error {
// check if user is already registered
found := regPreCond.UserExists(user.Email)
if found {
return fmt.Errorf("email '%s' already registered", user.Email)
}
// carry business logic and register the user in the system
log.Println(user)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment