This file contains 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
type ServiceContainer struct { | |
FactoryMap map[string]interface{} | |
AppConfig *config.AppConfig | |
} | |
func (sc *ServiceContainer) InitApp(filename string) error { | |
var err error | |
config, err := loadConfig(filename) | |
if err != nil { | |
return errors.Wrap(err, "loadConfig") |
This file contains 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
// Build creates concrete type for RegistrationUseCaseInterface | |
func (rf *RegistrationFactory) Build(c container.Container, appConfig *config.AppConfig, key string) (UseCaseInterface, error) { | |
uc := appConfig.UseCase.Registration | |
udi, err := buildUserData(c, &uc.UserDataConfig) | |
if err != nil { | |
return nil, errors.Wrap(err, "") | |
} | |
tdi, err := buildTxData(c, &uc.TxDataConfig) | |
if err != nil { | |
return nil, errors.Wrap(err, "") |
This file contains 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
// logger mapp to map logger code to logger builder | |
var logfactoryBuilderMap = map[string]logFbInterface{ | |
config.ZAP: &ZapFactory{}, | |
config.LOGRUS: &LogrusFactory{}, | |
} | |
// interface for logger factory | |
type logFbInterface interface { | |
Build(*config.LogConfig) error | |
} |
This file contains 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
// receiver for zap factory | |
type ZapFactory struct{} | |
// build zap logger | |
func (mf *ZapFactory) Build(lc *config.LogConfig) error { | |
err := zap.RegisterLog(*lc) | |
if err != nil { | |
return errors.Wrap(err, "") | |
} | |
return nil |
This file contains 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
import ( | |
"github.com/go-ozzo/ozzo-validation" | |
"time" | |
) | |
// User has a name, department and created date. Name and created are required, department is optional. | |
// Id is auto-generated by database after the user is persisted. | |
// json is for couchdb | |
type User struct { | |
Id int `json:"uid"` |
This file contains 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
// GrpcToUser converts from grpc User type to domain Model user type | |
func GrpcToUser(user *uspb.User) (*model.User, error) { | |
if user == nil { | |
return nil, nil | |
} | |
resultUser := model.User{} | |
resultUser.Id = int(user.Id) | |
resultUser.Name = user.Name | |
resultUser.Department = user.Department |
This file contains 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
// userDataServiceFactory is a empty receiver for Build method | |
type userDataServiceFactory struct {} | |
func (udsf *userDataServiceFactory) Build(c container.Container, dataConfig *configs.DataConfig) (DataServiceInterface, error) { | |
logger.Log.Debug("userDataServiceFactory") | |
key := dataConfig.Code | |
if USER_DATA != key { | |
errMsg := USER_DATA + " in userDataServiceFactory doesn't match key = " + key | |
return nil, errors.New(errMsg) | |
} |
This file contains 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
// RegistrationUseCaseInterface is for users to register themselves to an application. It has registration related functions. | |
// ModifyAndUnregisterWithTx() is the one supporting transaction, the other are not. | |
type RegistrationUseCaseInterface interface { | |
// RegisterUser register a user to an application, basically save it to a database. The returned resultUser that has | |
// a Id ( auto generated by database) after persisted | |
RegisterUser(user *model.User) (resultUser *model.User, err error) | |
// UnregisterUser unregister a user from an application by user name, basically removing it from a database. | |
UnregisterUser(username string) error | |
// ModifyUser change user information based on the User.Id passed in. | |
ModifyUser(user *model.User) error |
This file contains 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
// RegistrationUseCase implements RegistrationUseCaseInterface. | |
// It has UserDataInterface, which can be used to access persistence layer | |
// TxDataInterface is needed to support transaction | |
type RegistrationUseCase struct { | |
UserDataInterface dataservice.UserDataInterface | |
TxDataInterface dataservice.TxDataInterface | |
} | |
func (ruc *RegistrationUseCase) RegisterUser(user *model.User) (*model.User, error) { | |
err := user.Validate() |
This file contains 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
// UserDataInterface represents interface for user data access through database | |
type UserDataInterface interface { | |
// Remove deletes a user by user name from database. | |
Remove(username string) (rowsAffected int64, err error) | |
// Find retrieves a user from database based on a user's id | |
Find(id int) (*model.User, error) | |
// FindByName retrieves a user from database by User.Name | |
FindByName(name string) (user *model.User, err error) | |
// FindAll retrieves all users from database as an array of user | |
FindAll() ([]model.User, error) |