Ejemplo n.º 1
0
        private async Task <int> GetMaxPage(int PageSize)
        {
            int numProducts = await ClothingDb.GetNumClothing(_context);

            int maxPage = Convert.ToInt32(Math.Ceiling((double)numProducts / PageSize));

            return(maxPage);
        }
Ejemplo n.º 2
0
        private async Task <int> GetMaxPage(int PageSize)
        {
            int numProducts = await ClothingDb.GetNumClothing(_context);

            int maxPage = Convert.ToInt32(Math.Ceiling((double)numProducts / PageSize)); //need to cast a double for dividing of ints, then use Math.ceiling to go to next int up

            return(maxPage);
        }
        [ActionName("Delete")] //workaround because we can't have two methods with the same parameters with the same name
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            Clothing c = await ClothingDb.GetClothingById(id, _context);

            await ClothingDb.Delete(c, _context);

            TempData["Message"] = $"{c.Title} deleted successfully";
            return(RedirectToAction(nameof(ShowAll))); //turns "ShowAll" into a string
        }
Ejemplo n.º 4
0
        private async Task <int> GetMaxPage(int PageSize)
        {
            int numProducts = await ClothingDb.GetNumClothing(_context) / PageSize;

            // round up always, no partial page number (2.1 pages = 3 pages)
            int maxPage = Convert.ToInt32(Math.Ceiling((double)numProducts / PageSize));

            return(maxPage);
        }
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            Clothing c = await ClothingDb.GetClothingByID(id, _context);

            await ClothingDb.Delete(c, _context);

            TempData["Msg"] = $"{c.Title} Deleted Successfully.";
            return(RedirectToAction(nameof(ShowAll)));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> DeleteConfirmed(int id) /*change the method name, still posting to delete, just calling a different method, work around*/
        {
            Clothing c = await ClothingDb.GetClothingById(id, _context);

            await ClothingDb.Delete(c, _context);

            TempData["Message"] = $"{c.Title} deleted successfully";
            return(RedirectToAction(nameof(ShowAll)));
        }
        // Add a single product to the shopping cart.
        public async Task <IActionResult> AddToCartAsync(int id, string prevUrl)
        {
            Clothing c = await ClothingDb.GetClothingById(id, _context);

            if (c != null)
            {
                CartHelper.Add(c, _http);
            }
            return(Redirect(prevUrl));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Delete(int id)
        {
            Clothing c = await ClothingDb.GetClothingById(id, _context);

            if (c == null) // If clothing does not exist
            {
                return(NotFound());
            }
            return(View(c));
        }
        public async Task <IActionResult> Edit(Clothing c)
        {
            if (ModelState.IsValid)
            {
                await ClothingDb.Edit(c, _context);

                //tempData last for one redirect
                ViewData["Msg"] = c.Title + " Updated Successfully!"; // <- Same thing, ViewBag
            }
            return(View(c));
        }
        private async Task <int> GetMaxPage(int pageSize)
        {
            int NumProducts = await ClothingDb.GetNumClothing(_context);

            //num product and page size are both an int so to get the decimal you must convert to a double
            // than to round up you must use Math.Ceiling which is a double so you must convert back to int
            int maxPage = Convert.ToInt32
                              (Math.Ceiling((double)NumProducts / pageSize));

            return(maxPage);
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> Edit(Clothing c)
        {
            if (ModelState.IsValid)
            {
                await ClothingDb.Edit(c, _context);

                ViewData["Message"] = c.Title + "Updated successfully";
                return(View(c));
            }
            return(View(c));
        }
        public async Task <IActionResult> Delete(int id)
        {
            Clothing c = await ClothingDb.GetClothingByID(id, _context);

            //If id does not exist, go to 404 page
            if (c == null)
            {
                return(NotFound());
            }
            return(View(c));
        }
        public async Task <IActionResult> Delete(int id)
        {
            Clothing c = await ClothingDb.GetClothingById(id, _context);

            if (c == null) //if clothing is not in the database
            {
                //returns a HTTP 404 - Not found error
                return(NotFound());
            }
            return(View(c));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Adds a single object to the cart.
        /// Quantity does not matter if same object added 5 times displayed 5 times.
        /// </summary>
        public async Task <IActionResult> AddToCart(int id, string prevUrl)
        {
            //Grab the clothing Object by Id.
            Clothing c = await ClothingDb.GetClothingByID(id, _context);

            //Add Clothing object to be stored in cookie.
            if (c != null)
            {
                CartHelper.Add(c, _http);
            }
            //redirect: sends you back to the url you were previously using
            return(Redirect(prevUrl));
        }
        public async Task <IActionResult> Add(Clothing c)
        {
            if (ModelState.IsValid)
            {
                await ClothingDb.Add(_context, c);

                //Temp data last for one redirect
                TempData["Msg"] = $"{c.Title} added successfully!";
                return(RedirectToAction("ShowAll"));
            }
            //Return same view with validation msgs.
            return(View(c));
        }
Ejemplo n.º 16
0
        public async Task <IActionResult> Add(Clothing c)
        {
            if (ModelState.IsValid)
            {
                await ClothingDb.Add(c, _context);

                // TempData lasts for one redirect
                TempData["Message"] = $"{c.Title} added successfully";
                return(RedirectToAction("ShowAll"));
            }
            // Return same view with error/validation messages
            return(View(c));
        }
        public async Task <IActionResult> Add(Clothing c)
        {
            if (ModelState.IsValid)                //if data is valid
            {
                await ClothingDb.Add(c, _context); //add to database

                //TempData lasts for one redirect
                TempData["Message"] = $"{c.Title} added successfully";

                return(RedirectToAction("ShowAll"));
            }

            return(View(c)); //else return the same view with validation error messages
        }
Ejemplo n.º 18
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {   // HTTP 400
                return(BadRequest());
            }
            Clothing c = await ClothingDb.GetClothingById(id.Value, _context); // .Value gets value from a null

            if (c == null)                                                     // Clothing not in DB
            {
                return(NotFound());                                            // Returns a HTTP 404 - Not Found
            }
            return(View(c));
        }
        public async Task <IActionResult> ShowAll(int?page) //variable name need to match asp-route name, use a nullable int because don't have a page to begin with
        {
            const int PageSize   = 2;
            int       pageNumber = page.HasValue ? page.Value : 1;

            ViewData["CurrentPage"] = pageNumber;

            int maxPage = await GetMaxPage(PageSize);

            ViewData["MaxPage"] = maxPage;

            List <Clothing> clothes = await ClothingDb.GetClothingByPage(_context, pageNumber, PageSize); //database context is injected by the framework using the constructor makes it so you do have to create new database objects everyone you need it

            return(View(clothes));
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> ShowAll(int?page)
        {
            const int PageSize = 2;
            // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
            int pageNumber = page ?? 1;

            ViewData["CurrentPage"] = pageNumber;

            int maxPage = await GetMaxPage(PageSize);

            ViewData["MaxPage"] = maxPage;

            List <Clothing> clothes = await ClothingDb.GetClothingByPage(_context, pageNum : pageNumber, pageSize : PageSize);

            return(View(clothes));
        }
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }
            Clothing c =
                await ClothingDb.GetClothingByID(id.Value, _context);

            if (c == null)       //Clothing Not in the Db
            {
                //returns a Http 404 - Not Found
                return(NotFound());
            }
            return(View(c));
        }
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                // HTTP 400
                return(BadRequest());
            }
            Clothing c = await ClothingDb.GetClothingById(id.Value, _context);

            if (c == null)          // Clothing item is not found in the DB
            {
                return(NotFound()); // returns a HTTP 404 - Not Found
                // return RedirectToAction("ShowAll"); // Returns the user to the ShowAll Page
            }

            return(View(c));
        }
        public async Task <IActionResult> Search(SearchCriteria search)
        {
            if (ModelState.IsValid)
            {
                if (search.IsBeingSearched())
                {
                    await ClothingDb.BuildSearchQuery(search, _context);

                    return(View(search));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "You must search by at least one criteria");
                    return(View(search));
                }
            }
            return(View());
        }
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null) //returns a HTTP 404 - Not found error
            {
                //HTTP 400
                return(BadRequest());
            }

            Clothing c = await ClothingDb.GetClothingById(id.Value, _context);

            if (c == null)
            {
                //returns a HTTP 404 - Not found error
                return(NotFound());
            }

            return(View(c));
        }
        public async Task <IActionResult> ShowAll(int?page)
        {
            const int PageSize = 2;
            // if page is not null, use its value... otherwise use 1
            // int pageNumber = page.HasValue ? page.Value : 1;
            // NUll
            int pageNumber = page ?? 1; // same as above

            ViewData["CurrentPage"] = pageNumber;

            int maxPage = await GetMaxPage(PageSize);

            ViewData["MaxPage"] = maxPage;

            List <Clothing> clothes = await ClothingDb.GetClothingByPage(_context, pageNum : pageNumber, pageSize : PageSize);

            return(View(clothes));
        }
