예제 #1
0
        public ActionResult Create(CarItemViewModel model)
        {
            var dbOptions = _carOptionsManager.GetAll().ToList();

            foreach (var item in dbOptions)
            {
                model.AvalibleCarOptions.Add(_carOptionFieldCopier.CopyFields(item, new CarOptionViewModel()));
            }
            ModelState.Clear();
            return(View(model));
        }
        public CarItemViewModel CopyFields(CarItem from, CarItemViewModel to)
        {
            if (to == null)
            {
                throw new NullReferenceException();
            }
            if (from == null)
            {
                throw new NullReferenceException();
            }
            to.Id          = from.Id;
            to.BodyTypeId  = from.BodyTypeId;
            to.ModelId     = from.ModelId;
            to.Description = from.Description;
            to.CarBodyType = from.CarBodyType?.Name;
            to.OwnerId     = from.OwnerId;
            if (@from.CarModel != null)
            {
                to.CarModel       = from.CarModel.Name;
                to.ManufacturerId = @from.CarModel.ManufacturerId;
                to.Manufacturer   = @from.CarModel.Manufacturer?.Name;
                to.CountryId      = @from.CarModel.Manufacturer?.CountryId;
                to.Country        = @from.CarModel.Manufacturer?.Country?.Name;
            }
            to.SelectedCarOptions = new string[from.CarOption.Count];
            var arrayCo = new CarOption[from.CarOption.Count];

            from.CarOption.CopyTo(arrayCo, 0);
            for (var i = 0; i < from.CarOption.Count; i++)
            {
                to.SelectedCarOptions[i] = arrayCo[i].Id.ToString();
            }
            to.EditDate          = from.EditDate;
            to.LastEditorId      = from.LastEditorId;
            to.LastEditorName    = from.AspNetUsers1.UserName;
            to.FuelTypeId        = from.FuelTypeId;
            to.FuelType          = from.FuelType?.Name;
            to.TransmitionTypeId = from.TransmitionTypeId;
            to.TransmitionType   = from.TransmitionType?.Name;
            to.Price             = from.Price;
            to.Volume            = from.Volume;
            to.Status            = (CarItemStatus)from.Status;
            to.ReleaseYear       = from.ReleaseYear;
            return(to);
        }
예제 #3
0
        public ActionResult CreateCar(CarItemViewModel model, List <HttpPostedFileBase> files)
        {
            var userId = User.Identity.GetUserId();

            if (!ModelState.IsValid)
            {
                return(View("Create", model));
            }
            var dbItem = _carItemFieldCopier.CopyFields(model, new CarItem());

            SaveImagesAndLink(dbItem, files);

            dbItem.OwnerId      = userId;
            dbItem.LastEditorId = userId;
            dbItem.EditDate     = DateTime.Now;
            dbItem.Status       = (int)CarItemStatus.Open;

            //add new car into db
            _carItemManager.Add(dbItem);
            //notify all users
            var sb = new StringBuilder();

            if (dbItem.ModelId != null)
            {
                dbItem.CarModel = _carModelManager.GetById((int)dbItem.ModelId);
            }
            if (dbItem.BodyTypeId != null)
            {
                dbItem.CarBodyType = _carBodyTypeManager.GetById((int)dbItem.BodyTypeId);
            }
            if (dbItem.ModelId != null && dbItem.CarModel?.ManufacturerId != null)
            {
                dbItem.CarModel.Manufacturer = _manufacturerManager.GetById((int)dbItem.CarModel.ManufacturerId);
            }
            sb.AppendFormat($"Added : {dbItem.CarModel?.Manufacturer?.Name}");
            sb.AppendFormat($" {dbItem.CarModel?.Name}");
            sb.AppendFormat($" {dbItem.CarBodyType?.Name}");
            sb.AppendFormat($" {dbItem.ReleaseYear}r.y.");
            sb.AppendFormat($" {dbItem.Price}$");
            NotificationsHub.SendInfoMessage(sb.ToString());
            return(RedirectToAction("List"));
        }
예제 #4
0
        public ActionResult Edit(CarItemViewModel model, List <HttpPostedFileBase> files)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var userId = User.Identity.GetUserId();
            var dbItem = _carItemManager.GetById(model.Id);

            if (dbItem == null)
            {
                throw new NullReferenceException();
            }
            if (!User.IsInRole("Admin"))
            {
                if (dbItem.Status != (int)CarItemStatus.Open)
                {
                    throw new SecurityException();
                }
                if (dbItem.OwnerId != userId)
                {
                    throw new SecurityException();
                }
            }
            //delete disabled images
            var dbImagesIds        = dbItem.CarImages.Select(x => x.Id);
            var modelImagesIds     = model.Images.Select(x => x.Id);
            var imagesIdsForDelete = dbImagesIds.Except(modelImagesIds);

            DeleteImages(dbItem, imagesIdsForDelete);
            //add new images
            SaveImagesAndLink(dbItem, files);
            //copy edited fields
            dbItem = _carItemFieldCopier.CopyFields(model, dbItem);
            //edit options
            dbItem.LastEditorId = userId;
            dbItem.EditDate     = DateTime.Now;
            //save changes
            _carItemManager.Edit(dbItem);
            return(RedirectToAction("List"));
        }
        public CarItem CopyFields(CarItemViewModel from, CarItem to)
        {
            if (to == null)
            {
                throw new NullReferenceException();
            }
            if (from == null)
            {
                throw new NullReferenceException();
            }
            to.BodyTypeId  = from.BodyTypeId;
            to.ModelId     = from.ModelId;
            to.Description = from.Description;
            to.OwnerId     = from.OwnerId;

            to.CarOption.Clear();
            if (from.SelectedCarOptions != null)
            {
                foreach (var s in @from.SelectedCarOptions)
                {
                    int id;
                    if (int.TryParse(s, out id))
                    {
                        to.CarOption.Add(_carOptionManager.GetById(id));
                    }
                }
            }
            to.EditDate          = from.EditDate;
            to.LastEditorId      = from.LastEditorId;
            to.FuelTypeId        = from.FuelTypeId;
            to.TransmitionTypeId = from.TransmitionTypeId;
            to.Price             = from.Price;
            to.Volume            = from.Volume;
            to.Status            = (int)from.Status;
            to.ReleaseYear       = from.ReleaseYear;
            return(to);
        }
예제 #6
0
 public CarPage(CarItemViewModel carItemViewModel)
 {
     InitializeComponent();
     BindingContext    = Startup.ServiceProvider.GetService <CarViewModel>();
     _carItemViewModel = carItemViewModel;
 }