Ejemplo n.º 1
0
        public ActionResult ChangeName(ChangeNameViewModel changeNameView)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                PartialUser logged_user = IsLoggedIn();
                if (logged_user == null)
                {
                    throw new Exception("Forbidden access!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    User user = db.FindUser(logged_user.email);

                    user.first_name = changeNameView.New_first_name;
                    user.last_name  = changeNameView.New_last_name;

                    db.SaveChanges();
                }
            }
            catch (Exception error)
            {
                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("AccountDetails"));
        }
Ejemplo n.º 2
0
        public ActionResult Register(RegisterViewModel registerView)
        {
            try {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    if (db.FindUser(registerView.Email) != null)
                    {
                        throw new Exception("There is already user with that e-mail.");
                    }

                    User user = new User
                    {
                        email      = registerView.Email,
                        password   = EncodePassword(registerView.Password),
                        first_name = registerView.First_name,
                        last_name  = registerView.Last_name
                    };
                    db.Users.Add(user);
                    db.SaveChanges();
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(null);
        }
Ejemplo n.º 3
0
        public ActionResult Login(LoginViewModel loginView)
        {
            try {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    User user = db.FindUser(loginView.Email);
                    if (user == null)
                    {
                        throw new Exception("There is no user with that e-mail.");
                    }

                    if (user.password != EncodePassword(loginView.Password))
                    {
                        throw new Exception("Wrong password.");
                    }

                    Session["user"] = new PartialUser(user);
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(PartialView("_Header"));
        }
Ejemplo n.º 4
0
        public ActionResult ChangePassword(ChangePasswordViewModel changePassView)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                PartialUser logged_user = IsLoggedIn();
                if (logged_user == null)
                {
                    throw new Exception("Forbidden access!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    User user = db.FindUser(logged_user.email);

                    if (user.password != EncodePassword(changePassView.ConfirmPassword))
                    {
                        throw new Exception("Wrong password.");
                    }

                    user.password = EncodePassword(changePassView.NewPassword);
                    db.SaveChanges();
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(null);
        }
Ejemplo n.º 5
0
 //SIMULATED TOKEN WEB SERVICE
 private HttpStatusCodeResult TokenWebService(Guid id)
 {
     try
     {
         using (AuctionHouseModel db = new AuctionHouseModel())
         {
             TokenOrder order = db.GetTokenOrder(id);
             if (order.state == "SUBMITTED")
             {
                 order.state = "COMPLETED";
                 db.SaveChanges();
             }
             else
             {
                 order.state = "CANCELED";
                 db.SaveChanges();
                 throw new Exception("Error occured: Token order is invalid!");
             }
         }
     }
     catch (Exception error)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
     }
     return(new HttpStatusCodeResult(HttpStatusCode.Accepted));
 }
Ejemplo n.º 6
0
        public ActionResult Bid(BidViewModel bidView) //////////PROVERI DA LI IMAS DOVOLJNO PARA
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                PartialUser logged_user = IsLoggedIn();
                if (logged_user == null)
                {
                    throw new Exception("Forbidden access!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    Guid auction_id = new Guid(bidView.Auction_id);

                    if (logged_user.email == db.GetAuction(auction_id).owner)
                    {
                        throw new Exception("You can not bid your own auction!");
                    }

                    Bid last_bid = db.GetLastBid(auction_id);
                    if (bidView.Amount <= (last_bid != null ? last_bid.amount : db.GetAuction(auction_id).starting_price))
                    {
                        throw new Exception("Your bidding amount must be greater then the last one!");
                    }

                    if (db.GetAvailableTokens(logged_user.email) < bidView.Amount)
                    {
                        throw new Exception("You have not enough tokens to procceed with the transaction!");
                    }

                    Bid bid = new Bid
                    {
                        id         = Guid.NewGuid(),
                        auction_id = auction_id,
                        bidder     = logged_user.email,
                        created    = DateTime.Now,
                        amount     = bidView.Amount
                    };
                    db.Bids.Add(bid);
                    db.SaveChanges();

                    string name = logged_user.first_name + " " + logged_user.last_name;
                    AuctionHouseHub.HubContext.Clients.All.updatebid(logged_user.email, name, bidView.Auction_id, bidView.Amount, bid.created.ToString());
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(null);
        }
Ejemplo n.º 7
0
        public ActionResult CreateAuction(CreateAuctionViewModel auctionView)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                PartialUser logged_user = IsLoggedIn();
                if (logged_user == null)
                {
                    throw new Exception("Forbidden access!");
                }

                if (auctionView.Image == null)
                {
                    throw new Exception("File was not uploaded");
                }

                var postedFileExtension = Path.GetExtension(auctionView.Image.FileName);
                if (!string.Equals(postedFileExtension, ".png", StringComparison.OrdinalIgnoreCase))
                {
                    throw new Exception("Wrong image type: .png is required type!");
                }

                Guid guid = Guid.NewGuid();
                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    Auction auction = new Auction
                    {
                        id             = guid,
                        name           = auctionView.Name,
                        description    = auctionView.Description,
                        starting_price = auctionView.Starting_price,
                        duration       = auctionView.Days * 60 * 60 * 24 + auctionView.HH * 60 * 60 + auctionView.MM * 60 + auctionView.SS,
                        created        = DateTime.Now,
                        owner          = logged_user.email,
                        state          = "READY"
                    };
                    db.Auctions.Add(auction);
                    db.SaveChanges();
                }

                string path = Path.Combine(Server.MapPath("~/Images"), guid.ToString() + ".png");
                auctionView.Image.SaveAs(path);
                // OBAVESTI SVE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            }
            catch (Exception error)
            {
                TempData["error"] = error.Message;
                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 8
0
        public ActionResult Index()
        {
            using (AuctionHouseModel db = new AuctionHouseModel())
            {
                SystemParameter sp       = db.GetSystemParameters();
                var             auctions = SearchAuctions(new SearchAuctionsViewModel());
                ViewBag.Page_size = sp.N;
                ViewBag.Auctions  = auctions;
            }

            return(View());
        }
Ejemplo n.º 9
0
 public ActionResult AuctionDetails(string id)
 {
     if (id == null || id == "")
     {
         return(RedirectToAction("Index"));
     }
     using (AuctionHouseModel db = new AuctionHouseModel())
     {
         AuctionAllBids auction = db.GetAuctionWithAllBids(new Guid(id));
         ViewBag.Auction = auction;
         return(View());
     }
 }
Ejemplo n.º 10
0
        public ActionResult OrderTokens(OrderTokensViewModel orderView)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                PartialUser logged_user = IsLoggedIn();
                if (logged_user == null)
                {
                    throw new Exception("Forbidden access!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    SystemParameter sp    = db.GetSystemParameters();
                    TokenOrder      order = new TokenOrder
                    {
                        id      = Guid.NewGuid(),
                        orderer = logged_user.email,
                        amount  = (int)orderView.Package,
                        price   = (int)orderView.Package * sp.T,
                        state   = "SUBMITTED"
                    };

                    db.TokenOrders.Add(order);
                    db.SaveChanges();

                    HttpStatusCodeResult service_result = TokenWebService(order.id);
                    if (service_result.StatusCode != 0xca)
                    {
                        throw new Exception(service_result.ToString());
                    }

                    User user = db.FindUser(logged_user.email);
                    user.tokens_amount += order.amount;
                    db.SaveChanges();
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(null);
        }
Ejemplo n.º 11
0
        public ActionResult EditSystemParameters(SystemParametersViewModel SPView)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("All fields must be filled correctly!");
                }

                if (IsAdmin() == null)
                {
                    throw new Exception("Forbidden access!");
                }

                if (SPView.S >= SPView.G || SPView.G >= SPView.P)
                {
                    throw new Exception("Gold package must be greater than Silver and Platinum must be greater then Gold!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    SystemParameter sp = db.GetSystemParameters();

                    sp.N = SPView.N;
                    sp.D = SPView.D;
                    sp.S = SPView.S;
                    sp.G = SPView.G;
                    sp.P = SPView.P;
                    sp.C = SPView.C;
                    sp.T = SPView.T;

                    db.SaveChanges();
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(null);
        }
Ejemplo n.º 12
0
        public ActionResult SearchAuctions(SearchAuctionsViewModel auctionView)
        {
            PartialUser logged_user = IsLoggedIn();

            using (AuctionHouseModel db = new AuctionHouseModel())
            {
                string owned = null;
                string won   = null;
                string state = null;
                switch (auctionView.Filter)
                {
                case SearchAuctionsViewModel.FilterEnum.OWNED:
                    if (logged_user != null)
                    {
                        owned = logged_user.email;
                    }
                    break;

                case SearchAuctionsViewModel.FilterEnum.WON:
                    if (logged_user != null)
                    {
                        won = logged_user.email;
                    }
                    break;

                default:
                    state = auctionView.Filter.ToString();
                    break;
                }
                var auctions = db.GetAuctionsWithLastBid(1000, 0,
                                                         auctionView.Regex, state,
                                                         auctionView.Max_price, auctionView.Min_price,
                                                         won, owned);

                return(Json(auctions, JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 13
0
        public ActionResult AccountDetails()
        {
            try
            {
                PartialUser logged_user = IsLoggedIn();
                if (logged_user == null)
                {
                    throw new Exception("Forbidden access!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    UserDetails             user = db.GetUserDetails(logged_user.email);
                    PartialSystemParameters sp   = db.GetPartialSystemParameters();
                    ViewBag.User         = user;
                    ViewBag.SystemParams = sp;
                    return(View());
                }
            }
            catch (Exception error)
            {
                return(RedirectToAction("Index"));
            }
        }
Ejemplo n.º 14
0
        public ActionResult Approve(string id)
        {
            try
            {
                if (IsAdmin() == null)
                {
                    throw new Exception("Only administrator is allowed to approve auctions!");
                }

                using (AuctionHouseModel db = new AuctionHouseModel())
                {
                    Auction auction = db.GetAuction(new Guid(id));
                    auction.state  = "OPENED";
                    auction.opened = DateTime.Now;
                    db.SaveChanges();
                    // OBAVESTI SVE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                }
            }
            catch (Exception error)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, error.Message));
            }
            return(null);
        }