Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Id,BlogCategoryId,Title,Abstract,PostBody,IsReady")] CategoryPost categoryPost, IFormFile formFile)
        {
            if (ModelState.IsValid)
            {
                categoryPost.Created = DateTime.Now;

                categoryPost.ContentType = _imageService.RecordContentType(formFile);
                categoryPost.ImageData   = await _imageService.EncodeFileAsync(formFile);

                //using slugs
                var slug = _slugService.URLFriendly(categoryPost.Title);
                if (_slugService.Isunique(_context, slug))
                {
                    categoryPost.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Title", "This Title has a duplicate slug!");
                    ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name");
                    return(View(categoryPost));
                }
                _context.Add(categoryPost);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", categoryPost.BlogCategoryId);
            return(View(categoryPost));
        }
Exemplo n.º 2
0
        public static async Task SeedCategoryAsync(ApplicationDbContext _context, ISlugService _slugService)
        {
            if (_context.Category.ToList().Count() == 0)
            {
                Category Foods = new Category
                {
                    Name        = "Food",
                    Description = "Food Food Food Food Food Food",
                    Slug        = _slugService.URLFriendly("Food")
                };
                await _context.AddAsync(Foods);

                await _context.SaveChangesAsync();

                Category Drinks = new Category
                {
                    Name        = "Drinks",
                    Description = "Drinks Drinks Drinks Drinks Drinks Drinks Drinks Drinks",
                    Slug        = _slugService.URLFriendly("Drinks")
                };
                await _context.AddAsync(Drinks);

                await _context.SaveChangesAsync();

                Category Snacks = new Category
                {
                    Name        = "Snacks",
                    Description = "Snacks Snacks Snacks Snacks Snacks Snacks Snacks",
                    Slug        = _slugService.URLFriendly("Snacks")
                };
                await _context.AddAsync(Snacks);

                await _context.SaveChangesAsync();
            }
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Description,Content,Price,ImageData,ContentType,IsProductReady,RateValue,CategoryId,ItemSaleOffId,ItemStatusId,Slug")] Item item, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                item.ContentType = _imageService.RecordContentType(image);
                item.ImageData   = await _imageService.EncodeFileAsync(image);

                item.RateValue = 5;

                var slug = _slugService.URLFriendly(item.Name);
                if (_slugService.IsUnique(_context, slug))
                {
                    item.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Name", "This title cannot be used as it results in a duplicate Slug!");
                    ViewData["CategoryId"] = new SelectList(_context.Category, "Id", "Name", item.CategoryId);
                    return(View(item));
                }
                _context.Add(item);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"]    = new SelectList(_context.Category, "Id", "Name", item.CategoryId);
            ViewData["ItemSaleOffId"] = new SelectList(_context.ItemSaleOff, "Id", "Name", item.ItemSaleOffId);
            ViewData["ItemStatusId"]  = new SelectList(_context.ItemStatus, "Id", "Name", item.ItemStatusId);
            return(View(item));
        }
        public async Task <IActionResult> Create([Bind("Id,Title,Abstract,Content,CreateDate,UpdateDate,IsproductionReady,Slug,BlogCategoryId,ImageData")] PostCategory postCategory, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                postCategory.CreateDate = DateTime.Now;
                postCategory.UpdateDate = postCategory.CreateDate;
                postCategory.ViewCount  = 0;

                postCategory.ContentType = _imageService.RecordContentType(image);
                postCategory.ImageData   = await _imageService.EncodeFileAsync(image);


                var slug = _slugService.URLFriendly(postCategory.Title);
                if (_slugService.IsUnique(_context, slug))
                {
                    postCategory.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Title", "This title cannot be used as it results in a duplicate Slug!");
                    ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", postCategory.BlogCategoryId);
                    return(View(postCategory));
                }
                _context.Add(postCategory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", postCategory.BlogCategoryId);
            return(View(postCategory));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                var slug = _slugService.URLFriendly(category.Name);
                if (_slugService.IsUnique(_context, slug))
                {
                    category.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Name", "This title cannot be used as it results in a duplicate Slug!");
                    return(View(category));
                }
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Exemplo n.º 6
0
        public static async Task SeedItemAsync(ApplicationDbContext _context, ISlugService _slugService)
        {
            if (_context.Item.ToList().Count == 0)
            {
                var  normalItemId     = (await _context.ItemStatus.FirstOrDefaultAsync(i => i.Name == "Normal")).Id;
                var  NoSaleOffItemId  = (await _context.ItemSaleOff.FirstOrDefaultAsync(i => i.Name == "None")).Id;
                var  FoodCategoryId   = (await _context.Category.FirstOrDefaultAsync(i => i.Name == "Food")).Id;
                var  DrinksCategoryId = (await _context.Category.FirstOrDefaultAsync(i => i.Name == "Drinks")).Id;
                var  SnacksCategoryId = (await _context.Category.FirstOrDefaultAsync(i => i.Name == "Snacks")).Id;
                var  RateId           = (await _context.Rate.FirstOrDefaultAsync(i => i.Name == "Excellent")).Id;
                Item item1            = new Item
                {
                    Name           = "Snack one",
                    Description    = "This snack is a combination flavour of snack one",
                    Price          = "1",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = SnacksCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This snack is a combination flavour of snack one"),
                    RateValue      = 5
                };
                await _context.AddAsync(item1);

                await _context.SaveChangesAsync();

                Item item2 = new Item
                {
                    Name           = "Snack two",
                    Description    = "This snack is a combination flavour of snack two",
                    Price          = "2",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = SnacksCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This snack is a combination flavour of snack two"),
                    RateValue      = 5
                };
                await _context.AddAsync(item2);

                await _context.SaveChangesAsync();

                Item item3 = new Item
                {
                    Name           = "Snack three",
                    Description    = "This snack is a combination flavour of snack three",
                    Price          = "1.5",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = SnacksCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This snack is a combination flavour of snack three"),
                    RateValue      = 5
                };
                await _context.AddAsync(item3);

                await _context.SaveChangesAsync();

                Item item4 = new Item
                {
                    Name           = "Drinks One",
                    Description    = "This drinks is a combination flavour of Drinks One",
                    Price          = "1.2",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = DrinksCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This drinks is a combination flavour of Drinks One"),
                    RateValue      = 5
                };
                await _context.AddAsync(item4);

                await _context.SaveChangesAsync();

                Item item5 = new Item
                {
                    Name           = "Drinks Two",
                    Description    = "This drinks is a combination flavour of Drinks Two",
                    Price          = "0.7",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = DrinksCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This drinks is a combination flavour of Drinks Two"),
                    RateValue      = 5
                };
                await _context.AddAsync(item5);

                await _context.SaveChangesAsync();

                Item item6 = new Item
                {
                    Name           = "Drinks Three",
                    Description    = "This drinks is a combination flavour of Drinks Three",
                    Price          = "0.5",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = DrinksCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This drinks is a combination flavour of Drinks Three"),
                    RateValue      = 5
                };
                await _context.AddAsync(item6);

                await _context.SaveChangesAsync();

                Item item7 = new Item
                {
                    Name           = "Food One",
                    Description    = "This Food is a combination flavour of Food One",
                    Price          = "1.25",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = FoodCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This Food is a combination flavour of Food One"),
                    RateValue      = 5
                };
                await _context.AddAsync(item7);

                await _context.SaveChangesAsync();

                Item item8 = new Item
                {
                    Name           = "Food Two",
                    Description    = "This Food is a combination flavour of Food Two",
                    Price          = "1.75",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = FoodCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This Food is a combination flavour of Food Two"),
                    RateValue      = 5
                };
                await _context.AddAsync(item8);

                await _context.SaveChangesAsync();

                Item item9 = new Item
                {
                    Name           = "Food Three",
                    Description    = "This Food is a combination flavour of Food Three",
                    Price          = "1.75",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = FoodCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This Food is a combination flavour of Food Three"),
                    RateValue      = 5
                };
                await _context.AddAsync(item9);

                await _context.SaveChangesAsync();

                Item item10 = new Item
                {
                    Name           = "Food Four",
                    Description    = "This Food is a combination flavour of Food Four",
                    Price          = "1.30",
                    ImageData      = null,
                    ContentType    = null,
                    ItemSaleOffId  = NoSaleOffItemId,
                    ItemStatusId   = normalItemId,
                    CategoryId     = FoodCategoryId,
                    Number_Of_Sold = 0,
                    ViewCount      = 0,
                    IsProductReady = true,
                    Slug           = _slugService.URLFriendly("This Food is a combination flavour of Food Four"),
                    RateValue      = 5
                };
                await _context.AddAsync(item10);

                await _context.SaveChangesAsync();
            }
        }