Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis-pool #15

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f34736f
对readme做了简单修改,添加的数据库基本的设计文档
Jun 3, 2022
fa00c00
design for database and project structure
BaiZe1998 Jun 5, 2022
8e8af85
增加follow的单元测试
BaiZe1998 Jun 5, 2022
7605443
为comment表增加status字段
BaiZe1998 Jun 5, 2022
a6428e2
修改database.md
BaiZe1998 Jun 5, 2022
c60664a
修改follow注释
BaiZe1998 Jun 5, 2022
338d7a4
Merge pull request #1 from BaiZe1998/yc_project_struct_design
TremblingV5 Jun 5, 2022
5f0bbb7
数据库索引设计
BaiZe1998 Jun 6, 2022
84148ff
Merge pull request #2 from BaiZe1998/yc_db_index_design
TremblingV5 Jun 6, 2022
ea37482
完成用户信息、用户注册和登录功能,添加jwt ,自定义中间件校验jwt
1753589103 Jun 7, 2022
eca9937
Add files via upload
1753589103 Jun 7, 2022
73478ce
Delete douyin.sql
1753589103 Jun 7, 2022
7df0727
Add files via upload
1753589103 Jun 7, 2022
164c6e4
Merge pull request #3 from 1753589103/main
Mr1q Jun 7, 2022
a8a6d3a
Merge branch 'main' into git-dev
BaiZe1998 Jun 9, 2022
0b55ad6
Merge pull request #4 from 1753589103/git-dev
BaiZe1998 Jun 9, 2022
5e41679
围绕关注接口添加了功能实现,修改了go.mod的module名;
Jun 9, 2022
e5b7e76
Merge branch 'main' of github.com:BaiZe1998/douyin-simple-demo into x…
Jun 9, 2022
24edbd2
Merge pull request #5 from BaiZe1998/xinzf-database-design
BaiZe1998 Jun 9, 2022
be5d806
调整项目代码结构、修正user和relation相关的bug
BaiZe1998 Jun 9, 2022
52ee695
Merge pull request #6 from BaiZe1998/yc_auth
TremblingV5 Jun 9, 2022
6419920
修改了用户注册一直显示用户已存在的bug
1753589103 Jun 9, 2022
9cee7bc
修改了feed路由被拦截的bug
1753589103 Jun 9, 2022
f367d3a
Merge pull request #7 from 1753589103/main
Mr1q Jun 9, 2022
42bdef4
修正数据库password字段问题
BaiZe1998 Jun 9, 2022
ac3b358
Merge pull request #8 from BaiZe1998/yc_auth
Mr1q Jun 9, 2022
9090529
添加评论、删除评论、修改了feed流的返回结果
Jun 10, 2022
a55353c
关注列表和粉丝列表
Jun 10, 2022
f9f7607
Merge pull request #10 from BaiZe1998/relation_list
BaiZe1998 Jun 10, 2022
6f765e0
Merge pull request #9 from BaiZe1998/qjh_comment
BaiZe1998 Jun 10, 2022
0e8958f
验证token时,在context中添加一个"user_id"字段记录token所属的user_id
Jun 10, 2022
1094519
Merge pull request #11 from BaiZe1998/relation_list
BaiZe1998 Jun 10, 2022
33a2255
项目结构调整,将数据实体全部放入dto,model只存放数据库模型及其CRUD方法,修正部分bug
BaiZe1998 Jun 10, 2022
1824464
Merge pull request #12 from BaiZe1998/yc_structure
TremblingV5 Jun 10, 2022
49cf97f
添加软删除
Jun 10, 2022
270139f
软删除,更改评论数据表
Jun 10, 2022
32a08a9
Merge pull request #14 from BaiZe1998/qjh_new
BaiZe1998 Jun 10, 2022
bbab2c3
添加了基于go-redis的redis封装
Jun 10, 2022
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# Go workspace file
go.work

.idea
.idea

.vscode
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ go build && ./simple-demo

### 测试数据

测试数据写在 demo_data.go 中,用于列表接口的 mock 测试
测试数据写在 demo_data.go 中,用于列表接口的 mock 测试

### 目录结构说明

- document: 项目相关的一些文档记录
- model: gorm模型
62 changes: 38 additions & 24 deletions controller/comment.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,61 @@
package controller

import (
"github.com/gin-gonic/gin"
"context"
"fmt"
"net/http"
)

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

