diff --git a/offers/functions.go b/offers/functions.go index 906baea..7ebe2d4 100644 --- a/offers/functions.go +++ b/offers/functions.go @@ -1,24 +1,40 @@ package offers -import "time" +import ( + "strconv" -type Offer struct { - Id string `json:"id"` - Description string `json:"description"` - Store string `json:"store"` - MaxClaims int `json:"maxClaims"` - CreatedAt time.Time `json:"createdAt"` -} + "github.com/jaavier/sider" +) func FindOffer(offerId string) Offer { - return Offer{} + _, err := sider.Get("offer:id:" + offerId) + if err != nil { + return Offer{} + } + description, _ := sider.Get("offer:description:" + offerId) + store, _ := sider.Get("offer:store:" + offerId) + maxClaims, _ := sider.Get("offer:maxClaims:" + offerId) + maxClaimsInt, _ := strconv.Atoi(maxClaims) + // createdAt, _ := sider.Get("offer:createdAt:" + offerId) + return Offer{ + Description: description, + Store: store, + MaxClaims: maxClaimsInt, + } } func CreateOffer(offer Offer) bool { - return true + if offer.MaxClaims > 0 { + return true + } else { + return false + } } -func DeleteOffer(offerId) bool { - return true +func DeleteOffer(offerId string) bool { + if len(offerId) > 0 { + return true + } else { + return false + } } - diff --git a/offers/handlers.go b/offers/handlers.go index 72fdffa..cdfb49b 100644 --- a/offers/handlers.go +++ b/offers/handlers.go @@ -1,14 +1,32 @@ package offers -import "github.com/labstack/echo/v4" +import ( + "encoding/json" + "time" + + "github.com/labstack/echo/v4" +) + +type Offer struct { + Id string `json:"id"` + Description string `json:"description"` + Store string `json:"store"` + MaxClaims int `json:"maxClaims"` + CreatedAt time.Time `json:"createdAt"` +} func Get(c echo.Context) error { return c.String(200, "GET OFFER") } func Post(c echo.Context) error { - return c.String(200, "CREATE OFFER") - + var offer Offer + json.NewDecoder(c.Request().Body).Decode(&offer) + if CreateOffer(offer) { + return c.String(200, "Offer created successfully") + } else { + return c.String(400, "Error creating offer") + } } func Delete(c echo.Context) error {