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

106
main.go
View File

@ -25,32 +25,26 @@ func main() {
defer dbpool.Close()
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,
filepath text,
CONSTRAINT videos_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.videos
OWNER to postgres;
`
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil {
log.Fatalf("Unable to connection to database: %v\n", err)
log.Fatalf("main: create table: %v\n", err)
}
faker := faker.New()
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(), sqlStmt)
_, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", faker.File().AbsoluteFilePathForUnix(2))
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.GET("/videos", listVideos)
router.GET("/videos/:id", getVideo)
router.DELETE("/videos/:id", deleteVideo)
router.Run("localhost:8080")
}
func ReceiveChunk(c *gin.Context) {
/*
get data from request
write append data to file
*/
chunk, err := io.ReadAll(c.Request.Body)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Unknown Error")
c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
return
}
fileName := c.GetHeader("file-name")
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
@ -95,23 +84,21 @@ func ReceiveChunk(c *gin.Context) {
func ReceiveFile(c *gin.Context) {
currentDir, err := os.Getwd()
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)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, "Unknown Error")
c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
return
}
filepath := fmt.Sprintf("%s/%s", currentDir, fileName)
sqlStmt := fmt.Sprintf("insert into public.videos(filepath) values('%s')", filepath)
_, err = dbpool.Exec(context.Background(), sqlStmt)
_, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", currentDir, fileName))
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) {
@ -122,14 +109,16 @@ func listVideos(c *gin.Context) {
var filepath string
err = rows.Scan(&id, &filepath)
if err != nil {
log.Fatalf("Unable to list videos: %v\n", err)
log.Fatalf("listVideos: %v\n", err)
}
fmt.Println(id, filepath)
}
err = rows.Err()
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) {
@ -138,20 +127,59 @@ func getVideo(c *gin.Context) {
if err != nil {
log.Fatal(err)
}
sqlStmt := fmt.Sprintf("select * from videos where id = %d", inputId)
rows, _ := dbpool.Query(context.Background(), sqlStmt)
for rows.Next() {
var id int
var filepath string
err = rows.Scan(&id, &filepath)
if err != nil {
log.Fatalf("Unable to list videos: %v\n", err)
}
fmt.Println(id, filepath)
rows, _ := dbpool.Query(context.Background(), "select * from videos where id = $1", inputId)
rows.Next()
var id int
var filepath string
err = rows.Scan(&id, &filepath)
if err != nil {
log.Fatalf("getVideo: %v\n", err)
}
fmt.Println(id, filepath)
err = rows.Err()
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
}