示例#1
0
        async public static Task <Item> ToItemDbModel(ItemNewModel newModel, IRepository <Category> repository = null)
        {
            var dbModel = new Item {
                Name             = newModel.Name,
                Price            = newModel.Price,
                Image            = FromFileForm(newModel.Image),
                Categories       = null,
                InStock          = newModel.InStock,
                LongDescription  = newModel.LongDescription,
                ShortDescription = newModel.ShortDescription
            };

            if (repository != null && newModel.Categories != null)
            {
                dbModel.Categories = new List <Category>();

                foreach (var category in newModel.Categories)
                {
                    if (category.Checked)
                    {
                        var categoryDb = await repository.GetById(category.Id);

                        if (categoryDb != null)
                        {
                            dbModel.Categories.Add(categoryDb);
                        }
                    }
                }
            }

            return(dbModel);
        }
示例#2
0
        async public Task <IActionResult> Create(ItemNewModel itemModel)
        {
            if (ModelState.IsValid)
            {
                Console.WriteLine("Model valid!");
                var dbModel = await DataMapper.ModelMapper.ToItemDbModel(itemModel, _categoryRepository);

                await _itemRepository.Add(dbModel);

                await _itemRepository.SaveChangesAsync();
            }
            else
            {
                Console.WriteLine("Model invalid!");
                return(View("New", itemModel));
            }

            return(RedirectToAction("Index", "Home"));
        }
示例#3
0
        public static ItemNewModel ToItemNewModel(IEnumerable <Category> allDbCategories)
        {
            var categoriesCheckBoxes = new List <ItemNewCategoriesCheckBoxModel>();

            foreach (var category in allDbCategories)
            {
                categoriesCheckBoxes.Add(new ItemNewCategoriesCheckBoxModel {
                    Checked = false,
                    Id      = category.Id,
                    Name    = category.Name
                });
            }

            var newItemModel = new ItemNewModel {
                //Serialize the categories list because we can't use complex data types in views
                Categories = categoriesCheckBoxes
            };

            return(newItemModel);
        }
        public async Task <IActionResult> AddNew([FromBody] ItemNewModel model)
        {
            try
            {
                if (model == null)
                {
                    return(BadRequest("Error while recieving data"));
                }
                var userID = HttpContext.User.Identity.Name;
                if (userID == null)
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized));
                }
                ApplicationUser user = await _context.Set <ApplicationUser>().SingleOrDefaultAsync(x => x.UserName == userID);

                if (user == null)
                {
                    return(BadRequest("Token is not related to any Account"));
                }
                Account account = _context.Set <Account>().Include(x => x.Cart)
                                  .ThenInclude(x => x.CartItems).FirstOrDefault(x => x.ID == user.AccountID);
                if (user == null || account == null)
                {
                    return(null);
                }
                ItemCategory cat = _context.Set <ItemCategory>().FirstOrDefault(x => x.IsUsed == true);
                if (cat == null)
                {
                    return(StatusCode(StatusCodes.Status503ServiceUnavailable));
                }

                Item item = new Item()
                {
                    CashBack       = 0,
                    CreationDate   = DateTime.Now,
                    ItemCategoryID = cat.ID,
                    IsEnable       = true,

                    Price            = model.Price,
                    AccountID        = account.ID,
                    Name             = model.Name,
                    Quantity         = model.Quantity,
                    Description      = model.Description,
                    Owner            = model.Owner,
                    Mobile           = model.Mobile,
                    Location         = model.Location,
                    ShortDescription = model.Description.Substring(0, Math.Min(25, model.Description.Length))
                };
                #region Save Main Image
                if (model.Thumbnail != null && model.Thumbnail.Length > 0)
                {
                    item.ThumbnailImagePath = ImageOperations.SaveItemImage(model.Thumbnail, item);
                    var itemImage = new ItemImage()
                    {
                        Item = item
                    };
                    itemImage.ImagePath = ImageOperations.SaveItemImage(model.Thumbnail, itemImage);
                    if (!string.IsNullOrWhiteSpace(itemImage.ImagePath))
                    {
                        item.ItemImages.Add(itemImage);
                    }
                    else
                    {
                        itemImage.Item = null;
                    }
                }
                #endregion

                #region Save Other images
                if (model.Images != null && model.Images.Any(x => x.Length > 0))
                {
                    foreach (var file in model.Images.Where(x => x.Length > 0))
                    {
                        var itemImage = new ItemImage()
                        {
                            Item = item
                        };
                        itemImage.ImagePath = ImageOperations.SaveItemImage(file, itemImage);
                        if (!string.IsNullOrWhiteSpace(itemImage.ImagePath))
                        {
                            item.ItemImages.Add(itemImage);
                        }
                        else
                        {
                            itemImage.Item = null;
                        }
                    }
                }
                #endregion

                var data = _context.Set <Item>().Add(item);
                _context.SaveChanges();
                return(Ok(item.ID));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }