topics_controller.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package v1
  2. import (
  3. "github.com/runningwater/gohub/app/models/topic"
  4. "github.com/runningwater/gohub/app/requests"
  5. "github.com/runningwater/gohub/pkg/auth"
  6. "github.com/runningwater/gohub/pkg/response"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type TopicsController struct {
  10. BaseApiController
  11. }
  12. // func (ctrl *TopicsController) Index(c *gin.Context) {
  13. // topics := topic.All()
  14. // response.Data(c, topics)
  15. // }
  16. func (ctrl *TopicsController) Store(c *gin.Context) {
  17. request := requests.TopicRequest{}
  18. if ok := requests.Validate(c, &request, requests.TopicSave); !ok {
  19. return
  20. }
  21. topicModel := topic.Topic{
  22. Title: request.Title,
  23. Body: request.Body,
  24. CategoryID: request.CategoryID,
  25. UserID: auth.CurrentUID(c),
  26. }
  27. topicModel.Create()
  28. if topicModel.ID > 0 {
  29. response.Created(c, topicModel)
  30. } else {
  31. response.Abort500(c, "创建失败,请稍后尝试~")
  32. }
  33. }
  34. func (ctrl *TopicsController) Update(c *gin.Context) {
  35. topicModel := topic.Get(c.Param("id"))
  36. if topicModel.ID == 0 {
  37. response.Abort404(c)
  38. return
  39. }
  40. request := requests.TopicRequest{}
  41. if ok := requests.Validate(c, &request, requests.TopicSave); !ok {
  42. return
  43. }
  44. topicModel.Title = request.Title
  45. topicModel.Body = request.Body
  46. topicModel.CategoryID = request.CategoryID
  47. rowsAffected := topicModel.Save()
  48. if rowsAffected > 0 {
  49. response.Data(c, topicModel)
  50. } else {
  51. response.Abort500(c, "更新失败,请稍后尝试~")
  52. }
  53. }
  54. func (ctrl *TopicsController) Delete(c *gin.Context) {
  55. }