// // GET: /Winners/ public ActionResult Index(long facebookId, string oAuthToken) { using (var context = new MakroDataContext()) { List<Winner> winners = context.Winners.OrderBy(w => w.CompetitionId).ToList(); var viewModels = new List<ShowWinnersViewModel>(); foreach (var item in winners) { var model = new ShowWinnersViewModel() { FirstUserName = context.Participants.FirstOrDefault(p => p.FacebookId == item.FirstFacebookId).Name, SecondUserName = context.Participants.FirstOrDefault(p => p.FacebookId == item.SecondFacebookId).Name, ThirdUserName = context.Participants.FirstOrDefault(p => p.FacebookId == item.ThirdFacebookId).Name, StartDate = item.Competition.StartDate, EndDate = item.Competition.EndDate, CompetitionId = item.CompetitionId }; viewModels.Add(model); } if (winners.Count == 0) { ViewBag.Message = "Zapraszamy do regularnego odwiedzania tej zakładki – już wkrótce pojawią się tu wyniki pierwszego tygodnia konkursu."; } return View(viewModels); } }
public MakroDatabaseInitializer() { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MakroDataContext>()); if (!IsInitialized) { using (MakroDataContext makroDataContext = new MakroDataContext()) { makroDataContext.Database.Initialize(false); Reseed(makroDataContext); IsInitialized = true; } } }
public static int GetProperCompetition() { using (var context = new MakroDataContext()) { try { Competition competition = context.Competitons.FirstOrDefault(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now); return competition.CompetitionId; } catch (NullReferenceException e) { throw e; } } }
public PickWinnersActionResultModel() { using (var context = new MakroDataContext()) { int competitionId = CheckCompetition.GetProperCompetition(); if (competitionId != 0) { List<Recipe> recipes = context.Recipes.Where(r => r.CompetitionId == competitionId).ToList(); ViewModel = new List<PickWinnersViewModel>(); foreach (var recipe in recipes) { Winner claimedWinners = context.Winners.FirstOrDefault(w => w.CompetitionId == competitionId); if (claimedWinners != null) { bool isUserWon = claimedWinners.FirstFacebookId == recipe.User.FacebookId || claimedWinners.SecondFacebookId == recipe.User.FacebookId || claimedWinners.ThirdFacebookId == recipe.User.FacebookId; PickWinnersViewModel model = new PickWinnersViewModel() { FacebookId = recipe.User.FacebookId, Name = recipe.User.Name, Points = recipe.Points, CompetitionId = competitionId, IsWinner = isUserWon }; ViewModel.Add(model); AllReadyWon = true; } else { PickWinnersViewModel model = new PickWinnersViewModel() { FacebookId = recipe.User.FacebookId, Name = recipe.User.Name, Points = recipe.Points, CompetitionId = competitionId }; ViewModel.Add(model); } } } } }
public ActionResult SubmitWinners(List<PickWinnersViewModel> model, string competitionId, bool winnersPicked) { if (winnersPicked) { ViewBag.WinnersPicked = true; ViewBag.ErrorMessage = "Zwycięzcy obecnego tygodnia zostali już wybrani."; return View("PickWinners", model); } if (model.Count(m => m.Place != 0 && m.IsWinner == true) != 3) { ViewBag.ErrorMessage = "Wybierz 3 zwycięzców i przypisz im miejsca!"; ViewBag.WinnersPicked = false; return View("PickWinners", model); } using (var context = new MakroDataContext()) { int competitionNumber; int.TryParse(competitionId, out competitionNumber); List<PickWinnersViewModel> fixedModel = model.Where(m => m.IsWinner).OrderBy(m => m.Place).ToList(); if (fixedModel.Count() == 3) { Winner winner = new Winner() { CompetitionId = competitionNumber, FirstFacebookId = fixedModel[0].FacebookId, SecondFacebookId = fixedModel[1].FacebookId, ThirdFacebookId = fixedModel[2].FacebookId }; context.Winners.Add(winner); context.SaveChanges(); ViewBag.ErrorMessage = "Zwycięzcy zostali wybrani!"; return View("Index"); } else { ViewBag.ErrorMessage = "Wybierz 3 zwycięzców!"; ViewBag.WinnersPicked = false; return View("PickWinners", model); } } }
public void Reseed(MakroDataContext context) { if (!context.Participants.Any()) { context.Participants.AddOrUpdate(m => m.FacebookId, new DataContext.Models.Participant() { FacebookId = 1, Name = "Jan Kowalski" }, new DataContext.Models.Participant() { FacebookId = 2, Name = "Anna Nowak" }, new DataContext.Models.Participant() { FacebookId = 3, Name = "Stefan Batory" }, new DataContext.Models.Participant() { FacebookId = 4, Name = "Elzbieta Pierwsza" }, new DataContext.Models.Participant() { FacebookId = 5, Name = "Jan Kochanowski" }, new DataContext.Models.Participant() { FacebookId = 6, Name = "Maria Skłodowska-Curie" }, new DataContext.Models.Participant() { FacebookId = 7, Name = "Karolina Kaszubowska" }); context.SaveChanges(); } if (!context.Competitons.Any()) { context.Competitons.AddOrUpdate(m => m.CompetitionId, new DataContext.Models.Competition() { CompetitionId = 1, StartDate = DateTime.Parse("2013-10-07"), EndDate = DateTime.Parse("2013-10-14") }, new DataContext.Models.Competition() { CompetitionId = 2, StartDate = DateTime.Parse("2013-10-14"), EndDate = DateTime.Parse("2013-10-21") }, new DataContext.Models.Competition() { CompetitionId = 3, StartDate = DateTime.Parse("2013-10-21"), EndDate = DateTime.Parse("2013-10-28") }, new DataContext.Models.Competition() { CompetitionId = 4, StartDate = DateTime.Parse("2013-10-28"), EndDate = DateTime.Parse("2013-11-04") }, new DataContext.Models.Competition() { CompetitionId = 5, StartDate = DateTime.Parse("2013-11-04"), EndDate = DateTime.Parse("2013-11-11") }, new DataContext.Models.Competition() { CompetitionId = 6, StartDate = DateTime.Parse("2013-11-11"), EndDate = DateTime.Parse("2013-11-18") }, new DataContext.Models.Competition() { CompetitionId = 7, StartDate = DateTime.Parse("2013-11-18"), EndDate = DateTime.Parse("2013-11-25") }, new DataContext.Models.Competition() { CompetitionId = 8, StartDate = DateTime.Parse("2013-11-25"), EndDate = DateTime.Parse("2013-12-01") }); context.SaveChanges(); } if (!context.Recipes.Any()) { context.Recipes.AddOrUpdate(m => m.RecipeId, new DataContext.Models.Recipe() { CompetitionId = 1, FacebookId = 1, RecipeId = 1, FormSubmitDate = DateTime.Now.AddDays(-25), Title = "Pierogi", Content = "Oto przepis" }, new DataContext.Models.Recipe() { CompetitionId = 1, FacebookId = 2, RecipeId = 2, FormSubmitDate = DateTime.Now.AddDays(-25), Title = "Schabowy", Content = "Oto przepis" }, new DataContext.Models.Recipe() { CompetitionId = 1, FacebookId = 3, RecipeId = 3, FormSubmitDate = DateTime.Now.AddDays(-25), Title = "Mielone", Content = "Oto przepis" }, new DataContext.Models.Recipe() { CompetitionId = 2, FacebookId = 4, RecipeId = 4, FormSubmitDate = DateTime.Now.AddDays(-15), Title = "Bigos", Content = "Oto przepis" }, new DataContext.Models.Recipe() { CompetitionId = 2, FacebookId = 5, RecipeId = 5, FormSubmitDate = DateTime.Now.AddDays(-15), Title = "Piers z kurczaka", Content = "Oto przepis" }, new DataContext.Models.Recipe() { CompetitionId = 2, FacebookId = 6, RecipeId = 6, FormSubmitDate = DateTime.Now.AddDays(-15), Title = "Zupa owocowa", Content = "Oto przepis" }, new DataContext.Models.Recipe() { CompetitionId = 2, FacebookId = 7, RecipeId = 7, FormSubmitDate = DateTime.Now.AddDays(-15), Title = "Cacyki", Content = "Oto przepis" }); context.SaveChanges(); } }
public ActionResult AddRecipe(Recipe model, long facebookId, string oAuthToken, bool tc) { if (!ModelState.IsValidField("Title") || !ModelState.IsValidField("Content")) { ViewBag.TermsConditionMessage = "Upewnij się czy pola nie są puste i czy zaakceptowałeś regulamin"; return View("AddRecipe", model); } else if (tc == false) { ViewBag.TermsConditionMessage = "Musisz najpierw zaakceptować regulamin"; return View("AddRecipe", model); } else { using (var context = new MakroDataContext()) { string fbUserFullName = GetFbUserFullName.Get(facebookId); try { Participant user; Recipe recipe = new Recipe(); if (!string.IsNullOrEmpty(fbUserFullName)) { if (context.Participants.FirstOrDefault(p => p.FacebookId == facebookId) == null) { user = new Participant() { FacebookId = facebookId, Name = fbUserFullName }; context.Participants.Add(user); context.SaveChanges(); } else { user = context.Participants.FirstOrDefault(p => p.FacebookId == facebookId); } recipe.User = user; recipe.Title = model.Title; recipe.Content = model.Content.Replace(System.Environment.NewLine, "<br />"); recipe.FormSubmitDate = DateTime.Now; recipe.CompetitionId = CheckCompetition.GetProperCompetition(); context.Recipes.Add(recipe); context.SaveChanges(); } return RedirectToAction("ShowRecipe", new { facebookId = facebookId, recipeId = recipe.RecipeId, oAuthToken = oAuthToken }); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine( "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } } }
public ActionResult VoteForRecipe(long facebookId, int recipeId, string oAuthToken) { using (var context = new MakroDataContext()) { Vote vote = context.Votes.FirstOrDefault(v => v.RecipeId == recipeId && v.FacebookId == facebookId); if (vote == null) { try { Recipe recipe = context.Recipes.FirstOrDefault(r => r.RecipeId == recipeId); Vote newVote = new Vote() { FacebookId = facebookId, RecipeId = recipeId, VoteDate = DateTime.Now, }; context.Votes.Add(newVote); context.SaveChanges(); recipe.Points += 1; context.Entry(recipe).State = EntityState.Modified; context.SaveChanges(); TempData["VoteDone"] = true; return RedirectToAction("ShowRecipe", "Recipes", new { facebookId, recipeId, oAuthToken }); } catch (NullReferenceException e) { throw e; } } else { return RedirectToAction("ShowRecipe", "Recipes", new { facebookId, recipeId, oAuthToken }); } } }
public ActionResult ShowRecipe(long? facebookId, int recipeId, string oAuthToken) { using (var context = new MakroDataContext()) { if (!facebookId.HasValue) { SignedRequest signedRequest = this.FacebookInit(); if (signedRequest == null || signedRequest.OauthToken == null) { Session.Add("recipeId", recipeId); return RedirectToAction("OAuthRedirect", "Init"); } ViewBag.FacebookId = signedRequest.UserId; ViewBag.OAuthToken = signedRequest.OauthToken; } Recipe recipe = context.Recipes.FirstOrDefault(r => r.RecipeId == recipeId); Vote vote = context.Votes.FirstOrDefault(v => v.RecipeId == recipeId && v.FacebookId == facebookId); if (recipe != null) { ShowRecipeViewModel model = new ShowRecipeViewModel() { Content = recipe.Content, Title = recipe.Title, Points = recipe.Points, Author = recipe.User.Name, RecipeId = recipeId }; if (TempData["VoteDone"] != null || vote != null) { ViewBag.VoteDone = true; } ViewBag.RecipeId = recipeId; return View(model); } else { return View("Error"); } } }
// // GET: /Brands/ public ActionResult Index(long facebookId, string oAuthToken, int? pageNumber) { ViewBag.PageNumber = pageNumber; int recipeId = Convert.ToInt32(Session["recipeId"]); if (recipeId != 0) { Session.Remove("recipeId"); return RedirectToAction("ShowRecipe", "Recipes", new { facebookId = facebookId, oAuthToken = oAuthToken, recipeId = recipeId }); } if (pageNumber == null) { pageNumber = 1; } int toSkip = pageNumber.Value - 1; List<Recipe> recipes = new List<Recipe>(); List<RecipesListItemViewModel> recipesList = new List<RecipesListItemViewModel>(); RecipesListItemViewModel recipeListItem; using (var context = new MakroDataContext()) { recipes.AddRange(context.Recipes.OrderByDescending(x => x.FormSubmitDate).Skip(toSkip * 6).Take(6)); double pagesAmount = (context.Recipes.Count() / 6.0); int pages = 0; if (pagesAmount % 6.0 != 0) { pagesAmount += 1; pages = Convert.ToInt32(Math.Floor(pagesAmount)); } ViewBag.PagesAmount = pages; for (int i = 0; i < 6; i++) { recipeListItem = new RecipesListItemViewModel(); if (recipes.Count >= i+1) { recipeListItem.Id = recipes[i].RecipeId; recipeListItem.UserName = EditUserNameIfNeeded.Edit(recipes[i].User.Name); recipeListItem.Points = recipes[i].Points; recipeListItem.Title = recipes[i].Title.ToUpper(); recipeListItem.IsNotEmpty = true; } recipeListItem.NumberOnList = i + 6 * (pageNumber.Value - 1) + 1; if (i < 3) { recipeListItem.CssClass = "recipeNoteFirstRow"; } else { recipeListItem.CssClass = "recipeNoteSecondRow"; } recipesList.Add(recipeListItem); } } return View(recipesList); }