Beispiel #1
0
        public async Task <ActionResult> Nuevo([DataSourceRequest] DataSourceRequest request, CategoriaModel model)
        {
            DataSourceResult result = new[] { model }.ToDataSourceResult(request, ModelState);;

            if (ModelState.IsValid)
            {
                try
                {
                    ICategoriasServicio srv   = Servicios.CategoriasServicio();
                    Categoria           nuevo = srv.Create();
                    model.ActualizarEntidad(nuevo);
                    srv.Insert(nuevo);
                    await srv.ApplyChangesAsync();

                    result = new[] { CategoriaModel.FromEntity(nuevo) }.ToDataSourceResult(request, ModelState);
                }
                catch (CategoriaExisteException cee)
                {
                    log.Error($"Error al añadir {Txt.Categorias.ArtEntidad}. Usuario: {CurrentUserID()}", cee);
                    result.Errors = new[] { string.Format(Txt.ErroresComunes.Modificar + cee.Message, Txt.Categorias.ArtEntidad).Frase() };
                }
                catch (Exception e)
                {
                    log.Error("Error al añadir el categoría " + model.Nombre, e);
                    result.Errors = new[] { string.Format(Txt.ErroresComunes.Aniadir, Txt.Categorias.ArtEntidad).Frase() };
                }
            }
            return(Json(result));
        }
Beispiel #2
0
        public async Task <ActionResult> Modificar([DataSourceRequest] DataSourceRequest request, CategoriaModel model)
        {
            DataSourceResult result = new[] { model }.ToDataSourceResult(request, ModelState);;

            if (ModelState.IsValid)
            {
                try
                {
                    var srv       = Servicios.CategoriasServicio();
                    var modificar = srv.GetSingle(p => p.CategoriaID == model.CategoriaID);
                    if (modificar != null)
                    {
                        srv.CambiarOrdenCategoria(model.CategoriaID, model.Orden);
                        model.ActualizarEntidad(modificar);
                        await srv.ApplyChangesAsync();

                        result = new[] { CategoriaModel.FromEntity(modificar) }.ToDataSourceResult(request, ModelState);
                    }
                    else
                    {
                        result.Errors = new[] { string.Format(Txt.ErroresComunes.NoExiste, Txt.Categorias.ArtEntidad).Frase() };
                    }
                }
                catch (CategoriaExisteException cee)
                {
                    log.Error($"Error al modificar {Txt.Categorias.ArtEntidad}. Usuario: {CurrentUserID()}", cee);
                    result.Errors = new[] { string.Format(Txt.ErroresComunes.Modificar + cee.Message, Txt.Categorias.ArtEntidad).Frase() };
                }
                catch (Exception e)
                {
                    log.Error("Error al modificar el categoría con id=" + model.CategoriaID, e);
                    result.Errors = new[] { string.Format(Txt.ErroresComunes.Modificar, Txt.Categorias.ArtEntidad).Frase() };
                }
            }
            else
            {
                result.Errors = new[] { Txt.Categorias.NoPermitido };
            }
            return(Json(result));
        }