public HttpResponseMessage Post(string cultura, CategoriaFotoModel categoriafoto) { try { if (ModelState.IsValid) { if (categoriafoto.Id == 0) { var command = AutoMapper.Mapper.Map <CategoriaFotoModel, CreateOrUpdateCategoriaFotoCommand>(categoriafoto); var result = commandBus.Submit(command); if (result.Success) { var commandIdioma = AutoMapper.Mapper.Map <CategoriaFoto_IdiomaModel, CreateOrUpdateCategoriaFoto_IdiomaCommand>(new CategoriaFoto_IdiomaModel { IdRegistro = command.Id, Cultura = cultura, Nombre = command.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); if (resultIdioma.Success) { categoriafoto = AutoMapper.Mapper.Map <CreateOrUpdateCategoriaFotoCommand, CategoriaFotoModel>(command); var response = Request.CreateResponse <CategoriaFotoModel>(HttpStatusCode.Created, categoriafoto); string uri = Url.Route(null, new { Id = categoriafoto.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, CategoriaFotoModel categoriafoto) { try { if (ModelState.IsValid) { if (cultura != Localizacion.CulturaPorDefecto) { CategoriaFoto _categoriafoto = categoriafotoRepository.GetById(new string[] {}, (p => p.Id == Id)); CategoriaFotoModel _categoriafotoPorDefecto = new CategoriaFotoModel(); // Solo funciona el Mapper si se ha configurado el Mapper con Mapper.CreateMap<EmpresaModel, EmpresaModel>(); Se está creando en la carpeta mappers. _categoriafotoPorDefecto = AutoMapper.Mapper.Map <CategoriaFotoModel, CategoriaFotoModel>(categoriafoto, _categoriafotoPorDefecto); _categoriafotoPorDefecto.Nombre = _categoriafoto.Nombre; var command = AutoMapper.Mapper.Map <CategoriaFotoModel, CreateOrUpdateCategoriaFotoCommand>(_categoriafotoPorDefecto); var result = commandBus.Submit(command); } else { var command = AutoMapper.Mapper.Map <CategoriaFotoModel, CreateOrUpdateCategoriaFotoCommand>(categoriafoto); var result = commandBus.Submit(command); } CategoriaFoto_Idioma _categoriafotoIdioma = categoriafoto_IdiomaRepository.GetMany(t => t.IdRegistro == categoriafoto.Id && t.Cultura == cultura).FirstOrDefault(); var commandIdioma = AutoMapper.Mapper.Map <CategoriaFoto_IdiomaModel, CreateOrUpdateCategoriaFoto_IdiomaCommand>(new CategoriaFoto_IdiomaModel { Id = (_categoriafotoIdioma != null ? _categoriafotoIdioma.Id : (int)0), IdRegistro = categoriafoto.Id, Cultura = cultura, Nombre = categoriafoto.Nombre }); var resultIdioma = commandBus.Submit(commandIdioma); return(Request.CreateResponse <CategoriaFotoModel>(HttpStatusCode.OK, categoriafoto)); } 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)); } }