Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, AdEditViewModel ad)
        {
            if (id != ad.ID)
            {
                return(NotFound());
            }

            var userId = _userManager.GetUserId(HttpContext.User);

            if (userId != _context.Ad.SingleOrDefault(x => x.ID == id).CreatedBy)
            {
                return(RedirectToAction(nameof(Index)));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var oldEntry = _context.Ad.FirstOrDefault(x => x.ID == id);
                    _context.Update(new Ad()
                    {
                        Atk         = ad.Atk,
                        Def         = ad.Def,
                        SpeDef      = ad.SpeDef,
                        SpeAtk      = ad.SpeAtk,
                        Hp          = ad.Hp,
                        Level       = ad.Level,
                        Move1       = _context.PokemonMove.First(x => x.ID == ad.Move1),
                        Move2       = _context.PokemonMove.First(x => x.ID == ad.Move2),
                        Move3       = _context.PokemonMove.First(x => x.ID == ad.Move3),
                        Move4       = _context.PokemonMove.First(x => x.ID == ad.Move4),
                        Nature      = _context.PokemonNature.First(x => x.ID == ad.PokemonNature),
                        Pokemon     = _context.Pokemon.First(x => x.ID == ad.PokemonId),
                        Shiny       = ad.Shiny,
                        Speed       = ad.Speed,
                        ID          = ad.ID,
                        CreatedBy   = oldEntry.CreatedBy,
                        ReleaseDate = oldEntry.ReleaseDate
                    });
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AdExists(ad.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(ad));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> EditAd([FromUri] int adId, [FromBody] AdEditViewModel newAd)
        {
            var authtor = await uow.UserService.GetUserById(User.Identity.GetUserId <int>());

            if (authtor == null)
            {
                return(this.Unauthorized());
            }

            if (authtor.IsBlocked)
            {
                return(BadRequest("Your account is blocked."));
            }

            AdDTO ad = await uow.AdService.GetAdById(adId);

            if (ad == null)
            {
                return(NotFound());
            }

            if (ad.User.Id != authtor.Id)
            {
                return(BadRequest("It is not your post."));
            }

            if (ad.IsBlocked)
            {
                return(BadRequest("As is blocked."));
            }

            ad.PositionName        = newAd.PositionName;
            ad.Location            = newAd.Location;
            ad.Company             = newAd.Company;
            ad.PositionDescription = newAd.PositionDescription;
            ad.User     = null;
            ad.Category = null;

            await uow.AdService.EditAd(ad);

            return(Ok("Ad is edited"));
        }
        private async Task <AdEditViewModel> InitializeEditionModel(AdEditViewModel model)
        {
            var checkedFeatures = await this.vehicleElements.GetFeaturesByIdAsync(model.Id);

            var allFeatures = await this.vehicleElements.GetFeaturesAsync();

            allFeatures = new List <Feature>(allFeatures.Select(f => new Feature
            {
                Id        = f.Id,
                Name      = f.Name,
                IsChecked = f.IsChecked = checkedFeatures.Any(c => c.Id == f.Id)
            }));

            model.Vehicle.AllManufacturers = await this.cache.GetAllManufacturersAsync();

            model.Vehicle.AllFuelTypes = await this.cache.GetAllFuelTypesAsync();

            model.Vehicle.AllTransmissionTypes = await this.cache.GetAllTransmissionTypesAsync();

            model.Vehicle.AvailableYears = GetAvailableYears();
            model.Vehicle.AllFeatures    = allFeatures;
            return(model);
        }
        public async Task <IActionResult> Edit(AdEditViewModel model, int id)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.Urls.Any())
            {
                var success = await this.pictures.RemoveAsync(model.Vehicle.Id);

                if (!success)
                {
                    this.ShowNotification(NotificationMessages.InvalidOperation);
                    return(RedirectToAction(nameof(Edit), new { id = model.Id }));
                }
            }

            foreach (var pathUrl in model.Urls)
            {
                await this.pictures.CreateAsync(pathUrl, model.Vehicle.Id);
            }

            var serviceModel = this.mapper.Map <AdEditServiceModel>(model);

            serviceModel.Vehicle.FeatureIds = model.Vehicle
                                              .AllFeatures
                                              .Where(f => f.IsChecked)
                                              .Select(f => f.Id)
                                              .ToList();

            await this.ads.UpdateAsync(serviceModel);

            this.ShowNotification(NotificationMessages.AdUpdatedSuccessfully, NotificationType.Success);
            return(RedirectToAction(nameof(Details), new { id = model.Id }));
        }