favorite.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/RaymondCode/simple-demo/api"
  5. "github.com/RaymondCode/simple-demo/dao"
  6. "github.com/RaymondCode/simple-demo/service"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. )
  11. // FavoriteAction no practical effect, just check if token is valid
  12. // 三个参数,token,video_id,action_type
  13. func FavoriteAction(c *gin.Context) {
  14. token := c.Query("token")
  15. if exist, _ := service.GetToken(token); exist != 0 {
  16. c.JSON(http.StatusOK, api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
  17. return
  18. }
  19. fmt.Println(token)
  20. user_id := strings.Split(token, ":")[0]
  21. action_type := c.Query("action_type")
  22. video_id := c.Query("video_id")
  23. service.AddFavorite(video_id, user_id, action_type)
  24. c.JSON(http.StatusOK, api.Response{StatusCode: 0, StatusMsg: "点赞成功"})
  25. }
  26. // FavoriteList all users have same favorite video list
  27. func FavoriteList(c *gin.Context) {
  28. token := c.Query("token")
  29. if exist, _ := service.GetToken(token); exist != 0 {
  30. c.JSON(http.StatusOK, api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
  31. return
  32. }
  33. user_id := strings.Split(token, ":")[0]
  34. videoIdList := service.GetFavoriteVideoIdList(user_id)
  35. videoList := dao.GetFavoriteVideoList(*videoIdList)
  36. var resList []api.Video
  37. for _, node := range videoList {
  38. user := dao.GetUserByIdInt64(node.AuthorId)
  39. resList = append(resList, api.Video{
  40. Id: node.Id,
  41. Author: api.User{
  42. Id: user.Id,
  43. Name: user.Name,
  44. FollowCount: user.FollowCount,
  45. FollowerCount: user.FollowerCount,
  46. IsFollow: user.IsFollow,
  47. },
  48. PlayUrl: node.PlayUrl,
  49. CoverUrl: node.CoverUrl,
  50. FavoriteCount: node.FavoriteCount,
  51. CommentCount: node.CommentCount,
  52. IsFavorite: true,
  53. })
  54. }
  55. fmt.Printf("结果长度为:%d", len(resList))
  56. for _, node := range resList {
  57. fmt.Println("%+v\n", node)
  58. }
  59. c.JSON(http.StatusOK, VideoListResponse{
  60. Response: api.Response{
  61. StatusCode: 0,
  62. },
  63. VideoList: resList,
  64. })
  65. }