// 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.
typeRegistrationUseCaseInterfaceinterface {
// 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, errerror)
// UnregisterUser unregister a user from an application by user name, basically removing it from a database.
UnregisterUser(usernamestring) error// ModifyUser change user information based on the User.Id passed in.
ModifyUser(user*model.User) error// ModifyAndUnregister change user information and then unregister the user based on the User.Id passed in.
// It is created to illustrate transaction, no real use.
ModifyAndUnregister(user*model.User) error// ModifyAndUnregisterWithTx change user information and then unregister the user based on the User.Id passed in.
// It supports transaction
// It is created to illustrate transaction, no real use.
ModifyAndUnregisterWithTx(user*model.User) error// EnableTx enable transaction support on use case. Need to be included for each use case needs transaction
// It replaces the underline database handler to sql.Tx for each data service that used by this use case
EnableTxer
}
// UserDataInterface represents interface for user data access through database
typeUserDataInterfaceinterface {
// Remove deletes a user by user name from database.
Remove(usernamestring) (rowsAffectedint64, errerror)
// Find retrieves a user from database based on a user's id
Find(idint) (*model.User, error)
// FindByName retrieves a user from database by User.Name
FindByName(namestring) (user*model.User, errerror)
// FindAll retrieves all users from database as an array of user
FindAll() ([]model.User, error)
// Update changes user information on the User.Id passed in.
Update(user*model.User) (rowsAffectedint64, errerror)
// Insert adds a user to a database. The returned resultUser has a Id, which is auto generated by database
Insert(user*model.User) (resultUser*model.User, errerror)
// Need to add this for transaction support
EnableTxer
}
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
typeUserstruct {
Idint`json:"uid"`Namestring`json:"username"`Departmentstring`json:"department"`Createdtime.Time`json:"created"`
}
// Validate validates a newly created user, which has not persisted to database yet, so Id is empty
func (uUser) Validate() error {
returnvalidation.ValidateStruct(&u,
validation.Field(&u.Name, validation.Required),
validation.Field(&u.Created, validation.Required))
}
//ValidatePersisted validate a user that has been persisted to database, basically Id is not empty
func (uUser) ValidatePersisted() error {
returnvalidation.ValidateStruct(&u,
validation.Field(&u.Id, validation.Required),
validation.Field(&u.Name, validation.Required),
validation.Field(&u.Created, validation.Required))
}