format rating output + ability to sort

This commit is contained in:
2015-04-16 00:14:16 +02:00
parent 420dc8c1ad
commit a01246648f
4 changed files with 67 additions and 19 deletions
+23 -14
View File
@@ -84,23 +84,32 @@ namespace OnePeek.Api
throw new ArgumentException("Please provide a valid store culture"); throw new ArgumentException("Please provide a valid store culture");
} }
string xml = await ApiHttpClient.Instance.Get(
EndpointUris.GetWindowsPhoneMetadataUri(appId, storeCulture.ToString())
);
IEnumerable<XElement> xel = XDocument.Parse(xml).Elements().First().Descendants();
// create rating
AppRating result = new AppRating(); AppRating result = new AppRating();
result.Culture = storeCulture; result.Culture = storeCulture;
result.RatingCount = Convert.ToInt32(xel.Get("userRatingCount"));
result.AverageRating = xel.GetFloat("averageUserRating");
if (Configuration.UseFiveStarSystem)
{
result.AverageRating = (float)(result.AverageRating * 0.5);
}
return result; try
{
string xml = await ApiHttpClient.Instance.Get(
EndpointUris.GetWindowsPhoneMetadataUri(appId, storeCulture.ToString())
);
IEnumerable<XElement> xel = XDocument.Parse(xml).Elements().First().Descendants();
// update rating
result.RatingCount = Convert.ToInt32(xel.Get("userRatingCount"));
result.AverageRating = xel.GetFloat("averageUserRating");
if (Configuration.UseFiveStarSystem)
{
result.AverageRating = (float)(result.AverageRating * 0.5);
}
return result;
}
catch
{
result.RatingNotAvailable = true;
return result;
}
} }
+5
View File
@@ -10,6 +10,11 @@ namespace OnePeek.Entities
/// </summary> /// </summary>
public StoreCultureType Culture { get; set; } public StoreCultureType Culture { get; set; }
/// <summary>
/// In case this is set to true, the Microsoft service didn't respond with a valid dataset to the requested culture
/// </summary>
public bool RatingNotAvailable { get; set; }
/// <summary> /// <summary>
/// Average rating of the app from 1-10 (can be changed to 1-5 with Configuration.UseFiveStarSystem). /// Average rating of the app from 1-10 (can be changed to 1-5 with Configuration.UseFiveStarSystem).
/// Is dependent on the current culture. /// Is dependent on the current culture.
+23 -1
View File
@@ -2,6 +2,7 @@
using OnePeek.Api; using OnePeek.Api;
using OnePeek.Entities; using OnePeek.Entities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.IO; using System.IO;
namespace OnePeek.WebConsole.Modules namespace OnePeek.WebConsole.Modules
@@ -34,8 +35,29 @@ namespace OnePeek.WebConsole.Modules
Get["/ratings", true] = async (ctx, token) => Get["/ratings", true] = async (ctx, token) =>
{ {
var order = Request.Query["order"];
IEnumerable<AppRating> ratings = await metaEndpoint.GetRatingsForAllCultures(Request.Query["id"], StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null); IEnumerable<AppRating> ratings = await metaEndpoint.GetRatingsForAllCultures(Request.Query["id"], StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null);
return View["Ratings", new { Ratings = ratings }];
if (order == "rating")
{
ratings = ratings.OrderByDescending(x => x.AverageRating).ThenByDescending(x => x.RatingCount);
}
else if (order == "name")
{
ratings = ratings.OrderBy(x => x.Culture);
}
else
{
order = "count";
ratings = ratings.OrderByDescending(x => x.RatingCount);
}
return View["Ratings", new
{
Order = order,
Id = Request.Query["id"],
Ratings = ratings
}];
}; };
} }
} }
+16 -4
View File
@@ -3,17 +3,29 @@
Layout = "_Layout.cshtml"; Layout = "_Layout.cshtml";
} }
ORDER BY<br />
<ul class="button-group radius">
<li><a href="/ratings?id=@(Model.Id)" class="button radius@(Model.Order == "count" ? "" : " secondary")">count</a></li>
<li><a href="/ratings?id=@(Model.Id)&order=rating" class="button radius@(Model.Order == "rating" ? "" : " secondary")">rating</a></li>
<li><a href="/ratings?id=@(Model.Id)&order=name" class="button radius@(Model.Order == "name" ? "" : " secondary")">name</a></li>
</ul>
<br />
@foreach (var rating in Model.Ratings) @foreach (var rating in Model.Ratings)
{ {
string percentage = ((float)rating.AverageRating * 20f).ToString("0.00").Replace(",", ".");
<div class="rating"> <div class="rating">
<h3>@rating.Culture</h3> <h3>@rating.Culture</h3>
<p><b>@rating.AverageRating</b> (@rating.RatingCount)</p> <p><span class="label @(rating.RatingNotAvailable ? "alert" : "success")">@(rating.RatingNotAvailable ? "ERROR" : "OK")</span> <b>@rating.AverageRating</b> (@rating.RatingCount)</p>
</div> </div>
<hr />
<div class="progress round secondary"><span class="meter" style="width:@(percentage)%;"></span></div>
} }
<style> <style>
.rating { .progress {
background: white;
border: 1px solid #ddd;
} }
</style> </style>