public HttpResponseMessage Post(string cultura, FabricanteModel fabricante) { try { if (ModelState.IsValid) { if (fabricante.Id == 0) { var command = AutoMapper.Mapper.Map <FabricanteModel, CreateOrUpdateFabricanteCommand>(fabricante); var result = commandBus.Submit(command); if (result.Success) { var commandIdioma = AutoMapper.Mapper.Map <Fabricante_IdiomaModel, CreateOrUpdateFabricante_IdiomaCommand>(new Fabricante_IdiomaModel { IdRegistro = command.Id, Cultura = cultura, Nombre = command.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); if (resultIdioma.Success) { fabricante = AutoMapper.Mapper.Map <CreateOrUpdateFabricanteCommand, FabricanteModel>(command); var response = Request.CreateResponse <FabricanteModel>(HttpStatusCode.Created, fabricante); string uri = Url.Route(null, new { Id = fabricante.Id }); response.Headers.Location = new Uri(Request.RequestUri, uri); return(response); } } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, "No se puede insertar el registro porque ya existe otro con la misma clave. Por favor, revísela.")); } } 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 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)); } }