A web blog and personal homepage engine written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

71 lines
1.4 KiB

  1. package models
  2. import "net/http"
  3. type askMan struct{}
  4. // Questions is a singleton manager class for Question model access.
  5. var Questions = askMan{}
  6. // Question model.
  7. type Question struct {
  8. BaseModel
  9. Name string
  10. Question string
  11. Answered bool
  12. PostID int // FKey Post.id
  13. // Relationships.
  14. Post Post
  15. }
  16. // New creates a new Question model.
  17. func (m askMan) New() Question {
  18. return Question{}
  19. }
  20. // Load a comment by ID.
  21. func (m askMan) Load(id int) (Question, error) {
  22. var q Question
  23. r := DB.Preload("Post").First(&q, id)
  24. return q, r.Error
  25. }
  26. // Pending returns questions in need of answering.
  27. func (m askMan) Pending() ([]Question, error) {
  28. var q []Question
  29. r := DB.Preload("Post").Where("answered=false").Find(&q)
  30. return q, r.Error
  31. }
  32. // RecentlyAnswered returns questions that have blog posts attached.
  33. func (m askMan) RecentlyAnswered(depth int) ([]Question, error) {
  34. var qq []Question
  35. r := DB.Preload("Post").
  36. Where("answered=true AND post_id IS NOT NULL").
  37. Order("created_at desc").
  38. Limit(depth).
  39. Find(&qq)
  40. return qq, r.Error
  41. }
  42. // Save the question.
  43. func (q Question) Save() error {
  44. if DB.NewRecord(q) {
  45. return DB.Create(&q).Error
  46. }
  47. return DB.Save(&q).Error
  48. }
  49. // Delete the question.
  50. func (q Question) Delete() error {
  51. return DB.Delete(&q).Error
  52. }
  53. // ParseForm sets the question's attributes from HTTP form.
  54. func (q *Question) ParseForm(r *http.Request) {
  55. q.Name = r.FormValue("name")
  56. q.Question = r.FormValue("question")
  57. }