CRUD mostly implemented

This commit is contained in:
AustrianToast 2024-06-01 13:58:16 +02:00
parent 3df71a8302
commit bc278868da
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4

96
main.go
View File

@ -25,32 +25,26 @@ func main() {
defer dbpool.Close() defer dbpool.Close()
sqlStmt := ` sqlStmt := `
DROP TABLE IF EXISTS public.videos; DROP TABLE IF EXISTS videos;
CREATE TABLE IF NOT EXISTS public.videos CREATE TABLE IF NOT EXISTS videos
( (
id serial NOT NULL, id serial NOT NULL,
filepath text, filepath text,
CONSTRAINT videos_pkey PRIMARY KEY (id) CONSTRAINT videos_pkey PRIMARY KEY (id)
) )
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.videos
OWNER to postgres;
` `
_, err = dbpool.Exec(context.Background(), sqlStmt) _, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil { if err != nil {
log.Fatalf("Unable to connection to database: %v\n", err) log.Fatalf("main: create table: %v\n", err)
} }
faker := faker.New() faker := faker.New()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
sqlStmt = fmt.Sprintf("insert into public.videos(filepath) values('%s')", faker.File().AbsoluteFilePathForUnix(2)) _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", faker.File().AbsoluteFilePathForUnix(2))
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil { if err != nil {
log.Fatalf("Unable to add videos: %v\n", err) log.Fatalf("main: fill with fake data: %v\n", err)
} }
} }
@ -61,24 +55,19 @@ func main() {
router.POST("/video/completed", ReceiveFile) router.POST("/video/completed", ReceiveFile)
router.GET("/videos", listVideos) router.GET("/videos", listVideos)
router.GET("/videos/:id", getVideo) router.GET("/videos/:id", getVideo)
router.DELETE("/videos/:id", deleteVideo)
router.Run("localhost:8080") router.Run("localhost:8080")
} }
func ReceiveChunk(c *gin.Context) { func ReceiveChunk(c *gin.Context) {
/*
get data from request
write append data to file
*/
chunk, err := io.ReadAll(c.Request.Body) chunk, err := io.ReadAll(c.Request.Body)
if err != nil { if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Unknown Error") c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
return return
} }
fileName := c.GetHeader("file-name") f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -95,23 +84,21 @@ func ReceiveChunk(c *gin.Context) {
func ReceiveFile(c *gin.Context) { func ReceiveFile(c *gin.Context) {
currentDir, err := os.Getwd() currentDir, err := os.Getwd()
if err != nil { if err != nil {
log.Fatalf("Unable to get current working dir: %v\n", err) log.Fatalf("ReceiveFile: %v\n", err)
} }
fileName, err := io.ReadAll(c.Request.Body) fileName, err := io.ReadAll(c.Request.Body)
if err != nil { if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Unknown Error") c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
return return
} }
filepath := fmt.Sprintf("%s/%s", currentDir, fileName) _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", currentDir, fileName))
sqlStmt := fmt.Sprintf("insert into public.videos(filepath) values('%s')", filepath)
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil { if err != nil {
log.Fatalf("Unable to add video: %v\n", err) log.Fatalf("ReceiveFile: %v\n", err)
} }
c.IndentedJSON(http.StatusOK, "Completed Upload") c.IndentedJSON(http.StatusOK, gin.H{}) // return list of videos
} }
func listVideos(c *gin.Context) { func listVideos(c *gin.Context) {
@ -122,14 +109,16 @@ func listVideos(c *gin.Context) {
var filepath string var filepath string
err = rows.Scan(&id, &filepath) err = rows.Scan(&id, &filepath)
if err != nil { if err != nil {
log.Fatalf("Unable to list videos: %v\n", err) log.Fatalf("listVideos: %v\n", err)
} }
fmt.Println(id, filepath) fmt.Println(id, filepath)
} }
err = rows.Err() err = rows.Err()
if err != nil { if err != nil {
log.Fatalf("Unable to list videos: %v\n", err) log.Fatalf("listVideos: %v\n", err)
} }
c.IndentedJSON(http.StatusOK, gin.H{})
} }
func getVideo(c *gin.Context) { func getVideo(c *gin.Context) {
@ -138,20 +127,59 @@ func getVideo(c *gin.Context) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
sqlStmt := fmt.Sprintf("select * from videos where id = %d", inputId)
rows, _ := dbpool.Query(context.Background(), sqlStmt) rows, _ := dbpool.Query(context.Background(), "select * from videos where id = $1", inputId)
for rows.Next() { rows.Next()
var id int var id int
var filepath string var filepath string
err = rows.Scan(&id, &filepath) err = rows.Scan(&id, &filepath)
if err != nil { if err != nil {
log.Fatalf("Unable to list videos: %v\n", err) log.Fatalf("getVideo: %v\n", err)
} }
fmt.Println(id, filepath) fmt.Println(id, filepath)
}
err = rows.Err() err = rows.Err()
if err != nil { if err != nil {
log.Fatalf("Unable to list videos: %v\n", err) log.Fatalf("getVideo: %v\n", err)
} }
c.IndentedJSON(http.StatusOK, gin.H{})
}
func deleteVideo(c *gin.Context) {
inputId, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Fatal(err)
}
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
rows.Next()
var filepath string
err = rows.Scan(&filepath)
if err != nil {
log.Printf("deleteVideo: %v\n", err)
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Video does not exist"})
return
}
fmt.Println(filepath)
err = rows.Err()
if err != nil {
log.Printf("deleteVideo: %v\n", err)
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Scanning the row didn't work"})
return
}
_, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId)
if err != nil {
log.Printf("deleteVideo: %v\n", err)
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Couldn't delete entry from DB"})
return
}
if err = os.Remove(filepath); err != nil {
log.Printf("deleteVideo: %v\n", err)
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Couldn't delete video from server"})
return
}
c.IndentedJSON(http.StatusOK, gin.H{}) // return list of videos
} }