当前位置: 首页>>代码示例>>Golang>>正文


Golang models.GetDB函数代码示例

本文整理汇总了Golang中github.com/denisbakhtin/medical/models.GetDB函数的典型用法代码示例。如果您正苦于以下问题:Golang GetDB函数的具体用法?Golang GetDB怎么用?Golang GetDB使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了GetDB函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: MainMenu

//MainMenu returns the list of main menu items
func MainMenu() []MenuItem {
	db := models.GetDB()
	about := &models.Page{}
	db.First(about, 4)
	contacts := &models.Page{}
	db.First(contacts, 7)
	menu := []MenuItem{
		MenuItem{
			URL:   "/reviews",
			Title: "Отзывы",
		},
		MenuItem{
			URL:   "/articles",
			Title: "Статьи",
		},
		MenuItem{
			URL:      about.URL(),
			Title:    "О враче",
			CssClass: "small",
		},
		MenuItem{
			URL:      contacts.URL(),
			Title:    "Контакты",
			CssClass: "small",
		},
	}
	return menu
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:29,代码来源:template.go

示例2: PageShow

//PageShow handles /pages/:id route
func PageShow(c *gin.Context) {
	db := models.GetDB()
	session := sessions.Default(c)

	idslug := c.Param("idslug")
	id := helpers.Atouint(strings.Split(idslug, "-")[0])
	page := &models.Page{}
	db.First(page, id)
	if page.ID == 0 || !page.Published {
		c.HTML(404, "errors/404", nil)
		return
	}
	//redirect to canonical url
	if c.Request.URL.Path != page.URL() {
		c.Redirect(303, page.URL())
		return
	}
	c.HTML(200, "pages/show", gin.H{
		"Page":            page,
		"Title":           page.Name,
		"Active":          page.URL(),
		"MetaDescription": page.MetaDescription,
		"MetaKeywords":    page.MetaKeywords,
		"Authenticated":   (session.Get("user_id") != nil),
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:27,代码来源:pages.go

示例3: ReviewUpdatePost

func ReviewUpdatePost(c *gin.Context) {
	session := sessions.Default(c)
	db := models.GetDB()

	c.Request.ParseMultipartForm(32 << 20)
	review := &models.Review{}
	if err := c.Bind(review); err == nil {

		if mpartFile, mpartHeader, err := c.Request.FormFile("image"); err == nil {
			defer mpartFile.Close()
			review.Image, err = saveFile(mpartHeader, mpartFile)
			if err != nil {
				c.HTML(500, "errors/500", helpers.ErrorData(err))
				return
			}
		}

		if err := db.Model(&models.Review{}).Updates(review).Error; err != nil {
			session.AddFlash(err.Error())
			session.Save()
			c.Redirect(303, c.Request.RequestURI)
			return
		}
		session.AddFlash("Отзыв был успешно сохранен")
	} else {
		log.Println(err)
		session.AddFlash("Ошибка! Внимательно проверьте заполнение всех полей")
	}
	session.Save()
	c.Redirect(303, "/reviews")
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:31,代码来源:reviews.go

示例4: CommentCreatePost

//CommentCreatePost handles /new_comment route
func CommentCreatePost(c *gin.Context) {
	session := sessions.Default(c)
	db := models.GetDB()

	comment := &models.Comment{}
	if c.Bind(comment) == nil {
		//simple captcha check
		captcha, err := base64.StdEncoding.DecodeString(comment.Captcha)
		if err != nil {
			c.HTML(500, "errors/500", helpers.ErrorData(err))
			return
		}
		if string(captcha) != "100.00" {
			c.HTML(400, "errors/400", nil)
			return
		}
		comment.Published = false //leave unpublished
		if err := db.Create(comment).Error; err != nil {
			c.HTML(400, "errors/400", helpers.ErrorData(err))
			return
		}
		notifyAdminOfComment(comment)
		session.AddFlash("Спасибо! Ваш вопрос будет опубликован после проверки.")
		session.Save()
		c.Redirect(303, fmt.Sprintf("/articles/%d#comments", comment.ArticleID))
	} else {
		session.AddFlash("Ошибка! Внимательно проверьте заполнение всех полей!")
		session.Save()
		c.Redirect(303, "/")
	}
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:32,代码来源:comments.go

示例5: SignInPost

func SignInPost(c *gin.Context) {
	db := models.GetDB()
	session := sessions.Default(c)

	login := &models.Login{}
	if c.Bind(login) == nil {
		user := &models.User{}
		db.Where("lower(email) = lower(?)", login.Email).First(user)
		if user.ID == 0 {
			log.Printf("ERROR: Login failed, IP: %s, Email: %s\n", c.ClientIP(), login.Email)
			session.AddFlash("Эл. адрес или пароль указаны неверно")
			session.Save()
			c.Redirect(303, "/signin")
			return
		}
		//create user
		if err := user.ComparePassword(login.Password); err != nil {
			log.Printf("ERROR: Login failed, IP: %s, Email: %s\n", c.ClientIP(), login.Email)
			session.AddFlash("Эл. адрес или пароль указаны неверно")
			session.Save()
			c.Redirect(303, "/signin")
			return
		}

		session.Set("user_id", user.ID)
		session.Save()
		c.Redirect(303, "/")
	}
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:29,代码来源:auth.go

示例6: SignUpPost

func SignUpPost(c *gin.Context) {
	session := sessions.Default(c)
	db := models.GetDB()

	register := &models.Register{}
	if c.Bind(register) == nil {
		user := &models.User{}
		db.Where("lower(email) = lower(?)", register.Email).First(user)
		if user.ID != 0 {
			session.AddFlash("Пользователь с таким эл. адресом уже существует")
			session.Save()
			c.Redirect(303, "/signup")
			return
		}
		//create user
		user.Email = register.Email
		user.Password = register.Password
		if err := db.Create(user).Error; err != nil {
			session.AddFlash("Ошибка регистрации пользователя")
			session.Save()
			log.Printf("ERROR: ошибка регистрации пользователя: %v", err)
			c.Redirect(303, "/signup")
			return
		}
		session.Set("user_id", user.ID)
		session.Save()
		c.Redirect(303, "/")
	}
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:29,代码来源:auth.go

示例7: ReviewUpdateGet

//ReviewUpdate handles /edit_review?token=:secure_token route
func ReviewUpdateGet(c *gin.Context) {
	session := sessions.Default(c)
	flashes := session.Flashes()
	session.Save()
	db := models.GetDB()

	id := getIDFromToken(c.Request.FormValue("token"))
	review := &models.Review{}
	db.First(review, id)
	if review.ID == 0 || review.Published {
		err := fmt.Errorf("Отзыв не найден или уже был опубликован и не подлежит редактированию")
		c.HTML(404, "errors/404", helpers.ErrorData(err))
		return
	}

	var articles []models.Article
	db.Where("published = ?", true).Find(&articles)
	review.Published = true //set default to true
	c.HTML(200, "reviews/form", gin.H{
		"Title":      "Редактировать отзыв",
		"Articles":   articles,
		"Active":     "reviews",
		"Review":     review,
		"SecureEdit": true,
		"Flash":      flashes,
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:28,代码来源:reviews.go

示例8: ReviewAdminUpdatePost

func ReviewAdminUpdatePost(c *gin.Context) {
	session := sessions.Default(c)
	db := models.GetDB()

	c.Request.ParseMultipartForm(32 << 20)
	review := &models.Review{}
	if c.Bind(review) == nil {
		review.ArticleID = helpers.Atouintr(c.Request.FormValue("article_id"))
		if mpartFile, mpartHeader, err := c.Request.FormFile("image"); err == nil {
			defer mpartFile.Close()
			review.Image, err = saveFile(mpartHeader, mpartFile)
			if err != nil {
				c.HTML(500, "errors/500", helpers.ErrorData(err))
				return
			}
		}

		if err := db.Model(&models.Review{}).Updates(review).Error; err != nil {
			session.AddFlash(err.Error())
			session.Save()
			c.Redirect(303, c.Request.RequestURI)
			return
		}
		c.Redirect(303, "/admin/reviews")
	} else {
		session.AddFlash("Ошибка! Проверьте внимательно заполнение всех полей!")
		session.Save()
		c.Redirect(303, c.Request.RequestURI)
	}
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:30,代码来源:reviews.go

示例9: UsersAdminIndex

//UsersAdminIndex handles GET /admin/users route
func UsersAdminIndex(c *gin.Context) {
	db := models.GetDB()

	var list []models.User
	db.Find(&list)
	c.HTML(200, "users/admin/index", gin.H{
		"Title":  "Пользователи",
		"Active": "users",
		"List":   list,
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:12,代码来源:users.go

示例10: PagesAdminIndex

//PagesAdminIndex handles GET /admin/pages route
func PagesAdminIndex(c *gin.Context) {
	db := models.GetDB()

	var list []models.Page
	db.Order("published desc, id desc").Find(&list)
	c.HTML(200, "pages/admin/index", gin.H{
		"Title":  "Страницы",
		"Active": "pages",
		"List":   list,
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:12,代码来源:pages.go

示例11: CommentsAdminIndex

//CommentsAdminIndex handles GET /admin/comments route
func CommentsAdminIndex(c *gin.Context) {
	db := models.GetDB()

	var list []models.Comment
	db.Order("id desc").Find(&list)
	c.HTML(200, "comments/admin/index", gin.H{
		"Title":  "Вопросы посетителей",
		"Active": "comments",
		"List":   list,
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:12,代码来源:comments.go

示例12: ReviewsAdminIndex

//ReviewsAdminIndex handles GET /admin/reviews route
func ReviewsAdminIndex(c *gin.Context) {
	db := models.GetDB()

	var list []models.Review
	db.Order("id desc").Find(&list)
	c.HTML(200, "reviews/admin/index", gin.H{
		"Title":  "Отзывы",
		"Active": "reviews",
		"List":   list,
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:12,代码来源:reviews.go

示例13: ArticlesAdminIndex

//ArticlesAdminIndex handles GET /admin/articles route
func ArticlesAdminIndex(c *gin.Context) {
	db := models.GetDB()

	var list []models.Article
	if err := db.Order("published desc, id desc").Find(&list).Error; err != nil {
		c.HTML(500, "errors/500", helpers.ErrorData(err))
		return
	}
	c.HTML(200, "articles/admin/index", gin.H{
		"Title":  "Статьи",
		"Active": "articles",
		"List":   list,
	})
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:15,代码来源:articles.go

示例14: PageAdminDelete

//PageAdminDelete handles /admin/delete_page route
func PageAdminDelete(c *gin.Context) {
	db := models.GetDB()

	page := &models.Page{}
	db.First(page, c.Request.PostFormValue("id"))
	if page.ID == 0 {
		c.HTML(404, "errors/404", nil)
	}

	if err := db.Delete(page).Error; err != nil {
		c.HTML(500, "errors/500", helpers.ErrorData(err))
		return
	}
	c.Redirect(303, "/admin/pages")
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:16,代码来源:pages.go

示例15: ReviewAdminDelete

//ReviewAdminDelete handles /admin/delete_review route
func ReviewAdminDelete(c *gin.Context) {
	db := models.GetDB()

	review := &models.Review{}
	db.First(review, c.Request.PostFormValue("id"))
	if review.ID == 0 {
		c.HTML(404, "errors/404", nil)
	}

	if err := db.Delete(review).Error; err != nil {
		c.HTML(500, "errors/500", helpers.ErrorData(err))
		return
	}
	c.Redirect(303, "/admin/reviews")
}
开发者ID:denisbakhtin,项目名称:medical,代码行数:16,代码来源:reviews.go


注:本文中的github.com/denisbakhtin/medical/models.GetDB函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。