diff --git a/dao/db.go b/dao/db.go index be010a2..58e97a0 100644 --- a/dao/db.go +++ b/dao/db.go @@ -8,7 +8,7 @@ import ( var DB *gorm.DB func Init() { - dsn := "video:dLPmjweadG625DtH@tcp(114.115.206.93:3306)/video?charset=utf8mb4&parseTime=True&loc=Local" + dsn := "video_t:SDssrzALGiidPcjE@tcp(127.0.0.1:3306)/video_t?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { diff --git a/dao/device.go b/dao/device.go index ac4d199..07254bd 100644 --- a/dao/device.go +++ b/dao/device.go @@ -1,8 +1,8 @@ package dao import ( + "fmt" "gorm.io/gorm" - "time" ) type Device struct { @@ -14,14 +14,13 @@ type Device struct { DeviceLocation string `gorm:"column:device_location"` DeviceIP string `gorm:"column:device_ip"` DeviceInfo string `gorm:"column:device_info"` - CreatedAt time.Time - UpdatedAt time.Time } func CreateDevice(authID int, deviceName, deviceType string, deviceStatus, deviceLocation, deviceIP, deviceInfo string) uint { device := Device{AuthID: authID, DeviceName: deviceName, DeviceType: deviceType, DeviceStatus: deviceStatus, DeviceLocation: deviceLocation, DeviceIP: deviceIP, DeviceInfo: deviceInfo} result := DB.Debug().Create(&device) if result.Error != nil { + fmt.Println("CreateDevice failed", result.Error) return 0 } return device.ID diff --git a/dao/logger.go b/dao/logger.go index 07a0cc0..4bf1ddf 100644 --- a/dao/logger.go +++ b/dao/logger.go @@ -1 +1,5 @@ package dao + +type Logger struct { + ID uint `gorm:"primarykey"` +} diff --git a/dao/video.go b/dao/video.go index cb5ee3d..4b8937a 100644 --- a/dao/video.go +++ b/dao/video.go @@ -1,9 +1,12 @@ package dao -import "time" +import ( + "gorm.io/gorm" + "time" +) type Video struct { - ID int `gorm:"primaryKey column:id"` + gorm.Model CameraID int `gorm:"column:camera_id"` VideoPath string `gorm:"column:video_path"` VideoName string `gorm:"column:video_name"` @@ -16,15 +19,18 @@ type Video struct { FileSize int `gorm:"column:file_size"` } -func CreateVideo(videoPath, videoName string, authID, human, isDelete int, createTime, endTime string, fileSize int) int { +func CreateVideo(videoPath, videoName string, authID, human, isDelete int, createTime, endTime string, fileSize int) uint { video := Video{VideoPath: videoPath, VideoName: videoName, AuthId: authID, Human: human, IsDelete: isDelete, CreateTime: createTime, EndTime: endTime, FileSize: fileSize} - DB.Create(&video) + res := DB.Debug().Create(&video) + if res.Error != nil { + return 0 + } return video.ID } func DeleteVideoByID(id, user int) int { delete_time := time.Now().Format("2006-01-02 15:04:05") - DB.Updates(&Video{IsDelete: 1, DeleteTime: delete_time}).Where("id = ? and auth_id = ?", id, user) + DB.Debug().Where("id = ? and auth_id = ?", id, user).Delete(&Video{DeleteTime: delete_time, IsDelete: 1}) return id } diff --git a/handler/video.go b/handler/video.go index 7765c16..032b45d 100644 --- a/handler/video.go +++ b/handler/video.go @@ -38,6 +38,10 @@ type videoPReq struct { IP string `json:"ip"` } +type VideoDelReq struct { + ID int `json:"id"` +} + // video延迟视频请求 type delayReq struct { ID int `json:"id"` @@ -85,8 +89,13 @@ func GetVideo(c *gin.Context) { } func CreateVideo(c *gin.Context) { var video_req videoReq + user_id, _ := c.Get("id") if err := c.ShouldBindJSON(&video_req); err == nil { - id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, video_req.AuthId, video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.FileSize) + id := service.CreateVideo(video_req.VideoPath, video_req.VideoName, int(user_id.(float64)), video_req.Human, video_req.IsDelete, video_req.CreateTime, video_req.EndTime, video_req.FileSize) + if id == 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "create video failed", "code": 1, "message": "failed"}) + return + } c.JSON(http.StatusOK, gin.H{"id": id, "code": 0, "message": "success"}) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": 1, "message": "failed"}) @@ -116,7 +125,7 @@ func DelayVideo(c *gin.Context) { } func DeleteVideo(c *gin.Context) { - var video_req videoPReq + var video_req VideoDelReq id, _ := c.Get("id") if err := c.ShouldBindJSON(&video_req); err == nil { service.DeleteVideo(video_req.ID, int(id.(float64))) diff --git a/main.go b/main.go index f6fe30e..4c8d816 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ func main() { handler.SetUpVideoGroup(r) handler.SetUpUserGroup(r) handler.SetUpDeviceGroup(r) - r.Run(":8082") // listen and serve on 0.0.0.0:8082 + r.Run(":8083") // listen and serve on 0.0.0.0:8082 defer dao.Close() defer worker.CloseRedis() } diff --git a/service/videoService.go b/service/videoService.go index d7ff97b..37d98d0 100644 --- a/service/videoService.go +++ b/service/videoService.go @@ -20,14 +20,13 @@ func GetVideoList(auth_id int, start, end string) []dao.Video { } return dao.FindVideoListByTime(auth_id, start, end) } - } func DelayVideo(id, auth_id, day int) { dao.DelayVideo(id, auth_id, day) } -func CreateVideo(videoPath, videoName string, authID, human, isDelete int, createTime, endTime string, fileSize int) int { +func CreateVideo(videoPath, videoName string, authID, human, isDelete int, createTime, endTime string, fileSize int) uint { return dao.CreateVideo(videoPath, videoName, authID, human, isDelete, createTime, endTime, fileSize) } diff --git a/worker/redis.go b/worker/redis.go index 9dce23a..4f0c6b8 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -17,8 +17,8 @@ func InitRedis() { // 连接redis redisClient = redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", // Redis 服务器地址 - Password: "", // 如果 Redis 设置了密码 - DB: 0, // 使用的数据库编号 + Password: "lj502138", // 如果 Redis 设置了密码 + DB: 2, // 使用的数据库编号 }) // 验证 Redis 客户端是否可以正常工作