public ActionResult Create(EventViewModel model)
        {
            if (ModelState.IsValid)
            {
                eventRepository.SaveEvent(model.SelectedEvent);
                this.Success("The event has been created.");
                return RedirectToAction("Index");
            }

            model.Categories = categorySelectList();
            ViewBag.action = "Create";
            return View("EventForm", model);
        }
        public ActionResult Create()
        {
            int categories = eventRepository.Categories.Where(x => x.IsActive == true).Count();
            if (categories == 0)
            {
                this.Error(string.Format("You cannot create a new event because there are no active categories"));
                return RedirectToAction("Index");
            }
            else
            {
                var newevent = new Event();
                //set proposed deadline to next full hour
                var currentDatetime = DateTime.Now;
                newevent.DeadLine = new DateTime(currentDatetime.Year, currentDatetime.Month, currentDatetime.Day, currentDatetime.Hour, 0, 0).AddHours(1);
                newevent.StartTime = new DateTime(currentDatetime.Year, currentDatetime.Month, currentDatetime.Day, currentDatetime.Hour, 0, 0).AddHours(8);
                var model = new EventViewModel { SelectedEvent = newevent, Categories = categorySelectList() };

                ViewBag.action = "Create";
                return View("EventForm", model);
            }
        }
        public ActionResult CreateBasedOnOldEvent(int id)
        {
            var selectedEvent = eventRepository.Events.Where(x => x.EventId == id).FirstOrDefault();
            var newevent = new Event();
            if (selectedEvent != null)
            {
                var currentDatetime = DateTime.Now;
                newevent.DeadLine = new DateTime(currentDatetime.Year, currentDatetime.Month, currentDatetime.Day, currentDatetime.Hour, 0, 0).AddHours(1);
                newevent.StartTime = new DateTime(currentDatetime.Year, currentDatetime.Month, currentDatetime.Day, currentDatetime.Hour, 0, 0).AddHours(8);
                newevent.Name = selectedEvent.Name;
                newevent.AvailableSpots = selectedEvent.AvailableSpots;
                newevent.InformationUrl = selectedEvent.InformationUrl;
                newevent.Description = selectedEvent.Description;
                newevent.Category = selectedEvent.Category;
            }
            var model = new EventViewModel { SelectedEvent = newevent, Categories = categorySelectList() };

            ViewBag.action = "Create";
            return View("EventForm", model);
        }
        public ActionResult Edit(EventViewModel model)
        {
            if (ModelState.IsValid)
            {
                eventRepository.SaveEvent(model.SelectedEvent);
                this.Success("Changes on event has been saved");
                return RedirectToAction("Details", new { id = model.SelectedEvent.EventId });
            }

            model.Categories = categorySelectList();
            ViewBag.action = "Edit";
            return View("EventForm", model);
        }
        public ActionResult Edit(int id)
        {
            var selectedEvent = eventRepository.Events.Where(x => x.EventId == id).FirstOrDefault();
            if (!selectedEvent.Creator.Equals(HttpContext.User.Identity.Name))
            {
                this.Info("You can only edit your own events.");
                return RedirectToAction("Details", new { id = id });
            }

            var model = new EventViewModel { SelectedEvent = selectedEvent, Categories = categorySelectList() };
            ViewBag.action = "Edit";
            return View("EventForm", model);
        }
        public ActionResult Details(int id)
        {
            var selectedEvent = eventRepository.Events.Where(x => x.EventId == id).FirstOrDefault();
            var participant = selectedEvent.UserEvents.Where(x => x.UserEmail == HttpContext.User.Identity.Name).FirstOrDefault();
            var model = new EventViewModel
            {
                SelectedEvent = selectedEvent,
                UserIsParticipant = participant != null,
                LoggedInParticipant = participant,
                NumberOfSpots = NumberofSpotsList(selectedEvent.AvailableSpots),
                RaffleTypes = getRaffleTypes()
            };

            ViewBag.MenuController = "Event";
            ViewBag.MenuAction = "Index";
            ViewBag.isCreator = selectedEvent.Creator.Equals(HttpContext.User.Identity.Name);
            TempData["isCreator"] = selectedEvent.Creator.Equals(HttpContext.User.Identity.Name);
            return View(model);
        }
        public void Setup()
        {
            var mockHttpContext = new Mock<ControllerContext>();

            mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("creator 2");
            mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            eventMock = new Mock<IEventRepository>();
            eventMock.Setup(m => m.Events).Returns(new Event[] {
                new Event { EventId = 1, Name = "event 1", Created = DateTime.Now, Creator = "creator 1", AvailableSpots = 4, DeadLine = DateTime.Now, CategoryId = 1 },
                new Event { EventId = 2, Name = "event 2", Created = DateTime.Now, Creator = "creator 2", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 2 },
                new Event { EventId = 3, Name = "event 3", Created = DateTime.Now, Creator = "creator 3", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 3 },
                new Event { EventId = 4, Name = "event 4", Created = DateTime.Now, Creator = "creator 4", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 4 },
                new Event { EventId = 5, Name = "event 5", Created = DateTime.Now, Creator = "creator 5", AvailableSpots = 3, DeadLine = DateTime.Now, CategoryId = 5 }
            }.AsQueryable());

            eventMock.Setup(m => m.Participants).Returns(new UserEvent[] {
                new UserEvent { EventId = 1, UserEmail = "*****@*****.**", NumberOfSpots = 2 },
                new UserEvent { EventId = 1, UserEmail = "*****@*****.**", NumberOfSpots = 1 },
                new UserEvent { EventId = 2, UserEmail = "*****@*****.**", NumberOfSpots = 1 },
                new UserEvent { EventId = 5, UserEmail = "*****@*****.**", NumberOfSpots = 2 },
                new UserEvent { EventId = 5, UserEmail = "*****@*****.**", NumberOfSpots = 2 },
                new UserEvent { EventId = 1, UserEmail = "*****@*****.**", NumberOfSpots = 1 }
            }.AsQueryable());

            eventMock.Setup(m => m.EventParticipants(It.IsAny<int>()))
                .Returns((int eventId) => {
                    return eventMock.Object.Participants.Where(s => s.EventId == eventId);
                });

            eventMock.Setup(m => m.Categories).Returns(new Category[] {
                new Category { CategoryId = 1, Name = "Category1" },
                new Category { CategoryId = 2, Name = "Category2" },
                new Category { CategoryId = 3, Name = "Category3" },
                new Category { CategoryId = 4, Name = "Category4" },
                new Category { CategoryId = 5, Name = "Category5" }
            }.AsQueryable());

            IEnumerable<SelectListItem> categories = eventMock.Object.Categories.ToList().Select(x =>
                new SelectListItem { Text = x.Name, Value = x.CategoryId.ToString() }
                );
            drawingMock = new Mock<IDrawingRepository>();
            drawingMock.Setup(m => m.Winners).Returns(new Winner[] {
                new Winner { EventId = 1, UserEmail = "*****@*****.**", NumberOfSpotsWon = 2 },
                new Winner { EventId = 2, UserEmail = "*****@*****.**", NumberOfSpotsWon = 2}
            }.AsQueryable());
            categories.FirstOrDefault().Selected = true;

            selectedEvent = new EventViewModel { SelectedEvent = eventMock.Object.Events.FirstOrDefault(), Categories = categories };

            controller = new DrawWinnerController(drawingMock.Object);
        }
        public void setup()
        {
            //Arrange
            var mockHttpContext = new Mock<ControllerContext>();

            mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("creator 2");
            mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            mock = new Mock<IEventRepository>();
            mock.Setup(m => m.Events).Returns(new Event[] {
                new Event { EventId = 1, Name = "event 1", Created = DateTime.Now, Creator = "creator 1", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 1 },
                new Event { EventId = 2, Name = "event 2", Created = DateTime.Now, Creator = "creator 2", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 2 },
                new Event { EventId = 3, Name = "event 3", Created = DateTime.Now, Creator = "creator 3", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 3 },
                new Event { EventId = 4, Name = "event 4", Created = DateTime.Now, Creator = "creator 4", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 4 },
                new Event { EventId = 5, Name = "event 5", Created = DateTime.Now, Creator = "creator 5", AvailableSpots = 2, DeadLine = DateTime.Now, CategoryId = 5 }
            }.AsQueryable());

            mock.Setup(m => m.Categories).Returns(new Category[] {
                new Category { CategoryId = 1, Name = "Category1", IsActive = true },
                new Category { CategoryId = 2, Name = "Category2", IsActive = true },
                new Category { CategoryId = 3, Name = "Category3", IsActive = true },
                new Category { CategoryId = 4, Name = "Category4", IsActive = true },
                new Category { CategoryId = 5, Name = "Category5", IsActive = true }
            }.AsQueryable());

            newevent = new Event
            {
                EventId = 10,
                Name = "CanCreateNewEvents",
                Created = DateTime.Now,
                Creator = "CanCreateNewEventsTest",
                AvailableSpots = 2,
                DeadLine = DateTime.Now,
                CategoryId = 10
            };

            IEnumerable<SelectListItem> categories = mock.Object.Categories.ToList().Select(x =>
                new SelectListItem { Text = x.Name, Value = x.CategoryId.ToString() }
                );

            categories.FirstOrDefault().Selected = true;

            selectedEvent = new EventViewModel { SelectedEvent = newevent, Categories = categories };

            controller = new EventController(mock.Object);
            controller.ControllerContext = new ControllerContext(mockHttpContext.Object.HttpContext, new RouteData(), controller);
        }