| | | 1 | | using System; |
| | | 2 | | using System.Web; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 10 | | using Swashbuckle.AspNetCore.Annotations; |
| | | 11 | | using Microsoft.Extensions.Options; |
| | | 12 | | using SVETA.Api.Data.DTO; |
| | | 13 | | using Microsoft.AspNetCore.Authorization; |
| | | 14 | | using SVETA.Api.Data.Domain; |
| | | 15 | | using SVETA.Api.Services.Interfaces; |
| | | 16 | | |
| | | 17 | | |
| | | 18 | | namespace SVETA.Api.Controllers |
| | | 19 | | { |
| | | 20 | | [Route("api/v1/Feeds")] |
| | | 21 | | [ApiController] |
| | | 22 | | public class FeedsController : SvetaController |
| | | 23 | | { |
| | | 24 | | private readonly ILogger<FeedsController> _logger; |
| | | 25 | | private readonly IFeedsWorker _feedWorker; |
| | | 26 | | private readonly FeedsSettings _feedSettings; |
| | | 27 | | private readonly ConfigurationsSettings _confsSettings; |
| | | 28 | | |
| | | 29 | | public FeedsController( ILogger<FeedsController> logger, IFeedsWorker feedWorker, |
| | 0 | 30 | | IOptions<FeedsSettings> feedSettings, IOptions<ConfigurationsSettings> confsSettings) : base(logger) |
| | 0 | 31 | | { |
| | 0 | 32 | | _feedSettings = feedSettings.Value; |
| | 0 | 33 | | _confsSettings = confsSettings.Value; |
| | 0 | 34 | | _logger = logger; |
| | 0 | 35 | | _feedWorker = feedWorker; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Возвращает фид по товарам для Яндекса |
| | | 40 | | /// </summary> |
| | | 41 | | /// <remarks>author: i.rebenok</remarks> |
| | | 42 | | /// <returns>xml файл</returns> |
| | | 43 | | [HttpGet("Yandex")] |
| | | 44 | | [SwaggerResponse(302, "Успешно", typeof(RedirectResult))] |
| | | 45 | | [SwaggerResponse(429, "Слишком много запросов", typeof(EmptyResult))] |
| | | 46 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 47 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 48 | | public IActionResult GetFeedForYandex() |
| | 0 | 49 | | { |
| | 0 | 50 | | string filename = "YandexFeed.xml"; |
| | 0 | 51 | | if (DateTime.UtcNow.Subtract( |
| | 0 | 52 | | DateTime.TryParse(_confsSettings.GetConfValue("FeedsSettings", "YandexTimestamp"), out DateTime dt) ? dt |
| | 0 | 53 | | .TotalMinutes < (int.TryParse(_confsSettings.GetConfValue("FeedsSettings", "FrequencyTime"), out int res |
| | 0 | 54 | | return StatusCode(429); |
| | 0 | 55 | | var result = _feedWorker.CreateFeedForYandex(filename).Result; |
| | 0 | 56 | | _confsSettings.SetConfValue("FeedsSettings", "YandexTimestamp",DateTime.UtcNow.ToString()); |
| | 0 | 57 | | return Redirect(result); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Возвращает фид по товарам для Гугла |
| | | 62 | | /// </summary> |
| | | 63 | | /// <remarks>author: i.rebenok</remarks> |
| | | 64 | | /// <returns>xml файл</returns> |
| | | 65 | | [HttpGet("Google")] |
| | | 66 | | [SwaggerResponse(200, "Успешно", typeof(FileResult))] |
| | | 67 | | [SwaggerResponse(429, "Слишком много запросов", typeof(EmptyResult))] |
| | | 68 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 69 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 70 | | public IActionResult GetFeedForGoogle() |
| | 0 | 71 | | { |
| | 0 | 72 | | string filename = "GoogleFeed.txt"; |
| | 0 | 73 | | var result = _feedWorker.CreateFeedForGoogle(filename).Result; |
| | 0 | 74 | | Response.Headers.Add("Content-Disposition", $"attachment;filename={filename}.gz"); |
| | 0 | 75 | | return File(result, System.Net.Mime.MediaTypeNames.Application.Zip); |
| | 0 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |