topics_controller.go 894 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }