add faker

added faker to create fake video entries
also readded IndentedJSON, because it is more readable for now
This commit is contained in:
AustrianToast 2024-03-04 16:26:34 +01:00
parent 6171344b65
commit 09d5828812
No known key found for this signature in database
GPG Key ID: 8086574D3AAF2453
3 changed files with 71 additions and 60 deletions

5
go.mod
View File

@ -2,7 +2,10 @@ module api/main
go 1.21.6 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 ( require (
github.com/bytedance/sonic v1.9.1 // indirect github.com/bytedance/sonic v1.9.1 // indirect

2
go.sum
View File

@ -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/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/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/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 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 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= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=

124
main.go
View File

@ -1,122 +1,128 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jaswdr/faker"
) )
// album represents data about a record album. // This is taken from our DB model
type album struct { type video struct {
ID string `json:"id"` ID int `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
Filepath string `json:"filepath"` Filepath string `json:"filepath"`
} }
// albums slice to seed record album data. // videos slice to seed record video data.
var albums = []album{ var videos []video
{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"},
}
func remove(slice []album, s int) []album { func remove(slice []video, s int) []video {
return append(slice[:s], slice[s+1:]...) return append(slice[:s], slice[s+1:]...)
} }
func main() { 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 := gin.Default()
router.SetTrustedProxies(nil) router.SetTrustedProxies(nil)
router.GET("/albums", getAlbums) router.GET("/videos", getvideos)
router.GET("/albums/:id", getAlbumByID) router.GET("/videos/:id", getvideoByID)
router.POST("/albums", postAlbums) router.POST("/videos", postvideos)
router.DELETE("/albums", delAlbums) router.DELETE("/videos", delvideos)
router.DELETE("/albums/:id", delAlbumByID) router.DELETE("/videos/:id", delvideoByID)
router.PATCH("/albums/:id", updateAlbum) router.PATCH("/videos/:id", updatevideo)
router.Run("localhost:8080") router.Run("localhost:8080")
} }
// getAlbums responds with the list of all albums as JSON. // getvideos responds with the list of all videos as JSON.
func getAlbums(c *gin.Context) { func getvideos(c *gin.Context) {
c.JSON(http.StatusOK, albums) c.IndentedJSON(http.StatusOK, videos)
} }
// postAlbums adds an album from JSON received in the request body. // postvideos adds an video from JSON received in the request body.
func postAlbums(c *gin.Context) { func postvideos(c *gin.Context) {
var newAlbum album var newvideo video
// Call BindJSON to bind the received JSON to // Call BindJSON to bind the received JSON to
// newAlbum. // newvideo.
if err := c.BindJSON(&newAlbum); err != nil { if err := c.BindJSON(&newvideo); err != nil {
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
return return
} }
for _, a := range albums { for _, a := range videos {
if a == newAlbum { if a == newvideo {
c.Status(http.StatusBadRequest) c.Status(http.StatusBadRequest)
return return
} }
} }
// Add the new album to the slice. // Add the new video to the slice.
albums = append(albums, newAlbum) videos = append(videos, newvideo)
c.JSON(http.StatusCreated, newAlbum) c.IndentedJSON(http.StatusCreated, newvideo)
} }
// getAlbumByID locates the album whose ID value matches the id // getvideoByID locates the video whose ID value matches the id
// parameter sent by the client, then returns that album as a response. // parameter sent by the client, then returns that video as a response.
func getAlbumByID(c *gin.Context) { func getvideoByID(c *gin.Context) {
id := c.Param("id") inputId := c.Param("id")
// Loop through the list of albums, looking for // Loop through the list of videos, looking for
// an album whose ID value matches the parameter. // an video whose ID value matches the parameter.
for _, a := range albums { for _, a := range videos {
if a.ID == id { if strconv.Itoa(a.ID) == inputId {
c.JSON(http.StatusOK, a) c.IndentedJSON(http.StatusOK, a)
return 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) { func delvideos(c *gin.Context) {
albums = []album{} videos = []video{}
c.JSON(http.StatusOK, albums) c.IndentedJSON(http.StatusOK, videos)
} }
func delAlbumByID(c *gin.Context) { func delvideoByID(c *gin.Context) {
id := c.Param("id") inputId := c.Param("id")
for s, a := range albums { for s, a := range videos {
if a.ID == id { if strconv.Itoa(a.ID) == inputId {
albums = remove(albums, s) videos = remove(videos, s)
c.JSON(http.StatusCreated, albums) c.IndentedJSON(http.StatusCreated, videos)
return 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) { func updatevideo(c *gin.Context) {
id := c.Param("id") 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 return
} }
for s, a := range albums { for s, a := range videos {
if a.ID == id { if strconv.Itoa(a.ID) == inputId {
albums = remove(albums, s) videos = remove(videos, s)
albums = append(albums, newAlbum) videos = append(videos, newvideo)
c.JSON(http.StatusCreated, newAlbum) c.IndentedJSON(http.StatusCreated, newvideo)
return return
} }
} }
c.JSON(http.StatusNotFound, gin.H{"message": "album not found"}) c.IndentedJSON(http.StatusNotFound, gin.H{"message": "video not found"})
} }