public async Task <IActionResult> Edit(string id, SharedTravelEditInputViewModel input)
        {
            if (id != input.Id)
            {
                return(this.NotFound());
            }

            var userId = this.userManager.GetUserId(this.User);

            if (this.ModelState.IsValid)
            {
                var isEdited = await this.sharedTravelsService.UpdateAsync(id, input, userId);

                if (!isEdited)
                {
                    return(this.NotFound());
                }

                return(this.RedirectToAction(nameof(this.Index)));
            }

            var citiesAsKVP = await this.castCollectionsService.GetCitiesAsKeyValuePairs();

            var typesAsKVP = await this.castCollectionsService.GetTravelTypesAsKeyValuePairs();

            var advert = await this.sharedTravelsService.GetViewModelByIdAsync(id);

            advert.Id            = id;
            advert.Cities        = citiesAsKVP;
            advert.TypesOfTravel = typesAsKVP;

            return(this.View(advert));
        }
        public async Task <bool> UpdateAsync(string id, SharedTravelEditInputViewModel input, string userId)
        {
            var isAdmin = await this.IsAdmin(userId);

            var statusId = this.sharedTravelStatusRepository
                           .AllAsNoTracking()
                           .Where(x => x.Name == ServicesConstants.AdvertDefaultStatus)
                           .Select(x => x.Id)
                           .FirstOrDefault();

            var advert = await this.sharedTravelsRepository.All()
                         .FirstOrDefaultAsync(x => x.Id == id && (x.CreatedById == userId || isAdmin));

            if (advert == null)
            {
                return(false);
            }

            advert.Description        = input.Description;
            advert.EndDestinationId   = input.EndDestinationId;
            advert.StartDestinationId = input.StartDestinationId;
            advert.PlaceForLuggage    = IsTrue(input.PlaceForLuggage);
            advert.WithReturnTrip     = IsTrue(input.WithReturnTrip);
            advert.SmokeRestriction   = IsTrue(input.SmokeRestriction);
            advert.Seats            = input.Seats;
            advert.StartDateAndTime = input.StartDateAndTime;
            advert.TypeOfTravelId   = input.TypeOfTravelId;
            advert.StatusId         = statusId;
            advert.IsApproved       = input.IsApproved;

            this.sharedTravelsRepository.Update(advert);
            await this.sharedTravelsRepository.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(string id, SharedTravelEditInputViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                ////??????
                return(this.View("Error"));
            }

            input.IsApproved = true;

            var userId = this.userManager.GetUserId(this.User);

            var hasEdited = await this.sharedTravelsService.UpdateAsync(id, input, userId);

            if (hasEdited)
            {
                this.TempData["Message"] = "Your advert has been edited";
            }

            return(this.RedirectToAction(
                       nameof(this.TripDetails),
                       new RouteValueDictionary(new { controller = "SharedTravels", action = "TripDetails", Id = id })));
        }