Imagens
  • 11 May 2023
  • 1 Minute to read
  • Contributors
  • Dark
    Light

Imagens

  • Dark
    Light

Article summary

As imagens podem ser enviadas em qualquer formato e sempre estão vinculadas a um pedido. É possivel criar um pedido já com as imagens, ou então adicionar imagens a um pedido existente.

Criar pedido com imagens

O controller de pedidos permite criar pedidos com imagens acessando a action "create".

Exemplo com CURL:

curl -F "request[clinic_id]=19" \
     -F "request[request_status_id]=53" \
     -F "request[date]=2021-01-10T16:30" \
     -F "request[dentist_datum][name]=João das Couves" \
     -F "request[patient_datum][name]=Maria das Couves" \
     -F "request[patient_datum][gender]=true" \
     -F "request[patient_datum][birthdate]=1995-01-01" \
     -F "request[photos_attributes][1][image]=@/path/image1.jpg" \
     -F "request[photos_attributes][2][image]=@/path/image2.jpg" \
     https://max.cfaz.net/api/v1/requests?access_token=0cd675768fev8dab81fe1c1297d56b09

Exemplo com Gem RestClient:

RestClient.post "https://max.cfaz.net/api/v1/requests",
               { request:
                 { clinic_id: 19,
                   request_status_id: 53,
                   date: "2021-01-10T16:30",
                   dentist_datum: { name: "João das Couves" },
                   patient_datum: { name: "Maria das Couves", gender: true, birthdate: "1995-01-01"},
                   photos_attributes: [
                     { "1": { image: File.open("/path/image.jpg"),
                         image_file_name: "image1.jpg",
                         image_content_type: "image/jpg"}},
                     { "2": { image: File.open("/path/image.jpg"),
                         image_file_name: "image2.jpg",
                         image_content_type: "image/jpg"}}
                   ],
                 }
               },
               "authorization" => "Token token=0cd675768fev8dab81fe1c1297d56b09"

Adicionar imagens a um pedido existente

O controller de pedidos permite adicionar imagens a um pedido acessando a action "update".

Exemplo com CURL:

curl -X PUT \
     -F "request[photos_attributes][1][image]=@/path/image1.jpg" \
     -F "request[photos_attributes][2][image]=@/path/image2.jpg" \
     https://max.cfaz.net/api/v1/requests/{request_id}?access_token=0cd675768fev8dab81fe1c1297d56b09

Exemplo com Gem RestClient:

RestClient.put "https://max.cfaz.net/api/v1/requests/{request_id}",
               { request: {
                 photos_attributes: [
                  { "1": { image: File.open("/path/image.jpg"),
                       image_file_name: "image1.jpg",
                       image_content_type: "image/jpg"}
                  },
                  { "2": { image: File.open("/path/image.jpg"),
                       image_file_name: "image2.jpg",
                       image_content_type: "image/jpg"}}
                 ],
                }},
               "authorization" => "Token token=0cd675768fev8dab81fe1c1297d56b09"

Excluir imagens de um pedido existente

O controller de pedidos permite excluir imagens de um pedido acessando a action "update".

Exemplo com CURL:

curl -X PUT \
     -F "request[photos_attributes][1][id]={photo_id}" \
     -F "request[photos_attributes][1][_destroy]=1" \
     http://localhost:3000/api/v1/requests/{request_id}?access_token=0cd675768fev8dab81fe1c1297d56b09

Exemplo com Gem RestClient:

RestClient.put "https://max.cfaz.net/api/v1/requests/{request_id}",
               { request: {
                  id: {request.id},
                  photos_attributes: [
                    { id: {photo.id},
                      _destroy: true
                    },
                  ],
                }
                },
               "authorization" => "Token token=0cd675768fev8dab81fe1c1297d56b09"

What's Next