Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #768 from OpenBazaar/ratings
Browse files Browse the repository at this point in the history
Add get all ratings functionality to GET ratings endpoint
  • Loading branch information
cpacia authored Dec 4, 2017
2 parents 596a7c9 + 4a2e1b2 commit 0c14c63
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 208 deletions.
60 changes: 48 additions & 12 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3172,38 +3172,74 @@ func (i *jsonAPIHandler) GETRatings(w http.ResponseWriter, r *http.Request) {
urlPath, slug := path.Split(r.URL.Path)
_, peerId := path.Split(urlPath[:len(urlPath)-1])

if peerId == "ratings" {
peerId = slug
slug = ""
}

var indexBytes []byte
if peerId != i.node.IpfsNode.Identity.Pretty() {
indexBytes, _ = ipfs.ResolveThenCat(i.node.Context, ipnspath.FromString(path.Join(peerId, "ratings.json")))

} else {
indexBytes, _ = ioutil.ReadFile(path.Join(i.node.RepoPath, "root", "ratings.json"))
}
var rating *core.SavedRating
if indexBytes == nil {
rating = new(core.SavedRating)
rating.Slug = slug
rating := new(core.SavedRating)
rating.Ratings = []string{}
} else {
var ratingList []core.SavedRating
err := json.Unmarshal(indexBytes, &ratingList)
ret, err := json.MarshalIndent(rating, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
SanitizedResponse(w, string(ret))
return
}

var ratingList []core.SavedRating
err := json.Unmarshal(indexBytes, &ratingList)
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

if slug != "" {
rating := new(core.SavedRating)
for _, r := range ratingList {
if r.Slug == slug {
rating = &r
break
}
}
ret, err := json.MarshalIndent(rating, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
SanitizedResponse(w, string(ret))
} else {
type resp struct {
Count int `json:"count"`
Average float32 `json:"average"`
Ratings []string `json:"ratings"`
}
ratingRet := new(resp)
total := float32(0)
count := 0
for _, r := range ratingList {
total += r.Average * float32(r.Count)
count += r.Count
ratingRet.Ratings = append(ratingRet.Ratings, r.Ratings...)
}
ratingRet.Count = count
ratingRet.Average = total / float32(count)
ret, err := json.MarshalIndent(ratingRet, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
SanitizedResponse(w, string(ret))
}
ret, err := json.MarshalIndent(rating, "", " ")
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
SanitizedResponse(w, string(ret))
}

func (i *jsonAPIHandler) GETRating(w http.ResponseWriter, r *http.Request) {
Expand Down
13 changes: 13 additions & 0 deletions core/confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ func (n *OpenBazaarNode) NewOrderConfirmation(contract *pb.RicardianContract, ad
metadata.ListingSlug = listing.Slug
metadata.ModeratorKey = contract.BuyerOrder.Payment.ModeratorKey

if contract.BuyerOrder.Version > 0 {
metadata.ListingTitle = listing.Item.Title
if len(listing.Item.Images) > 0 {
metadata.Thumbnail = &pb.RatingSignature_TransactionMetadata_Image{
Tiny: listing.Item.Images[0].Tiny,
Small: listing.Item.Images[0].Small,
Medium: listing.Item.Images[0].Medium,
Large: listing.Item.Images[0].Large,
Original: listing.Item.Images[0].Original,
}
}
}

ser, err := proto.Marshal(metadata)
if err != nil {
return nil, err
Expand Down
20 changes: 18 additions & 2 deletions core/fulfillment.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func (n *OpenBazaarNode) FulfillOrder(fulfillment *pb.OrderFulfillment, contract
fulfillment.Payout = payout
}
var keyIndex int
for i, listing := range contract.VendorListings {
if listing.Slug == fulfillment.Slug {
var listing *pb.Listing
for i, list := range contract.VendorListings {
if list.Slug == fulfillment.Slug {
keyIndex = i
listing = list
break
}
}
Expand All @@ -108,6 +110,20 @@ func (n *OpenBazaarNode) FulfillOrder(fulfillment *pb.OrderFulfillment, contract
metadata := new(pb.RatingSignature_TransactionMetadata)
metadata.RatingKey = contract.BuyerOrder.RatingKeys[keyIndex]
metadata.ListingSlug = fulfillment.Slug

if contract.BuyerOrder.Version > 0 {
metadata.ListingTitle = listing.Item.Title
if len(listing.Item.Images) > 0 {
metadata.Thumbnail = &pb.RatingSignature_TransactionMetadata_Image{
Tiny: listing.Item.Images[0].Tiny,
Small: listing.Item.Images[0].Small,
Medium: listing.Item.Images[0].Medium,
Large: listing.Item.Images[0].Large,
Original: listing.Item.Images[0].Original,
}
}
}

ser, err := proto.Marshal(metadata)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions core/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func (n *OpenBazaarNode) Purchase(data *PurchaseData) (orderId string, paymentAd
func (n *OpenBazaarNode) createContractWithOrder(data *PurchaseData) (*pb.RicardianContract, error) {
contract := new(pb.RicardianContract)
order := new(pb.Order)
order.Version = 1
if data.RefundAddress != nil {
order.RefundAddress = *(data.RefundAddress)
} else {
Expand Down
Loading

0 comments on commit 0c14c63

Please sign in to comment.