Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# GoDance
字节跳动青训营GoDance队大项目开发
项目包含自动迁移建表功能,请大家修改model/mysql.go中的数据库信息后再运行

controller/publish 中的ResourceDir也请修改成自己的本机IP

目前项目仍存在问题:

- 视频缩略图无法展示,本机上传的视频无法播放
- 喜欢列表与点赞列表还未完成
- 视频点赞数量与评论数量没有实时更新
- 用户点赞视频没有持久化
106 changes: 106 additions & 0 deletions controller/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package controller

import (
"GoDance/model"
"GoDance/service"
"fmt"
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
)

// CommentAction no practical effect, just check if token is valid
// CommentAction
// @Summary 评论操作
// @Description 登录用户对视频进行评论
// @Tags 互动接口
// @param token query string true "用户鉴权token"
// @param video_id query string true "视频id"
// @param action_type query string true "1-发布评论,2-删除评论"
// @param comment_text query string false "用户填写的评论内容,在action_type=1的时候使用"
// @param comment_id query string false "要删除的评论id,在action_type=2的时候使用"
// @Success 200 {string} success
// @Router /comment/action/ [post]
func CommentAction(c *gin.Context) {
token := c.Query("token")
actionType := c.Query("action_type")
videoId, _ := strconv.Atoi(c.Query("video_id"))
user, err := service.FindUserByToken(token)
if err == nil { //改动了user的访问
var comment model.CommentData
if actionType == "1" { //新增
comment.UserId = user.Id
comment.VideoId = int64(videoId)
text := c.Query("comment_text")
comment.CommentText = text
comment.CreateDate = time.Now()
if err := model.InsertComment(&comment); err != nil {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "评论失败"})
}
c.JSON(http.StatusOK, CommentActionResponse{Response: Response{StatusCode: 0, StatusMsg: "评论成功"},
Comment: model.Comment{
Id: comment.Id,
User: user,
Content: text,
CreateDate: comment.CreateDate.Format("01-02"),
}})
return
} else if actionType == "2" { //删除
commentId, _ := strconv.Atoi(c.Query("comment_id"))
comment, err := model.FindCommentById(int64(commentId)) //补充删除不存在的情况
if comment.Cancel == 1 || err != nil {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "评论已删除或不存在"})
return
}
if comment.UserId == user.Id { //是该评论主人
if err = model.DeleteComment(int64(commentId), int64(comment.VideoId)); err != nil {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "评论删除失败"})
} else {
c.JSON(http.StatusOK, Response{StatusCode: 0, StatusMsg: "评论删除成功"})
}
} else { //不能删除他人评论
c.JSON(http.StatusForbidden, Response{StatusCode: 1, StatusMsg: "没有权限删除他人评论"})
}
} else {
c.JSON(http.StatusBadRequest, Response{StatusCode: 1, StatusMsg: "评论状态参数错误"})
}
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: err.Error()})
}
}

// CommentList all videos have same demo comment list
// CommentList
// @Summary 评论列表
// @Description 查看视频的所有评论,按发布时间倒序
// @Tags 互动接口
// @param token query string true "用户鉴权token"
// @param video_id query string true "视频id"
// @Success 200 {string} success
// @Router /comment/list/ [get]
func CommentList(c *gin.Context) {
token := c.Query("token")
videoId, _ := strconv.Atoi(c.Query("video_id"))
_, err := service.FindUserByToken(token)
if err == nil {
comments := model.FindCommentsByVideoId(int64(videoId))
var commentsVO = make([]model.Comment, len(comments))
for i := 0; i < len(comments); i++ {
comment := comments[i]
commentOwner, err := service.FindUserById(comment.UserId)
if err != nil {
fmt.Printf(err.Error())
return
}
commentsVO[i] = model.Comment{Id: comment.Id, User: commentOwner, Content: comment.CommentText, CreateDate: comment.CreateDate.Format("01-02")}
}
c.JSON(http.StatusOK, CommentListResponse{
Response: Response{StatusCode: 0, StatusMsg: "获取评论列表成功"},
CommentList: commentsVO,
})
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: err.Error()})
}
}
50 changes: 50 additions & 0 deletions controller/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controller

import "GoDance/model"

type Response struct {
StatusCode int32 `json:"status_code"`
StatusMsg string `json:"status_msg,omitempty"`
}

// Comment About comment

type CommentListResponse struct {
Response
CommentList []model.Comment `json:"comment_list,omitempty"`
}

type CommentActionResponse struct {
Response
Comment model.Comment `json:"comment,omitempty"`
}

// Video About Video

type FeedResponse struct {
Response
VideoList []model.Video `json:"video_list,omitempty"`
NextTime int64 `json:"next_time,omitempty"`
}

type VideoListResponse struct {
Response
VideoList []model.Video `json:"video_list"`
}
type PublishVideoListResponse struct {
Response
PublishVideoList []model.PublishListVideoStruct `json:"video_list"`
}

// User About User

type UserResponse struct {
Response
User model.User `json:"user"`
}

type LoginResponse struct {
Response
Id int64 `json:"user_id,omitempty"`
Token string `json:"token,omitempty"`
}
43 changes: 43 additions & 0 deletions controller/favorite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package controller

import (
"GoDance/model"
"GoDance/service"
"github.com/gin-gonic/gin"
"net/http"
)

func FavoriteAction(c *gin.Context) {
token := c.Query("token")
videoId := c.Query("video_id")
actionType := c.Query("action_type")
_, err := service.FindUserByToken(token)
if err == nil {
if actionType == "2" { //delete the favorite
favorite := model.FavoriteData{Token: token, VideoId: videoId}
model.Db.Table("favorite_data").Where("token=?", token).Delete(&favorite)
c.JSON(http.StatusOK, Response{StatusCode: 0})
} else if actionType == "1" {
favorite := model.FavoriteData{Token: token, VideoId: videoId}
model.Db.Create(&favorite)
c.JSON(http.StatusOK, Response{StatusCode: 0})
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "The entry doesn't exist"})
}
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
}
}

// FavoriteList all users have same favorite video list
func FavoriteList(c *gin.Context) {
var video_list []model.VideoData
token := c.Query("token")
model.Db.Table("favorite_data").Find(&video_list, "token=?", token)
c.JSON(http.StatusOK, VideoListResponse{
Response: Response{
StatusCode: 0,
},
//VideoList: video_list,
})
}
41 changes: 41 additions & 0 deletions controller/feed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package controller

import (
"GoDance/service"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"time"
)

// Feed same demo video list for every request
func Feed(c *gin.Context) {

//token := c.Query("token")
//userIds, _ := c.Get("UserId")
//userId := userIds.(int64)
currentTime, err := strconv.ParseInt(c.Query("latest_time"), 10, 64)

if err != nil || currentTime == 0 {
currentTime = time.Now().Unix()
}
//FeedGet的参数二是userId,这里测试所以设置为0
feedList, nextTime, _ := service.FeedGet(currentTime, 0)

c.JSON(http.StatusOK, FeedResponse{
Response: Response{StatusCode: 0}, //成功
VideoList: feedList,
NextTime: nextTime,
})
}

//demo
/*
func Feed(c *gin.Context) {
c.JSON(http.StatusOK, FeedResponse{
Response: Response{StatusCode: 0},
VideoList: DemoVideos,
NextTime: time.Now().Unix(),
})
}
*/
Loading