示例#1
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"));
        }