CRUD implemented

also features better and more error messages
This commit is contained in:
AustrianToast 2024-06-10 23:53:15 +02:00
parent 3db31b6ddc
commit 7df7bc7611
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4

34
main.go
View File

@ -140,6 +140,8 @@ func finishUpload(c *gin.Context) {
} }
func listVideos(c *gin.Context) { func listVideos(c *gin.Context) {
allVideos := map[int]string{}
var err error var err error
rows, _ := dbpool.Query(context.Background(), "select * from videos") rows, _ := dbpool.Query(context.Background(), "select * from videos")
for rows.Next() { for rows.Next() {
@ -150,7 +152,7 @@ func listVideos(c *gin.Context) {
c.JSON(http.StatusInternalServerError, "") c.JSON(http.StatusInternalServerError, "")
log.Panicf("listVideos: %v\n", err) log.Panicf("listVideos: %v\n", err)
} }
fmt.Println(id, filepath) allVideos[id] = filepath
} }
err = rows.Err() err = rows.Err()
if err != nil { if err != nil {
@ -158,8 +160,7 @@ func listVideos(c *gin.Context) {
log.Panicf("listVideos: %v\n", err) log.Panicf("listVideos: %v\n", err)
} }
allVideos := gin.H{} fmt.Printf("%v\n", allVideos)
c.JSON(http.StatusOK, allVideos) c.JSON(http.StatusOK, allVideos)
} }
@ -172,15 +173,16 @@ func getVideo(c *gin.Context) {
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId) rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
rows.Next() rows.Next()
var filepath string err = rows.Err()
err = rows.Scan(&filepath)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, "") c.JSON(http.StatusInternalServerError, "")
log.Panicf("getVideo: %v\n", err) log.Panicf("getVideo: %v\n", err)
} }
err = rows.Err()
var filepath string
err = rows.Scan(&filepath)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, "") c.JSON(http.StatusBadRequest, "Video does not exist")
log.Panicf("getVideo: %v\n", err) log.Panicf("getVideo: %v\n", err)
} }
@ -195,27 +197,27 @@ func deleteVideo(c *gin.Context) {
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId) rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
rows.Next() rows.Next()
var filepath string
err = rows.Scan(&filepath)
if err != nil {
c.JSON(http.StatusBadRequest, "Video does not exist")
log.Panicf("deleteVideo: %v\n", err)
}
fmt.Println(filepath)
err = rows.Err() err = rows.Err()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, "") c.JSON(http.StatusInternalServerError, "")
log.Panicf("deleteVideo: %v\n", err) log.Panicf("deleteVideo: %v\n", err)
} }
var filepath string
err = rows.Scan(&filepath)
if err != nil {
c.JSON(http.StatusBadRequest, "Video does not exist")
log.Panicf("deleteVideo: %v\n", err)
}
_, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId) _, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, "") c.JSON(http.StatusInternalServerError, "Id was likely invalid")
log.Panicf("deleteVideo: %v\n", err) log.Panicf("deleteVideo: %v\n", err)
} }
if err = os.Remove(filepath); err != nil { if err = os.Remove(filepath); err != nil {
c.JSON(http.StatusInternalServerError, "") c.JSON(http.StatusInternalServerError, "DB entry was deleted, but file likely doesn't exist")
log.Panicf("deleteVideo: %v\n", err) log.Panicf("deleteVideo: %v\n", err)
} }