2
0

PackageReviewService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Serialization;
  6. using ServiceStack;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.Net;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api
  13. {
  14. /// <summary>
  15. /// Class InstallPackage
  16. /// </summary>
  17. [Route("/Packages/Reviews/{Id}", "POST", Summary = "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("/Packages/{Id}/Reviews", "GET", Summary = "Gets 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. [Authenticated]
  89. public class PackageReviewService : BaseApiService
  90. {
  91. private readonly IHttpClient _httpClient;
  92. private readonly IJsonSerializer _serializer;
  93. private const string MbAdminUrl = "https://www.mb3admin.com/admin/";
  94. private readonly IServerApplicationHost _appHost;
  95. public PackageReviewService(IHttpClient httpClient, IJsonSerializer serializer, IServerApplicationHost appHost)
  96. {
  97. _httpClient = httpClient;
  98. _serializer = serializer;
  99. _appHost = appHost;
  100. }
  101. public async Task<object> Get(ReviewRequest request)
  102. {
  103. var parms = "?id=" + request.Id;
  104. if (request.MaxRating > 0)
  105. {
  106. parms += "&max=" + request.MaxRating;
  107. }
  108. if (request.MinRating > 0)
  109. {
  110. parms += "&min=" + request.MinRating;
  111. }
  112. if (request.MinRating > 0)
  113. {
  114. parms += "&limit=" + request.Limit;
  115. }
  116. if (request.ForceTitle)
  117. {
  118. parms += "&title=true";
  119. }
  120. using (var result = await _httpClient.Get(MbAdminUrl + "/service/packageReview/retrieve" + parms, CancellationToken.None)
  121. .ConfigureAwait(false))
  122. {
  123. var reviews = _serializer.DeserializeFromStream<List<PackageReviewInfo>>(result);
  124. return ToOptimizedResult(reviews);
  125. }
  126. }
  127. public void Post(CreateReviewRequest request)
  128. {
  129. var reviewText = WebUtility.HtmlEncode(request.Review ?? string.Empty);
  130. var title = WebUtility.HtmlEncode(request.Title ?? string.Empty);
  131. var review = new Dictionary<string, string>
  132. { { "id", request.Id.ToString(CultureInfo.InvariantCulture) },
  133. { "mac", _appHost.SystemId },
  134. { "rating", request.Rating.ToString(CultureInfo.InvariantCulture) },
  135. { "recommend", request.Recommend.ToString() },
  136. { "title", title },
  137. { "review", reviewText },
  138. };
  139. Task.WaitAll(_httpClient.Post(MbAdminUrl + "/service/packageReview/update", review, CancellationToken.None));
  140. }
  141. }
  142. }