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

Yc project struct design #1

Merged
merged 6 commits into from
Jun 5, 2022
Merged
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
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模型
89 changes: 89 additions & 0 deletions db/douyin.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Navicat Premium Data Transfer

Source Server : 127.0.0.1
Source Server Type : MySQL
Source Server Version : 80029
Source Host : 127.0.0.1:3306
Source Schema : douyin

Target Server Type : MySQL
Target Server Version : 80029
File Encoding : 65001

Date: 05/06/2022 19:34:13
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for comment
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL COMMENT '用户id',
`video_id` datetime DEFAULT NULL COMMENT '视频id',
`content` text CHARACTER SET utf8mb3 COLLATE utf8_general_ci COMMENT '内容',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Table structure for favorite
-- ----------------------------
DROP TABLE IF EXISTS `favorite`;
CREATE TABLE `favorite` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL COMMENT '用户id',
`video_id` int DEFAULT NULL COMMENT '视频id',
`status` tinyint DEFAULT NULL COMMENT '点赞状态',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
`updated_at` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Table structure for follow
-- ----------------------------
DROP TABLE IF EXISTS `follow`;
CREATE TABLE `follow` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL COMMENT '用户id',
`followed_user` int DEFAULT NULL COMMENT '被关注者id',
`status` tinyint DEFAULT '1' COMMENT '关注状态',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
`updated_at` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8_general_ci DEFAULT NULL COMMENT '姓名',
`follow_count` int DEFAULT NULL COMMENT '关注数',
`follower_count` int DEFAULT NULL COMMENT '粉丝数',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

-- ----------------------------
-- Table structure for video
-- ----------------------------
DROP TABLE IF EXISTS `video`;
CREATE TABLE `video` (
`id` int NOT NULL AUTO_INCREMENT,
`author_id` int DEFAULT NULL COMMENT '作者id',
`play_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8_general_ci DEFAULT NULL COMMENT '播放路径',
`cover_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8_general_ci DEFAULT NULL COMMENT '封面路径',
`favorite_count` int DEFAULT NULL COMMENT '喜欢数',
`comment_count` int DEFAULT NULL COMMENT '评论数',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

SET FOREIGN_KEY_CHECKS = 1;
10 changes: 10 additions & 0 deletions db/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package db

import (
"github.com/RaymondCode/simple-demo/db/model"
)

// Init init db
func Init() {
model.Init() // mysql
}
13 changes: 13 additions & 0 deletions db/model/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model

import "time"

type Comment struct {
ID int64 `gorm:"primarykey"`
UserId int64
VideoId int64
Content string
Status int
CreatedAt time.Time
UpdatedAt time.Time
}
12 changes: 12 additions & 0 deletions db/model/favorite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

import "time"

type Favorite struct {
ID int64 `gorm:"primarykey"`
UserId int64
VideoId int64
Status int
CreatedAt time.Time
UpdatedAt time.Time
}
59 changes: 59 additions & 0 deletions db/model/follow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package model

import (
"context"
"gorm.io/gorm"
"time"
)

type Follow struct {
ID int64 `gorm:"primarykey"`
UserId int64
FollowedUser int64
Status int
CreatedAt time.Time
UpdatedAt time.Time
}

// CreateFollow create follow info
func CreateFollow(ctx context.Context, follow *Follow) error {
if err := DB.Table("follow").WithContext(ctx).Create(follow).Error; err != nil {
return err
}
return nil
}

// UpdateFollow update follow info
func UpdateFollow(ctx context.Context, userID, followedUser uint, status *int) error {
params := map[string]interface{}{}
if status != nil {
params["status"] = *status
}
return DB.Table("follow").WithContext(ctx).Model(&Follow{}).Where("user_id = ? and followed_user = ?", userID, followedUser).
Updates(params).Error
}

// DeleteFollow delete follow info
func DeleteFollow(ctx context.Context, userID uint, followedUser uint) error {
return DB.Table("follow").WithContext(ctx).Where("user_id = ? and followed_user = ? ", userID, followedUser).Delete(&Follow{}).Error
}

// QueryFollow query list of note info
func QueryFollow(ctx context.Context, userID int64, status, limit, offset int) ([]*Follow, int64, error) {
var total int64
var res []*Follow
var conn *gorm.DB
// query for followed users
if status == 1 {
conn = DB.Table("follow").WithContext(ctx).Model(&Follow{}).Where("user_id = ?", userID)
} else { // query for followers
conn = DB.Table("follow").WithContext(ctx).Model(&Follow{}).Where("followed_user = ?", userID)
}
if err := conn.Count(&total).Error; err != nil {
return res, total, err
}
if err := conn.Limit(limit).Offset(offset).Find(&res).Error; err != nil {
return res, total, err
}
return res, total, nil
}
23 changes: 23 additions & 0 deletions db/model/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package model

import (
"github.com/RaymondCode/simple-demo/pkg/constants"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

var DB *gorm.DB

// Init init DB
func Init() {
var err error
DB, err = gorm.Open(mysql.Open(constants.MySQLDefaultDSN),
&gorm.Config{
PrepareStmt: true, // executes the given query in cached statement
SkipDefaultTransaction: true, // disable default transaction
},
)
if err != nil {
panic(err)
}
}
11 changes: 11 additions & 0 deletions db/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

import "time"

type User struct {
ID uint `gorm:"primarykey"`
Name string
FollowCount int64
FollowerCount int64
CreatedAt time.Time
}
15 changes: 15 additions & 0 deletions db/model/video.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import (
"time"
)

type Video struct {
ID int64 `gorm:"primarykey"`
AuthorID int64
PlayUrl string
CoverUrl string
FavoriteCount int64
CommentCount int64
CreatedAt time.Time
}
Loading