public HttpResponseMessage Post(string cultura, TipoColeccionCalendarioModel tipocoleccioncalendario) { try { if (ModelState.IsValid) { if (tipocoleccioncalendario.Id == 0) { var command = AutoMapper.Mapper.Map <TipoColeccionCalendarioModel, CreateOrUpdateTipoColeccionCalendarioCommand>(tipocoleccioncalendario); var result = commandBus.Submit(command); if (result.Success) { var commandIdioma = AutoMapper.Mapper.Map <TipoColeccionCalendario_IdiomaModel, CreateOrUpdateTipoColeccionCalendario_IdiomaCommand>(new TipoColeccionCalendario_IdiomaModel { IdRegistro = command.Id, Cultura = cultura, Nombre = command.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); if (resultIdioma.Success) { tipocoleccioncalendario = AutoMapper.Mapper.Map <CreateOrUpdateTipoColeccionCalendarioCommand, TipoColeccionCalendarioModel>(command); var response = Request.CreateResponse <TipoColeccionCalendarioModel>(HttpStatusCode.Created, tipocoleccioncalendario); string uri = Url.Route(null, new { Id = tipocoleccioncalendario.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, TipoColeccionCalendarioModel tipocoleccioncalendario) { try { if (ModelState.IsValid) { if (cultura != Localizacion.CulturaPorDefecto) { TipoColeccionCalendario _tipocoleccioncalendario = tipocoleccioncalendarioRepository.GetById(new string[] {}, (p => p.Id == Id)); TipoColeccionCalendarioModel _tipocoleccioncalendarioPorDefecto = new TipoColeccionCalendarioModel(); // Solo funciona el Mapper si se ha configurado el Mapper con Mapper.CreateMap<EmpresaModel, EmpresaModel>(); Se está creando en la carpeta mappers. _tipocoleccioncalendarioPorDefecto = AutoMapper.Mapper.Map <TipoColeccionCalendarioModel, TipoColeccionCalendarioModel>(tipocoleccioncalendario, _tipocoleccioncalendarioPorDefecto); _tipocoleccioncalendarioPorDefecto.Nombre = _tipocoleccioncalendario.Nombre; var command = AutoMapper.Mapper.Map <TipoColeccionCalendarioModel, CreateOrUpdateTipoColeccionCalendarioCommand>(_tipocoleccioncalendarioPorDefecto); var result = commandBus.Submit(command); } else { var command = AutoMapper.Mapper.Map <TipoColeccionCalendarioModel, CreateOrUpdateTipoColeccionCalendarioCommand>(tipocoleccioncalendario); var result = commandBus.Submit(command); } TipoColeccionCalendario_Idioma _tipocoleccioncalendarioIdioma = tipocoleccioncalendario_IdiomaRepository.GetMany(t => t.IdRegistro == tipocoleccioncalendario.Id && t.Cultura == cultura).FirstOrDefault(); var commandIdioma = AutoMapper.Mapper.Map <TipoColeccionCalendario_IdiomaModel, CreateOrUpdateTipoColeccionCalendario_IdiomaCommand>(new TipoColeccionCalendario_IdiomaModel { Id = (_tipocoleccioncalendarioIdioma != null ? _tipocoleccioncalendarioIdioma.Id : (short)0), IdRegistro = tipocoleccioncalendario.Id, Cultura = cultura, Nombre = tipocoleccioncalendario.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); return(Request.CreateResponse <TipoColeccionCalendarioModel>(HttpStatusCode.OK, tipocoleccioncalendario)); } 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)); } }