public async Task <ActionResult> AnuncioDetalles(int?id, ConsultarDetalles consulta, string query) { if (id == null) { return(HttpNotFound("Parámetro inválido se espera un id de un anuncio")); } HttpNotFoundResult result = null; PortalDetallesAnuncioViewModel model = null; await Task.Run(() => { using (var _dbContext = new MercampoEntities()) { Startup.OpenDatabaseConnection(_dbContext); if (_dbContext.Database.Connection.State != ConnectionState.Open) { result = HttpNotFound("Error en la base de datos"); } else { var anuncio = _dbContext.Anuncios.Where(a => a.id == id).FirstOrDefault(); if (anuncio == null) { result = HttpNotFound("No se encontro el anuncio con el id solicitado"); } else { anuncio.clicks += 1; _dbContext.SaveChanges(); var anuncioViewModel = new PortalAnuncioViewModel(anuncio.id, anuncio.titulo, anuncio.precio, anuncio.Subcategoria.Categoria.nombre, anuncio.Subcategoria.nombre, anuncio.Estado1.nombre, anuncio.Fotos_Anuncio.Where(f => f.principal == true).FirstOrDefault()?.ruta); List <PaginaFotoViewModel> fotos = new List <PaginaFotoViewModel>(); var rutaVideo = anuncio.Videos_Anuncio.Where(v => v.idAnuncio == id).FirstOrDefault()?.ruta; string nombre = anuncio.Usuario.nombre + " " + anuncio.Usuario.apellidos; var owner = new PaginaOwnerAnuncioViewModel(anuncio.Usuario.id, nombre, anuncio.Usuario.telefono, anuncio.Usuario.email); foreach (var foto in anuncio.Fotos_Anuncio) { fotos.Add(new PaginaFotoViewModel(foto.principal, foto.ruta)); } model = new PortalDetallesAnuncioViewModel(anuncioViewModel, anuncio.descripcion, fotos, rutaVideo, owner, anuncio.clicks, consulta, query); } } } }); if (result != null) { return(result); } return(View(model)); }
public PortalDetallesAnuncioViewModel(PortalAnuncioViewModel anuncio, string descripcion, List <PaginaFotoViewModel> fotos, string video, PaginaOwnerAnuncioViewModel owner, int?clicks, ConsultarDetalles consulta, string query) { _anuncio = anuncio; _descripcion = descripcion; _fotos = fotos; _video = video; _owner = owner; _clicks = clicks; _consulta = consulta; _query = query; }