2
0

PackageReviewService.cs 6.3 KB

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