コード例 #1
0
 public HttpResponseMessage Post(Foto_IdiomaModel foto_idioma)
 {
     try {
         if (ModelState.IsValid)
         {
             if (foto_idioma.Id == 0)
             {
                 var command = AutoMapper.Mapper.Map <Foto_IdiomaModel, CreateOrUpdateFoto_IdiomaCommand>(foto_idioma);
                 var result  = commandBus.Submit(command);
                 if (result.Success)
                 {
                     foto_idioma = AutoMapper.Mapper.Map <CreateOrUpdateFoto_IdiomaCommand, Foto_IdiomaModel>(command);
                     var    response = Request.CreateResponse <Foto_IdiomaModel>(HttpStatusCode.Created, foto_idioma);
                     string uri      = Url.Route(null, new { Id = foto_idioma.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));
     }
 }
コード例 #2
0
 public HttpResponseMessage Put(int Id, Foto_IdiomaModel foto_idioma)
 {
     try {
         if (ModelState.IsValid)
         {
             var command = AutoMapper.Mapper.Map <Foto_IdiomaModel, CreateOrUpdateFoto_IdiomaCommand>(foto_idioma);
             var result  = commandBus.Submit(command);
             return(Request.CreateResponse <Foto_IdiomaModel>(HttpStatusCode.OK, foto_idioma));
         }
         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));
     }
 }