comment.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package controller
  2. import (
  3. "github.com/RaymondCode/simple-demo/api"
  4. "github.com/RaymondCode/simple-demo/dao"
  5. "github.com/RaymondCode/simple-demo/service"
  6. "github.com/gin-gonic/gin"
  7. "net/http"
  8. "strings"
  9. )
  10. type CommentListResponse struct {
  11. api.Response
  12. CommentList []api.Comment `json:"comment_list,omitempty"`
  13. }
  14. type CommentActionResponse struct {
  15. api.Response
  16. Comment api.Comment `json:"comment,omitempty"`
  17. }
  18. const DATE_TIME_FORMAT = "2006-01-02 15:04:05"
  19. // CommentAction no practical effect, just check if token is valid
  20. // 三个参数:token,video_id,action_type(1-发布,2-删除),comment_text
  21. func CommentAction(c *gin.Context) {
  22. token := c.Query("token")
  23. actionType := c.Query("action_type")
  24. if res, _ := service.GetToken(token); res == 0 {
  25. if actionType == "1" {
  26. user_id := strings.Split(token, ":")[0]
  27. text := c.Query("comment_text")
  28. video_id := c.Query("video_id")
  29. commentRes := dao.InsertAndGetComment(user_id, text, video_id)
  30. user := dao.GetUserById(user_id)
  31. c.JSON(http.StatusOK, CommentActionResponse{Response: api.Response{StatusCode: 0},
  32. Comment: api.Comment{
  33. Id: commentRes.ID,
  34. User: api.User{
  35. Id: user.Id,
  36. Name: user.Name,
  37. FollowCount: user.FollowCount,
  38. FollowerCount: user.FollowerCount,
  39. IsFollow: user.IsFollow,
  40. },
  41. Content: commentRes.Content,
  42. CreateDate: commentRes.CreateTime.Format(DATE_TIME_FORMAT),
  43. }})
  44. return
  45. }
  46. c.JSON(http.StatusOK, api.Response{StatusCode: 0})
  47. } else {
  48. c.JSON(http.StatusOK, api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
  49. }
  50. }
  51. // CommentList all videos have same demo comment list
  52. // token和video_id
  53. func CommentList(c *gin.Context) {
  54. videoId := c.Query("video_id")
  55. tempList := dao.GetVideoCommentList(videoId)
  56. var res []api.Comment
  57. for i := 0; i < len(tempList); i++ {
  58. user := dao.GetUserByUserIdInt64(tempList[i].UserID)
  59. res = append(res, api.Comment{
  60. Id: tempList[i].ID,
  61. User: api.User{
  62. Id: user.Id,
  63. Name: user.Name,
  64. FollowCount: user.FollowCount,
  65. FollowerCount: user.FollowerCount,
  66. IsFollow: user.IsFollow,
  67. },
  68. Content: tempList[i].Content,
  69. CreateDate: tempList[i].CreateTime.Format(DATE_TIME_FORMAT),
  70. })
  71. }
  72. c.JSON(http.StatusOK, CommentListResponse{
  73. Response: api.Response{StatusCode: 0},
  74. CommentList: res,
  75. })
  76. }