Búsqueda de tags coincidentes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Se requiere un paquete Nuget adicional | |
#r "Newtonsoft.Json" | |
#r "Microsoft.WindowsAzure.Storage" | |
// Espacios de nombre requeridos | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Net.Http.Headers; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using Newtonsoft.Json; | |
// La clase Analisis es la tabla que almacena los resultados del análisis | |
public class Analisis : TableEntity | |
{ | |
public string Nombre { get; set; } | |
public string Descripcion { get; set; } | |
public string Tags { get; set; } | |
} | |
public static async Task<IActionResult> Run(HttpRequest req, CloudTable inputTable, ILogger log) | |
{ | |
// El nombre de la imagen (= partition key) | |
string pk = req.Query["nombre"]; | |
// Devuelve un solo registro (partition key + row key) | |
TableOperation operacion = TableOperation.Retrieve<Analisis>(pk, "1"); | |
TableResult resultado = await inputTable.ExecuteAsync(operacion); | |
Analisis analisis = (Analisis)resultado.Result; | |
// Accedemos a los tags del registro localizado y los convertimos a lista | |
List<string> tags = analisis.Tags.Split(',').ToList(); | |
// Recorreremos toda la tabla de Analisis | |
TableContinuationToken token = null; | |
TableQuery<Analisis> query = new TableQuery<Analisis>(); | |
CancellationToken ct = default(CancellationToken); | |
// Aqui acumularemos los resultados | |
List<Analisis> imagenes = new List<Analisis>(); | |
do | |
{ | |
// El recorrido se hace por bloques | |
TableQuerySegment<Analisis> pagina = await inputTable.ExecuteQuerySegmentedAsync<Analisis>(query, token); | |
token = pagina.ContinuationToken; | |
// Por cada analisis del bloque | |
foreach(var registro in pagina) | |
{ | |
// Obtenemos la lista de tags | |
List<string> tagsRegistro = registro.Tags.Split(',').ToList(); | |
// Buscamos si coincide un tag al menos | |
// En caso afirmativo, lo mandamos a la lista de resultados | |
if (tagsRegistro.Any(tr => tags.Any(t => t == tr))) | |
imagenes.Add(registro); | |
} | |
}while (token != null && !ct.IsCancellationRequested); | |
// Se devuelve la colección de imágenes que contienen al menos un tag coincidente | |
return new OkObjectResult(imagenes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment