Skip to content

Commit

Permalink
create preorder sample api
Browse files Browse the repository at this point in the history
  • Loading branch information
nomi3 committed Feb 7, 2024
1 parent a707dbb commit 3cf9797
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
60 changes: 24 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
package main

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
)

// album represents data about a record album.
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}

// albums slice to seed record album data.
var albums = []album{
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
}

// getAlbums responds with the list of all albums as JSON.
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
func generatePreorderRelation(set []int) map[string]bool {
preorderRelation := make(map[string]bool)
for _, element := range set {
preorderRelation[fmt.Sprintf("[%d,%d]", element, element)] = true
for _, otherElement := range set {
if element <= otherElement {
preorderRelation[fmt.Sprintf("[%d,%d]", element, otherElement)] = true
}
}
}
return preorderRelation
}

// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
var newAlbum album
func main() {
r := gin.Default()

// Call BindJSON to bind the received JSON to
// newAlbum.
if err := c.BindJSON(&newAlbum); err != nil {
r.POST("/preorder", func(c *gin.Context) {
var set []int
if err := c.BindJSON(&set); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
}

// Add the new album to the slice.
albums = append(albums, newAlbum)
c.IndentedJSON(http.StatusCreated, newAlbum)
}
preorderRelation := generatePreorderRelation(set)
c.JSON(http.StatusOK, preorderRelation)
})

func main() {
router := gin.Default()
router.GET("/albums", getAlbums)
router.POST("/albums", postAlbums)

router.Run("localhost:8080")
}
r.Run()
}
4 changes: 2 additions & 2 deletions preorder/any_preorder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestgenerateAnyPreorderRelationInts(t *testing.T) {
func TestGenerateAnyPreorderRelationInts(t *testing.T) {
set := []interface{}{1, 2, 3}
expected := map[[2]interface{}]bool{
{1, 1}: true, {1, 2}: true, {1, 3}: true,
Expand All @@ -19,7 +19,7 @@ func TestgenerateAnyPreorderRelationInts(t *testing.T) {
}
}

func TestgenerateAnyPreorderRelationStrings(t *testing.T) {
func TestGenerateAnyPreorderRelationStrings(t *testing.T) {
set := []interface{}{"apple", "banana", "cherry"}
expected := map[[2]interface{}]bool{
{"apple", "apple"}: true, {"apple", "banana"}: true, {"apple", "cherry"}: true,
Expand Down

0 comments on commit 3cf9797

Please sign in to comment.