示例#1
0
        private async Task InsertaSitioWebFondoProgramaUpdate(FondoPrograma model)
        {
            try
            {
                foreach (var item in model.sitiosWebNuevos)
                {
                    //Crea el objeto de la tabla en la que se desea agregar el registro
                    SitioWebFondoPrograma objSitioWebFondoPrograma = new SitioWebFondoPrograma();

                    //Agrega los datos de la tabla de acuerdo a lo que trae el modelo
                    objSitioWebFondoPrograma.FondoProgramaId = model.FondoProgramaId;
                    objSitioWebFondoPrograma.Url             = item;
                    objSitioWebFondoPrograma.Descripcion     = "Url de " + model.NombreFP;
                    objSitioWebFondoPrograma.FechaRegistro   = model.FechaRegistro;
                    objSitioWebFondoPrograma.Autor           = model.Autor;
                    objSitioWebFondoPrograma.Estado          = true;

                    var entities = _db.SitioWebFondoPrograma.Add(objSitioWebFondoPrograma);
                    await _db.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
示例#2
0
        private async Task EliminaSitioWebFondoPrograma(FondoPrograma model)
        {
            try
            {
                foreach (var item in model.sitiosWebAntDel)
                {
                    //Crea el objeto de la tabla en la que se desea agregar el registro
                    SitioWebFondoPrograma objSitioWebFondoPrograma = new SitioWebFondoPrograma();

                    var _model = await _db.SitioWebFondoPrograma.FirstOrDefaultAsync(e => e.SitioWebFondoProgramaId == item);

                    if (_model != null)
                    {
                        _db.SitioWebFondoPrograma.Remove(_model);
                        await _db.SaveChangesAsync();
                    }

                    //var entities = _db.SitioWebFondoPrograma.Remove(objSitioWebFondoPrograma);
                    //await _db.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task <Boolean> PreCreate(SitioWebFondoPrograma model)
        {
            var entities = await _db.SitioWebFondoPrograma.AsNoTracking()
                           .FirstOrDefaultAsync(e => e.Url == model.Url);

            if (entities == null)
            {
                return(true);
            }
            return(false);
        }
        public async Task <SitioWebFondoPrograma> Create(SitioWebFondoPrograma model)
        {
            try
            {
                var result = _db.SitioWebFondoPrograma.Add(model);
                await _db.SaveChangesAsync();

                return(result);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task Update(SitioWebFondoPrograma model)
        {
            try
            {
                var _model = await _db.SitioWebFondoPrograma.FirstOrDefaultAsync(e => e.SitioWebFondoProgramaId == model.SitioWebFondoProgramaId);

                if (_model != null)
                {
                    _db.Entry(_model).CurrentValues.SetValues(model);
                    await _db.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task UpdateEstado(SitioWebFondoPrograma model)
        {
            try
            {
                var _model = await _db.SitioWebFondoPrograma.FirstOrDefaultAsync(e => e.SitioWebFondoProgramaId == model.SitioWebFondoProgramaId);

                if (_model != null)
                {
                    _model.Estado = model.Estado;

                    await _db.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
                                                                   public async Task <IHttpActionResult> UpdateEstado([FromBody] SitioWebFondoPrograma model)
                                                                   {
                                                                       try { log.Info(new MDCSet(this.ControllerContext.RouteData));
                                                                             await _entityRepo.UpdateEstado(model);

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

                                                                                             return(InternalServerError(e)); }
                                                                   }
                                                                   public async Task <IHttpActionResult> Update([FromBody] SitioWebFondoPrograma model)
                                                                   {
                                                                       if (!ModelState.IsValid)
                                                                       {
                                                                           return(BadRequest(ModelState));
                                                                       }

                                                                       try { log.Info(new MDCSet(this.ControllerContext.RouteData));
                                                                             await _entityRepo.Update(model);

                                                                             return(Ok("Registro actualizado exitosamente!")); }
                                                                       catch (Exception e) { log.Error(new MDCSet(this.ControllerContext.RouteData), e);
                                                                                             return(InternalServerError(e)); }
                                                                   }
                                                                   [Authorize] public async Task <IHttpActionResult> Create(SitioWebFondoPrograma model)
                                                                   {
                                                                       if (!ModelState.IsValid)
                                                                       {
                                                                           return(BadRequest(ModelState));
                                                                       }

                                                                       try
                                                                       {
                                                                           log.Info(new MDCSet(this.ControllerContext.RouteData));

                                                                           var result = await _entityRepo.Create(model);

                                                                           return(Ok(result));
                                                                       }
                                                                       catch (Exception e)
                                                                       {
                                                                           log.Error(new MDCSet(this.ControllerContext.RouteData), e);
                                                                           if (e.Message.Substring(0, 44) == "An error occurred while updating the entries")
                                                                           {
                                                                               return(BadRequest("Ya existe un registro con ese nombre"));
                                                                           }
                                                                           return(InternalServerError(e));
                                                                       }
                                                                   }