db

package
v0.0.0-...-49721f6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

  • @Author: Sangs

  • @Date: 2023-08-25

  • @LastEditTime: 2023-09-02

  • @Description:

  • Description: 用于执行跟视频有关的数据库操作;定义了视频的数据结构。

  • Func: 已经实现的函数

  • InsertVideo() --- 插入视频信息进入mysql

  • Author: sangs

  • Date: 2023-08-22

Index

Constants

This section is empty.

Variables

View Source
var DB *gorm.DB

Functions

func DeleteComment

func DeleteComment(user_id int64, comment_id int64, video_id int64) (int64, error)

func FindUser2Login

func FindUser2Login(ctx context.Context, username string, password string) (int64, error)
  • Description: 该函数进行用户查找,输入参数为用户名和密码。
  • 由于只是单表简单查找操作,所以不需要使用事务
  • 使用idx_username索引,针对用户名进行查找对应的id和密码
  • 对密码使用安全比较,避免时序攻击
  • Retn: user.ID 自增的用户id,err 用于判定插入过程是否存在错误
  • -1 err表示没有处理的错误
  • -2 err表示密码错误或用户名有误
  • Author: sangs
  • Date: 2023-08-19

func GetIsFavor

func GetIsFavor(current_id int64, video_id int64) (bool, error)

func GetUserFanNum

func GetUserFanNum(userid int64) (follower_count int64, err error)

func GetUserFavorite

func GetUserFavorite(userid int64) (favorite_count int64, err error)

func GetUserFollowNum

func GetUserFollowNum(userid int64) (follow_count int64, err error)

func GetUserTotalF

func GetUserTotalF(userid int64) (total_favorite int64, err error)

func GetUserVideosNum

func GetUserVideosNum(userid int64) (work_count int64, err error)
  • Description: 通过用户id用户发布的视频个数 以后尽量还是使用redis实现,这样的查找在用户个数很多的时候往往会出问题
  • Retn: user.ID 自增的用户id,err 用于判定插入过程是否存在错误
  • -1 err表示查找过程出问题了
  • Author: sangs
  • Date: 2023-08-25

func GetVideoCommentNum

func GetVideoCommentNum(videoid int64) (comment_count int64, err error)

func GetVideoFavoriteNum

func GetVideoFavoriteNum(videoid int64) (favorite_count int64, err error)

func GiveALike

func GiveALike(user_id int64, video_id int64) (int64, error)

func GiveAUnlike

func GiveAUnlike(user_id int64, video_id int64) (int64, error)

func InsertUser

func InsertUser(ctx context.Context, username string, password string) (int64, error)
  • Description: 如果完成了所有的安全性检查,则可以调用该函数进行新用户的创建,输入参数为用户名和密码。
  • 通过User结构将其绑定为user变量上。
  • 设置了username的唯一索引,所以不需要使用NoUserWithName()判断是否存在当前用户名了
  • 直接判断是否是1062号错误,即可判断出是否存在重复用户名。
  • 通过返回id为-2,这样在外面就不需要再调用mysql包来判断错误类型
  • 由于只是单表简单操作,所以不需要使用事务
  • Retn: user.ID 自增的用户id,err 用于判定插入过程是否存在错误
  • -1 err表示没有处理的错误
  • -2 err表示username出现重复
  • Author: sangs
  • Date: 2023-08-13

func InsertVideo

func InsertVideo(userid int64, title string, videoURL string, coverURL string, pubtime int64) error

* Description: 根据所有相关的信息,将视频数据插入videos库中 * Author: sangs * Date: 2023-08-22

func IsFollow

func IsFollow(current_id int64, find_id int64) (bool, error)

func PostComment

func PostComment(user_id int64, video_id int64, comment_text string, t int64) (int64, error)

Types

type Comment

type Comment struct {
	ID      int64
	UserID  int64
	VideoID int64
	Content string
	Comtime int64
}

func GetCommentAboutVideo

func GetCommentAboutVideo(video_id int64) (comments []Comment, err error)

func (Comment) TableName

func (Comment) TableName() string

type Favorite

type Favorite struct {
	ID      int64
	UserID  int64
	VideoID int64
}

func FavoriteVideosWithUserID

func FavoriteVideosWithUserID(user_id int64) (fv []Favorite, err error)

func (Favorite) TableName

func (Favorite) TableName() string

type Relationship

type Relationship struct {
	ID       int64
	UserID   int64
	FollowID int64
	Status   bool
}

func (Relationship) TableName

func (Relationship) TableName() string

type User

type User struct {
	ID              int64  // 用户id
	Username        string // 用户名 唯一
	Password        string // 密码
	Avatar          string `gorm:"default:'-'"`             // 默认头像路径
	BackgroundImage string `gorm:"default:'-'"`             // 默认背景路径
	Signature       string `gorm:"default:'这个人比较懒,什么都没有写'"` // 默认简介
}

* Description: 定义单个User的结构体(GORM会自动使用字段名的蛇形寻找列名,所以可以跟数据库对应上) * 实际上真正可以修改的只有用户名和密码,头像、背景图、简介貌似都不能修改,只能查看。可以在一开始创建的时候就随机 * Author: sangs * Date: 2023-08-13

func GetUserByID

func GetUserByID(ctx context.Context, id int64) (*User, error)
  • Description: 通过用户id查找所有的信息
  • 由于只是单表简单查找操作,所以不需要使用事务
  • 使用idx_username索引,针对用户名进行查找对应的id和密码
  • 对密码使用安全比较,避免时序攻击
  • Retn: user.ID 自增的用户id,err 用于判定插入过程是否存在错误
  • -1 err表示没有处理的错误
  • -2 err表示密码错误或用户名有误
  • Author: sangs
  • Date: 2023-08-21

func (User) TableName

func (User) TableName() string

GORM中将结构体User<结构体名>映射到数据库中对应的users表<表名>中

type Video

type Video struct {
	ID       int64  // 视频id
	AuthorID int64  // 作者id 唯一
	Title    string // 视频标题
	VideoURL string // 视频路径
	CoverURL string // 封面路径
	Pubtime  int64  // 发布时间
}

* Description: 定义单个Video的结构体(GORM会自动使用字段名的蛇形寻找列名,所以可以跟数据库对应上) * Author: sangs * Date: 2023-08-22

func GetVideoBeforeTime

func GetVideoBeforeTime(last_time int64) (videos []Video, err error)

func GetVideoByAuthor2Redis

func GetVideoByAuthor2Redis(userid int64) (videos []Video, err error)

* Description: 根据用户id,将该用户的所有视频信息存入redis,为了后面统计视频数据,以及倒序退出做准备 * Author: sangs * Date: 2023-08-25

func GetVideoByID

func GetVideoByID(video_id int64) (videos []Video, err error)

func (Video) TableName

func (Video) TableName() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL