This commit is contained in:
Techywarrior 2013-04-13 12:39:02 -07:00
commit 89ed33bbbc
7 changed files with 279 additions and 51 deletions

View File

@ -90,6 +90,7 @@
<Compile Include="UserLibrary\BaseItemsByNameService.cs" />
<Compile Include="UserLibrary\BaseItemsRequest.cs" />
<Compile Include="UserLibrary\GenresService.cs" />
<Compile Include="UserLibrary\ItemByNameUserDataService.cs" />
<Compile Include="UserLibrary\ItemsService.cs" />
<Compile Include="UserLibrary\PersonsService.cs" />
<Compile Include="UserLibrary\StudiosService.cs" />

View File

@ -9,7 +9,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api.UserLibrary
@ -203,30 +202,6 @@ namespace MediaBrowser.Api.UserLibrary
return dto;
}
/// <summary>
/// Marks the favorite.
/// </summary>
/// <param name="getItem">The get item.</param>
/// <param name="userId">The user id.</param>
/// <param name="isFavorite">if set to <c>true</c> [is favorite].</param>
/// <returns>Task.</returns>
protected async Task MarkFavorite(Func<Task<TItemType>> getItem, Guid userId, bool isFavorite)
{
var user = UserManager.GetUserById(userId);
var item = await getItem().ConfigureAwait(false);
var key = item.GetUserDataKey();
// Get the user data for this item
var data = await UserDataRepository.GetUserData(user.Id, key).ConfigureAwait(false);
// Set favorite status
data.IsFavorite = isFavorite;
await UserDataRepository.SaveUserData(user.Id, key, data, CancellationToken.None).ConfigureAwait(false);
}
}
/// <summary>

View File

