/// <summary>
        /// Shows the auction view,
        /// where ViewModel of AuctionModel is used as container for 4 different models.
        /// Shows all the models in one view.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Auction(int id)
        {
            B_AuctionController bACtr = new B_AuctionController();

            ConvertViewModel converter = new ConvertViewModel();

            //Auction details from database.
            AuctionInfoViewModel auctionInfoModel = converter.ConvertFromAuctionToAuctionModel(bACtr.GetAuction(id));

            //If auction is not in database - For handling if users put in illegal auction id in url.
            if (auctionInfoModel == null)
            {
                return(View("NotFound"));
            }

            //Get Images info from database.
            List <PictureViewModel> sAPM = converter.ConvertFromImagesToShowAuctionPictureModels(bACtr.GetImages(id));

            //Get bids on the auction from database.
            List <BidViewModel> showBids = converter.ConvertFromBidsToShowBids(bACtr.GetBids(id));

            //Get highest bid from database - Create insert bids model
            InsertBidModel insertBidModel = new InsertBidModel();

            insertBidModel.CurrentHighestBid = bACtr.GetHighestBidOnAuction(id);
            insertBidModel.MinimumValidBid   = insertBidModel.CurrentHighestBid + auctionInfoModel.BidInterval;

            //Return the AuctionModel
            return(View(new AuctionViewModel()
            {
                AuctionInfoModel = auctionInfoModel, PictureViewModels = sAPM, ShowBids = showBids, InsertBidModel = insertBidModel
            }));
        }
        public ActionResult CreateAuctionDetails(Models.AuctionSetUpModel auctionDetails)
        {
            if (ModelState.IsValid)
            {
                B_AuctionController bACtr = new B_AuctionController();

                ConvertViewModel converter = new ConvertViewModel();

                bool successful = bACtr.CreateAuction(converter.ConvertFromAuctionSetUpToCreateAuction(auctionDetails, User.Identity.Name));

                if (successful)
                {
                    //Sends message to user about auction was created.
                    TempData["Referer"] = "AuctionSuccessful";
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    //Sends message to user about auction was not created.
                    TempData["Referer"] = "AuctionFailed";
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                //If modelstate invalid get categories again, so it can be passed down to View again.
                List <SelectListItem> selectList = GetCategoriesAndConvertToSelectListItem();

                auctionDetails.Categories = new SelectList(selectList, "Value", "Text");
            }
            return(View("CreateAuction", auctionDetails));
        }
        public ActionResult InsertBidDetail(InsertBidModel insertBid, int id)
        {
            bool successful;

            if (ModelState.IsValid)
            {
                B_AuctionController bACtr = new B_AuctionController();

                ConvertViewModel converter = new ConvertViewModel();

                successful = bACtr.InsertBid(converter.ConvertFromBidInsertModelToBid(insertBid, User.Identity.Name, id));

                //For messages to the user.
                if (successful)
                {
                    TempData["Referer"] = "InsertSuccessful";
                }
                else
                {
                    TempData["Referer"] = "InsertFailed";
                }
            }

            return(RedirectToAction("Auction", new { id }));
        }
        /// <summary>
        /// Shows view of auctions that was searched for.
        /// </summary>
        /// <param name="searchDetails"></param>
        /// <returns></returns>
        public ActionResult SearchAuctionsDetails(SearchModel searchDetails)
        {
            B_AuctionController bACtr     = new B_AuctionController();
            ConvertViewModel    converter = new ConvertViewModel();

            List <AuctionInfoViewModel> auctionModels = converter.ConvertFromAuctionsToAuctionModels(bACtr.GetAuctionsByDesc(searchDetails.SearchString));

            return(View("AuctionsPartial", auctionModels));
        }
        /// <summary>
        /// Shows a partial view of latest created auctions.
        /// </summary>
        /// <returns></returns>
        public ActionResult LatestAuctionsPartial()
        {
            B_AuctionController bACtr = new B_AuctionController();

            ConvertViewModel converter = new ConvertViewModel();

            List <Auction> auctions = bACtr.GetLatestAuctions();

            List <AuctionInfoViewModel> auctionModels = converter.ConvertFromAuctionsToAuctionModels(auctions);

            return(PartialView("AuctionsPartial", auctionModels));
        }
        /// <summary>
        /// Shows a partial view of auctions for logged in user.
        /// </summary>
        /// <returns></returns>
        public ActionResult MyAuctionsPartial()
        {
            B_AuctionController bACtr = new B_AuctionController();

            ConvertViewModel converter = new ConvertViewModel();

            List <Auction> auctions = bACtr.GetUserAuctions(User.Identity.Name);

            List <AuctionInfoViewModel> auctionModels = converter.ConvertFromAuctionsToAuctionModels(auctions);

            return(View("AuctionsPartial", auctionModels));
        }
        /// <summary>
        /// Shows an image.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fileName"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public FileStreamResult AuctionShowImage(int id, string fileName, string userName)
        {
            B_AuctionController bActr = new B_AuctionController();

            Image image = bActr.GetPicture(userName, id, fileName);

            string imageType = "image/" + Path.GetExtension(fileName).Replace(".", "");

            var fileStreamResult = new FileStreamResult(image.FileStream, imageType);

            fileStreamResult.FileDownloadName = image.FileName;

            return(fileStreamResult);
        }
        /// <summary>
        /// Gets categories from DB and converts them to list of selectlistitems.
        /// </summary>
        /// <returns></returns>
        private List <SelectListItem> GetCategoriesAndConvertToSelectListItem()
        {
            B_AuctionController bACtr      = new B_AuctionController();
            List <string>       categories = bACtr.GetCategories();

            List <SelectListItem> selectList = new List <SelectListItem>();

            foreach (string category in categories)
            {
                selectList.Add(new SelectListItem {
                    Value = category, Text = category
                });
            }
            return(selectList);
        }
        public ActionResult AddPictureDetails(InsertPictureModel picture, int id)
        {
            B_AuctionController bACtr = new B_AuctionController();

            //GET ALL PICTURES FROM AUCTION
            if (ModelState.IsValid)
            {
                ConvertViewModel converter = new ConvertViewModel();

                bACtr.InsertPicture(converter.ConvertFromAuctionPictureToImage(picture), User.Identity.Name, id);

                TempData["Referer"] = "PictureSuccessful";
            }
            else
            {
                TempData["Referer"] = "PictureFailed";
            }
            return(RedirectToAction("AddPictures", new { id }));
        }