public IActionResult GuardarCategorias(CategoriasDto categoria) { try { ApiCallResult result = this.blogService.GuardarCategoria(categoria.Nombre); return(StatusCode((int)System.Net.HttpStatusCode.OK, result)); } catch (NegocioExecption e) { return(StatusCode((int)System.Net.HttpStatusCode.NotFound, e.Message)); } catch (Exception e) { return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError, e.Message)); } }
/// <summary> /// Lista todas las categorias con la catidad de post que tienen /// </summary> /// <returns></returns> public List <CategoriasDto> ListarCategorias() { try { List <CategoriasDto> categorias = this.blogRepository.ListarCategorias(); if (categorias.Count > 0) { List <CategoriasDto> categoriasDtos = new List <CategoriasDto>(); foreach (var line in categorias.GroupBy(info => info.Nombre) .Select(group => new CategoriasDto { Nombre = group.Key, Id = group.Select(s => s.Id).FirstOrDefault(), Cantidad = group.Count() }).OrderBy(x => x.Cantidad)) { if (line.Cantidad > 0) { CategoriasDto categoriasDto = new CategoriasDto { Cantidad = line.Cantidad, Nombre = line.Nombre, Id = line.Id }; categoriasDtos.Add(categoriasDto); } } return(categoriasDtos.OrderByDescending(o => o.Cantidad).ToList()); } return(null); } catch (Exception) { throw; } }