can now create and read
This commit is contained in:
parent
cb437f3bb6
commit
3df71a8302
180
main.go
180
main.go
@ -14,17 +14,11 @@ import (
|
|||||||
"github.com/jaswdr/faker"
|
"github.com/jaswdr/faker"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This is taken from our DB model
|
var dbpool *pgxpool.Pool
|
||||||
type video struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Filepath string `json:"filepath"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// videos slice to seed record video data.
|
|
||||||
var videos []video
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dbpool, err := pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2:5432/postgres")
|
var err error
|
||||||
|
dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2:5432/postgres")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -35,7 +29,7 @@ func main() {
|
|||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS public.videos
|
CREATE TABLE IF NOT EXISTS public.videos
|
||||||
(
|
(
|
||||||
id integer NOT NULL,
|
id serial NOT NULL,
|
||||||
filepath text,
|
filepath text,
|
||||||
CONSTRAINT videos_pkey PRIMARY KEY (id)
|
CONSTRAINT videos_pkey PRIMARY KEY (id)
|
||||||
)
|
)
|
||||||
@ -53,70 +47,42 @@ func main() {
|
|||||||
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(id, filepath) values(%d, '%s')", i, faker.File().AbsoluteFilePathForUnix(2))
|
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(), sqlStmt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to add videos: %v\n", err)
|
log.Fatalf("Unable to add videos: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, _ := dbpool.Query(context.Background(), "select * from videos")
|
|
||||||
for rows.Next() {
|
|
||||||
var id int
|
|
||||||
var name string
|
|
||||||
err = rows.Scan(&id, &name)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Unable to list videos: %v\n", err)
|
|
||||||
}
|
|
||||||
fmt.Println(id, name)
|
|
||||||
}
|
|
||||||
err = rows.Err()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Unable to list videos: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Legacy
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
videos = append(videos, video{ID: i, Filepath: faker.File().AbsoluteFilePathForUnix(5)})
|
|
||||||
}
|
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.SetTrustedProxies(nil)
|
router.SetTrustedProxies(nil)
|
||||||
|
|
||||||
router.POST("/upload", ReceiveFile)
|
router.POST("/video", ReceiveChunk)
|
||||||
router.GET("/videos", getvideos)
|
router.POST("/video/completed", ReceiveFile)
|
||||||
router.GET("/videos/:id", getvideoByID)
|
router.GET("/videos", listVideos)
|
||||||
router.POST("/videos", postvideos)
|
router.GET("/videos/:id", getVideo)
|
||||||
router.DELETE("/videos", delvideos)
|
|
||||||
router.DELETE("/videos/:id", delvideoByID)
|
|
||||||
router.PATCH("/videos/:id", updatevideo)
|
|
||||||
|
|
||||||
router.Run("localhost:8080")
|
router.Run("localhost:8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
func remove(slice []video, s int) []video {
|
func ReceiveChunk(c *gin.Context) {
|
||||||
return append(slice[:s], slice[s+1:]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReceiveFile(c *gin.Context) {
|
|
||||||
// maybe even re-verify mime-type (before or after upload)
|
|
||||||
|
|
||||||
fileName := c.GetHeader("file-name")
|
|
||||||
/*
|
/*
|
||||||
get data from request
|
get data from request
|
||||||
write append data to file
|
write append data to file
|
||||||
*/
|
*/
|
||||||
data, 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, "Unknown Error")
|
||||||
return
|
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(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if _, err := f.Write(data); err != nil {
|
if _, err := f.Write(chunk); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := f.Close(); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
@ -126,84 +92,66 @@ func ReceiveFile(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusOK, "Received chunk")
|
c.IndentedJSON(http.StatusOK, "Received chunk")
|
||||||
}
|
}
|
||||||
|
|
||||||
// getvideos responds with the list of all videos as JSON.
|
func ReceiveFile(c *gin.Context) {
|
||||||
func getvideos(c *gin.Context) {
|
currentDir, err := os.Getwd()
|
||||||
c.IndentedJSON(http.StatusOK, videos)
|
if err != nil {
|
||||||
}
|
log.Fatalf("Unable to get current working dir: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
// postvideos adds an video from JSON received in the request body.
|
fileName, err := io.ReadAll(c.Request.Body)
|
||||||
func postvideos(c *gin.Context) {
|
if err != nil {
|
||||||
var newvideo video
|
c.IndentedJSON(http.StatusBadRequest, "Unknown Error")
|
||||||
|
|
||||||
// Call BindJSON to bind the received JSON to
|
|
||||||
// newvideo.
|
|
||||||
if err := c.BindJSON(&newvideo); err != nil {
|
|
||||||
c.Status(http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, a := range videos {
|
filepath := fmt.Sprintf("%s/%s", currentDir, fileName)
|
||||||
if a == newvideo {
|
sqlStmt := fmt.Sprintf("insert into public.videos(filepath) values('%s')", filepath)
|
||||||
c.Status(http.StatusBadRequest)
|
_, err = dbpool.Exec(context.Background(), sqlStmt)
|
||||||
return
|
if err != nil {
|
||||||
}
|
log.Fatalf("Unable to add video: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the new video to the slice.
|
c.IndentedJSON(http.StatusOK, "Completed Upload")
|
||||||
videos = append(videos, newvideo)
|
|
||||||
c.IndentedJSON(http.StatusCreated, newvideo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getvideoByID locates the video whose ID value matches the id
|
func listVideos(c *gin.Context) {
|
||||||
// parameter sent by the client, then returns that video as a response.
|
var err error
|
||||||
func getvideoByID(c *gin.Context) {
|
rows, _ := dbpool.Query(context.Background(), "select * from videos")
|
||||||
inputId := c.Param("id")
|
for rows.Next() {
|
||||||
|
var id int
|
||||||
// Loop through the list of videos, looking for
|
var filepath string
|
||||||
// an video whose ID value matches the parameter.
|
err = rows.Scan(&id, &filepath)
|
||||||
for _, a := range videos {
|
if err != nil {
|
||||||
if strconv.Itoa(a.ID) == inputId {
|
log.Fatalf("Unable to list videos: %v\n", err)
|
||||||
c.IndentedJSON(http.StatusOK, a)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
fmt.Println(id, filepath)
|
||||||
|
}
|
||||||
|
err = rows.Err()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to list videos: %v\n", err)
|
||||||
}
|
}
|
||||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func delvideos(c *gin.Context) {
|
func getVideo(c *gin.Context) {
|
||||||
videos = []video{}
|
var err error
|
||||||
c.IndentedJSON(http.StatusOK, videos)
|
inputId, err := strconv.Atoi(c.Param("id"))
|
||||||
}
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
func delvideoByID(c *gin.Context) {
|
}
|
||||||
inputId := c.Param("id")
|
sqlStmt := fmt.Sprintf("select * from videos where id = %d", inputId)
|
||||||
|
|
||||||
for s, a := range videos {
|
rows, _ := dbpool.Query(context.Background(), sqlStmt)
|
||||||
if strconv.Itoa(a.ID) == inputId {
|
for rows.Next() {
|
||||||
videos = remove(videos, s)
|
var id int
|
||||||
c.IndentedJSON(http.StatusCreated, videos)
|
var filepath string
|
||||||
return
|
err = rows.Scan(&id, &filepath)
|
||||||
}
|
if err != nil {
|
||||||
}
|
log.Fatalf("Unable to list videos: %v\n", err)
|
||||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"})
|
}
|
||||||
}
|
fmt.Println(id, filepath)
|
||||||
|
}
|
||||||
func updatevideo(c *gin.Context) {
|
err = rows.Err()
|
||||||
inputId := c.Param("id")
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to list videos: %v\n", err)
|
||||||
var newvideo video
|
}
|
||||||
|
|
||||||
if err := c.BindJSON(&newvideo); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for s, a := range videos {
|
|
||||||
if strconv.Itoa(a.ID) == inputId {
|
|
||||||
videos = remove(videos, s)
|
|
||||||
videos = append(videos, newvideo)
|
|
||||||
c.IndentedJSON(http.StatusCreated, newvideo)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"})
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user