forked from muety/wakapi
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config options and load items from hc api
- Loading branch information
1 parent
f7cf28c
commit 820d218
Showing
12 changed files
with
175 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sort" | ||
"time" | ||
|
||
"github.com/kcoderhtml/hackatime/config" | ||
"github.com/kcoderhtml/hackatime/models" | ||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
type ShopService struct { | ||
config *config.Config | ||
cache *cache.Cache | ||
} | ||
|
||
func NewShopService() *ShopService { | ||
return &ShopService{ | ||
config: config.Get(), | ||
cache: cache.New(6*time.Hour, 6*time.Hour), | ||
} | ||
} | ||
|
||
type HackClubProduct struct { | ||
Name string `json:"name"` | ||
SmallName string `json:"smallName"` | ||
Description string `json:"description"` | ||
Hours int `json:"hours"` | ||
ImageURL string `json:"imageURL"` | ||
Stock *int `json:"stock"` // Change to pointer to allow null | ||
} | ||
|
||
func (srv *ShopService) GetProducts() ([]*models.Product, error) { | ||
// Check if products are in cache | ||
if cachedProducts, found := srv.cache.Get("products"); found { | ||
return cachedProducts.([]*models.Product), nil | ||
} | ||
|
||
// Fetch products from Hack Club API | ||
resp, err := http.Get("https://hackclub.com/api/arcade/shop/") | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching products: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var hackClubProducts []HackClubProduct | ||
if err := json.NewDecoder(resp.Body).Decode(&hackClubProducts); err != nil { | ||
return nil, fmt.Errorf("error decoding products: %v", err) | ||
} | ||
|
||
formattedProducts := []*models.Product{} | ||
|
||
for i, product := range hackClubProducts { | ||
description := product.Description | ||
if description == "" { | ||
description = product.SmallName | ||
} | ||
|
||
stock := -1 | ||
if product.Stock != nil { | ||
stock = *product.Stock | ||
} | ||
|
||
formattedProducts = append(formattedProducts, &models.Product{ | ||
ID: uint(i + 1), // Use index + 1 as ID | ||
Name: product.Name, | ||
Description: description, | ||
Price: product.Hours, | ||
Stock: stock, | ||
Image: product.ImageURL, | ||
}) | ||
} | ||
|
||
// Sort products by price | ||
sort.Slice(formattedProducts, func(i, j int) bool { | ||
return formattedProducts[i].Price < formattedProducts[j].Price | ||
}) | ||
|
||
// Cache the sorted formatted products | ||
srv.cache.Set("products", formattedProducts, cache.DefaultExpiration) | ||
|
||
return formattedProducts, nil | ||
} | ||
|
||
func (srv *ShopService) ClearProductsCache() { | ||
srv.cache.Delete("products") | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters