Example #1
0
        public async Task <IActionResult> Edit(TripEditInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction("Edit", new { id = inputModel.Id }));
            }

            if (inputModel.DateOfDeparture.Date.CompareTo(DateTime.Now.Date) < 0 ||
                (inputModel.DateOfDeparture.Date.CompareTo(DateTime.Now.Date) == 0 &&
                 inputModel.TimeOfDeparture.TimeOfDay.TotalMinutes < DateTime.Now.TimeOfDay.TotalMinutes))
            {
                return(this.RedirectToAction("Edit", new { id = inputModel.Id }));
            }

            var tripId = await this.tripsService.UpdateAsync(inputModel);

            if (tripId == null)
            {
                return(this.RedirectToAction("BadRequest", "Errors"));
            }

            this.TempData["Notification"] = "Trip was successfully edited.";

            return(this.RedirectToAction("Details", new { id = tripId }));
        }
Example #2
0
        public async Task EditAsyncShouldThrowNullReferenceIfTripDoesNotExist()
        {
            await this.InitializeAsync();

            var nonExistenceModel = new TripEditInputModel
            {
                Id = "nonExistenceId",
            };

            await Assert.ThrowsAsync <NullReferenceException>(() => this.tripsService.EditAsync(nonExistenceModel));
        }
Example #3
0
        public async Task <IActionResult> Edit(TripEditInputModel tripToEditViewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(tripToEditViewModel));
            }

            await this.tripsService.EditAsync(tripToEditViewModel);

            return(this.RedirectToAction("Details", "Trips", new { area = "", id = tripToEditViewModel.Id }));
        }
Example #4
0
        public async Task EditAsync(TripEditInputModel tripToEdit)
        {
            var trip = this.unitOfWork.Trips.All().FirstOrDefault(t => t.Id == tripToEdit.Id);

            if (trip == null)
            {
                throw new NullReferenceException($"Activity with id {tripToEdit.Id} not found");
            }

            trip.FromDestinationName   = tripToEdit.FromDestinationName;
            trip.ToDestinationName     = tripToEdit.ToDestinationName;
            trip.DateOfDeparture       = tripToEdit.DateOfDeparture;
            trip.AdditionalInformation = tripToEdit.AdditionalInformation;
            trip.CarId = tripToEdit.CarId;

            this.unitOfWork.Trips.Update(trip);
            await this.unitOfWork.CompleteAsync();
        }
Example #5
0
        public async Task <string> UpdateAsync(TripEditInputModel inputModel)
        {
            var trip = await this.tripsRepository
                       .All()
                       .FirstOrDefaultAsync(t => t.Id == inputModel.Id);

            if (trip == null)
            {
                return(null);
            }

            trip.DateOfDeparture       = inputModel.DateOfDeparture;
            trip.TimeOfDeparture       = inputModel.TimeOfDeparture;
            trip.FreeSeats             = inputModel.FreeSeats;
            trip.ExpensePerPerson      = inputModel.ExpensePerPerson;
            trip.AdditionalInformation = inputModel.AdditionalInformation;

            this.tripsRepository.Update(trip);
            await this.tripsRepository.SaveChangesAsync();

            return(trip.Id);
        }
Example #6
0
        public async Task EditAsyncShouldEditCorrectly()
        {
            await this.InitializeAsync();

            var tripId = Guid.NewGuid().ToString();

            await this.dbContext.Users.AddAsync(new ApplicationUser
            {
                UserName = "******",
            });

            await this.dbContext.Trips.AddAsync(new Trip
            {
                Id = tripId,
                FromDestinationName = "Silistra",
                ToDestinationName   = "Sofia",
            });

            await this.dbContext.SaveChangesAsync();

            var editModel = new TripEditInputModel
            {
                Id = tripId,
                FromDestinationName = "Varna",
                ToDestinationName   = "Burgas",
            };

            await this.tripsService.EditAsync(editModel);

            var result = await this.dbContext.Trips.FirstOrDefaultAsync();

            var exceptionResult = await this.dbContext.Trips.FirstOrDefaultAsync(x => x.Id == "nonExistenceId");

            Assert.NotNull(result);
            Assert.Equal("Varna", result.FromDestinationName);
            Assert.Equal("Burgas", result.ToDestinationName);
        }
        public ActionResult Edit(TripEditInputModel editModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(editModel);
            }

            var trip = this.TripServices.GetById(editModel.Id);

            if (trip.Driver.Id != this.User.Identity.GetUserId())
            {
                throw new Exception("Not authorized to edit.");
            }

            string[] usernamesToBeRemoved = new List<string>().ToArray();
            if (!string.IsNullOrEmpty(editModel.UsernamesToBeRemoved))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                usernamesToBeRemoved = js.Deserialize<string[]>(editModel.UsernamesToBeRemoved);
            }

            var serviceResponse = this.TripServices.Edit(
                trip.Id,
                editModel.DateOfLeaving,
                editModel.LeftAvailableSeats,
                editModel.PlaceOfLeaving,
                editModel.PickUpFromAddress,
                editModel.Description,
                editModel.ETA,
                usernamesToBeRemoved);

            NotificationHub.UpdateNotify(serviceResponse.SignalRModel);
            return this.RedirectToRoute("TripDetails", new { id = trip.Id, slug = trip.From.Name, area = string.Empty });
        }