Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
fb51c7c
docs:new file name documents, add README for project build and sql fi…
Aug 12, 2023
8c7b1e1
完成sql文件
Aug 17, 2023
dd17c2d
提交.gitkeep防止空文件夹忽略
Aug 17, 2023
16dbca1
完成流程图
bubulunda Aug 18, 2023
6c07154
Merge pull request #1 from life-studied/doc
MoreKindness Aug 18, 2023
66f1ac1
Add files via upload
Abysmiler Aug 19, 2023
3f55656
Add files via upload
Ohgiao Aug 19, 2023
22ce2c1
提交流程图
Aug 19, 2023
a26454b
Merge branch 'yunyin' into doc
Aug 19, 2023
24c9eeb
提交sql对应的gorm结构体
Aug 19, 2023
c97719a
Merge branch 'yunyin' into doc
Aug 19, 2023
f3d0e36
提交流程图
MoreKindness Aug 19, 2023
d7542db
Merge pull request #2 from life-studied/xiaofeng_branch
life-studied Aug 19, 2023
f6aaf81
Delete 用户信息接口.jpg
Ohgiao Aug 19, 2023
c77f108
Add files via upload
Ohgiao Aug 19, 2023
3d517bd
Delete 用户信息.png
Ohgiao Aug 19, 2023
510a787
Add files via upload
Ohgiao Aug 19, 2023
23c46f9
Merge branch 'No8' into doc
Aug 19, 2023
cbbceb3
完成
bubulunda Aug 19, 2023
27620aa
Add files via upload
c1184387860 Aug 19, 2023
5300b82
Merge pull request #3 from life-studied/zuimeng
life-studied Aug 19, 2023
3d062ab
change the message in controller
Aug 20, 2023
1d1acef
change the go mod
Aug 20, 2023
6b29b96
测试viper管理配置以及gorm连接MySQL
c1184387860 Aug 22, 2023
bb06de5
Merge pull request #8 from life-studied/zuimeng_mysql
life-studied Aug 22, 2023
975d5cb
修改.gitignore
Aug 22, 2023
3e54b07
删除额外的文件
Aug 22, 2023
b122ae6
修复ini依赖controller导致可能的循环依赖
Aug 22, 2023
3236285
上一次提交的补充提交
Aug 22, 2023
8279ef0
更改router,删除sum,以及和之前的一样完成了评论列表这个Get接口 (#14)
c1184387860 Aug 22, 2023
56b3936
finish controller、service (#12)
bubulunda Aug 22, 2023
2f57ba8
删除额外的文件
Aug 22, 2023
c720ba1
完成feed接口
Aug 22, 2023
381ad4f
Abysmiler (#16)
Abysmiler Aug 24, 2023
5c7c8db
Bulunda (#17)
bubulunda Aug 24, 2023
99568da
finish dao
Aug 24, 2023
b9d56aa
添加测试文档目录
Aug 24, 2023
eef8185
Abysmiler (#20)
Abysmiler Aug 25, 2023
665b56d
Merge branch 'yunyin' into dev
Aug 25, 2023
7aaebc6
初步完善Favorite接口 (#21)
MoreKindness Aug 25, 2023
85ac01e
提交
Echoesran Aug 25, 2023
49754dc
first (#22)
Ohgiao Aug 27, 2023
d589e6a
update user.go
Aug 27, 2023
bd07944
update
Aug 27, 2023
5aedca3
update
Aug 27, 2023
3ae56b2
update
Aug 28, 2023
5cf0afd
update
Aug 28, 2023
0f5bfd5
update
Aug 28, 2023
b6e3612
merge Echo to dev
Aug 28, 2023
b7753e7
merge conflict
Aug 28, 2023
99ab73d
add init cache from mysql function
Aug 28, 2023
4801c65
update
Aug 29, 2023
87fbd5d
Merge branch 'Echo' into dev
Aug 29, 2023
fa8c90c
改进comment使其能通过测试 (#23)
c1184387860 Aug 29, 2023
828e297
update publish funcitoni
Aug 29, 2023
ab79591
Merge branch 'yunyin' into dev
Aug 29, 2023
3de9671
修复favorite接口的bug (#24)
MoreKindness Aug 29, 2023
8a57e90
update feed
Aug 29, 2023
4c2fe71
Merge branch 'dev' of github.com:life-studied/douyin-simple into dev
Aug 29, 2023
ba25e94
update comment action function
Aug 29, 2023
35bf418
添加测试用例
Aug 29, 2023
f1c1114
修改后测试评论接口 (#26)
c1184387860 Aug 30, 2023
0348abf
No8 (#27)
Ohgiao Aug 30, 2023
6325aa9
update README
Aug 30, 2023
966e6d4
update
Sep 6, 2023
085b8b7
update
Sep 6, 2023
b70ef3a
update
Sep 6, 2023
1714732
update
Sep 6, 2023
8d3a0c0
update
Sep 7, 2023
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# vendor/

# Go workspace file
go.work
**/go.work
**/go.sum

.idea
# goland IDE file
**/.idea/
2 changes: 2 additions & 0 deletions Go-Project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# video files
/public/
9 changes: 9 additions & 0 deletions Go-Project/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mysql:
path: 47.113.185.186
port: 3306
config: charset=utf8&parseTime=True&loc=Local
db-name: douyin_simple
username: root
password: 11LvmIa1wz2tMI5P
max-idle-conns: 10
max-open-conns: 100
6 changes: 6 additions & 0 deletions Go-Project/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

type Server struct {
Mysql Mysql `mapstructure:"mysql" yaml:"mysql"`
//接下来Redis这些也在这相似地定义
}
16 changes: 16 additions & 0 deletions Go-Project/config/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config

type Mysql struct {
Path string `mapstructure:"path" yaml:"path"` // 服务器地址
Port string `mapstructure:"port" yaml:"port"` // 端口
Config string `mapstructure:"config" yaml:"config"` // 高级配置
Dbname string `mapstructure:"db-name" yaml:"db-name"` // 数据库名
Username string `mapstructure:"username" yaml:"username"` // 数据库用户名
Password string `mapstructure:"password" yaml:"password"` // 数据库密码
MaxIdleConns int `mapstructure:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
}

func (m *Mysql) Dsn() string {
return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
}
75 changes: 75 additions & 0 deletions Go-Project/controller/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package controller

import (
"github.com/gin-gonic/gin"
"github.com/life-studied/douyin-simple/response"
"github.com/life-studied/douyin-simple/service"
"net/http"
"strconv"
)

// CommentAction no practical effect, just check if token is valid
func CommentAction(c *gin.Context) {
// 获取请求参数
videoID, err := strconv.ParseInt(c.Query("video_id"), 10, 64)
if err != nil {
// 处理videoID解析错误
c.JSON(http.StatusBadRequest, response.CommentActionResponse{
Response: response.Response{StatusCode: http.StatusBadRequest, StatusMsg: "无效的video_id"},
})
return
}
actionType := c.Query("action_type")
commentText := c.Query("comment_text")

token := c.Query("token")
user, exists := usersLoginInfo[token]
if !exists {
c.JSON(http.StatusBadRequest, response.CommentActionResponse{
Response: response.Response{StatusCode: http.StatusBadRequest, StatusMsg: "无效的token"},
})
return
}
userID := user.Id

// 判断操作类型
if actionType == "1" {
commentActionResponse, _ := service.CreateComment(userID, videoID, commentText)
c.JSON(http.StatusOK, commentActionResponse)

} else if actionType == "2" {
commentID, err := strconv.ParseInt(c.Query("comment_id"), 10, 64)
if err != nil {
// 处理commentID解析错误
c.JSON(http.StatusBadRequest, response.CommentActionResponse{
Response: response.Response{StatusCode: http.StatusBadRequest, StatusMsg: "无效的comment_id"},
})
return
}
commentActionResponse, _ := service.DeleteComment(userID, videoID, commentID)
c.JSON(http.StatusOK, commentActionResponse)
}

return
}

func CommentList(c *gin.Context) {
videoID, err := strconv.ParseInt(c.Query("video_id"), 10, 64)
if err != nil {
// 处理videoID解析错误
c.JSON(http.StatusBadRequest, response.CommentActionResponse{
Response: response.Response{StatusCode: http.StatusBadRequest, StatusMsg: "无效的video_id"},
})
return
}

// 将获取到的评论添加到commentList列表中

comments, err := service.GetCommentList(videoID)
// 返回response
c.JSON(http.StatusOK, response.CommentListResponse{
Response: response.Response{StatusCode: 0, StatusMsg: "OK"},
CommentList: comments,
})
return
}
2 changes: 1 addition & 1 deletion controller/common.go → Go-Project/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Video struct {

type Comment struct {
Id int64 `json:"id,omitempty"`
User User `json:"user"`
User User `json:"user" gorm:"foreignKey:user_id;references:id;"`
Content string `json:"content,omitempty"`
CreateDate string `json:"create_date,omitempty"`
}
Expand Down
12 changes: 12 additions & 0 deletions Go-Project/controller/demo_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package controller

var DemoVideos []Video

var DemoComments = []Comment{
{
Id: 1,
User: usersLoginInfo["user_1password_1"],
Content: "Test Comment",
CreateDate: "05-01",
},
}
156 changes: 156 additions & 0 deletions Go-Project/controller/favorite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package controller

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/life-studied/douyin-simple/service"
"net/http"
)

// FavoriteAction no practical effect, just check if token is valid
func FavoriteAction(c *gin.Context) {
//接收参数,并判断是否合法
token, tokenOk := c.GetQuery("token")
if !tokenOk {
c.JSON(http.StatusBadRequest, Response{StatusCode: 400, StatusMsg: "Lack of token"})
return
}
userFromToken, exist := usersLoginInfo[token]
if !exist {
c.JSON(http.StatusUnprocessableEntity, Response{StatusCode: 422, StatusMsg: "Token is invalid"})
return
}

videoId, videoIdOk := c.GetQuery("video_id")
if !videoIdOk {
c.JSON(http.StatusBadRequest, Response{StatusCode: 400, StatusMsg: "Lack of video_id"})
return
}
actionType, actionTypeOk := c.GetQuery("action_type")
if !actionTypeOk {
c.JSON(http.StatusBadRequest, Response{StatusCode: 400, StatusMsg: "Lack of action_type"})
return
}

//判断操作类型
var video = service.FavoriteVideoID{
VideoID: videoId,
}
var user = service.FavoriteUserID{
UserID: userFromToken.Id,
UserName: userFromToken.Name,
}
if actionType == "1" {
err := service.FavoriteVideo(video, user)
if err != nil {
c.JSON(http.StatusInternalServerError, Response{StatusCode: 500, StatusMsg: "Favorite failed"})
//打印报错
fmt.Printf("\033[1;37;41m%s\033[0m\n", "|BUG | "+err.Error()+"form Favorite")
return
}

} else if actionType == "2" {
err := service.UnfavoriteVideo(video, user)
if err != nil {
c.JSON(http.StatusInternalServerError, Response{StatusCode: 500, StatusMsg: "Unfavorite failed"})
//打印报错
fmt.Printf("\033[1;37;41m%s\033[0m\n", "|BUG | "+err.Error()+"form Favorite")
return
}
} else {
c.JSON(http.StatusUnprocessableEntity, Response{StatusCode: 422, StatusMsg: "Invalid action_type"})
return
}
c.JSON(http.StatusOK, Response{StatusCode: 0, StatusMsg: "Success"})
}

// FavoriteList all users have same favorite video list
func FavoriteList(c *gin.Context) {
//接收参数,并判断是否合法
token, tokenOk := c.GetQuery("token")
if !tokenOk {
c.JSON(http.StatusBadRequest, VideoListResponse{
Response: Response{
StatusCode: 400,
StatusMsg: "Lack of token",
},
VideoList: nil,
})
return
}
//检测token
userFromToken, exist := usersLoginInfo[token]
if !exist {
c.JSON(http.StatusUnprocessableEntity, VideoListResponse{
Response: Response{
StatusCode: 422,
StatusMsg: "Token is invalid",
},
VideoList: nil,
})
return
}
//转为service层使用的结构体类型
user := service.FavoriteUserID{
UserID: userFromToken.Id,
UserName: userFromToken.Name,
}
//用不到但是必须要有的接收参数
_, userIdOk := c.GetQuery("user_id")
if !userIdOk {
c.JSON(http.StatusBadRequest, VideoListResponse{
Response: Response{
StatusCode: 400,
StatusMsg: "Lack of user_id",
},
VideoList: nil,
})
return
}
//获取用户收藏的视频列表
videoList, err := service.ReadFavoriteVideo(user)
if err != nil {
fmt.Printf("\033[1;37;41m%s\033[0m\n", "| BUG | "+err.Error()+"form Favorite")
c.JSON(http.StatusInternalServerError, VideoListResponse{
Response: Response{
StatusCode: 500,
StatusMsg: "Internal server error",
},
VideoList: nil,
})
return
}
//转为前端需要的结构体类型
c.JSON(http.StatusOK, serviceToVideoList(videoList))
}

func serviceToVideoList(favoriteVideoList []service.FavoriteVideoList) (response VideoListResponse) {

response.Response = Response{
StatusCode: 0,
StatusMsg: "Success",
}
response.VideoList = make([]Video, 0, len(favoriteVideoList))
for _, favoriteVideo := range favoriteVideoList {
response.VideoList = append(response.VideoList, Video{
Id: favoriteVideo.Id,
Author: serviceToUser(favoriteVideo.Author),
PlayUrl: favoriteVideo.PlayUrl,
CoverUrl: favoriteVideo.CoverUrl,
FavoriteCount: favoriteVideo.FavoriteCount,
CommentCount: favoriteVideo.CommentCount,
IsFavorite: true,
})
}
return response
}

func serviceToUser(favoriteUser service.FavoriteUserID) (user User) {
user = User{
Id: favoriteUser.UserID,
Name: favoriteUser.UserName,
FollowCount: favoriteUser.FollowCount,
FollowerCount: favoriteUser.FollowerCount,
}
return user
}
37 changes: 37 additions & 0 deletions Go-Project/controller/feed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controller

import (
"github.com/gin-gonic/gin"
"github.com/life-studied/douyin-simple/service"
"net/http"
)

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

// Feed same demo video list for every request
func Feed(c *gin.Context) {
strLatestTime, exists := c.GetQuery("latest_time")
if !exists {
c.JSON(http.StatusBadRequest, FeedResponse{Response: Response{StatusCode: 1, StatusMsg: "latest_time is empty"}})
return
}
nextTime, startId, err := service.GetNextTime(strLatestTime)
if err != nil {
c.JSON(http.StatusBadRequest, FeedResponse{Response: Response{StatusCode: 1, StatusMsg: err.Error()}})
return
}
var endId int64
if startId+30 > int64(len(DemoVideos)) {
endId = int64(len(DemoVideos))
} else {
endId = startId + 30
}
c.JSON(http.StatusOK, FeedResponse{Response: Response{StatusCode: 0},
VideoList: DemoVideos[startId:endId],
NextTime: nextTime,
})
}
Loading