Ejemplo n.º 1
0
        public async Task <ActionResult> ApproveReview(string id, bool approved)
        {
            if (!string.IsNullOrEmpty(id))
            {
                try
                {
                    var model = await db.GameReviews.FindAsync(id);

                    if (model == null)
                    {
                        TempData["message"] = string.Format("Review with {0} id was not found", id);
                    }
                    else
                    {
                        if (approved)
                        {
                            model.Approved = true;
                            await db.SaveChangesAsync();
                        }
                        else
                        {
                            db.GameReviews.Remove(model);
                            await db.SaveChangesAsync();
                        }
                    }
                    RedirectToAction(nameof(Index));
                }
                catch
                {
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
        // Get: Events/Register
        public async Task <ActionResult> Register(string eventId)
        {
            var  userId        = User.Identity.GetUserId();
            bool validRegister = true;

            // Check if event exists
            if (!eventExists(eventId))
            {
                TempData["message"] = "Event does not exist.";
                validRegister       = false;
            }

            // Check if user is not registered
            if (db.EventAttendees.Any(i => i.EventId == eventId && i.AttendeeId == userId))
            {
                TempData["message"] = "You have already registered for this event.";
                validRegister       = false;
            }

            if (validRegister)
            {
                db.EventAttendees.Add(new EventAttendee {
                    EventId = eventId, AttendeeId = userId
                });
                var eventToUpdate = db.Events.Find(eventId);
                eventToUpdate.AttendeeNumber += 1;
                await db.SaveChangesAsync();
            }

            return(RedirectToAction("Details", new RouteValueDictionary(new { controller = "Events", action = "Details", Id = eventId })));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Create(Event newEvent)
        {
            var db = new CVGSAppEntities();

            // Event name unique
            if (db.Events.Any(e => e.Title == newEvent.Title))
            {
                ModelState.AddModelError("Title", "An event with this name already exists.");
            }

            ValidateEvent(newEvent);

            if (!ModelState.IsValid)
            {
                return(View(newEvent));
            }

            try
            {
                newEvent.Id = Guid.NewGuid().ToString();
                db.Events.Add(newEvent);
                await db.SaveChangesAsync();

                TempData["message"] = $"Record for '{newEvent.Title}' successfully added";
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Error: {ex.GetBaseException().Message}");
                return(View(newEvent));
            }
        }
        public async Task <ActionResult> Add(string id)
        {
            var userId = User.Identity.GetUserId();

            if (db.WishLists.Any(c => c.userId == userId && c.gameId == id))
            {
                TempData["message"] = "This game is already in your wish list.";
                return(RedirectToAction("ViewGame", new RouteValueDictionary(new { controller = "Home", action = "ViewGame", Id = id })));
            }

            var newWish = new WishList()
            {
                userId = userId, gameId = id
            };

            db.WishLists.Add(newWish);
            await db.SaveChangesAsync();

            return(RedirectToAction("ViewGame", new RouteValueDictionary(new { controller = "Home", action = "ViewGame", Id = id })));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> Create(Game model)
        {
            model.Rating = 0;
            CVGSAppEntities db = new CVGSAppEntities();

            try
            {
                //Game name unique
                if (db.Games.Any(g => g.Title == model.Title))
                {
                    ModelState.AddModelError("Title", "A game with this name already exists.");
                }

                // Release Year is integer between 1985 and 2020
                if (model.ReleaseYear != null)
                {
                    if (model.ReleaseYear != (int)model.ReleaseYear)
                    {
                        ModelState.AddModelError("ReleaseYear", "The release year must be an integer.");
                    }
                    else if ((int)model.ReleaseYear < 1985 || (int)model.ReleaseYear > 2020)
                    {
                        ModelState.AddModelError("ReleaseYear", "The release year must be between 1985 and 2020.");
                    }
                }
                if (!ModelState.IsValid)
                {
                    var gameCategories = new SelectList(db.GameCategories.ToList(), "Id", "Name");
                    ViewData["GameCategories"] = gameCategories;
                    var gamePlatforms = new SelectList(db.GamePlatforms.ToList(), "Id", "Name");
                    ViewData["GamePlatforms"] = gamePlatforms;
                    return(View(model));
                }
                model.Id = Guid.NewGuid().ToString();
                db.Games.Add(model);
                await db.SaveChangesAsync();

                TempData["message"] = $"Record for '{model.Title}' successfully added";
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Error: {ex.GetBaseException().Message}");
                var gameCategories = new SelectList(db.GameCategories.ToList(), "Id", "Name");
                ViewData["GameCategories"] = gameCategories;
                var gamePlatforms = new SelectList(db.GamePlatforms.ToList(), "Id", "Name");
                ViewData["GamePlatforms"] = gamePlatforms;
                return(View(model));
            }
        }
Ejemplo n.º 6
0
        public async Task <ActionResult> Edit(Event updatedEvent)
        {
            CVGSAppEntities db = new CVGSAppEntities();

            if (updatedEvent == null || string.IsNullOrEmpty(updatedEvent.Id))
            {
                return(RedirectToAction(nameof(Index)));
            }

            // Unqiue Name
            if (db.Events.Any(e => e.Title == updatedEvent.Title && e.Id != updatedEvent.Id))
            {
                ModelState.AddModelError("Title", "An event with this name already exists.");
            }

            try
            {
                var existingEvent = await db.Events.FindAsync(updatedEvent.Id);

                if (existingEvent == null)
                {
                    TempData["message"] = string.Format("Event record with {0} id was not found", updatedEvent.Id);
                    RedirectToAction(nameof(Index));
                }

                ValidateEvent(updatedEvent);

                if (!ModelState.IsValid)
                {
                    return(View(updatedEvent));
                }

                existingEvent.Title             = updatedEvent.Title;
                existingEvent.Description       = updatedEvent.Description;
                existingEvent.StartTime         = updatedEvent.StartTime;
                existingEvent.EndTime           = updatedEvent.EndTime;
                existingEvent.MaxAttendeeNumber = updatedEvent.MaxAttendeeNumber;
                existingEvent.IsIRL             = updatedEvent.IsIRL;
                existingEvent.Location          = updatedEvent.Location;

                await db.SaveChangesAsync();

                TempData["message"] = $"Record for '{updatedEvent.Title}' successfully updated";
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View(updatedEvent));
            }
        }
        public async Task <ActionResult> Add(string id)
        {
            var userId = User.Identity.GetUserId();

            if (db.UserCartItems.Any(c => c.UserId == userId && c.GameId == id))
            {
                // add displaying errors to the home page
                //return RedirectToAction("ViewGame", "Home", new { area = "" });
                TempData["message"] = "This game is already in your cart.";
                return(RedirectToAction("ViewGame", new RouteValueDictionary(new { controller = "Home", action = "ViewGame", Id = id })));
            }

            var newCartItem = new UserCartItem()
            {
                UserId = userId, GameId = id
            };

            db.UserCartItems.Add(newCartItem);
            await db.SaveChangesAsync();

            Session["cart"] = db.UserCartItems.Where(c => c.UserId == userId).Count().ToString();
            return(RedirectToAction("ViewGame", new RouteValueDictionary(new { controller = "Home", action = "ViewGame", Id = id })));
        }
Ejemplo n.º 8
0
        public async Task <ActionResult> Delete(Event model)
        {
            CVGSAppEntities db = new CVGSAppEntities();

            try
            {
                db.Events.Attach(model);
                db.Events.Remove(model);
                await db.SaveChangesAsync();

                TempData["message"] = $"Record successfully deleted";
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Error: {ex.GetBaseException().Message}");
                return(View(model));
            }
        }
Ejemplo n.º 9
0
        public async Task <ActionResult> Delete(Game model)
        {
            CVGSAppEntities db = new CVGSAppEntities();

            if (string.IsNullOrEmpty(model.Id))
            {
                return(RedirectToAction(nameof(Index)));
            }

            var game = await db.Games.FindAsync(model.Id);

            if (game == null)
            {
                TempData["message"] = string.Format("Game record with {0} id was not found", model.Id);
                RedirectToAction(nameof(Index));
            }
            try
            {
                // remove all ratings for the game
                var gameRatings = db.GameRatings;
                foreach (var rating in gameRatings.Where(g => g.GameId == model.Id))
                {
                    gameRatings.Remove(rating);
                }

                // remove all reviews for the game
                var gameReviews = db.GameReviews;
                foreach (var review in gameReviews.Where(g => g.GameId == model.Id))
                {
                    gameReviews.Remove(review);
                }

                // remove the game from orders
                var orderItems = db.OrderItems;
                foreach (var orderItem in orderItems.Where(g => g.GameId == model.Id))
                {
                    orderItems.Remove(orderItem);
                }

                // remove the game from user carts
                var userCartItems = db.UserCartItems;
                foreach (var userCartItem in userCartItems.Where(g => g.GameId == model.Id))
                {
                    userCartItems.Remove(userCartItem);
                }

                // remove the game from user carts
                var wishListItems = db.WishLists;
                foreach (var item in wishListItems.Where(g => g.gameId == model.Id))
                {
                    wishListItems.Remove(item);
                }

                var title = game.Title;
                db.Games.Attach(game);
                db.Games.Remove(game);
                await db.SaveChangesAsync();

                TempData["message"] = string.Format("Record '{0}' successfully deleted", title);
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Error: {ex.GetBaseException().Message}");
                return(View(model));
            }
        }
Ejemplo n.º 10
0
        public async Task <ActionResult> Edit(Game model)
        {
            if (string.IsNullOrEmpty(model.Id))
            {
                return(RedirectToAction(nameof(Index)));
            }
            CVGSAppEntities db = new CVGSAppEntities();

            try
            {
                // Unique Name
                if (db.Games.Any(e => e.Title == model.Title && e.Id != model.Id))
                {
                    ModelState.AddModelError("Title", "An event with this name already exists.");
                }
                // Release Year is integer between 1985 and 2020
                if (model.ReleaseYear != null)
                {
                    if (model.ReleaseYear != (int)model.ReleaseYear)
                    {
                        ModelState.AddModelError("ReleaseYear", "The release year must be an integer.");
                    }
                    else if ((int)model.ReleaseYear < 1985 || (int)model.ReleaseYear > 2020)
                    {
                        ModelState.AddModelError("ReleaseYear", "The release year must be between 1985 and 2020.");
                    }
                }
                if (!ModelState.IsValid)
                {
                    var gameCategories = new SelectList(db.GameCategories.ToList(), "Id", "Name");
                    ViewData["GameCategories"] = gameCategories;
                    var gamePlatforms = new SelectList(db.GamePlatforms.ToList(), "Id", "Name");
                    ViewData["GamePlatforms"] = gamePlatforms;
                    return(View(model));
                }

                var game = await db.Games.FindAsync(model.Id);

                if (game == null)
                {
                    TempData["message"] = string.Format("Game record with {0} id was not found", model.Id);
                    RedirectToAction(nameof(Index));
                }
                game.Title       = model.Title;
                game.Description = model.Description;
                game.ReleaseYear = model.ReleaseYear;
                game.CategoryId  = model.CategoryId;
                game.PlatformId  = model.PlatformId;
                game.Price       = model.Price;
                game.ImageUrl    = model.ImageUrl;
                await db.SaveChangesAsync();

                TempData["message"] = $"Record for '{model.Title}' successfully updated";
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", $"Error: {ex.GetBaseException().Message}");
                var gameCategories = new SelectList(db.GameCategories.ToList(), "Id", "Name");
                ViewData["GameCategories"] = gameCategories;
                var gamePlatforms = new SelectList(db.GamePlatforms.ToList(), "Id", "Name");
                ViewData["GamePlatforms"] = gamePlatforms;
                return(View(model));
            }
        }
        public async Task <ActionResult> ChangeAddressDetails(AddressesViewModel model)
        {
            CVGSAppEntities db = new CVGSAppEntities();

            // if only some parts of mailing address are entered
            if ((!string.IsNullOrEmpty(model.MailingAddressApartment) || !string.IsNullOrEmpty(model.MailingAddressStreetNumber) || !string.IsNullOrEmpty(model.MailingAddressStreetName) ||
                 !string.IsNullOrEmpty(model.MailingAddressCity) || !string.IsNullOrEmpty(model.MailingAddressProvince)) &&
                (string.IsNullOrEmpty(model.MailingAddressStreetNumber) || string.IsNullOrEmpty(model.MailingAddressStreetName) ||
                 string.IsNullOrEmpty(model.MailingAddressCity) || string.IsNullOrEmpty(model.MailingAddressProvince)))
            {
                ModelState.AddModelError("", "Please provide a full mailing address or clear all mailing address fields.");
            }
            // if only some parts of shipping address are entered
            if ((!string.IsNullOrEmpty(model.ShippingAddressApartment) || !string.IsNullOrEmpty(model.ShippingAddressStreetNumber) || !string.IsNullOrEmpty(model.ShippingAddressStreetName) ||
                 !string.IsNullOrEmpty(model.ShippingAddressCity) || !string.IsNullOrEmpty(model.ShippingAddressProvince)) &&
                (string.IsNullOrEmpty(model.ShippingAddressStreetNumber) || string.IsNullOrEmpty(model.ShippingAddressStreetName) ||
                 string.IsNullOrEmpty(model.ShippingAddressCity) || string.IsNullOrEmpty(model.ShippingAddressProvince)))
            {
                ModelState.AddModelError("", "Please provide a full shipping address or clear all shipping address fields.");
            }
            if (!ModelState.IsValid)
            {
                List <SelectListItem> provinces = db.Provinces
                                                  .OrderBy(p => p.Name)
                                                  .Select(p =>
                                                          new SelectListItem
                {
                    Value = p.Code,
                    Text  = p.Name
                }).ToList();

                provinces.Insert(0, new SelectListItem {
                    Text = "", Value = ""
                });
                ViewData["ProvinceList"] = provinces;
                return(View(model));
            }


            // get member user object
            var userId     = User.Identity.GetUserId();
            var memberUser = await db.MemberUsers.FindAsync(userId);

            // format the postal codes
            if (!string.IsNullOrEmpty(model.MailingAddressPostalCode))
            {
                int startingIndexSecondPart = model.MailingAddressPostalCode.Length == 6 ? 3 : 4;
                memberUser.MailingAddressPostalCode = formatPostalCode(model.MailingAddressPostalCode, startingIndexSecondPart);
            }
            else
            {
                memberUser.MailingAddressPostalCode = null;
            }
            if (model.ShippingAddressSame)
            {
                memberUser.ShippingAddressPostalCode = memberUser.MailingAddressPostalCode;
            }
            else if (!string.IsNullOrEmpty(model.ShippingAddressPostalCode))
            {
                int startingIndexSecondPart = model.ShippingAddressPostalCode.Length == 6 ? 3 : 4;
                memberUser.ShippingAddressPostalCode = formatPostalCode(model.ShippingAddressPostalCode, startingIndexSecondPart);
            }
            else
            {
                memberUser.ShippingAddressPostalCode = null;
            }

            // update and save member user record
            memberUser.MailingAddressApartment    = !string.IsNullOrEmpty(model.MailingAddressApartment) ? model.MailingAddressApartment.Trim() : string.Empty;
            memberUser.MailingAddressStreetNumber = !string.IsNullOrEmpty(model.MailingAddressStreetNumber) ? model.MailingAddressStreetNumber.Trim() : string.Empty;
            memberUser.MailingAddressStreetName   = !string.IsNullOrEmpty(model.MailingAddressStreetName) ? model.MailingAddressStreetName.Trim() : string.Empty;
            memberUser.MailingAddressCity         = !string.IsNullOrEmpty(model.MailingAddressCity) ? model.MailingAddressCity.Trim() : string.Empty;
            memberUser.MailingAddressProvince     = !string.IsNullOrEmpty(model.MailingAddressProvince) ? model.MailingAddressProvince.Trim() : string.Empty;
            if (model.ShippingAddressSame)
            {
                memberUser.ShippingAddressApartment    = !string.IsNullOrEmpty(model.MailingAddressApartment) ? model.MailingAddressApartment.Trim() : string.Empty;
                memberUser.ShippingAddressStreetNumber = !string.IsNullOrEmpty(model.MailingAddressStreetNumber) ? model.MailingAddressStreetNumber.Trim() : string.Empty;
                memberUser.ShippingAddressStreetName   = !string.IsNullOrEmpty(model.MailingAddressStreetName) ? model.MailingAddressStreetName.Trim() : string.Empty;
                memberUser.ShippingAddressCity         = !string.IsNullOrEmpty(model.MailingAddressCity) ? model.MailingAddressCity.Trim() : string.Empty;
                memberUser.ShippingAddressProvince     = !string.IsNullOrEmpty(model.MailingAddressProvince) ? model.MailingAddressProvince.Trim() : string.Empty;
            }
            else
            {
                memberUser.ShippingAddressApartment    = !string.IsNullOrEmpty(model.ShippingAddressApartment) ? model.ShippingAddressApartment.Trim() : string.Empty;
                memberUser.ShippingAddressStreetNumber = !string.IsNullOrEmpty(model.ShippingAddressStreetNumber) ? model.ShippingAddressStreetNumber.Trim() : string.Empty;
                memberUser.ShippingAddressStreetName   = !string.IsNullOrEmpty(model.ShippingAddressStreetName) ? model.ShippingAddressStreetName.Trim() : string.Empty;
                memberUser.ShippingAddressCity         = !string.IsNullOrEmpty(model.ShippingAddressCity) ? model.ShippingAddressCity.Trim() : string.Empty;
                memberUser.ShippingAddressProvince     = !string.IsNullOrEmpty(model.ShippingAddressProvince) ? model.ShippingAddressProvince.Trim() : string.Empty;
            }
            try
            {
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Index"));
            }
        }