Added api methods to set user ratings for items

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 12 years ago
parent c1c4c85fc2
commit 2441ba0c6d

@ -0,0 +1,59 @@
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using System.ComponentModel.Composition;
using System.Net;
using System.Threading.Tasks;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Provides a handler to set a user's rating for an item
/// </summary>
[Export(typeof(BaseHandler))]
public class UserItemRatingHandler : BaseSerializationHandler<DTOUserItemData>
{
public override bool HandlesRequest(HttpListenerRequest request)
{
return ApiService.IsApiUrlMatch("UserItemRating", request);
}
protected override Task<DTOUserItemData> GetObjectToSerialize()
{
// Get the item
BaseItem item = ApiService.GetItemById(QueryString["id"]);
// Get the user
User user = ApiService.GetUserById(QueryString["userid"], true);
// Get the user data for this item
UserItemData data = item.GetUserData(user);
if (data == null)
{
data = new UserItemData();
item.AddUserData(user, data);
}
// If clearing the rating, set it to null
if (QueryString["clear"] == "1")
{
data.Rating = null;
}
// If the user's rating mode is set to like/dislike
else if (user.ItemRatingMode == ItemRatingMode.LikeOrDislike)
{
data.Likes = QueryString["likes"] == "1";
}
// If the user's rating mode is set to numeric
else if (user.ItemRatingMode == ItemRatingMode.Numeric)
{
data.Rating = float.Parse(QueryString["value"]);
}
return Task.FromResult<DTOUserItemData>(ApiService.GetDTOUserItemData(data));
}
}
}

@ -75,6 +75,7 @@
<Compile Include="HttpHandlers\StudioHandler.cs" />
<Compile Include="HttpHandlers\StudiosHandler.cs" />
<Compile Include="HttpHandlers\UserAuthenticationHandler.cs" />
<Compile Include="HttpHandlers\UserItemRatingHandler.cs" />
<Compile Include="HttpHandlers\UsersHandler.cs" />
<Compile Include="HttpHandlers\VideoHandler.cs" />
<Compile Include="HttpHandlers\WeatherHandler.cs" />

@ -398,7 +398,46 @@ namespace MediaBrowser.ApiInteraction.Portable
GetDataAsync(url, callback);
}
/// <summary>
/// Clears a user's rating for an item
/// </summary>
public void ClearUserItemRatingAsync(Guid itemId, Guid userId, Action<DTOUserItemData> callback)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&clear=1";
GetDataAsync(url, callback);
}
/// <summary>
/// Updates a user's rating for an item, based on a numeric scale
/// </summary>
public void UpdateUserItemRatingAsync(Guid itemId, Guid userId, float value, Action<DTOUserItemData> callback)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&value=" + value;
GetDataAsync(url, callback);
}
/// <summary>
/// Updates a user's rating for an item, based on likes or dislikes
/// </summary>
public void UpdateUserItemRatingAsync(Guid itemId, Guid userId, bool likes, Action<DTOUserItemData> callback)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&likes=" + (likes ? "1" : "0");
GetDataAsync(url, callback);
}
/// <summary>
/// Performs a GET request, and deserializes the response stream to an object of Type T
/// </summary>

@ -454,6 +454,54 @@ namespace MediaBrowser.ApiInteraction
}
}
/// <summary>
/// Clears a user's rating for an item
/// </summary>
public async Task<DTOUserItemData> ClearUserItemRatingAsync(Guid itemId, Guid userId)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&clear=1";
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
return DeserializeFromStream<DTOUserItemData>(stream);
}
}
/// <summary>
/// Updates a user's rating for an item, based on a numeric scale
/// </summary>
public async Task<DTOUserItemData> UpdateUserItemRatingAsync(Guid itemId, Guid userId, float value)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&value=" + value;
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
return DeserializeFromStream<DTOUserItemData>(stream);
}
}
/// <summary>
/// Updates a user's rating for an item, based on likes or dislikes
/// </summary>
public async Task<DTOUserItemData> UpdateUserItemRatingAsync(Guid itemId, Guid userId, bool likes)
{
string url = ApiUrl + "/UserItemRating?id=" + itemId;
url += "&userid=" + userId;
url += "&likes=" + (likes ? "1" : "0");
using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false))
{
return DeserializeFromStream<DTOUserItemData>(stream);
}
}
/// <summary>
/// Authenticates a user and returns the result
/// </summary>

@ -1,4 +1,5 @@
using ProtoBuf;
using MediaBrowser.Model.Entities;
using ProtoBuf;
using System;
namespace MediaBrowser.Model.DTO
@ -23,5 +24,8 @@ namespace MediaBrowser.Model.DTO
[ProtoMember(6)]
public DateTime? LastActivityDate { get; set; }
[ProtoMember(7)]
public ItemRatingMode ItemRatingMode { get; set; }
}
}

@ -17,5 +17,16 @@ namespace MediaBrowser.Model.Entities
public DateTime? LastLoginDate { get; set; }
public DateTime? LastActivityDate { get; set; }
/// <summary>
/// This allows the user to configure how they want to rate items
/// </summary>
public ItemRatingMode ItemRatingMode { get; set; }
}
public enum ItemRatingMode
{
LikeOrDislike,
Numeric
}
}

Loading…
Cancel
Save