예제 #1
0
        public ActionResult AddAd(CarAdInputModel model)
        {
            var userId         = User.Identity.GetUserId();
            var carModelIdGuid = Guid.Parse(model.CarModelId);
            var townIdGuid     = Guid.Parse(model.TownId);

            var selectedCarFeaturesIds = model
                                         .CarFeatures
                                         .Where(x => x.IsChecked == true)
                                         .Select(x => x.Id);

            Guard.WhenArgument(selectedCarFeaturesIds, "selectedCarFeaturesIds").IsNull().Throw();

            this.carAdServices.AddNewCarAd(
                model.Title,
                carModelIdGuid,
                model.CarType,
                model.ManufactureYear,
                model.Mileage,
                model.FuelType,
                model.TransmissionType,
                selectedCarFeaturesIds,
                townIdGuid,
                model.Price,
                model.AdditionalInfo,
                model.CarImageUrl,
                userId);

            return(this.RedirectToAction("BrowseCarAds", "CarAd"));
        }
        public async Task <ActionResult <CreateCarAdOutputModel> > Create(CarAdInputModel input)
        {
            var dealer = await this.dealers.FindByUser(this.currentUser.UserId);

            var category = await this.categories.Find(input.Category);

            if (category == null)
            {
                return(BadRequest(Result.Failure("Category does not exist.")));
            }

            var manufacturer = await this.manufacturers.FindByName(input.Manufacturer);

            manufacturer ??= new Manufacturer
            {
                Name = input.Manufacturer
            };

            var carAd = new CarAd
            {
                Dealer       = dealer,
                Manufacturer = manufacturer,
                Model        = input.Model,
                Description  = input.Description,
                Category     = category,
                ImageUrl     = input.ImageUrl,
                PricePerDay  = input.PricePerDay,
                Options      = new Options
                {
                    HasClimateControl = input.HasClimateControl,
                    NumberOfSeats     = input.NumberOfSeats,
                    TransmissionType  = input.TransmissionType
                }
            };

            this.carAds.Add(carAd);

            var message = new CarAdCreatedMessage
            {
                CarAdId      = carAd.Id,
                Manufacturer = carAd.Manufacturer.Name,
                Model        = carAd.Model,
                PricePerDay  = carAd.PricePerDay,
                ImageUrl     = carAd.ImageUrl
            };

            await this.carAds.Save(message);

            await this.fullTextSearch.Index(new CarAdFullTextSearchModel
            {
                CarAdId     = carAd.Id,
                Description = carAd.Description
            });

            return(new CreateCarAdOutputModel(carAd.Id));
        }
        public async Task <ActionResult> Edit(int id, CarAdInputModel input)
        {
            var dealerId = await this.dealers.GetIdByUser(this.currentUser.UserId);

            var dealerHasCar = await this.dealers.HasCarAd(dealerId, id);

            if (!dealerHasCar)
            {
                return(BadRequest(Result.Failure("You cannot edit this car ad.")));
            }

            var category = await this.categories.Find(input.Category);

            var manufacturer = await this.manufacturers.FindByName(input.Manufacturer);

            manufacturer ??= new Manufacturer
            {
                Name = input.Manufacturer
            };

            var carAd = await this.carAds.Find(id);

            carAd.Manufacturer = manufacturer;
            carAd.Model        = input.Model;
            carAd.Category     = category;
            carAd.ImageUrl     = input.ImageUrl;
            carAd.PricePerDay  = input.PricePerDay;
            carAd.Options      = new Options
            {
                HasClimateControl = input.HasClimateControl,
                NumberOfSeats     = input.NumberOfSeats,
                TransmissionType  = input.TransmissionType
            };

            await this.carAds.Save(carAd);

            await this.publisher.Publish(new CarAdUpdatedMessage
            {
                CarAdId      = carAd.Id,
                Manufacturer = carAd.Manufacturer.Name,
                Model        = carAd.Model
            });

            return(Result.Success);
        }
예제 #4
0
        public ActionResult AddAd()
        {
            this.ViewBag.CarBrandsDropdown = this.GetAllBrandsAsDropDown();
            this.ViewBag.TownsDropDown     = this.GetAllTownsAsDropDown();

            var allFeatures = this.carFeatureServices
                              .GetAllCarFeatures()
                              .ProjectTo <CarFeatureInputViewModel>()
                              .ToList();

            Guard.WhenArgument(allFeatures, "allFeatures").IsNull().Throw();

            var model = new CarAdInputModel
            {
                CarFeatures = allFeatures
            };

            return(this.View(model));
        }