Пример #1
0
        public async Task <ActionResult> Create(TripCreateViewModel vm)
        {
            try
            {
                byte[] uploadedImage = new byte[vm.MainImage.InputStream.Length];
                vm.MainImage.InputStream.Read(uploadedImage, 0, uploadedImage.Length);

                vm.Trip.MainPictureBytes = uploadedImage;

                var httpClient = TripGalleryHttpClient.GetClient();

                var serializedTrip = JsonConvert.SerializeObject(vm.Trip);

                var response = await httpClient.PostAsync("api/trips",
                                                          new StringContent(serializedTrip, System.Text.Encoding.Unicode, "application/json")).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index", "Trips"));
                }
                else
                {
                    return(View("Error",
                                new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(response),
                                                    "Trips", "Create")));
                }
            }
            catch (Exception ex)
            {
                return(View("Error", new HandleErrorInfo(ex, "Trips", "Create")));
            }
        }
Пример #2
0
        public async Task <ActionResult> SwitchPrivacyLevel(Guid id, bool isPublic)
        {
            // create a patchdocument to change the privacy level of this trip

            JsonPatchDocument <Trip> tripPatchDoc = new JsonPatchDocument <Trip>();

            tripPatchDoc.Replace(t => t.IsPublic, !isPublic);

            var httpClient = TripGalleryHttpClient.GetClient();

            var rspPatchTrip = await httpClient.PatchAsync("api/trips/" + id.ToString(),
                                                           new StringContent(JsonConvert.SerializeObject(tripPatchDoc), System.Text.Encoding.Unicode, "application/json"))
                               .ConfigureAwait(false);

            if (rspPatchTrip.IsSuccessStatusCode)
            {
                // the patch was succesful.  Reload.
                return(RedirectToAction("Index", "Trips"));
            }
            else
            {
                return(View("Error",
                            new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(rspPatchTrip),
                                                "Trips", "SwitchPrivacyLevel")));
            }
        }
Пример #3
0
        // GET: Trips
        public async Task <ActionResult> Index()
        {
            if (this.User.Identity.IsAuthenticated)
            {
                var identity = this.User.Identity as ClaimsIdentity;
                foreach (var claim in identity.Claims)
                {
                    Debug.WriteLine(claim.Type + " - " + claim.Value);
                }
            }

            var httpClient = TripGalleryHttpClient.GetClient();

            var rspTrips = await httpClient.GetAsync("api/trips").ConfigureAwait(false);

            if (rspTrips.IsSuccessStatusCode)
            {
                var lstTripsAsString = await rspTrips.Content.ReadAsStringAsync().ConfigureAwait(false);

                var vm = new TripsIndexViewModel();
                vm.Trips = JsonConvert.DeserializeObject <IList <Trip> >(lstTripsAsString).ToList();

                return(View(vm));
            }
            else
            {
                return(View("Error",
                            new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(rspTrips),
                                                "Trips", "Index")));
            }
        }
Пример #4
0
        public async Task <ActionResult> About()
        {
            HttpClient client = TripGalleryHttpClient.GetClient();

            var response = await client.GetAsync("http://localhost:51528/api/Pictures");

            ViewBag.Message = "Following content is received from API: " + response.Content.ReadAsStringAsync().Result;

            return(View());
        }
        public async Task <ActionResult> Delete(Guid tripId, Guid pictureId)
        {
            var httpClient = TripGalleryHttpClient.GetClient();

            var response = await httpClient.DeleteAsync("api/trips/" + tripId.ToString() + "/pictures/" + pictureId.ToString())
                           .ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                return(RedirectToAction("Index", new { tripId = tripId }));
            }
            else
            {
                return(View("Error",
                            new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(response),
                                                "Pictures", "Delete")));
            }
        }
Пример #6
0
        // GET: Trips
        public async Task <ActionResult> Index()
        {
            var httpClient = TripGalleryHttpClient.GetClient();

            var rspTrips = await httpClient.GetAsync("api/trips").ConfigureAwait(false);

            if (rspTrips.IsSuccessStatusCode)
            {
                var lstTripsAsString = await rspTrips.Content.ReadAsStringAsync().ConfigureAwait(false);

                var vm = new TripsIndexViewModel();
                vm.Trips = JsonConvert.DeserializeObject <IList <Trip> >(lstTripsAsString).ToList();

                return(View(vm));
            }
            else
            {
                return(View("Error",
                            new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(rspTrips),
                                                "Trips", "Index")));
            }
        }
        public async Task <ActionResult> Index(Guid tripId)
        {
            var httpClient = TripGalleryHttpClient.GetClient();

            var rspPictures = await httpClient.GetAsync("api/trips/" + tripId.ToString() + "/pictures").ConfigureAwait(false);

            if (rspPictures.IsSuccessStatusCode)
            {
                var lstPicturesAsString = await rspPictures.Content.ReadAsStringAsync().ConfigureAwait(false);

                var vm = new PicturesIndexViewModel(
                    JsonConvert.DeserializeObject <IList <Picture> >(lstPicturesAsString).ToList()
                    , tripId);

                return(View(vm));
            }
            else
            {
                return(View("Error",
                            new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(rspPictures),
                                                "Pictures", "Index")));
            }
        }