public async Task <IActionResult> Create(CreateTripViewModel tripModel) { var user = await GetCurrentUserAsync(); if (ModelState.IsValid) { _context.Add(tripModel); tripModel.user = user; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { List <Client> clients = await _context.Client.ToListAsync(); var viewModel = new CreateTripViewModel() { ClientOptions = clients.Where(c => c.ApplicationUserId == user.Id).Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.FullName }).ToList() }; return(View(viewModel)); } }
public ActionResult Create(CreateTripViewModel model) { if (ModelState.IsValid) { if (model.Description == null) { model.Description = UiMessageConstants.NoTripDescription; } if (this.trips.GetTripByName(model.TripName) != null) { ModelState.AddModelError("TripName", ErrorMessageConstants.TripNameAlreadyExists); return(View(model)); } if (model.TripDate < DateTime.UtcNow.AddMinutes(model.LocalTimeOffsetMinutes)) { ModelState.AddModelError("TripDate", ErrorMessageConstants.TripDateInThePast); return(View(model)); } var trip = this.mapper.Map <Trip>(model); this.trips.AddTrip(trip, model.TripDate, model.TripTime); this.cacheService.Remove("trips"); var viewModel = this.mapper.Map <FullTripViewModel>(trip); return(RedirectToAction("ById", new { urlId = viewModel.UrlId })); } return(View()); }
public ActionResult Create(CreateTripViewModel tripInfo) { if (!ModelState.IsValid) { return(this.View(tripInfo)); } var hourOfDeparture = TimeSpan.ParseExact(tripInfo.HourOfDeparture, @"hh\:mm", null); var timeOfDeparture = tripInfo.DateOfDeparture.Add(hourOfDeparture); var currentUSerId = this.GetLoggedUserId(); var selectedTags = tripInfo.Tags ?.Where(x => x.IsSelected) ?.Select(x => x.Id); selectedTags = selectedTags ?? new List <int>(); var trip = this.mappingProvider.Map <CreateTripViewModel, TripCreationInfo>(tripInfo); trip.TimeOfDeparture = timeOfDeparture; trip.TagIds = selectedTags; trip.DriverId = currentUSerId; this.tripService.CreateTrip(trip); return(RedirectToAction(nameof(this.Create))); }
public void PassCorrectTripInfo_ToTripService() { // Arrange var mockedTripService = new Mock <ITripService>(); var mockedTagService = new Mock <ITagService>(); var mockedCarService = new Mock <ICarService>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var controller = new TripController( mockedTripService.Object, mockedTagService.Object, mockedCarService.Object, mockedMappingProvider.Object); string loggedUserId = "LoggedusrId"; controller.GetLoggedUserId = () => loggedUserId; var viewModel = new CreateTripViewModel() { HourOfDeparture = "12:14" }; var tripCreationInfo = new TripCreationInfo(); mockedMappingProvider.Setup(x => x.Map <CreateTripViewModel, TripCreationInfo>(viewModel)) .Returns(tripCreationInfo); // Act and Assert controller.WithCallTo(x => x.Create(viewModel)) .ShouldRedirectTo(x => x.Create()); mockedTripService.Verify(x => x.CreateTrip(tripCreationInfo), Times.Once); }
public ActionResult Create(Guid customerId) { var newTrip = new CreateTripViewModel { CustomerId = customerId }; return(View(newTrip)); }
public async Task <IActionResult> CreateAsync([FromBody] CreateTripViewModel createTripViewModel) { var createTripDto = _mapper.Map <CreateTripDto>(createTripViewModel); var createdTripDto = await _tripService.CreateAsync(createTripDto); var createdTripViewModel = _mapper.Map <TripViewModel>(createdTripDto); return(Ok(createdTripViewModel)); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { CreateTripViewModel model = validationContext?.ObjectInstance as CreateTripViewModel; if (model != null && model.StartDate >= model.EndDate) { return(new ValidationResult("Start Date should be before End Date!")); } return(ValidationResult.Success); }
public IActionResult Add(CreateTripViewModel model) { if (!ModelState.IsValid) { return(this.Redirect("/Trips/Add")); } this.tripService.AddTrip(model); return(this.Redirect("/Trips/All")); }
public async Task CreateTripAsync(CreateTripViewModel trip) { var request = new CreateTripHandlerRequest { CustomerId = trip.CustomerId, Arrival = trip.Arrival, Departure = trip.Departure, Title = trip.Title }; await CallHandlerAsync <CreateTripHandlerRequest, CreateTripHandlerResponse>(request); }
// ------------------------------------------------------------------START OF CREATE NEW TRIP // GET: Trips/Create public async Task <IActionResult> Create() { //get continents to build out drop down in viewmodel List <Continent> AllContinents = await _context.Continent.ToListAsync(); List <SelectListItem> allContinentOptions = new List <SelectListItem>(); foreach (Continent c in AllContinents) { SelectListItem sli = new SelectListItem(); sli.Text = c.Name; sli.Value = c.ContinentId.ToString(); allContinentOptions.Add(sli); } ; SelectListItem defaultSli = new SelectListItem { Text = "Select Continent", Value = "0" }; allContinentOptions.Insert(0, defaultSli); CreateTripViewModel viewmodel = new CreateTripViewModel { AllContinentOptions = allContinentOptions }; //get TravelTypes to build out secect checkboxes in the the viewmodel viewmodel.AllTravelTypes = _context.TravelType .AsEnumerable() .Select(li => new SelectListItem { Text = li.Type, Value = li.TravelTypeId.ToString() }).ToList(); ; // viewmodel.LocationTypes = await _context.LocationType.ToListAsync(); ViewData["scripts"] = new List <string>() { "CreateTrip" }; return(View(viewmodel)); }
public void AddTrip(CreateTripViewModel model) { var trip = new Trip { DepartureTime = model.DepartureTime, Description = model.Description, EndPoint = model.EndPoint, ImagePath = model.ImagePath, StartPoint = model.StartPoint, Seats = model.Seats, }; this.context.Trips.Add(trip); this.context.SaveChanges(); }
public void Test_Trip_EndsAt_Validation( string testCaseDisplayName, DateTimeOffset?invalidEndsAt) { // Arrange CreateTripViewModel createTripViewModel = Get_CreateTripViewModel(endsAt: invalidEndsAt); // Act bool result = Validator.TryValidateObject(createTripViewModel, new ValidationContext(createTripViewModel, null, null), null, true); // Assert Assert.IsFalse(result, testCaseDisplayName); }
public void Test_Trip_Description_Validation( string testCaseDisplayName, string invalidDescription) { // Arrange CreateTripViewModel createTripViewModel = Get_CreateTripViewModel(description: invalidDescription); // Act bool result = Validator.TryValidateObject(createTripViewModel, new ValidationContext(createTripViewModel, null, null), null, true); // Assert Assert.IsFalse(result, testCaseDisplayName); }
public void Test_Trip_Title_Validation( string testCaseDisplayName, string validTitle) { // Arrange CreateTripViewModel createTripViewModel = Get_Valid_CreateTripViewModel(title: validTitle); // Act bool result = Validator.TryValidateObject(createTripViewModel, new ValidationContext(createTripViewModel, null, null), null, true); // Assert Assert.IsTrue(result, testCaseDisplayName); }
public async Task <IActionResult> Create() { var user = await GetCurrentUserAsync(); List <Client> clients = await _context.Client.ToListAsync(); var viewModel = new CreateTripViewModel() { ClientOptions = clients.Where(c => c.ApplicationUserId == user.Id).Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.FullName }).ToList() }; return(View(viewModel)); }
public void AtachOnlySelectedTagsToTheModel() { // Arrange var mockedTripService = new Mock <ITripService>(); var mockedTagService = new Mock <ITagService>(); var mockedCarService = new Mock <ICarService>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var controller = new TripController( mockedTripService.Object, mockedTagService.Object, mockedCarService.Object, mockedMappingProvider.Object); string loggedUserId = "LoggedusrId"; controller.GetLoggedUserId = () => loggedUserId; var viewModel = new CreateTripViewModel() { HourOfDeparture = "12:14", Tags = new List <TagViewModel>() { new TagViewModel() { Id = 1, IsSelected = true }, new TagViewModel() { Id = 2, IsSelected = false }, } }; var tripCreationInfo = new TripCreationInfo(); mockedMappingProvider.Setup(x => x.Map <CreateTripViewModel, TripCreationInfo>(viewModel)) .Returns(tripCreationInfo); // Act and Assert controller.WithCallTo(x => x.Create(viewModel)) .ShouldRedirectTo(x => x.Create()); mockedTripService.Verify(x => x.CreateTrip(tripCreationInfo), Times.Once); Assert.AreEqual(1, tripCreationInfo.TagIds.Count()); }
public IHttpActionResult CreateTrip([FromBody] CreateTripViewModel request) { string username = Utilities.GetUserNameFromRequest(Request); if (username == null) { return(BadRequest("Access Denied")); } CreateTripViewModelResponse response = new CreateTripViewModelResponse() { Status_code = "200", Message = "OK", Earning = 0, NumberOrder = 0, }; try { request.FromDate.Replace("%2F", "/"); request.ToDate.Replace("%2F", "/"); string userId = _userService.GetUsers().Where(u => u.UserName.Equals(username)).FirstOrDefault()?.Id; Trip trip = new Trip() { UserId = userId, FromCityId = _cityService.GetCities().FirstOrDefault(c => c.Code.Equals(request.FromCityGeonameId)).Id, ToCityId = _cityService.GetCities().FirstOrDefault(c => c.Code.Equals(request.ToCityGeonameId)).Id, FromDate = DateTime.ParseExact(request.FromDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), ToDate = DateTime.ParseExact(request.ToDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), Name = "" }; _tripService.CreateTrip(trip); _tripService.SaveTrip(); response.TripId = trip.Id; response.DateCreated = trip.DateCreated.GetValueOrDefault().ToString("dd/MM/yyyy"); } catch (Exception ex) { return(BadRequest(ex.ToString())); } return(Ok(response)); }
public async Task <CommandResult <Guid> > CreateTrip([FromBody] CreateTripViewModel viewModel) { var createTripModel = _mapper.Map <WebSiteTripModel>(viewModel); var result = await _tripManager.CreateTrip(createTripModel); if (result.IsSucceeded) { foreach (var image in viewModel.Images) { this.ConfirmImageAdded(image); this.ConfirmFileAdded(image); } if (!string.IsNullOrEmpty(viewModel.PdfName)) { this.ConfirmFileAdded(viewModel.PdfName); } } return(result); }
public async Task <ActionResult> Create(CreateTripViewModel tripmodel) { try { HttpResponseMessage response = await PostJsonEncodedContent(tripmodel.Excursionn); if ((response.StatusCode == System.Net.HttpStatusCode.OK) && !string.IsNullOrEmpty(await response.Content.ReadAsStringAsync())) { return(RedirectToAction("Index", "Home")); } else { return(View()); } } catch { return(View()); } }
public IActionResult Create(CreateTripViewModel model) { if (!ModelState.IsValid) { IEnumerable <SelectListItem> countries = this.countries.All() .Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }); IEnumerable <SelectListItem> companies = this.companies.AllByUser(User.Identity.Name) .Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }); model.Companies = companies; model.Countries = countries; return(View(model)); } this.trips.Create(model.Name, model.Company, model.Destination, model.Capacity, model.Price, model.StartDate, model.EndDate); this.AddTempDataNotification("Trip created!", NotificationType.Success); return(RedirectToAction(nameof(Index), new { id = model.Company })); }
public ActionResult Create() { var tags = this.tagService.GetAllTags(); var userId = this.GetLoggedUserId(); var cars = this.carService.GetUserCars(userId); if (cars == null || cars.Count() < 1) { return(this.RedirectToAction(nameof(ManageController.RegisterCar), "Manage")); } var model = new CreateTripViewModel(); var carsVModel = this.mappingProvider.Map <IEnumerable <CarBasicInfo>, IEnumerable <CarViewModel> >(cars); var tagsvModel = this.mappingProvider.Map <IEnumerable <TagInfo>, IList <TagViewModel> >(tags); model.UserCars = carsVModel; model.Tags = tagsvModel; return(View(model)); }
public void ReturnDefaulView_WhenModelStateIsNotValid() { // Arrange var mockedTripService = new Mock <ITripService>(); var mockedTagService = new Mock <ITagService>(); var mockedCarService = new Mock <ICarService>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var controller = new TripController( mockedTripService.Object, mockedTagService.Object, mockedCarService.Object, mockedMappingProvider.Object); controller.ModelState.AddModelError("", ""); var viewModel = new CreateTripViewModel(); // Act and Assert controller.WithCallTo(x => x.Create(viewModel)) .ShouldRenderDefaultView() .WithModel(viewModel); }
public ActionResult Create(CreateTripViewModel viewModel) { if (ModelState.IsValid) { Trip trip = new Trip(); trip.Title = viewModel.Title; trip.Description = viewModel.Description; trip.From = viewModel.From; trip.To = viewModel.To; trip.DepartureDate = viewModel.DepartureDate; trip.ReturnDate = viewModel.DepartureDate; trip.Price = viewModel.Price; trip.Coach = new Coach(); trip.Coach.Capacity = viewModel.Capacity; db.Trips.Add(trip); db.SaveChanges(); return(RedirectToAction("Index")); } return(RedirectToAction("Index")); }
public ActionResult CreateTrip(CreateTripViewModel tripToRegister) { if (!this.ModelState.IsValid) { return this.View(tripToRegister); } var locations = this.locations.GetAll().ToList(); this.ViewBag.Locations = new SelectList(locations, "Id", "Name"); var tripToCreate = new Trip() { Description = tripToRegister.Description, StartDate = tripToRegister.StartDate, AvailableSeats = tripToRegister.AvailableSeats, FromId = tripToRegister.From, ToId = tripToRegister.To, DriverId = this.User.Identity.GetUserId() }; this.trips.Create(tripToCreate); return this.RedirectToAction("Index", "Home", new { area = string.Empty }); }
public async Task <IActionResult> Create(CreateTripViewModel viewmodel) { ModelState.Remove("Trip.User"); ModelState.Remove("Trip.UserId"); ApplicationUser user = await GetCurrentUserAsync(); viewmodel.Trip.UserId = user.Id; viewmodel.Trip.IsPreTrip = true; if (ModelState.IsValid) { _context.Add(viewmodel.Trip); //checks to see if there are selectedTravelTypeIds to loop over if (viewmodel.SelectedTravelTypeIds != null) { //makes joiner table for TripTravelType foreach (int TypeId in viewmodel.SelectedTravelTypeIds) { TripTravelType newTripTT = new TripTravelType() { //pulls tripid out of context bag TripId = viewmodel.Trip.TripId, TravelTypeId = TypeId }; _context.Add(newTripTT); } } //this runs though all the inputed food places and makes a joiner table for it if (viewmodel.EnteredTripFoodLocations != null) { foreach (TripVisitLocation foodL in viewmodel.EnteredTripFoodLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = viewmodel.Trip.TripId, LocationTypeId = 1, Name = foodL.Name, Description = foodL.Description, IsCompleted = false }; _context.Add(newTripVL); } } //this runs though all the inputed food places and makes a joiner table for it if (viewmodel.EnteredTripVisitLocations != null) { foreach (TripVisitLocation placeL in viewmodel.EnteredTripVisitLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = viewmodel.Trip.TripId, LocationTypeId = 2, Name = placeL.Name, Description = placeL.Description, IsCompleted = false }; _context.Add(newTripVL); } } await _context.SaveChangesAsync(); return(RedirectToAction("PlannedTrips", "Trips")); } return(View(viewmodel)); }
public CreateTripView() { InitializeComponent(); BindingContext = new CreateTripViewModel(this.Navigation); }
// GET: Trips/Create public ActionResult Create() { CreateTripViewModel viewModel = new CreateTripViewModel(); return(View(viewModel)); }
public async Task <ActionResult> CreateAsync(CreateTripViewModel trip) { await _tripService.CreateTripAsync(trip); return(RedirectToAction("Index", new { customerId = trip.CustomerId })); }