I have the following models
type PrivateGormModel struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-"`
}
type Employee struct {
PrivateGormModel
Person `gorm:"embedded" json:"person,omitempty"`
Contact `gorm:"embedded" json:"contact,omitempty"`
Address `gorm:"embedded" json:"address,omitempty"`
AltContact `gorm:"embedded" json:"privateContact,omitempty"`
BankAccount `gorm:"embedded" json:"bankAccount,omitempty"`
EmployeeGroupID uint `json:"groupID"`
EmployeeGroup `json:"group"`
EmployeeRoleID uint `json:"roleID"`
EmployeeRole `json:"role"`
}
func (e Employee) Validate() error {
return validation.ValidateStruct(&e,
validation.Field(&e.Person, validation.Required),
validation.Field(&e.Contact),
validation.Field(&e.Address),
validation.Field(&e.AltContact),
validation.Field(&e.BankAccount),
validation.Field(&e.EmployeeGroup),
validation.Field(&e.EmployeeRole),
)
}
type EmployeeGroup struct {
PrivateGormModel
Title string `json:"title" gorm:"primaryKey;unique"`
}
func (e EmployeeGroup) Validate() error {
return validation.ValidateStruct(&e,
validation.Field(&e.Title, validation.Required, validation.Length(1, 32), validation.Match(regexp.MustCompile(`^[a-zA-Z0-9_ ]*$`))),
)
}
type EmployeeRole struct {
PrivateGormModel
Title string `json:"title" gorm:"primaryKey;unique"`
}
func (e EmployeeRole) Validate() error {
return validation.ValidateStruct(&e,
validation.Field(&e.Title, validation.Required, validation.Length(1, 32), validation.Match(regexp.MustCompile(`^[a-zA-Z0-9_ ]*$`))),
)
}
My employee group and employee role are both just a struct with a gorm model and a title of type string. I have a role with id 1 in the database an a group with id 1. This is the handler that creates the employee
func CreateEmployee(db *database.Database) fiber.Handler {
return func(c *fiber.Ctx) error {
employee := new(model.Employee)
if err := c.BodyParser(employee); err != nil {
fmt.Printf("%v", err)
return c.JSON(myResponse.ParsingError())
}
// if err := employee.Validate(); err != nil {
// return c.JSON(myResponse.ValidationError(err))
// }
if result := db.Omit("EmployeeRole.*").Omit("EmployeeGroup.*").Create(&employee); result.Error != nil {
return c.JSON(myResponse.RecordCreateError())
}
return c.JSON(myResponse.RecordCreateSuccess(employee))
}
}
I parse the incomming JSON into the model which looks like this and I try to create it in the database
{
"Person":{
"Initials":"",
"FirstName":"",
"MiddleName":"",
"LastName":"",
"DateOfBirth":"",
"Language":""
},
"Address":{
"Country":"",
"Zip":"",
"Number":"0",
"Addition":"",
"Street":"",
"State":"",
"City":""
},
"Contact":{
"Tel":"",
"Mail":"",
"URL":""
},
"BankAccount":{
"Bank":"",
"BIC":"",
"IBAN":"",
"AccountHolder":"",
"Establishment":""
},
"EmployeeRoleID":1,
"EmployeeRole":{
"Title":"Test"
},
"EmployeeGroupID":1,
"EmployeeGroup":{
"Title":"Test"
}
}
Which gives me the following response
{
"data": {
"id": 7,
"person": {
"initials": "",
"firstName": "",
"middleName": "",
"lastName": "",
"dateOfBirth": "2021-01-05T11:14:38+01:00",
"language": ""
},
"contact": {
"tel": "",
"mail": "",
"url": ""
},
"address": {
"country": "",
"zip": "",
"number": "0",
"addition": "",
"street": "",
"state": "",
"city": ""
},
"privateContact": {
"tel": "",
"mail": "",
"url": ""
},
"bankAccount": {
"bank": "",
"bic": "",
"iban": "",
"accountHolder": "",
"establishment": ""
},
"groupID": 0,
"group": {
"id": 0,
"title": ""
},
"roleID": 0,
"role": {
"id": 0,
"title": ""
}
},
"message": "record created successfully",
"status": "success"
}
Which gives me the following response even if the the role record with ID 1 and the group record with ID 1 does not exist. It the role or group does not exist it should give me a error and not create the record.