public async Task <Trip> CalculateTripAsync(DayOfWork dow) { _logger.Log(LogLevel.Information, $"{nameof(TripCalculationService)}: {nameof(CalculateTripAsync)}: started", dow); var user = _userService.GetUserByIdentityNumber(dow.User.IdentityNumber); if (user == null) { dow.User = await _userService.AddUserAsync(dow.User); } dow = await _dayOfWorkService.AddDayOfWorkAsync(dow); var trip = new Trip(); while (trip.ElementsAmount < dow.Elements.Count) { var tripBag = CreateTripBag(dow.Elements.Where(e => !trip.Bags.Any(b => b.Elements.Contains(e)))); if (tripBag.AparentBagWeight < _maxWeight) { MergeBagToLighestBag(trip, tripBag); } else { trip.Bags.Add(tripBag); } } trip = await _tripService.AddTripAsync(trip); _logger.LogInformation($"{nameof(TripCalculationService)}: {nameof(CalculateTripAsync)}: finished", trip); return(trip); }
public async Task <IActionResult> Create([Bind("Date,Title,Level,Distance,ElevationGain,Description,MaxParticipants")] Trip trip) { var loggedInMember = await _memberService.GetLoggedInMemberAsync(); PlaceLoggedInNameInViewData(loggedInMember); // Set error message ready if something goes wrong. ViewData["ErrorMessage"] = "An error ocurred trying to add the trip. Please try again."; if (ModelState.IsValid) { // Add trip to database and test result of action try { if (!await _tripService.AddTripAsync(trip)) { return(View(appErrorPath)); } } catch (DbUpdateConcurrencyException) { return(View(appErrorPath)); } // Add signup to this trip for logged in user as leader Signup leaderSignup = new Signup { MemberID = loggedInMember.MemberID, TripID = trip.TripID, AsLeader = true }; // Set error message ready if something goes wrong. ViewData["ErrorMessage"] = "An error ocurred trying to add the leader signup for this trip. Please check in the trip list if the trip has been created without a leader. If so, please delete the trip and try creating it again."; try { if (!await _signupService.AddSignupAsync(leaderSignup)) { return(View(appErrorPath)); } } catch (DbUpdateConcurrencyException) { return(View(appErrorPath)); } ViewData["ErrorMessage"] = ""; return(RedirectToAction(nameof(Index))); } return(View(trip)); }
public async Task <IActionResult> PostTripAsync([FromBody] TripDTO tripDto) { var id = await _tripService.AddTripAsync(tripDto); return(Created($"trips/{id}", null)); }