@ -0,0 +1,232 @@
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Persistence;
using ServiceStack.ServiceHost;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class GetItemByNameUserData
/// </summary>
[Route("/Users/{UserId}/ItemsByName/{Name}/UserData", "GET")]
[Api(Description = "Gets user data for an item")]
public class GetItemByNameUserData : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Name { get; set; }
}
/// <summary>
/// Class MarkItemByNameFavorite
/// </summary>
[Route("/Users/{UserId}/ItemsByName/Favorites/{Name}", "POST")]
[Api(Description = "Marks something as a favorite")]
public class MarkItemByNameFavorite : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
}
/// <summary>
/// Class UnmarkItemByNameFavorite
/// </summary>
[Route("/Users/{UserId}/ItemsByName/Favorites/{Name}", "DELETE")]
[Api(Description = "Unmarks something as a favorite")]
public class UnmarkItemByNameFavorite : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
[Route("/Users/{UserId}/ItemsByName/{Name}/Rating", "POST")]
[Api(Description = "Updates a user's rating for an item")]
public class UpdateItemByNameRating : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
[ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool Likes { get; set; }
}
[Route("/Users/{UserId}/ItemsByName/{Name}/Rating", "DELETE")]
[Api(Description = "Deletes a user's saved personal rating for an item")]
public class DeleteItemByNameRating : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist, album)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
/// <summary>
/// Class ItemByNameUserDataService
/// </summary>
public class ItemByNameUserDataService : BaseApiService
{
/// <summary>
/// The user data repository
/// </summary>
protected readonly IUserDataRepository UserDataRepository;
/// <summary>
/// Initializes a new instance of the <see cref="ItemByNameUserDataService" /> class.
/// </summary>
/// <param name="userDataRepository">The user data repository.</param>
public ItemByNameUserDataService(IUserDataRepository userDataRepository)
{
UserDataRepository = userDataRepository;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetItemByNameUserData request)
{
// Get the user data for this item
var data = UserDataRepository.GetUserData(request.UserId, request.Name).Result;
return ToOptimizedResult(DtoBuilder.GetUserItemDataDto(data));
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(MarkItemByNameFavorite request)
{
var task = MarkFavorite(request.UserId, request.Name, true);
Task.WaitAll(task);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdateItemByNameRating request)
{
var task = MarkLike(request.UserId, request.Name, request.Likes);
Task.WaitAll(task);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Delete(UnmarkItemByNameFavorite request)
{
var task = MarkFavorite(request.UserId, request.Name, false);
Task.WaitAll(task);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Delete(DeleteItemByNameRating request)
{
var task = MarkLike(request.UserId, request.Name, null);
Task.WaitAll(task);
}
/// <summary>
/// Marks the favorite.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="key">The key.</param>
/// <param name="isFavorite">if set to <c>true</c> [is favorite].</param>
/// <returns>Task.</returns>
protected async Task MarkFavorite(Guid userId, string key, bool isFavorite)
{
// Get the user data for this item
var data = await UserDataRepository.GetUserData(userId, key).ConfigureAwait(false);
// Set favorite status
data.IsFavorite = isFavorite;
await UserDataRepository.SaveUserData(userId, key, data, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Marks the like.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="key">The key.</param>
/// <param name="likes">if set to <c>true</c> [likes].</param>
/// <returns>Task.</returns>
protected async Task MarkLike(Guid userId, string key, bool? likes)
{
// Get the user data for this item
var data = await UserDataRepository.GetUserData(userId, key).ConfigureAwait(false);
data.Likes = likes;
await UserDataRepository.SaveUserData(userId, key, data, CancellationToken.None).ConfigureAwait(false);
}
}
}

View File

@ -161,7 +161,7 @@ namespace MediaBrowser.Api.UserLibrary
public string Id { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating"/> is likes.
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
[ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")]

View File

@ -682,7 +682,7 @@ namespace MediaBrowser.Controller.Dto
/// <param name="data">The data.</param>
/// <returns>DtoUserItemData.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public UserItemDataDto GetUserItemDataDto(UserItemData data)
public static UserItemDataDto GetUserItemDataDto(UserItemData data)
{
if (data == null)
{

View File

@ -1724,12 +1724,12 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
};
/**
* Updates a user's favorite status for a person.
* Updates a user's favorite status for an item by name.
* @param {String} userId
* @param {String} name
* @param {Boolean} isFavorite
*/
self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
self.updateItemByNameFavoriteStatus = function (userId, name, isFavorite) {
if (!userId) {
throw new Error("null userId");
@ -1739,7 +1739,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
throw new Error("null name");
}
var url = self.getUrl("Users/" + userId + "/FavoritePersons/" + name);
var url = self.getUrl("Users/" + userId + "/ItemsByName/Favorites/" + name);
var method = isFavorite ? "POST" : "DELETE";
@ -1751,12 +1751,12 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
};
/**
* Updates a user's favorite status for a genre.
* @param {String} userId
* @param {String} name
* @param {Boolean} isFavorite
*/
self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
* Updates a user's rating for an item by name.
* @param {String} userId
* @param {String} name
* @param {Boolean} likes
*/
self.updateItemByNameRating = function (userId, name, likes) {
if (!userId) {
throw new Error("null userId");
@ -1766,24 +1766,46 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
throw new Error("null name");
}
var url = self.getUrl("Users/" + userId + "/FavoriteGenre/" + name);
var method = isFavorite ? "POST" : "DELETE";
var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/Rating", {
likes: likes
});
return self.ajax({
type: method,
type: "POST",
url: url
});
};
/**
* Clears a user's rating for an item by name.
* @param {String} userId
* @param {String} name
*/
self.clearItemByNameRating = function (userId, name) {
if (!userId) {
throw new Error("null userId");
}
if (!name) {
throw new Error("null name");
}
var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/Rating");
return self.ajax({
type: "DELETE",
url: url,
dataType: "json"
});
};
/**
* Updates a user's favorite status for a studio.
* @param {String} userId
* @param {String} name
* @param {Boolean} isFavorite
*/
self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
* Gets the full user data object for an item by name.
* @param {String} userId
* @param {String} name
*/
self.getItembyNameUserData = function (userId, name) {
if (!userId) {
throw new Error("null userId");
@ -1793,12 +1815,10 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
throw new Error("null name");
}
var url = self.getUrl("Users/" + userId + "/FavoriteStudios/" + name);
var method = isFavorite ? "POST" : "DELETE";
var url = self.getUrl("Users/" + userId + "/ItemsByName/" + name + "/UserData");
return self.ajax({
type: method,
type: "GET",
url: url,
dataType: "json"
});

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.74" targetFramework="net45" />
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.76" targetFramework="net45" />
<package id="ServiceStack.Common" version="3.9.43" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.9.43" targetFramework="net45" />
</packages>