Esempio n. 1
0
 public HttpResponseMessage Get([FromUri] int Id, [FromUri] string[] include, [FromUri] bool indent = false)
 {
     try {
         var formatter = new MaxDepthJsonMediaTypeFormatter()
         {
             Indent = indent
         };
         if (include.Length > 0)
         {
             formatter.SerializerSettings.MaxDepth = 100;                     //include.Max<string>(s => s.Split('.').Length * 5);
             formatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
         }
         else
         {
             formatter.SerializerSettings.MaxDepth = 1;
         }
         Fabricante_Idioma fabricante_idioma = fabricante_idiomaRepository.GetById(include, (p => p.Id == Id));
         if (fabricante_idioma == null)
         {
             throw new HttpResponseException(HttpStatusCode.NotFound);
         }
         return(Request.CreateResponse <Fabricante_Idioma>(HttpStatusCode.OK, fabricante_idioma, formatter));
     } catch (Exception _excepcion) {
         log.Error(_excepcion);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, _excepcion));
     }
 }
Esempio n. 2
0
        public ICommandResult Execute(CreateOrUpdateFabricante_IdiomaCommand command)
        {
            Fabricante_Idioma _Fabricante_Idioma = AutoMapper.Mapper.Map <CreateOrUpdateFabricante_IdiomaCommand, Fabricante_Idioma>(command);

            if (command.Id == 0)
            {
                Fabricante_IdiomaRepository.Add(_Fabricante_Idioma);
            }
            else
            {
                Fabricante_IdiomaRepository.Update(_Fabricante_Idioma);
            }
            unitOfWork.Commit();

            AutoMapper.Mapper.Map <Fabricante_Idioma, CreateOrUpdateFabricante_IdiomaCommand>(_Fabricante_Idioma, command);

            return(new CommandResult(true));
        }
        public HttpResponseMessage Put(int Id, string cultura, FabricanteModel fabricante)
        {
            try {
                if (ModelState.IsValid)
                {
                    if (cultura != Localizacion.CulturaPorDefecto)
                    {
                        Fabricante      _fabricante           = fabricanteRepository.GetById(new string[] {}, (p => p.Id == Id));
                        FabricanteModel _fabricantePorDefecto = new FabricanteModel();
                        // Solo funciona el Mapper si se ha configurado el Mapper con Mapper.CreateMap<EmpresaModel, EmpresaModel>(); Se está creando en la carpeta mappers.
                        _fabricantePorDefecto = AutoMapper.Mapper.Map <FabricanteModel, FabricanteModel>(fabricante, _fabricantePorDefecto);

                        _fabricantePorDefecto.Nombre = _fabricante.Nombre;

                        var command = AutoMapper.Mapper.Map <FabricanteModel, CreateOrUpdateFabricanteCommand>(_fabricantePorDefecto);
                        var result  = commandBus.Submit(command);
                    }
                    else
                    {
                        var command = AutoMapper.Mapper.Map <FabricanteModel, CreateOrUpdateFabricanteCommand>(fabricante);
                        var result  = commandBus.Submit(command);
                    }
                    Fabricante_Idioma _fabricanteIdioma = fabricante_IdiomaRepository.GetMany(t => t.IdRegistro == fabricante.Id && t.Cultura == cultura).FirstOrDefault();
                    var commandIdioma = AutoMapper.Mapper.Map <Fabricante_IdiomaModel, CreateOrUpdateFabricante_IdiomaCommand>(new Fabricante_IdiomaModel {
                        Id = (_fabricanteIdioma != null ? _fabricanteIdioma.Id : (int)0), IdRegistro = fabricante.Id, Cultura = cultura, Nombre = fabricante.Nombre
                    });
                    var resultIdioma = commandBus.Submit(commandIdioma);
                    return(Request.CreateResponse <FabricanteModel>(HttpStatusCode.OK, fabricante));
                }
                else
                {
                    var errors = new Dictionary <string, IEnumerable <string> >();
                    foreach (var keyValue in ModelState)
                    {
                        errors[keyValue.Key] = keyValue.Value.Errors.Select(e => (!string.IsNullOrWhiteSpace(e.ErrorMessage) ? e.ErrorMessage : (e.Exception != null ? e.Exception.Message : string.Empty)));
                    }
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, errors));
                }
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            } catch (Exception _excepcion) {
                log.Error(_excepcion);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, _excepcion));
            }
        }
        public HttpResponseMessage Get([FromUri] int Id, [FromUri] string cultura, [FromUri] string[] include, [FromUri] bool indent = false)
        {
            try {
                var formatter = new MaxDepthJsonMediaTypeFormatter()
                {
                    Indent = indent
                };
                if (include.Length > 0)
                {
                    formatter.SerializerSettings.MaxDepth = 100;                     //include.Max<string>(s => s.Split('.').Length * 5);
                    formatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
                }
                else
                {
                    formatter.SerializerSettings.MaxDepth = 1;
                }
                Fabricante _fabricante = fabricanteRepository.GetById(include, (p => p.Id == Id));
                if (cultura != Localizacion.CulturaPorDefecto)
                {
                    Fabricante_Idioma _fabricanteIdioma = fabricante_IdiomaRepository.GetMany(p => p.IdRegistro == Id && p.Cultura == cultura).FirstOrDefault();

                    // Campos multiidioma
                    if (_fabricanteIdioma != null)
                    {
                        _fabricante.Nombre = _fabricanteIdioma.Nombre;
                    }
                }

                if (_fabricante == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return(Request.CreateResponse <Fabricante>(HttpStatusCode.OK, _fabricante, formatter));
            } catch (Exception _excepcion) {
                log.Error(_excepcion);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, _excepcion));
            }
        }