PackageReviewService.cs 6.2 KB

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