Example #1
0
        public static async Task <string> Publicar(Ocorrencia ocorrencia)
        {
            string msg_return = "Não foi possível publicar sua ocorrẽncia. Tente novamente dentro de alguns minutos.";

            try
            {
                string token = await RequestSecurityToken();

                ocorrencia.ImageId = await SendOccurencyPicture(token);

                var result = await new RestService(TAVAZANDO_API_SAVE_RESOURCE, TAVAZANDO_API_END_POINT, TAVAZANDO_SERVER_URI).EnviarOcorrencia <string>(ocorrencia, token);

                if (result != null && Newtonsoft.Json.Linq.JObject.Parse(result)["message"].ToString().ToLower().Equals("ok"))
                {
                    msg_return = "Ocorrência publicada com sucesso. Obrigado por sua colaboração";
                }
            }
            catch (Exception ex)
            {
                msg_return = string.Format("{0} : {1} - {2}", "Publicar", ex.Message, ex.StackTrace);
            }


            return(msg_return);
        }
Example #2
0
        public async Task <T> EnviarOcorrencia <T>(Ocorrencia ocorrencia, string token) where T : class
        {
            try
            {
                if (ocorrencia == null)
                {
                    throw new Exception("Não foi possível ler o objeto ocorrẽncia");
                }

                if (string.IsNullOrEmpty(token))
                {
                    throw new Exception("Token de segurança não informado");
                }

                //trata os valores
                string lat         = string.Empty;
                string lng         = string.Empty;
                string image_id    = string.Empty;
                string type        = string.Empty;
                string description = string.Empty;
                string address     = string.Empty;
                string zipcode     = string.Empty;
                string city        = string.Empty;

                if (!string.IsNullOrEmpty(ocorrencia.TipoOcorrencia.Id.ToString()))
                {
                    type = ocorrencia.TipoOcorrencia.Id.ToString();
                }
                else
                {
                    throw new Exception("Tipo de ocorrẽncia deve ser informado");
                }

                if (!string.IsNullOrEmpty(ocorrencia.Descricao))
                {
                    description = ocorrencia.Descricao;
                }
                else
                {
                    description = FIELD_NI;
                }

                if (!string.IsNullOrEmpty(ocorrencia.EnderecoFormatado))
                {
                    address = ocorrencia.EnderecoFormatado;
                }
                else
                {
                    address = FIELD_NI;
                }

                if (!string.IsNullOrEmpty(ocorrencia.Cep))
                {
                    zipcode = ocorrencia.Cep;
                }
                else
                {
                    zipcode = FIELD_NI;
                }

                if (!string.IsNullOrEmpty(ocorrencia.Cidade))
                {
                    city = ocorrencia.Cidade;
                }
                else
                {
                    city = FIELD_NI;
                }

                if (!string.IsNullOrEmpty(ocorrencia.ImageId))
                {
                    image_id = ocorrencia.ImageId;
                }
                else
                {
                    image_id = "0";
                }

                if (ocorrencia.Latitude != null)
                {
                    if (ocorrencia.Latitude.ToString().IndexOf(",") > -1)
                    {
                        lat = ocorrencia.Latitude.ToString().Replace(",", ".");
                    }
                    else
                    {
                        lat = ocorrencia.Latitude.ToString();
                    }
                }
                else
                {
                    lat = "0";
                }

                if (ocorrencia.Longitude != null)
                {
                    if (ocorrencia.Longitude.ToString().IndexOf(",") > -1)
                    {
                        lng = ocorrencia.Longitude.ToString().Replace(",", ".");
                    }
                    else
                    {
                        lng = ocorrencia.Longitude.ToString();
                    }
                }
                else
                {
                    lng = "0";
                }

                var request = new RestRequest(resource, HttpMethod.Post, ContentTypes.FormUrlEncoded);

//				request.AddHeader("authorization", string.Format("Bearer {0}", token));
//				request.AddParameter("tipo", ocorrencia.TipoOcorrencia.Id.ToString());
//				request.AddParameter("descricao", ocorrencia.Descricao);
//				request.AddParameter("endereco", ocorrencia.EnderecoFormatado);
//				request.AddParameter("cep", ocorrencia.Cep);
//				request.AddParameter("cidade", ocorrencia.Cidade);
//
//				if(!string.IsNullOrEmpty(ocorrencia.ImageId))
//					request.AddParameter("arquivos", ocorrencia.ImageId);
//				else
//					request.AddParameter("arquivos", string.Empty);
//
//				request.AddParameter("longitude", lng);
//				request.AddParameter("latitude", lat);

                request.AddHeader("authorization", string.Format("Bearer {0}", token));
                request.AddParameter("tipo", type);
                request.AddParameter("descricao", description);
                request.AddParameter("endereco", address);
                request.AddParameter("cep", zipcode);
                request.AddParameter("cidade", city);
                request.AddParameter("arquivos", image_id);
                request.AddParameter("longitude", lng);
                request.AddParameter("latitude", lat);

                var results = await ExecuteAsync <T>(request);

                return(results);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("{0} : {1}", "EnviarOcorrencia", ex.Message));
            }
        }