From 09d5828812cf61ab70ca26a6992c588b1e528df4 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Mon, 4 Mar 2024 16:26:34 +0100 Subject: [PATCH] add faker added faker to create fake video entries also readded IndentedJSON, because it is more readable for now --- go.mod | 5 ++- go.sum | 2 + main.go | 124 +++++++++++++++++++++++++++++--------------------------- 3 files changed, 71 insertions(+), 60 deletions(-) diff --git a/go.mod b/go.mod index 1dc6561..ba26c35 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module api/main go 1.21.6 -require github.com/gin-gonic/gin v1.9.1 +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/jaswdr/faker v1.19.1 +) require ( github.com/bytedance/sonic v1.9.1 // indirect diff --git a/go.sum b/go.sum index e968ca4..f626efb 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jaswdr/faker v1.19.1 h1:xBoz8/O6r0QAR8eEvKJZMdofxiRH+F0M/7MU9eNKhsM= +github.com/jaswdr/faker v1.19.1/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= diff --git a/main.go b/main.go index e79f44d..340b915 100644 --- a/main.go +++ b/main.go @@ -1,122 +1,128 @@ package main import ( + "fmt" "net/http" + "strconv" "github.com/gin-gonic/gin" + "github.com/jaswdr/faker" ) -// album represents data about a record album. -type album struct { - ID string `json:"id"` +// This is taken from our DB model +type video struct { + ID int `json:"id"` Title string `json:"title"` Description string `json:"description"` Filepath string `json:"filepath"` } -// albums slice to seed record album data. -var albums = []album{ - {ID: "1", Title: "Blue Train", Description: "John Coltrane", Filepath: "56.99"}, - {ID: "2", Title: "Jeru", Description: "Gerry Mulligan", Filepath: "17.99"}, - {ID: "3", Title: "Sarah Vaughan and Clifford Brown", Description: "Sarah Vaughan", Filepath: "39.99"}, -} +// videos slice to seed record video data. +var videos []video -func remove(slice []album, s int) []album { +func remove(slice []video, s int) []video { return append(slice[:s], slice[s+1:]...) } func main() { + faker := faker.New() + + for i := 0; i < 10; i++ { + fmt.Println(i) + videos = append(videos, video{ID: i, Title: faker.RandomStringWithLength(20), Description: faker.Lorem().Sentence(50), Filepath: faker.File().AbsoluteFilePathForUnix(5)}) + } + router := gin.Default() router.SetTrustedProxies(nil) - router.GET("/albums", getAlbums) - router.GET("/albums/:id", getAlbumByID) - router.POST("/albums", postAlbums) - router.DELETE("/albums", delAlbums) - router.DELETE("/albums/:id", delAlbumByID) - router.PATCH("/albums/:id", updateAlbum) + router.GET("/videos", getvideos) + router.GET("/videos/:id", getvideoByID) + router.POST("/videos", postvideos) + router.DELETE("/videos", delvideos) + router.DELETE("/videos/:id", delvideoByID) + router.PATCH("/videos/:id", updatevideo) router.Run("localhost:8080") } -// getAlbums responds with the list of all albums as JSON. -func getAlbums(c *gin.Context) { - c.JSON(http.StatusOK, albums) +// getvideos responds with the list of all videos as JSON. +func getvideos(c *gin.Context) { + c.IndentedJSON(http.StatusOK, videos) } -// postAlbums adds an album from JSON received in the request body. -func postAlbums(c *gin.Context) { - var newAlbum album +// postvideos adds an video from JSON received in the request body. +func postvideos(c *gin.Context) { + var newvideo video // Call BindJSON to bind the received JSON to - // newAlbum. - if err := c.BindJSON(&newAlbum); err != nil { + // newvideo. + if err := c.BindJSON(&newvideo); err != nil { c.Status(http.StatusBadRequest) return } - for _, a := range albums { - if a == newAlbum { + for _, a := range videos { + if a == newvideo { c.Status(http.StatusBadRequest) return } } - // Add the new album to the slice. - albums = append(albums, newAlbum) - c.JSON(http.StatusCreated, newAlbum) + // Add the new video to the slice. + videos = append(videos, newvideo) + c.IndentedJSON(http.StatusCreated, newvideo) } -// getAlbumByID locates the album whose ID value matches the id -// parameter sent by the client, then returns that album as a response. -func getAlbumByID(c *gin.Context) { - id := c.Param("id") +// getvideoByID locates the video whose ID value matches the id +// parameter sent by the client, then returns that video as a response. +func getvideoByID(c *gin.Context) { + inputId := c.Param("id") - // Loop through the list of albums, looking for - // an album whose ID value matches the parameter. - for _, a := range albums { - if a.ID == id { - c.JSON(http.StatusOK, a) + // Loop through the list of videos, looking for + // an video whose ID value matches the parameter. + for _, a := range videos { + if strconv.Itoa(a.ID) == inputId { + c.IndentedJSON(http.StatusOK, a) return } } - c.JSON(http.StatusNotFound, gin.H{"message": "album not found"}) + c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"}) } -func delAlbums(c *gin.Context) { - albums = []album{} - c.JSON(http.StatusOK, albums) +func delvideos(c *gin.Context) { + videos = []video{} + c.IndentedJSON(http.StatusOK, videos) } -func delAlbumByID(c *gin.Context) { - id := c.Param("id") +func delvideoByID(c *gin.Context) { + inputId := c.Param("id") - for s, a := range albums { - if a.ID == id { - albums = remove(albums, s) - c.JSON(http.StatusCreated, albums) + for s, a := range videos { + if strconv.Itoa(a.ID) == inputId { + videos = remove(videos, s) + c.IndentedJSON(http.StatusCreated, videos) return } } - c.JSON(http.StatusNotFound, gin.H{"message": "album not found"}) + c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"}) } -func updateAlbum(c *gin.Context) { - id := c.Param("id") +func updatevideo(c *gin.Context) { + inputId := c.Param("id") - var newAlbum album + var newvideo video - if err := c.BindJSON(&newAlbum); err != nil { + if err := c.BindJSON(&newvideo); err != nil { return } - for s, a := range albums { - if a.ID == id { - albums = remove(albums, s) - albums = append(albums, newAlbum) - c.JSON(http.StatusCreated, newAlbum) + 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.JSON(http.StatusNotFound, gin.H{"message": "album not found"}) + c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"}) }