Created
August 5, 2020 01:14
-
-
Save justinpage/909facc5648d4f704222afcf47165314 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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