public HttpResponseMessage Post(string cultura, TipoEventoModel tipoevento) { try { if (ModelState.IsValid) { if (tipoevento.Id == 0) { var command = AutoMapper.Mapper.Map <TipoEventoModel, CreateOrUpdateTipoEventoCommand>(tipoevento); var result = commandBus.Submit(command); if (result.Success) { var commandIdioma = AutoMapper.Mapper.Map <TipoEvento_IdiomaModel, CreateOrUpdateTipoEvento_IdiomaCommand>(new TipoEvento_IdiomaModel { IdRegistro = command.Id, Cultura = cultura, Nombre = command.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); if (resultIdioma.Success) { tipoevento = AutoMapper.Mapper.Map <CreateOrUpdateTipoEventoCommand, TipoEventoModel>(command); var response = Request.CreateResponse <TipoEventoModel>(HttpStatusCode.Created, tipoevento); string uri = Url.Route(null, new { Id = tipoevento.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(short Id, string cultura, TipoEventoModel tipoevento) { try { if (ModelState.IsValid) { if (cultura != Localizacion.CulturaPorDefecto) { TipoEvento _tipoevento = tipoeventoRepository.GetById(new string[] {}, (p => p.Id == Id)); TipoEventoModel _tipoeventoPorDefecto = new TipoEventoModel(); // Solo funciona el Mapper si se ha configurado el Mapper con Mapper.CreateMap<EmpresaModel, EmpresaModel>(); Se está creando en la carpeta mappers. _tipoeventoPorDefecto = AutoMapper.Mapper.Map <TipoEventoModel, TipoEventoModel>(tipoevento, _tipoeventoPorDefecto); _tipoeventoPorDefecto.Nombre = _tipoevento.Nombre; var command = AutoMapper.Mapper.Map <TipoEventoModel, CreateOrUpdateTipoEventoCommand>(_tipoeventoPorDefecto); var result = commandBus.Submit(command); } else { var command = AutoMapper.Mapper.Map <TipoEventoModel, CreateOrUpdateTipoEventoCommand>(tipoevento); var result = commandBus.Submit(command); } TipoEvento_Idioma _tipoeventoIdioma = tipoevento_IdiomaRepository.GetMany(t => t.IdRegistro == tipoevento.Id && t.Cultura == cultura).FirstOrDefault(); var commandIdioma = AutoMapper.Mapper.Map <TipoEvento_IdiomaModel, CreateOrUpdateTipoEvento_IdiomaCommand>(new TipoEvento_IdiomaModel { Id = (_tipoeventoIdioma != null ? _tipoeventoIdioma.Id : (short)0), IdRegistro = tipoevento.Id, Cultura = cultura, Nombre = tipoevento.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); return(Request.CreateResponse <TipoEventoModel>(HttpStatusCode.OK, tipoevento)); } 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)); } }