コード例 #1
0
        public async Task <IActionResult> UpdateTravelAsync(TripCreateViewModel tripCreateViewModel)
        {
            _parteiDbContext.Travels.Update(tripCreateViewModel.Travel);
            var travel = _parteiDbContext.Travels.Include(t => t.TravelStops).Include(i => i.Images).Single(t => t.TravelId == tripCreateViewModel.Travel.TravelId);

            _parteiDbContext.TravelStops.RemoveRange(travel.TravelStops);
            tripCreateViewModel.Travel = travel;
            _parteiDbContext.SaveChanges();

            if (HttpContext.Request.Form.Files != null)
            {
                var files = HttpContext.Request.Form.Files;
                List <TravelImage> travelImages = new List <TravelImage>();
                List <Image>       images       = new List <Image>();

                foreach (var file in files)
                {
                    travelImages.Add(new TravelImage()
                    {
                        File = file, FileName = file.FileName
                    });
                    images.Add(new Image()
                    {
                        ImageName     = file.FileName,
                        ImageFileSize = int.Parse(file.Length.ToString()),
                        ImageFileType = file.ContentType,
                    });
                }

                if (files.Count > 0)
                {
                    var result = await BlobManager.AddTravelImagesAsync(travelImages);

                    if (result.Successfull)
                    {
                        for (int i = 0; i < files.Count; i++)
                        {
                            images[i].ImageUrl = ((IList <string>)result.Payload)[i];
                        }
                    }
                    if (tripCreateViewModel.Travel.Images.Count > 0)
                    {
                        tripCreateViewModel.Travel.Images.RemoveRange(tripCreateViewModel.Travel.Images.IndexOf(tripCreateViewModel.Travel.Images.First()), tripCreateViewModel.Travel.Images.Count());
                    }
                    tripCreateViewModel.Travel.Images = images;
                }
                else
                {
                    tripCreateViewModel.Travel.Images = new List <Image>();
                }
            }

            _parteiDbContext.SaveChanges();


            foreach (int stop in tripCreateViewModel.SelectedStops)
            {
                TravelStop travelStop = new TravelStop
                {
                    TravelId = tripCreateViewModel.Travel.TravelId,
                    StopId   = stop,
                };
                _parteiDbContext.Add(travelStop);
            }
            _parteiDbContext.SaveChanges();

            return(RedirectToAction("Index", "TripOverview"));
        }
コード例 #2
0
        public async Task <IActionResult> AddTravelAsync(TripCreateViewModel tripCreateViewModel)
        {
            if (HttpContext.Request.Form.Files != null)
            {
                var files = HttpContext.Request.Form.Files;
                List <TravelImage> travelImages = new List <TravelImage>();
                List <Image>       images       = new List <Image>();

                foreach (var file in files)
                {
                    travelImages.Add(new TravelImage()
                    {
                        File = file, FileName = file.FileName
                    });
                    images.Add(new Image()
                    {
                        ImageName     = file.FileName,
                        ImageFileSize = int.Parse(file.Length.ToString()),
                        ImageFileType = file.ContentType,
                    });
                }

                if (files.Count > 0)
                {
                    var result = await BlobManager.AddTravelImagesAsync(travelImages);

                    if (result.Successfull)
                    {
                        for (int i = 0; i < files.Count; i++)
                        {
                            images[i].ImageUrl = ((IList <string>)result.Payload)[i];
                        }
                    }
                    tripCreateViewModel.Travel.Images = images;
                }
                else
                {
                    tripCreateViewModel.Travel.Images = new List <Image>();
                }
            }

            if (tripCreateViewModel.Travel.Images.Count == 0)
            {
                tripCreateViewModel.Travel.Images.Add(new Image
                {
                    ImageUrl      = null,
                    ImageName     = "Kein Foto hochgeladen",
                    ImageFileSize = 0,
                    ImageFileType = null,
                });
            }
            else
            {
                // COSMOS BEGIN
                foreach (var image in tripCreateViewModel.Travel.Images)
                {
                    try
                    {
                        CosmosManager.Images.InsertOne(new CosmosDB.DBModels.Image
                        {
                            BlobUrl   = image.ImageUrl,
                            Id        = CosmosManager.Images.FindSync <CosmosDB.DBModels.Image>(new BsonDocument()).ToList().Count,
                            ImageName = image.ImageName,
                            TravelId  = image.TravelId
                        });
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
                // COSMOS END
            }

            Console.WriteLine(tripCreateViewModel.Travel.Description);
            _parteiDbContext.Add(tripCreateViewModel.Travel);
            _parteiDbContext.SaveChanges();

            foreach (int stop in tripCreateViewModel.SelectedStops)
            {
                TravelStop travelStop = new TravelStop
                {
                    TravelId = tripCreateViewModel.Travel.TravelId,
                    StopId   = stop,
                };
                _parteiDbContext.Add(travelStop);
            }

            _parteiDbContext.SaveChanges();

            return(RedirectToAction("Index", "TripOverview"));
        }