public async Task Update(EstudiosMercado Obj)// UpdateSolicitud
                                                           {
                                                               try
                                                               {
                                                                   var result = await _ctx.EstudiosMercado.FirstOrDefaultAsync(e => e.EstudiosMercadoId == Obj.EstudiosMercadoId);

                                                                   if (result != null)
                                                                   {
                                                                       _ctx.Entry(result).CurrentValues.SetValues(Obj);
                                                                       await _ctx.SaveChangesAsync();

                                                                       var listaArchivosViejos = await _ctx.AdjuntosEstudiosMercado.Where(e => e.EstudiosMercadoId == Obj.EstudiosMercadoId).ToListAsync();

                                                                       if (listaArchivosViejos.Count() > 0)
                                                                       {
                                                                           var adjuntos = listaArchivosViejos.Select(x => x.AdjuntoId).ToList();

                                                                           _ctx.AdjuntosEstudiosMercado.RemoveRange(listaArchivosViejos);
                                                                           await _ctx.SaveChangesAsync();


                                                                           foreach (var c in adjuntos)
                                                                           {
                                                                               await new AdjuntoRepository().Delete(c);
                                                                           }
                                                                       }


                                                                       AdjuntoRepository adjuntorepo = new AdjuntoRepository();
                                                                       if (Obj.listaAdjuntos != null)
                                                                       {
                                                                           foreach (var c in Obj.listaAdjuntos)
                                                                           {
                                                                               await adjuntorepo.Create(c);

                                                                               AdjuntosEstudiosMercado nuevoAdjunto = new AdjuntosEstudiosMercado();
                                                                               nuevoAdjunto.EstudiosMercadoId = Obj.EstudiosMercadoId;
                                                                               nuevoAdjunto.AdjuntoId         = c.AdjuntoId;

                                                                               _ctx.AdjuntosEstudiosMercado.Add(nuevoAdjunto);
                                                                               await _ctx.SaveChangesAsync();
                                                                           }
                                                                       }
                                                                   }
                                                               }
                                                               catch (Exception e)
                                                               {
                                                                   throw new Exception(e.Message, e);
                                                               }
                                                           }
Пример #2
0
                                                             [Authorize] public async Task <IHttpActionResult> Create(EstudiosMercado Obj)
                                                             {
                                                                 try { log.Info(new MDCSet(this.ControllerContext.RouteData));
                                                                       int id = await _repository.Create(Obj);

                                                                       //return Ok("Registro creado correctamente!");
                                                                       return(Ok(id)); }
                                                                 catch (Exception e) { log.Error(new MDCSet(this.ControllerContext.RouteData), e);

                                                                                       return(InternalServerError(e)); }
                                                             }
Пример #3
0
                                                             public async Task <IHttpActionResult> GetConsultaParametrizadaEstudios(EstudiosMercado parametros)
                                                             {
                                                                 try
                                                                 {
                                                                     var result = await _repository.GetConsultaParametrizadaEstudios(parametros);

                                                                     return(Ok(result));
                                                                 }
                                                                 catch (Exception e)
                                                                 {
                                                                     log.Error(new MDCSet(this.ControllerContext.RouteData), e);
                                                                     return(InternalServerError(e));
                                                                 }
                                                             }
