func (c Contributor) Update(id, name, handle, email, privilege string) revel.Result { c.Validation.Required(name).Message(c.Message("contributor.validation.name")) c.Validation.Required(handle).Message(c.Message("contributor.validation.handle.required")) c.Validation.Match(handle, regexp.MustCompile(`^\w*$`)).Message(c.Message("contributor.validation.handle.invalid")) c.Validation.Required(email).Message("contributor.validation.email.required") c.Validation.Email(email).Message("contributor.validation.email.invalid") c.Validation.Required(privilege).Message("contributor.validation.privilege") cType, err := strconv.ParseInt(privilege, 10, 64) realID, iderr := strconv.ParseInt(id, 10, 64) if err != nil || iderr != nil { revel.ERROR.Printf("[LGWRD] Privilege parse error. Value: %s, should be valid int64 value. Parser Error: %s", privilege, err, ) c.Flash.Error(c.Message("contributor.privilege.parseerror")) return c.Redirect(routes.Contributor.Edit(realID)) } if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Contributor.Edit(realID)) } var contributor models.Contributor if err = c.Trx.Preload("Type").Where("id = ?", id).First(&contributor).Error; err != nil { revel.ERROR.Printf("[LGFATAL] Failed to get Contributor ID %d data. Error: %s", id, err, ) c.Flash.Error(c.Message("errors.db.generic")) return c.Redirect(routes.Contributor.Edit(realID)) } contributor.Name = name contributor.Handle = handle contributor.Email = email contributor.TypeID = cType if err := c.Trx.Save(&contributor).Error; err != nil { revel.ERROR.Printf("[LGFATAL] Error in updating new contributor %s. Error: %s", email, err, ) // Gorm appears to have no concept of error code. // We'll have to transfer the direct SQL Error so user // can have more clue of what's happening in case of // duplicate email error leaking here. c.Flash.Error(err.Error()) return c.Redirect(routes.Contributor.Edit(realID)) } return c.Redirect(routes.Contributor.List()) }
func (c Contributor) Save(email, name, handle, password, privilege string) revel.Result { c.Validation.Required(name).Message(c.Message("contributor.validation.name")) c.Validation.Required(handle).Message(c.Message("contributor.validation.handle.required")) c.Validation.Match(handle, regexp.MustCompile(`^\w*$`)).Message(c.Message("contributor.validation.handle.invalid")) c.Validation.Required(email).Message("contributor.validation.email.required") c.Validation.Email(email).Message("contributor.validation.email.invalid") c.Validation.Required(password).Message("contributor.validation.password") c.Validation.Required(privilege).Message("contributor.validation.privilege") cType, err := strconv.ParseInt(privilege, 10, 64) if err != nil { revel.ERROR.Printf("[LGWRD] Privilege parse error. Value: %s, should be valid int64 value. Parser Error: %s", privilege, err, ) c.Flash.Error(c.Message("contributor.privilege.parseerror")) return c.Redirect(routes.Contributor.New()) } if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Contributor.New()) } // existing contributor check _, dupe := c.GetContributor(email) if dupe == nil { c.Flash.Error(c.Message("contributor.validation.email.duplicate")) return c.Redirect(routes.Contributor.New()) } _, handleDupe := c.GetContributorByHandle(handle) if handleDupe == nil { c.Flash.Error(c.Message("contributor.validation.handle.duplicate")) return c.Redirect(routes.Contributor.New()) } contributor := models.Contributor{ Name: name, Handle: handle, Email: email, TypeID: cType, Photo: "/public/img/default-user.png", About: "A contributor of ObrolanSubuh.com", } contributor.SetPassword(password) if err = c.Trx.Create(&contributor).Error; err != nil { revel.ERROR.Printf("[LGFATAL] Error in creating new contributor %s. Error: %s", email, err, ) // Gorm appears to have no concept of error code. // We'll have to transfer the direct SQL Error so user // can have more clue of what's happening in case of // duplicate email error leaking here. c.Flash.Error(err.Error()) return c.Redirect(routes.Contributor.New()) } return c.Redirect(routes.Contributor.List()) }
func InitDB() { driver := revel.Config.StringDefault("db.driver", "mysql") conn := revel.Config.StringDefault("db.spec", "root:@/obrolansubuh") dbm, err := gorm.Open(driver, conn) if err != nil { errMessage := fmt.Sprintf("[DBFatalError] Failed to open database (driver: %s, spec: %s).\nError Message: %s\n", driver, conn, err.Error()) revel.ERROR.Panicln(errMessage) panic("[DBFE] Database Connection Error. Please contact web administrator.") } ORM = &dbm dbm.AutoMigrate( &models.ContributorType{}, &models.Contributor{}, &models.Post{}, &models.Category{}, &models.SiteInfo{}, ) /* // Foreign key don't work now. // See: https://github.com/jinzhu/gorm/issues/593?_pjax=%23js-repo-pjax-container // This will be uncommented once the issue is closed. // // The resulting query of this is ALTER TABLE so it shouldn't be a problem even // if we already have datas. dbm.Debug().Model(&models.Contributor{}). AddForeignKey("type_id", "contributor_types(id)", "RESTRICT", "CASCADE") dbm.Debug().Model(&models.Post{}). AddForeignKey("author_id", "contributors(id)", "RESTRICT", "CASCADE") dbm.Debug().Table("post_categories"). AddForeignKey("post_id", "posts(id)", "RESTRICT", "CASCADE"). AddForeignKey("category_id", "categories(id)", "RESTRICT", "CASCADE") */ siteInfo := models.SiteInfo{} dbm.FirstOrCreate(&siteInfo, models.SiteInfo{ AboutUsTitle: "About Us", AboutUsContent: "This is ObrolanSubuh.com", TwitterURL: "obrolansubuh", FacebookURL: "obrolansubuh", }) // If there's no user, create default admin user count := 0 if dbm.Model(&models.Contributor{}).Count(&count); count < 1 { typeAdmin := models.ContributorType{Type: "ADMIN"} typeWriter := models.ContributorType{Type: "WRITER"} dbm.Create(&typeAdmin) dbm.Create(&typeWriter) admin := models.Contributor{ Name: "Default Admin", Handle: "DefaultAdmin", Email: "*****@*****.**", About: "Default Admin ObrolanSubuh.com", Photo: "/public/img/default-user.png", Type: &typeAdmin, } admin.SetPassword("*****@*****.**") writer := models.Contributor{ Name: "Default Writer", Handle: "DefaultWriter", Email: "*****@*****.**", About: "Default Writer ObrolanSubuh.com", Photo: "/public/img/default-user.png", Type: &typeWriter, } writer.SetPassword("*****@*****.**") dbm.Create(&admin) dbm.Create(&writer) } }