2
0

PackageReviewService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Net;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Constants;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Serialization;
  10. using ServiceStack.ServiceHost;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Class InstallPackage
  15. /// </summary>
  16. [Route("/PackageReviews/{Id}", "POST")]
  17. [Api(("Creates or updates a package review"))]
  18. public class CreateReviewRequest : IReturnVoid
  19. {
  20. /// <summary>
  21. /// Gets or sets the Id.
  22. /// </summary>
  23. /// <value>The Id.</value>
  24. [ApiMember(Name = "Id", Description = "Package Id", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "POST")]
  25. public int Id { get; set; }
  26. /// <summary>
  27. /// Gets or sets the rating.
  28. /// </summary>
  29. /// <value>The review.</value>
  30. [ApiMember(Name = "Rating", Description = "The rating value (1-5)", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "POST")]
  31. public int Rating { get; set; }
  32. /// <summary>
  33. /// Gets or sets the recommend value.
  34. /// </summary>
  35. /// <value>Whether or not this review recommends this item.</value>
  36. [ApiMember(Name = "Recommend", Description = "Whether or not this review recommends this item", IsRequired = true, DataType = "bool", ParameterType = "query", Verb = "POST")]
  37. public bool Recommend { get; set; }
  38. /// <summary>
  39. /// Gets or sets the title.
  40. /// </summary>
  41. /// <value>The title.</value>
  42. [ApiMember(Name = "Title", Description = "Optional short description of review.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  43. public string Title { get; set; }
  44. /// <summary>
  45. /// Gets or sets the full review.
  46. /// </summary>
  47. /// <value>The full review.</value>
  48. [ApiMember(Name = "Review", Description = "Optional full review.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  49. public string Review { get; set; }
  50. }
  51. /// <summary>
  52. /// Class InstallPackage
  53. /// </summary>
  54. [Route("/PackageReviews/{Id}", "GET")]
  55. [Api(("Retrieve reviews for a package"))]
  56. public class ReviewRequest : IReturn<List<PackageReviewInfo>>
  57. {
  58. /// <summary>
  59. /// Gets or sets the Id.
  60. /// </summary>
  61. /// <value>The Id.</value>
  62. [ApiMember(Name = "Id", Description = "Package Id", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
  63. public int Id { get; set; }
  64. /// <summary>
  65. /// Gets or sets the max rating.
  66. /// </summary>
  67. /// <value>The max rating.</value>
  68. [ApiMember(Name = "MaxRating", Description = "Retrieve only reviews less than or equal to this", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  69. public int MaxRating { get; set; }
  70. /// <summary>
  71. /// Gets or sets the min rating.
  72. /// </summary>
  73. /// <value>The max rating.</value>
  74. [ApiMember(Name = "MinRating", Description = "Retrieve only reviews greator than or equal to this", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  75. public int MinRating { get; set; }
  76. /// <summary>
  77. /// Only retrieve reviews with at least a short review.
  78. /// </summary>
  79. /// <value>True if should only get reviews with a title.</value>
  80. [ApiMember(Name = "ForceTitle", Description = "Whether or not to restrict results to those with a title", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  81. public bool ForceTitle { get; set; }
  82. /// <summary>
  83. /// Gets or sets the limit for the query.
  84. /// </summary>
  85. /// <value>The max rating.</value>
  86. [ApiMember(Name = "Limit", Description = "Limit the result to this many reviews (ordered by latest)", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  87. public int Limit { get; set; }
  88. }
  89. public class PackageReviewService : BaseApiService
  90. {
  91. private readonly IHttpClient _httpClient;
  92. private readonly INetworkManager _netManager;
  93. private readonly IJsonSerializer _serializer;
  94. public PackageReviewService(IHttpClient client, INetworkManager net, IJsonSerializer serializer)
  95. {
  96. _httpClient = client;
  97. _netManager = net;
  98. _serializer = serializer;
  99. }
  100. public object Get(ReviewRequest request)
  101. {
  102. var parms = "?id=" + request.Id;
  103. if (request.MaxRating > 0)
  104. {
  105. parms += "&max=" + request.MaxRating;
  106. }
  107. if (request.MinRating > 0)
  108. {
  109. parms += "&min=" + request.MinRating;
  110. }
  111. if (request.MinRating > 0)
  112. {
  113. parms += "&limit=" + request.Limit;
  114. }
  115. if (request.ForceTitle)
  116. {
  117. parms += "&title=true";
  118. }
  119. var result = _httpClient.Get(Constants.MbAdminUrl + "/service/packageReview/retrieve"+parms, CancellationToken.None).Result;
  120. var reviews = _serializer.DeserializeFromStream<List<PackageReviewInfo>>(result);
  121. return ToOptimizedResult(reviews);
  122. }
  123. public void Post(CreateReviewRequest request)
  124. {
  125. var reviewText = WebUtility.HtmlEncode(request.Review ?? string.Empty);
  126. var title = WebUtility.HtmlEncode(request.Title ?? string.Empty);
  127. var review = new Dictionary<string, string>
  128. { { "id", request.Id.ToString(CultureInfo.InvariantCulture) },
  129. { "mac", _netManager.GetMacAddress() },
  130. { "rating", request.Rating.ToString(CultureInfo.InvariantCulture) },
  131. { "recommend", request.Recommend.ToString() },
  132. { "title", title },
  133. { "review", reviewText },
  134. };
  135. Task.WaitAll(_httpClient.Post(Constants.MbAdminUrl + "/service/packageReview/update", review, CancellationToken.None));
  136. }
  137. }
  138. }