type CommentActionResponse struct {
Response
Comment Comment `json:"comment,omitempty"`
}
"github.com/BaiZe1998/douyin-simple-demo/db/model"
"github.com/BaiZe1998/douyin-simple-demo/dto"
"github.com/BaiZe1998/douyin-simple-demo/pkg/util"
"github.com/BaiZe1998/douyin-simple-demo/service"
"github.com/gin-gonic/gin"
)

// CommentAction no practical effect, just check if token is valid
func CommentAction(c *gin.Context) {
token := c.Query("token")
actionType := c.Query("action_type")
videoId, _ := strconv.ParseInt(c.Query("video_id"), 10, 64)
userClaims, _ := util.ParseToken(token)
userModel, _ := model.QueryUserById(context.Background(), userClaims.ID)
users := dto.User{
Id: userModel.ID,
Name: userModel.Name,
FollowCount: userModel.FollowCount,
FollowerCount: userModel.FollowerCount,
IsFollow: false,
}

if user, exist := usersLoginInfo[token]; exist {
if userModel.ID > 0 {
if actionType == "1" {
text := c.Query("comment_text")
c.JSON(http.StatusOK, CommentActionResponse{Response: Response{StatusCode: 0},
Comment: Comment{
Id: 1,
User: user,
Content: text,
CreateDate: "05-01",
}})
return
//comment addcomment
responseComment := service.AddComment(text, users, videoId)
c.JSON(http.StatusOK,
dto.CommentActionResponse{
Response: dto.Response{StatusCode: 0},
Comment: responseComment,
})
} else {
commentId, _ := strconv.ParseInt(c.Query("comment_id"), 10, 64)
//comment delete
model.DeleteCommnet(context.Background(), commentId)
c.JSON(http.StatusOK, Response{StatusCode: 0})
}
c.JSON(http.StatusOK, Response{StatusCode: 0})
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
}
}

// CommentList all videos have same demo comment list
func CommentList(c *gin.Context) {
c.JSON(http.StatusOK, CommentListResponse{
Response: Response{StatusCode: 0},
CommentList: DemoComments,
videoId, _ := strconv.ParseInt(c.Query("video_id"), 10, 64)
res, total, _ := model.QueryComment(context.Background(), videoId, 10, 0)
c.JSON(http.StatusOK, dto.CommentListResponse{
Response: dto.Response{StatusCode: 0, StatusMsg: ""},
CommentList: res,
})
fmt.Println(total)
}
27 changes: 15 additions & 12 deletions controller/common.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package controller

import "github.com/BaiZe1998/douyin-simple-demo/dto"

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

type Video struct {
Id int64 `json:"id,omitempty"`
Author User `json:"author"`
PlayUrl string `json:"play_url" json:"play_url,omitempty"`
CoverUrl string `json:"cover_url,omitempty"`
FavoriteCount int64 `json:"favorite_count,omitempty"`
CommentCount int64 `json:"comment_count,omitempty"`
IsFavorite bool `json:"is_favorite,omitempty"`
Id int64 `json:"id,omitempty"`
Author dto.User `json:"author"`
PlayUrl string `json:"play_url" json:"play_url,omitempty"`
CoverUrl string `json:"cover_url,omitempty"`
FavoriteCount int64 `json:"favorite_count,omitempty"`
CommentCount int64 `json:"comment_count,omitempty"`
IsFavorite bool `json:"is_favorite,omitempty"`
}

type Comment struct {
Id int64 `json:"id,omitempty"`
User User `json:"user"`
Content string `json:"content,omitempty"`
CreateDate string `json:"create_date,omitempty"`
Id int64 `json:"id,omitempty"`
User dto.User `json:"dto.user"`
Content string `json:"content,omitempty"`
CreateDate string `json:"create_date,omitempty"`
}

type User struct {
Id int64 `json:"id,omitempty"`
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
FollowCount int64 `json:"follow_count,omitempty"`
FollowerCount int64 `json:"follower_count,omitempty"`
Expand Down
41 changes: 28 additions & 13 deletions controller/demo_data.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
package controller

import "github.com/BaiZe1998/douyin-simple-demo/dto"

var DemoVideos = []Video{
{
Id: 1,
Author: DemoUser,
PlayUrl: "https://www.w3schools.com/html/movie.mp4",
Author: DemoUser1,
PlayUrl: "http://niuyefan.oss-cn-beijing.aliyuncs.com/douyin/bear.mp4",
CoverUrl: "https://cdn.pixabay.com/photo/2016/03/27/18/10/bear-1283347_1280.jpg",
FavoriteCount: 0,
CommentCount: 0,
FavoriteCount: 2,
CommentCount: 2,
IsFavorite: false,
},
}

var DemoComments = []Comment{
{
Id: 1,
User: DemoUser,
Content: "Test Comment",
CreateDate: "05-01",
},
}
// var DemoComments = []Comment{
// {
// Id: 1,
// User: DemoUser1,
// Content: "Test Comment",
// CreateDate: "05-01",
// },
// {
// Id: 2,
// User: DemoUser1,
// Content: "Test Commen22",
// CreateDate: "05-012",
// },
// }

var DemoUser = User{
var DemoUser1 = dto.User{
Id: 1,
Name: "TestUser",
FollowCount: 0,
FollowerCount: 0,
IsFollow: false,
}
var DemoUser = User{
Id: "1",
Name: "TestUser",
FollowCount: 0,
FollowerCount: 0,
IsFollow: false,
}
12 changes: 6 additions & 6 deletions controller/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

// FavoriteAction no practical effect, just check if token is valid
func FavoriteAction(c *gin.Context) {
token := c.Query("token")
//token := c.Query("token")

if _, exist := usersLoginInfo[token]; exist {
c.JSON(http.StatusOK, Response{StatusCode: 0})
} else {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
}
//if _, exist := usersLoginInfo[token]; exist {
// c.JSON(http.StatusOK, Response{StatusCode: 0})
//} else {
// c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
//}
}

// FavoriteList all users have same favorite video list
Expand Down
13 changes: 8 additions & 5 deletions controller/feed.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package controller

import (
"github.com/gin-gonic/gin"
"net/http"
"time"

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

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

// Feed same demo video list for every request
func Feed(c *gin.Context) {
c.JSON(http.StatusOK, FeedResponse{
Response: Response{StatusCode: 0},
Response: Response{StatusCode: 0,
StatusMsg: "",
NextTime: time.Now().Unix()},
VideoList: DemoVideos,
NextTime: time.Now().Unix(),
})
}
66 changes: 32 additions & 34 deletions controller/publish.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package controller

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"path/filepath"
)

type VideoListResponse struct {
Expand All @@ -14,38 +12,38 @@ type VideoListResponse struct {

// Publish check token then save upload file to public directory
func Publish(c *gin.Context) {
token := c.PostForm("token")

if _, exist := usersLoginInfo[token]; !exist {
c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
return
}

data, err := c.FormFile("data")
if err != nil {
c.JSON(http.StatusOK, Response{
StatusCode: 1,
StatusMsg: err.Error(),
})
return
}

filename := filepath.Base(data.Filename)
user := usersLoginInfo[token]
finalName := fmt.Sprintf("%d_%s", user.Id, filename)
saveFile := filepath.Join("./public/", finalName)
if err := c.SaveUploadedFile(data, saveFile); err != nil {
c.JSON(http.StatusOK, Response{
StatusCode: 1,
StatusMsg: err.Error(),
})
return
}

c.JSON(http.StatusOK, Response{
StatusCode: 0,
StatusMsg: finalName + " uploaded successfully",
})
//token := c.PostForm("token")
//
//if _, exist := usersLoginInfo[token]; !exist {
// c.JSON(http.StatusOK, Response{StatusCode: 1, StatusMsg: "User doesn't exist"})
// return
//}
//
//data, err := c.FormFile("data")
//if err != nil {
// c.JSON(http.StatusOK, Response{
// StatusCode: 1,
// StatusMsg: err.Error(),
// })
// return
//}
//
////filename := filepath.Base(data.Filename)
////user := usersLoginInfo[token]
//finalName := fmt.Sprintf("%d_%s", user.Id, filename)
//saveFile := filepath.Join("./public/", finalName)
//if err := c.SaveUploadedFile(data, saveFile); err != nil {
// c.JSON(http.StatusOK, Response{
// StatusCode: 1,
// StatusMsg: err.Error(),
// })
// return
//}
//
//c.JSON(http.StatusOK, Response{
// StatusCode: 0,
// StatusMsg: finalName + " uploaded successfully",
//})
}

// PublishList all users have same publish video list
Expand Down
Loading