Beispiel #1
0
        public ActionResult Index(int?trainId = null)
        {
            var routes = _context.Routes
                         .OrderBy(r => r.RouteId).ToArray();
            var vm = new TripEditViewModel()
            {
                TrainId    = trainId,
                RouteNames = routes.Select(r => new SelectListItem()
                {
                    Text  = r.RouteName,
                    Value = r.RouteName
                }).ToArray(),
                StationNames = GetStationNames(),
                RouteIds     = (from route in routes
                                select new SelectListItem()
                {
                    Text = route.RouteName,
                    Value = route.RouteId.ToString()
                }).ToArray(),
                TrainIds = (from train in _context.Trains
                            orderby train.TrainId
                            select new SelectListItem()
                {
                    Text = train.TrainId.ToString(),
                    Value = train.TrainId.ToString()
                }).ToArray(),
                Configurator = new Configurator <Trip, TripRow>()
                               .Configure()
                               .Url(Url.Action(nameof(HandleTrips), new { trainid = trainId }))
            };

            return(View("Index", vm));
        }
        public void Update_DoNotChangeTripPhotoIfDoNotHaveGivenPhoto()
        {
            int monumentId = 1;
            int hotelId    = 2;

            this.dbContext.Monuments.Add(new Monument {
                Id = monumentId
            });
            this.dbContext.Hotels.Add(new Hotel {
                Id = hotelId
            });

            int    tripId   = 3;
            string imageUrl = "testImageUrl";

            this.dbContext.Trips.Add(new Trip {
                Id = tripId, ImageUrl = imageUrl
            });
            this.dbContext.SaveChanges();

            var model = new TripEditViewModel {
                Id = tripId, SelectedHotelId = hotelId, SelectedMonumentId = monumentId
            };

            this.tripsService.Update(model);

            string result = this.dbContext.Trips.First().ImageUrl;

            result.ShouldBe(imageUrl);
        }
        public void Update_UpdateGivenTripCorrectly()
        {
            int tripId     = 1;
            int hotelId    = 2;
            int monumentId = 3;

            this.dbContext.Trips.Add(new Trip {
                Id = tripId
            });
            this.dbContext.Hotels.Add(new Hotel {
                Id = hotelId
            });
            this.dbContext.Monuments.Add(new Monument {
                Id = monumentId
            });
            this.dbContext.SaveChanges();

            string    description = "testDescription";
            string    name        = "testName";
            IFormFile photo       = new Mock <IFormFile>().Object;
            var       model       = new TripEditViewModel
            {
                Id                 = tripId,
                Description        = description,
                Name               = name,
                SelectedHotelId    = hotelId,
                SelectedMonumentId = monumentId,
                Photo              = photo,
            };

            string imageUrl             = "testUrl";
            string imagesDirectory      = "wwwroot/images/trips/";
            string imagesFolderName     = "trips";
            var    mockedImagesUploader = new Mock <ImagesUploader>(null);

            mockedImagesUploader
            .Setup(x => x.Upload(photo, imagesDirectory, imagesFolderName))
            .Returns(imageUrl);

            typeof(TripsService)
            .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
            .First(x => x.FieldType == typeof(ImagesUploader))
            .SetValue(this.tripsService, mockedImagesUploader.Object);

            this.tripsService.Update(model);

            Trip result = this.dbContext.Trips.First();

            result.ShouldSatisfyAllConditions
            (
                () => result.Description.ShouldBe(description),
                () => result.Name.ShouldBe(name),
                () => result.HotelId.ShouldBe(hotelId),
                () => result.MonumentId.ShouldBe(monumentId),
                () => result.ImageUrl.ShouldBe(imageUrl)
            );
        }
Beispiel #4
0
        public IActionResult Edit(TripEditViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(base.RedirectToAction("Edit", new { tripId = model.Id }));
            }

            if (!this.tripsService.CheckForTripOwn(model.Id, this.User.Identity.Name))
            {
                string errorMsg = $"Trip with id {model.Id} is not your trip!";
                return(base.View(GlobalConstants.ErrorViewName, errorMsg));
            }

            this.tripsService.Update(model);

            return(base.RedirectToAction("Details", new { tripId = model.Id }));
        }
        public void Update(TripEditViewModel model)
        {
            Monument monument = this.monumentsService.GetById(model.SelectedMonumentId);
            Hotel    hotel    = this.hotelsService.GetById(model.SelectedHotelId);

            Trip trip = this.GetById(model.Id);

            trip.Name        = model.Name;
            trip.Description = model.Description;
            trip.Monument    = monument;
            trip.Hotel       = hotel;

            if (model.Photo != null)
            {
                trip.ImageUrl = this.imagesUploader.Upload(model.Photo, ImagesDirectory, ImagesFolderName);
            }

            this.dbContext.SaveChanges();
        }