Пример #4
0
                                                             public async Task <IHttpActionResult> Update(EstudiosMercado Obj)
                                                             {
                                                                 try { log.Info(new MDCSet(this.ControllerContext.RouteData));
                                                                       if (Obj.Adjunto != null)
                                                                       {
                                                                           //Elimar archivo
                                                                           if (Obj.Adjunto.nombre == "eliminar")
                                                                           {
                                                                               int id = Convert.ToInt32(Obj.AdjuntoId);
                                                                               Obj.AdjuntoId = null;
                                                                               await _repository.Update(Obj);

                                                                               await _adjuntoRepo.Delete(id);

                                                                               return(Ok());
                                                                           }
                                                                           ///Agregar archivo al editar
                                                                           if (Obj.Adjunto.AdjuntoId == 0)
                                                                           {
                                                                               Adjunto key = await _adjuntoRepo.CreateAd(Obj.Adjunto);

                                                                               Obj.AdjuntoId         = key.AdjuntoId;
                                                                               Obj.Adjunto.AdjuntoId = key.AdjuntoId;
                                                                               await _repository.Update(Obj);

                                                                               return(Ok(key));
                                                                           }
                                                                       }
                                                                       //solución de ALAN replicada
                                                                       if (Obj.Adjunto != null)
                                                                       {
                                                                           Obj.AdjuntoId = Obj.Adjunto.AdjuntoId;
                                                                       }
                                                                       await _repository.Update(Obj);

                                                                       return(Ok(Obj)); }
                                                                 catch (Exception e) { log.Error(new MDCSet(this.ControllerContext.RouteData), e);

                                                                                       return(InternalServerError(e)); }
                                                             }
                                                           public async Task <int> Create(EstudiosMercado Obj)
                                                           {
                                                               try
                                                               {
                                                                   _ctx.EstudiosMercado.Add(Obj);
                                                                   await _ctx.SaveChangesAsync();

                                                                   if (Obj.listaAdjuntos != null)
                                                                   {
                                                                       AdjuntoRepository adjuntorepo = new AdjuntoRepository();
                                                                       foreach (var archivo in Obj.listaAdjuntos)
                                                                       {
                                                                           await adjuntorepo.Create(archivo);

                                                                           AdjuntosEstudiosMercado nuevoAdjunto = new AdjuntosEstudiosMercado();
                                                                           nuevoAdjunto.AdjuntoId         = archivo.AdjuntoId;
                                                                           nuevoAdjunto.EstudiosMercadoId = Obj.EstudiosMercadoId;
                                                                           _ctx.AdjuntosEstudiosMercado.Add(nuevoAdjunto);
                                                                           await _ctx.SaveChangesAsync();
                                                                       }
                                                                   }
                                                                   return(Obj.EstudiosMercadoId);
                                                               }
                                                               catch (Exception e)
                                                               {
                                                                   throw new Exception(e.Message, e);
                                                               }
                                                           }
                                                           public async Task <IEnumerable <Object> > GetConsultaParametrizadaEstudios(EstudiosMercado parametros)
                                                           {
                                                               try{
                                                                   var estudios = _ctx.EstudiosMercado.Select(e => e);
                                                                   if (estudios != null)
                                                                   {
                                                                       if (!String.IsNullOrEmpty(parametros.busquedaFecha)) //busqueda por fecha
                                                                       {
                                                                           estudios = estudios.Where(e => (DbFunctions.TruncateTime(e.Fecha) >= DbFunctions.TruncateTime(parametros.fechaInicioComparacion) &&
                                                                                                           DbFunctions.TruncateTime(e.Fecha) <= DbFunctions.TruncateTime(parametros.fechaFinalComparacion)));
                                                                       }
                                                                       if (!String.IsNullOrEmpty(parametros.Tema)) //busqueda por nombre del estudio de mercado
                                                                       {
                                                                           var listaNombres = await GetEstudiosLikeNombreLatin1(parametros.Tema);

                                                                           estudios = estudios.Where(e => listaNombres.Contains(e.EstudiosMercadoId));
                                                                       }
                                                                       if (!String.IsNullOrEmpty(parametros.Autores)) //Busqueda por autores
                                                                       {
                                                                           var listaClaves = await GetEstudiosByAutoresLikeNombreLatin1(parametros.Autores);

                                                                           estudios = estudios.Where(e => listaClaves.Contains(e.EstudiosMercadoId));
                                                                       }
                                                                       if (!String.IsNullOrEmpty(parametros.proposito)) //Busqueda por proposito del estudio mercado
                                                                       {
                                                                           var listaPropositos = await GetPropositoEstudiosLikeNombreLatin1(parametros.proposito);

                                                                           estudios = estudios.Where(e => listaPropositos.Contains(e.EstudiosMercadoId));
                                                                       }
                                                                       if (!String.IsNullOrEmpty(parametros.ProyectoId)) //Busqueda por proyecto
                                                                       {
                                                                           estudios = estudios.Where(e => e.ProyectoId == parametros.ProyectoId);
                                                                       }
                                                                       if (!String.IsNullOrEmpty(parametros.ClaveUnidad)) //Busqueda por ClaveUnidad
                                                                       {
                                                                           estudios = estudios.Where(e => e.ClaveUnidad == parametros.ClaveUnidad);
                                                                       }

                                                                       //******Se inicia el proceso de proyeccion******
                                                                       //Los resultados lo guardaremos en una lista de X objeto
                                                                       List <BusquedaParamsCR> datos = estudios.Select(x => new BusquedaParamsCR //Es una clase no mapeada que contiene caracteristicas
                                                                                                                                                 //que nos permiten albergar SOLO los datos necesarios
                    {
                        EstudiosMercadoId = x.EstudiosMercadoId,                                                                                 //Rescatamos los parametros que se requieren para el front
                        Tema        = x.Tema,
                        Fecha       = x.Fecha,
                        Proposito   = x.proposito,
                        ProyectoId  = x.ProyectoId,
                        ClaveUnidad = x.ClaveUnidad
                    }).ToList();


                                                                       //Recuperamos las claves de unidad
                                                                       GEN_Context _gen = new GEN_Context();

                                                                       var unidadesId = await(_ctx.EstudiosMercado
                                                                                              .Where(e => e.ClaveUnidad != "")
                                                                                              .Select(e => e.ClaveUnidad))
                                                                                        .AsNoTracking().ToListAsync();

                                                                       var unidades = await _gen.dbSetUnidadOrganizacional.AsNoTracking()
                                                                                      .Where(x => unidadesId.Contains(x.ClaveUnidad) && //)
                                                                                             x.FechaEfectiva == _gen.dbSetUnidadOrganizacional
                                                                                             .Where(f => f.FechaEfectiva <= DateTime.Now &&
                                                                                                    f.ClaveUnidad == x.ClaveUnidad)
                                                                                             .Max(f => f.FechaEfectiva)
                                                                                             ).Select(x => new
                    {
                        ClaveUnidad  = x.ClaveUnidad,
                        NombreUnidad = x.NombreUnidad
                    })
                                                                                      .ToListAsync();

                                                                       foreach (var c in datos)
                                                                       {
                                                                           var listaAutores = await _ctx.AutoresEstudioMercado.Where(e => e.EstudiosMercadoId == c.EstudiosMercadoId).Select(e => new { clavePersona = e.ClavePersona, e.NombrePersona }).ToListAsync();

                                                                           List <Contacto> contacts = new List <Contacto>();
                                                                           foreach (var dato in listaAutores)
                                                                           {
                                                                               Contacto contact = new Contacto();
                                                                               contact.NombreContacto = dato.NombrePersona;
                                                                               contact.ClaveUnidad    = dato.clavePersona;
                                                                               contacts.Add(contact);
                                                                           }

                                                                           c.listaDeInfo = contacts;

                                                                           if (c.ClaveUnidad != null)
                                                                           {
                                                                               c.NombreUnidad = unidades.Where(e => e.ClaveUnidad == c.ClaveUnidad).Select(e => e.NombreUnidad).FirstOrDefault();
                                                                           }
                                                                           if (c.ProyectoId != null)
                                                                           {
                                                                               c.NombreProyecto = await(from proyecto in _gen.dbSetProyectoGEN
                                                                                                        where proyecto.ProyectoId == c.ProyectoId
                                                                                                        select proyecto.Nombre)
                                                                                                  .FirstOrDefaultAsync();
                                                                           }
                                                                       }

                                                                       return(datos);
                                                                   }
                                                                   return(null);
                                                               }catch (Exception e)
                                                               {
                                                                   throw new Exception(e.Message, e);
                                                               }
                                                           }