diff --git a/VkNet.Tests/Categories/Polls/GetBackgroundsTest.cs b/VkNet.Tests/Categories/Polls/GetBackgroundsTest.cs new file mode 100644 index 000000000..6fb16b78e --- /dev/null +++ b/VkNet.Tests/Categories/Polls/GetBackgroundsTest.cs @@ -0,0 +1,32 @@ +using NUnit.Framework; +using VkNet.Enums.SafetyEnums; +using VkNet.Tests.Infrastructure; + +namespace VkNet.Tests.Categories.Polls +{ + [TestFixture] + public class GetBackgroundsTest : CategoryBaseTest + { + protected override string Folder => "Polls"; + + [Test] + public void GetBackgrounds() + { + Url = "https://api.vk.com/method/polls.getBackgrounds"; + + ReadCategoryJsonPath(nameof(Api.PollsCategory.GetBackgrounds)); + + var result = Api.PollsCategory.GetBackgrounds(); + + Assert.That(result[0].Type, Is.TypeOf()); + Assert.That(result[0].Angle, Is.EqualTo("225")); + Assert.That(result[0].Points[0].Color, Is.EqualTo("f24973")); + Assert.That(result[0].Points[0].Position, Is.EqualTo(0)); + + Assert.That(result[1].Type, Is.TypeOf()); + Assert.That(result[1].Angle, Is.EqualTo("180")); + Assert.That(result[1].Points[1].Color, Is.EqualTo("2f733f")); + Assert.That(result[1].Points[1].Position, Is.EqualTo(1)); + } + } +} \ No newline at end of file diff --git a/VkNet.Tests/Categories/Polls/GetPhotoUploadServerTest.cs b/VkNet.Tests/Categories/Polls/GetPhotoUploadServerTest.cs new file mode 100644 index 000000000..b4527f5b2 --- /dev/null +++ b/VkNet.Tests/Categories/Polls/GetPhotoUploadServerTest.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using VkNet.Tests.Infrastructure; + +namespace VkNet.Tests.Categories.Polls +{ + [TestFixture] + public class GetPhotoUploadServerTest : CategoryBaseTest + { + protected override string Folder => "Polls"; + + [Test] + public void GetPhotoUploadServer() + { + Url = "https://api.vk.com/method/polls.getPhotoUploadServer"; + + ReadCategoryJsonPath(nameof(Api.PollsCategory.GetPhotoUploadServer)); + + var result = Api.PollsCategory.GetPhotoUploadServer(450138623); + + Assert.IsNotEmpty(result.UploadUrl); + } + } +} \ No newline at end of file diff --git a/VkNet.Tests/Categories/Polls/PollsSavePhotoTest.cs b/VkNet.Tests/Categories/Polls/PollsSavePhotoTest.cs new file mode 100644 index 000000000..939613a7c --- /dev/null +++ b/VkNet.Tests/Categories/Polls/PollsSavePhotoTest.cs @@ -0,0 +1,31 @@ +using NUnit.Framework; +using VkNet.Model.RequestParams; +using VkNet.Tests.Infrastructure; + +namespace VkNet.Tests.Categories.Polls +{ + [TestFixture] + public class PollsSavePhotoTest : CategoryBaseTest + { + protected override string Folder => "Polls"; + + [Test] + public void PollsSavePhoto() + { + Url = "https://api.vk.com/method/polls.savePhoto"; + + ReadCategoryJsonPath(nameof(Api.PollsCategory.SavePhoto)); + + var result = Api.PollsCategory.SavePhoto(new SavePhotoParams + { + Photo = "242344", + Hash = "fe8f7aaa03ff650cc2" + }); + + Assert.That(result.Id, Is.EqualTo(457245390)); + Assert.That(result.Color, Is.EqualTo("BE272E")); + Assert.That(result.Images[0].Height, Is.EqualTo(600)); + Assert.IsNotEmpty(result.Images[0].Url.ToString()); + } + } +} \ No newline at end of file diff --git a/VkNet.Tests/TestData/Categories/Polls/GetBackgrounds.json b/VkNet.Tests/TestData/Categories/Polls/GetBackgrounds.json new file mode 100644 index 000000000..02607d4ec --- /dev/null +++ b/VkNet.Tests/TestData/Categories/Polls/GetBackgrounds.json @@ -0,0 +1,29 @@ +{ + "response": [{ + "angle": 225, + "color": "6248cb", + "id": 1, + "name": "фон в тёплых тонах", + "points": [{ + "color": "f24973", + "position": 0 + }, { + "color": "3948e6", + "position": 1 + }], + "type": "gradient" + }, { + "angle": 180, + "color": "4b8642", + "id": 2, + "name": "зелёный фон", + "points": [{ + "color": "679945", + "position": 0 + }, { + "color": "2f733f", + "position": 1 + }], + "type": "gradient" + }] +} \ No newline at end of file diff --git a/VkNet.Tests/TestData/Categories/Polls/GetPhotoUploadServer.json b/VkNet.Tests/TestData/Categories/Polls/GetPhotoUploadServer.json new file mode 100644 index 000000000..48b74cb42 --- /dev/null +++ b/VkNet.Tests/TestData/Categories/Polls/GetPhotoUploadServer.json @@ -0,0 +1,5 @@ +{ + "response": { + "upload_url": "https://pu.vk.com...jYjEwNzRjMTY4ZTIifQ" + } +} \ No newline at end of file diff --git a/VkNet.Tests/TestData/Categories/Polls/SavePhoto.json b/VkNet.Tests/TestData/Categories/Polls/SavePhoto.json new file mode 100644 index 000000000..8a9675809 --- /dev/null +++ b/VkNet.Tests/TestData/Categories/Polls/SavePhoto.json @@ -0,0 +1,15 @@ +{ + "response": { + "color": "BE272E", + "id": 457245390, + "images": [{ + "height": 600, + "url": "https://sun9-79.userapi.com/impf/M1JmC0VPcPYhRKB-emOIZwNX3ERNBdhpXHZBzw/gabphYGgM_Y.jpg", + "width": 1001 + }, { + "height": 200, + "url": "https://sun9-79.userapi.com/impf/M1JmC0VPcPYhRKB-emOIZwNX3ERNBdhpXHZBzw/gabphYGgM_Y.jpg", + "width": 510 + }] + } +} \ No newline at end of file diff --git a/VkNet/Abstractions/Category/Async/IPollsCategoryAsync.cs b/VkNet/Abstractions/Category/Async/IPollsCategoryAsync.cs index 9c4c708c6..f178eb246 100644 --- a/VkNet/Abstractions/Category/Async/IPollsCategoryAsync.cs +++ b/VkNet/Abstractions/Category/Async/IPollsCategoryAsync.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using System.Collections.ObjectModel; using VkNet.Model; using VkNet.Model.Attachments; using VkNet.Model.RequestParams; @@ -86,5 +87,40 @@ public interface IPollsCategoryAsync /// Страница документации ВКонтакте https://vk.com/dev/polls.create /// Task CreateAsync(PollsCreateParams @params); + + /// + /// Получает варианты фонового изображения для опросов. + /// + /// + /// Возвращает массив объектов, описывающих фоновое изображение опроса. + /// + /// + /// Страница документации ВКонтакте https://vk.com/dev/polls.getBackgrounds + /// + Task> GetBackgroundsAsync(); + + /// + /// Получает адрес сервера для загрузки фоновой фотографии в опрос. + /// + /// + /// + /// Возвращает объект с полем содержащим URL для загрузки фотографии + /// + /// + /// Страница документации ВКонтакте http://vk.com/dev.php?method=polls.getPhotoUploadServer + /// + Task GetPhotoUploadServerAsync(long ownerId); + + /// + /// Сохраняет фотографию, загруженную в опрос. + /// + /// + /// + /// В случае успешного сохранения возвращает объект описывающий фотографию + /// + /// + /// Страница документации ВКонтакте http://vk.com/dev/polls.savePhoto + /// + public Task SavePhotoAsync(SavePhotoParams @params); } } \ No newline at end of file diff --git a/VkNet/Abstractions/Category/IPollsCategory.cs b/VkNet/Abstractions/Category/IPollsCategory.cs index e9c2612b9..3576d888f 100644 --- a/VkNet/Abstractions/Category/IPollsCategory.cs +++ b/VkNet/Abstractions/Category/IPollsCategory.cs @@ -1,4 +1,5 @@ -using VkNet.Model; +using System.Collections.ObjectModel; +using VkNet.Model; using VkNet.Model.Attachments; using VkNet.Model.RequestParams; using VkNet.Model.RequestParams.Polls; @@ -26,5 +27,14 @@ public interface IPollsCategory : IPollsCategoryAsync /// Poll Create(PollsCreateParams @params); + + /// + ReadOnlyCollection GetBackgrounds(); + + /// + PhotoUploadServer GetPhotoUploadServer(long ownerId); + + /// + SavePhotoResult SavePhoto(SavePhotoParams @params); } } \ No newline at end of file diff --git a/VkNet/Categories/Async/PollsCategoryAsync.cs b/VkNet/Categories/Async/PollsCategoryAsync.cs index 81ce870c1..7852e9e49 100644 --- a/VkNet/Categories/Async/PollsCategoryAsync.cs +++ b/VkNet/Categories/Async/PollsCategoryAsync.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using System.Collections.ObjectModel; using VkNet.Model; using VkNet.Model.Attachments; using VkNet.Model.RequestParams; @@ -13,37 +14,55 @@ public partial class PollsCategory /// public Task GetByIdAsync(PollsGetByIdParams @params) { - return TypeHelper.TryInvokeMethodAsync(func: () =>GetById(@params: @params)); + return TypeHelper.TryInvokeMethodAsync(func: () => GetById(@params: @params)); } /// public Task EditAsync(PollsEditParams @params) { - return TypeHelper.TryInvokeMethodAsync(func: () =>Edit(@params: @params)); + return TypeHelper.TryInvokeMethodAsync(func: () => Edit(@params: @params)); } /// public Task AddVoteAsync(PollsAddVoteParams @params) { - return TypeHelper.TryInvokeMethodAsync(func: () =>AddVote(@params: @params)); + return TypeHelper.TryInvokeMethodAsync(func: () => AddVote(@params: @params)); } /// public Task DeleteVoteAsync(PollsDeleteVoteParams @params) { - return TypeHelper.TryInvokeMethodAsync(func: () =>DeleteVote(@params: @params)); + return TypeHelper.TryInvokeMethodAsync(func: () => DeleteVote(@params: @params)); } /// public Task> GetVotersAsync(PollsGetVotersParams @params) { - return TypeHelper.TryInvokeMethodAsync(func: () =>GetVoters(@params: @params)); + return TypeHelper.TryInvokeMethodAsync(func: () => GetVoters(@params: @params)); } /// public Task CreateAsync(PollsCreateParams @params) { - return TypeHelper.TryInvokeMethodAsync(func: () =>Create(@params: @params)); + return TypeHelper.TryInvokeMethodAsync(func: () => Create(@params: @params)); + } + + /// + public Task> GetBackgroundsAsync() + { + return TypeHelper.TryInvokeMethodAsync(func: () => GetBackgrounds()); + } + + /// + public Task GetPhotoUploadServerAsync(long ownerId) + { + return TypeHelper.TryInvokeMethodAsync(func: () => GetPhotoUploadServer(ownerId)); + } + + /// + public Task SavePhotoAsync(SavePhotoParams @params) + { + return TypeHelper.TryInvokeMethodAsync(func: () => SavePhoto(@params: @params)); } } } \ No newline at end of file diff --git a/VkNet/Categories/PollsCategory.cs b/VkNet/Categories/PollsCategory.cs index 438bb7508..07c10c36b 100644 --- a/VkNet/Categories/PollsCategory.cs +++ b/VkNet/Categories/PollsCategory.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Text; using VkNet.Abstractions; using VkNet.Model; @@ -129,5 +130,30 @@ public Poll Create(PollsCreateParams @params) { "background_id", @params.BackgroundId } }); } + + /// + public ReadOnlyCollection GetBackgrounds() + { + return _vk.Call>("polls.getBackgrounds", + new VkParameters()); + } + + /// + public PhotoUploadServer GetPhotoUploadServer(long ownerId) + { + return _vk.Call("polls.getPhotoUploadServer", + new VkParameters { {"owner_id", ownerId} }); + } + + /// + public SavePhotoResult SavePhoto(SavePhotoParams @params) + { + return _vk.Call("polls.savePhoto", + new VkParameters + { + { "photo", @params.Photo }, + { "hash", @params.Hash } + }); + } } } \ No newline at end of file diff --git a/VkNet/Model/RequestParams/Polls/SavePhotoParams.cs b/VkNet/Model/RequestParams/Polls/SavePhotoParams.cs new file mode 100644 index 000000000..21a7abb7f --- /dev/null +++ b/VkNet/Model/RequestParams/Polls/SavePhotoParams.cs @@ -0,0 +1,22 @@ +using System; +using Newtonsoft.Json; + +namespace VkNet.Model.RequestParams +{ + /// + /// Параметры для метода SavePhoto + /// + [Serializable] + public class SavePhotoParams + { + /// + /// Строка полученная в результате загрузки фотографии. + /// + public string Photo { get; set; } + + /// + /// Хеш полученный в результате загрузки фотографии. + /// + public string Hash { get; set; } + } +} \ No newline at end of file diff --git a/VkNet/Model/Results/Polls/GetBackgroundsResult.cs b/VkNet/Model/Results/Polls/GetBackgroundsResult.cs new file mode 100644 index 000000000..ca54a3c31 --- /dev/null +++ b/VkNet/Model/Results/Polls/GetBackgroundsResult.cs @@ -0,0 +1,79 @@ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using VkNet.Enums.SafetyEnums; +using VkNet.Model.Attachments; +using VkNet.Utils; +using VkNet.Utils.JsonConverter; + +namespace VkNet.Model +{ + /// + /// Результат метода GetBackgrounds + /// + [Serializable] + public class GetBackgroundsResult + { + /// + /// Тип фона. + /// + [JsonProperty("type")] + [JsonConverter(typeof(SafetyEnumJsonConverter))] + public PollBackgroundType Type { get; set; } + + /// + /// Угол градиента по оси X. + /// + [JsonProperty("angle")] + public string Angle { get; set; } + + /// + /// HEX-код замещающего цвета (без #). + /// + [JsonProperty("color")] + public string Color { get; set; } + + /// + /// Ширина плитки паттерна. + /// + [JsonProperty("width")] + public long Width { get; set; } + + /// + /// Высота плитки паттерна. + /// + [JsonProperty("height")] + public long Height { get; set; } + + /// + /// Точки градиента. + /// + [JsonProperty("points")] + public PollBackgroundPoint[] Points { get; set; } + + /// + /// Идентификатор фона. + /// + [JsonProperty("id")] + public long Id { get; set; } + + /// + /// Разобрать из Json + /// + /// Ответ сервераю + /// + public static GetBackgroundsResult FromJson(VkResponse response) + { + return new GetBackgroundsResult + { + Type = response["type"], + Angle = response["angle"], + Color = response["color"], + Width = response["width"], + Height = response["height"], + Points = response["points"].ToListOf(x => x).ToArray(), + Id = response["id"] + }; + } + } +} \ No newline at end of file diff --git a/VkNet/Model/Results/Polls/PhotoUploadServer.cs b/VkNet/Model/Results/Polls/PhotoUploadServer.cs new file mode 100644 index 000000000..42aaecb7f --- /dev/null +++ b/VkNet/Model/Results/Polls/PhotoUploadServer.cs @@ -0,0 +1,32 @@ +using System; +using Newtonsoft.Json; +using VkNet.Utils; + +namespace VkNet.Model +{ + /// + /// Результат метода GetPhotoUploadServer + /// + [Serializable] + public class PhotoUploadServer + { + /// + /// Идентификатор владельца опроса. + /// + [JsonProperty("upload_url")] + public string UploadUrl { get; set; } + + /// + /// Разобрать из Json + /// + /// + /// + public static PhotoUploadServer FromJson(VkResponse response) + { + return new PhotoUploadServer + { + UploadUrl = response["upload_url"] + }; + } + } +} \ No newline at end of file diff --git a/VkNet/Model/Results/Polls/SavePhotoResult.cs b/VkNet/Model/Results/Polls/SavePhotoResult.cs new file mode 100644 index 000000000..0ae29de93 --- /dev/null +++ b/VkNet/Model/Results/Polls/SavePhotoResult.cs @@ -0,0 +1,41 @@ +using System; +using Newtonsoft.Json; +using VkNet.Utils; + +namespace VkNet.Model +{ + /// + /// Результат метода SavePhoto + /// + [Serializable] + public class SavePhotoResult + { + /// + /// + /// + [JsonProperty("color")] + public string Color { get; set; } + + /// + /// Идентификатор загруженной фотографии + /// + [JsonProperty("id")] + public long Id { get; set; } + + /// + /// Массив изображений разных размеров + /// + [JsonProperty("images")] + public Image[] Images { get; set; } + + public static SavePhotoResult FromJson(VkResponse response) + { + return new SavePhotoResult + { + Color = response["color"], + Id = response["id"], + Images = response["images"].ToListOf(x => x).ToArray() + }; + } + } +} \ No newline at end of file