Ejemplo n.º 26
0
        public async Task <IActionResult> ShowAll(int?page)
        {
            const int PageSize = 2;
            // Null-coalescing operator ??
            int pageNumber = page ?? 1; //use page number, if not there use 1, C# Null coalescing Operator, look it up

            ViewData["CurrentPage"] = pageNumber;

            int maxPage = await GetMaxPage(PageSize);

            ViewData["MaxPage"] = maxPage;

            // Just a placeholder ...
            List <Clothing> clothes =
                await ClothingDb.GetClothingByPage(_context, pageNum : pageNumber, pageSize : PageSize);

            return(View(clothes));
        }
        public async Task <JsonResult> AddJS(int id)
        {
            //Get id of clothing
            Clothing c = await ClothingDb.GetClothingById(id, _context);

            //Add clothing to the cart
            if (c == null)
            {
                //Return not found message
            }
            CartHelper.Add(c, _http);

            //Send success response
            JsonResult result = new JsonResult("Success");

            result.StatusCode = 200; //Http ok
            return(result);
        }
Ejemplo n.º 28
0
        public async Task <IActionResult> ShowAll(int?page) //page comes from "asp-route-page" in the ShowAll.cshtml (located in pagination ul)
        {
            const int PAGE_SIZE = 2;

            // Null-coalescing operator: ??
            // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
            int pageNumber = page ?? 1;

            ViewData["CurrentPage"] = pageNumber;

            int maxPage = await GetMaxPage(PAGE_SIZE);

            ViewData["MaxPage"] = maxPage;

            List <Clothing> clothes = await ClothingDb.GetClothingByPage(_context, pageNum : pageNumber, pageSize : PAGE_SIZE);

            return(View(clothes));
        }
Ejemplo n.º 29
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                // HTTP 400
                return(BadRequest());
            }
            Clothing c =
                await ClothingDb.GetClothingById(id.Value, _context); //context is a field in the controller, hence _context

            if (c == null)                                            // Clothing not in the DB
            {
                // RETURNS A HTTP 404 - nOT FOUND
                return(NotFound());
                //Redirects to page
                //return RedirectToAction("ShowAll");
            }

            return(View(c));
        }
        public async Task <IActionResult> Search(SearchCriteria search)
        {
            //Prepare query: Select * from Clothing.
            //Does not get sent to DB.
            if (ModelState.IsValid)
            {
                if (search.IsBeingSearched())
                {
                    await ClothingDb.BuildSearchQuery(search, _context);

                    return(View(search));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "You must search by atleast one criteria.");
                    return(View(search));
                }
            }
            return(View());
        }