I have created enum class UserType under /src/groovy/
as:
public enum UserType {
USER(1),
DEVSADMIN(2),
RESTAURANTADMIN(3)
}
My Domain class "User" is like :
class User {
String firstName
String lastName
String emailAddress
String contactNumber
String password
String image
Date dateOfBirth
UserType userType
Still my domain class having user_type field of string type not enum.Also I would like to know about,how can I persist data for domain class USER in which user_type is enum?
Looking for short example which will have enum groovy class,domain class,controller and service.
I have found a workaround for my problem: I have created a enum class "UserType" at path : src/groovy/
Enum Class UserType looks like :
public enum UserType {
USER('user'),
DEVSADMIN('devsAdmin'),
RESTAURANTADMIN('restaurantAdmin')
String id
UserType(String id){
this.id = id
}
}
Domain class "User" in which I am using the above enum class as:
class User {
String firstName
String lastName
String emailAddress
String contactNumber
String password
String image
Date dateOfBirth
UserType userType
static constraints = {
userType blank : false
}
}
enum handling in controller :
class AuthenticationController {
def authenticationService
def userRegistration(){
Date date = Date.parse("yyyy-MM-dd","1991-01-08")
authenticationService.userSignUp( "Abhinandan", "Satpute", abhinandan.satpute@gmail.com", "8796105046", "123", "abc_image",
date, UserType.RESTAURANTADMIN)
}
}
Persisting enum values using Service:
class AuthenticationService {
def userSignUp(String firstName, String lastName, String emailAddress, String contactNumber, String password, String image,
Date dateOfBirth, Enum userType){
User user = new User("firstName" : firstName, "lastName" : lastName, "emailAddress" : emailAddress, "contactNumber" :contactNumber,
"password" : password, "image" : image, "dateOfBirth" : dateOfBirth, "userType" : userType)
user.save(flush: true)
}
}
Finally , schema of table "User" looks like :
In this table "user_type" is the enum field which stored as VARCHAR in domain class.
After inserting record to user table :