private void AddNewFakeWitness()
        {
            var aWitnness = new WitnessViewModel
            {

                FirstName = "Witness" + currentWitnessIteration,
                LastName = "Jones",
                Telephone = "witness@witness" + currentWitnessIteration + ".com",
                Email = "07862 182 726"
            };

            currentWitnessViewModel = aWitnness;
        }
        private async void ButtonAddWitness_OnClicked(object sender, EventArgs e)
        {


            var witness = new WitnessViewModel
            {
                FirstName = FirstNameEntry.Text,
                LastName = LastNameEntry.Text,
                Telephone = Contact1Entry.Text,
                Email = Contact2Entry.Text

            };

            parentVm.Witnessses.Add(witness);

            ClearAllFields();
            AddNewFakeWitness();

            await DisplayAlert("Thanks, your witness has been added", "Feel free to add another", "OK");
        }
Exemple #3
0
        public static async Task<HttpResponseMessage> CreateWitnessDeviceAndAPIIfNoKey(WitnessViewModel vm)
        {
            vm.Accident = App.CurrentServerSideAccident;

            var isThereExistingWitness  =
                                parentViewModel.Witnessses.FirstOrDefault(x => x.Client_Id == vm.Client_Id);

            Context.WitnessRepository.AddOrUpdate(vm);

            if (isThereExistingWitness == null)
            {
                parentViewModel.Witnessses.Add(vm);
            }
            else
            {
                isThereExistingWitness = vm;
            }


            if (await App.CheckForValidAccountAndTokenAndRefreshIfExpired())
            {
                if (vm.Id == 0)
                {
                    using (
                        var witnessClient = new AutoResolveWebserviceClient<WitnessViewModel>("api/witnesses")
                        )
                    {
                        witnessClient.AddBearerTokenHeader(parentViewModel.CurrentAccount.LatestBearerToken);


                        var response = await witnessClient.PostAsync(vm);

                        if (response.IsSuccessStatusCode)
                        {
                            var previousClientSideID = vm.Client_Id;
                            var content = await response.Content.ReadAsStringAsync();

                            var pagedResult = JsonConvert.DeserializeObject<StandardPagedResultV1>(content);
                            var driver =
                                JsonConvert.DeserializeObject<WitnessViewModel>(
                                    JsonConvert.SerializeObject(pagedResult.Data));


                            driver.Client_Id = previousClientSideID;
                            var current =
                                parentViewModel.Witnessses.FirstOrDefault(x => x.Client_Id == previousClientSideID);

                            current = driver;
                            parentViewModel.Witnessses.Add(Context.WitnessRepository.AddOrUpdate(current));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();
                            SetLatestAPIErrorMessage(error);
                        }

                        return response;
                    }
                }

            }
            return null;
        }
Exemple #4
0
        public static async Task<HttpResponseMessage> UpdateWitnessDeviceAndAPIIfNoKey(WitnessViewModel vm)
        {
            Context.WitnessRepository.AddOrUpdate(vm);

            if (await App.CheckForValidAccountAndTokenAndRefreshIfExpired())
            {
                if (parentViewModel.Customer.Id != 0)
                {
                    //create in API
                    using (var otherDriverClient = new AutoResolveWebserviceClient<WitnessViewModel>("api/otherdrivers/" + vm.Id))
                    {
                        otherDriverClient.AddBearerTokenHeader(parentViewModel.CurrentAccount.LatestBearerToken);
                        var response = await otherDriverClient.PutAsync(vm);

                        if (response.IsSuccessStatusCode)
                        {
                            //nothing
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();
                            SetLatestAPIErrorMessage(error);
                        }

                        return response;
                    }
                }
            }
            return null;
        }
        /* [Route("api/v1/witnesses", Name = "PostWitnesses")]*/
        public async Task<IHttpActionResult> Post(WitnessViewModel witnessViewModel)
        {
            this.Validate(witnessViewModel);
            if (ModelState.IsValid)
            {
                try
                {

                    if (await AuthorisePost(witnessViewModel))
                    {
                        if (ModelState.IsValid)
                        {
                            var created =
                                TheModelFactoryV1.Create(await repo.Add(TheModelFactoryV1.Parse(witnessViewModel)));


                            GenerateBaseLog(ApiLogType.Information,
                   LogMessageGenerator.Generate(ApiLogggingBaseMessages.Created, __loggingResourceName),
                   GetType(), GetCaller());

                            return CreatedAtRoute("Witnesses", new { id = created.Id }, new StandardPagedResultV1 { Meta = new MetaDescription(), Data = created });
                           

                        }


                        GenerateBaseLog(ApiLogType.Warning,
                        LogMessageGenerator.Generate(ApiLogggingBaseMessages.BadRequestMessage, __loggingResourceName),
                        GetType(), GetCaller());
                        return BadRequest(ModelState);
                    }


                    GenerateBaseLog(ApiLogType.Error,
                    LogMessageGenerator.Generate(ApiLogggingBaseMessages.UnauthorisedAccess, __loggingResourceName),
                    GetType(), GetCaller());
                    return
                        ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized,
                            "You are not authorised to access this resource."));
                }
                catch (Exception ex)
                {

                    GenerateBaseLog(ApiLogType.Error,
 LogMessageGenerator.Generate(ApiLogggingBaseMessages.InternalServerErrorMessage, __loggingResourceName),
  GetType(), GetCaller(), ex);

                    return
                        InternalServerError(
                            new Exception(
                                "An internal server error occurred when creating a Witnesses. Please contact api support."));
                }
            }


            GenerateBaseLog(ApiLogType.Warning,
            LogMessageGenerator.Generate(ApiLogggingBaseMessages.BadRequestMessage, __loggingResourceName),
            GetType(), GetCaller());
            return BadRequest(ModelState);

        }