Пример #1
0
        public static void SaveListing(int listingId, int modelId, int bodyStyleid, int interiorColorid, int exteriorColorid,
                                       Condition condition, Transmission transmission, int mileage, int modelYear, string VIN, decimal MSRP,
                                       decimal salePrice, string vdesc, string imageFileUrl, bool isFeatured, bool isSold, int month, int day, int year, bool expected)
        {
            DateTime DateAdded = new DateTime(year, month, day);

            ListingManager manager = new ListingManager(new ListingMemRepo_GOODDATA());

            Listing listing = new Listing
            {
                ListingId          = listingId,
                ModelId            = modelId,
                BodyStyleId        = bodyStyleid,
                InteriorColorId    = interiorColorid,
                ExteriorColorId    = exteriorColorid,
                Condition          = condition,
                Transmission       = transmission,
                Mileage            = mileage,
                ModelYear          = modelYear,
                VIN                = VIN,
                MSRP               = MSRP,
                SalePrice          = salePrice,
                VehicleDescription = vdesc,
                ImageFileUrl       = imageFileUrl,
                IsFeatured         = isFeatured,
                IsSold             = isSold,
                DateAdded          = DateAdded,
            };

            var test = manager.SaveListing(listing);

            Assert.AreEqual(expected, test.Success);
        }
Пример #2
0
        public ActionResult AddVehicle(AddListingVM model)
        {
            _listingManager = ListingManagerFactory.Create();

            if (ModelState.IsValid)
            {
                try
                {
                    model.Listing.DateAdded = DateTime.Now;

                    if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                    {
                        var savepath = Server.MapPath("~/Images/");

                        string fileName  = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName);
                        string extension = Path.GetExtension(model.ImageUpload.FileName);

                        var filePath = Path.Combine(savepath, fileName + extension);

                        int counter = 1;
                        while (System.IO.File.Exists(filePath))
                        {
                            filePath = Path.Combine(savepath, fileName + counter.ToString() + extension);
                            counter++;
                        }

                        model.ImageUpload.SaveAs(filePath);
                        model.Listing.ImageFileUrl = Path.GetFileName(filePath);
                    }


                    //send to manager and repo
                    var listingResponse = _listingManager.SaveListing(model.Listing);

                    if (listingResponse.Success)
                    {
                        return(RedirectToAction("EditVehicle", new { id = listingResponse.Payload.ListingId }));
                    }
                    else
                    {
                        return(new HttpStatusCodeResult(500, $"Error in cloud. Message:" +
                                                        $"{listingResponse.Message}"));
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Something wrong happened while trying to add a new listing:", ex);
                }
            }
            else
            {
                //reset page with items
                _makeManager          = MakeManagerFactory.Create();
                _modelManager         = ModelManagerFactory.Create();
                _interiorColorManager = InteriorColorManagerFactory.Create();
                _exteriorColorManager = ExteriorColorManagerFactory.Create();
                _bodyStyleManager     = BodyStyleManagerFactory.Create();

                //load all the items
                var modelResponse    = _modelManager.GetAllModels();
                var makeResponse     = _makeManager.GetAllMakes();
                var interiorResponse = _interiorColorManager.GetAll();
                var exteriorReponse  = _exteriorColorManager.GetAll();
                var bodyResponse     = _bodyStyleManager.GetAll();

                //verify they all loaded
                if (!modelResponse.Success ||
                    !makeResponse.Success ||
                    !interiorResponse.Success ||
                    !exteriorReponse.Success ||
                    !bodyResponse.Success)
                {
                    return(new HttpStatusCodeResult(500, $"Error in cloud. Message:" +
                                                    $"{modelResponse.Message} " +
                                                    $"{makeResponse.Message}" +
                                                    $"{interiorResponse.Message}" +
                                                    $"{exteriorReponse.Message}" +
                                                    $"{bodyResponse.Message}"));
                }
                else
                {
                    //create select list items
                    model.Makes = makeResponse.Payload.Select(m => new SelectListItem
                    {
                        Text  = m.MakeName,
                        Value = m.MakeId.ToString()
                    });

                    model.Models = modelResponse.Payload.Select(m => new SelectListItem
                    {
                        Text  = m.ModelName,
                        Value = m.ModelId.ToString()
                    });

                    model.ExteriorColors = exteriorReponse.Payload.Select(m => new SelectListItem
                    {
                        Text  = m.ExteriorColorName,
                        Value = m.ExteriorColorId.ToString()
                    });

                    model.InteriorColors = interiorResponse.Payload.Select(m => new SelectListItem
                    {
                        Text  = m.InteriorColorName,
                        Value = m.InteriorColorId.ToString()
                    });

                    model.BodyStyles = bodyResponse.Payload.Select(m => new SelectListItem
                    {
                        Text  = m.BodyStyleName,
                        Value = m.BodyStyleId.ToString()
                    });

                    return(View(model));
                }
            }
        }