Esempio n. 1
0
        public async Task <ActionResult> StopCompetition(int?id)
        {
            if (id == null)
            {
                throw new HttpException(404, "Are you sure you're in the right place?");
            }
            db.Competitions.FirstOrDefault(x => x.Id == id).Status = Status.End;

            List <UserInCompetition> reqs = db.UserInCompetitions.Where(x => x.Location.CompetitionId == id).ToList();

            foreach (UserInCompetition item in reqs)
            {
                var umessages = db.Users.FirstOrDefault(x => x.Id == item.UserId).Messages;
                umessages.Add(new Message {
                    Subject = Resources.Messages.CompEnded, Body = Resources.Messages.Competition + " \"" + db.Competitions.FirstOrDefault(x => x.Id == id).Name + "\" " + Resources.Messages.isEnd + ".\n" + Resources.Messages.YourRes + " \"" + item.Result + "\".", Type = "alert-success"
                });
                if (item.User.EmailConfirmed)
                {
                    EmailSendController.SendEmail(item.User.Email /*"*****@*****.**"*/, Resources.Messages.CompEnded, Resources.Messages.Competition + " \"" + db.Competitions.FirstOrDefault(x => x.Id == id).Name + "\" " + Resources.Messages.isEnd + ".\n" + Resources.Messages.YourRes + " \"" + item.Result + "\".");
                }
                db.Users.FirstOrDefault(x => x.Id == item.UserId).CompetitionCount += 1;
            }
            var uid      = this.User.Identity.GetUserId();
            var messages = db.Users.FirstOrDefault(x => x.Id == uid).Messages;

            messages.Add(new Message {
                Subject = Resources.Messages.Success + "!", Body = Resources.Messages.StatusEnded, Type = "alert-success"
            });

            await db.SaveChangesAsync();

            return(RedirectToAction("Manage", "Home"));
        }
Esempio n. 2
0
        public async Task <ActionResult> EndRequests(int?id)
        {
            if (id == null)
            {
                throw new HttpException(404, "Are you sure you're in the right place?");
            }

            db.Competitions.FirstOrDefault(x => x.Id == id).Status = Status.Prepare;

            List <Request> reqs = db.Requests.Where(x => x.CompetitionId == id && x.Accepted == true).OrderByDescending(x => x.ApplicationUser.CompetitionCount).ToList();


            Dictionary <int, bool> locCount = new Dictionary <int, bool>();

            foreach (var item in db.Locations.Where(x => x.CompetitionId == id).ToList())
            {
                locCount.Add(item.Id, false);
            }

            foreach (Request item in reqs)//перебираем пользователей в запросах
            {
                foreach (Location loc in db.Locations.Where(x => x.CompetitionId == id).ToList())
                {
                    int con = db.UserInCompetitions.Where(x => x.LocationId == loc.Id).ToList().Count();
                    if (!locCount[loc.Id] && con < loc.UserCount) //если в локации достаточно места, то закидываем пользователя
                    {
                        db.UserInCompetitions.Add(new UserInCompetition {
                            LocationId = loc.Id, Location = loc, UserId = item.ApplicationUserId, User = db.Users.FirstOrDefault(x => x.Id == item.ApplicationUserId)
                        });
                        var umessages = db.Users.FirstOrDefault(x => x.Id == item.ApplicationUserId).Messages;
                        umessages.Add(new Message {
                            Subject = Resources.Messages.PrepareMain, Body = Resources.Messages.PrepareComp + " \"" + db.Competitions.FirstOrDefault(x => x.Id == id).Name + "\".\n" + Resources.Messages.PrepareLoc + " \"" + loc.Name + "\".", Type = "alert-success"
                        });
                        await db.SaveChangesAsync();

                        if (item.ApplicationUser.EmailConfirmed)
                        {
                            EmailSendController.SendEmail(item.ApplicationUser.Email /*"*****@*****.**"*/, Resources.Messages.PrepareMain, Resources.Messages.PrepareComp + " \"" + db.Competitions.FirstOrDefault(x => x.Id == id).Name + "\".\n" + Resources.Messages.PrepareLoc + " \"" + loc.Name + "\".");
                        }
                        break;
                    }
                    else
                    {
                        locCount[loc.Id] = true;
                    }

                    if (locCount.Where(x => x.Value == false).Count() == 0) //если места нет нигде => ошибка
                    {
                        var cuid      = this.User.Identity.GetUserId();
                        var cmessages = db.Users.FirstOrDefault(x => x.Id == cuid).Messages;
                        cmessages.Add(new Message {
                            Subject = Resources.Messages.Error + "!", Body = Resources.Messages.UserCountError, Type = "alert-danger"
                        });
                        await db.SaveChangesAsync();

                        return(RedirectToAction("Manage", "Home"));
                    }
                }
            }

            db.Requests.RemoveRange(db.Requests.Where(x => x.CompetitionId == id));//???
            var uid      = this.User.Identity.GetUserId();
            var messages = db.Users.FirstOrDefault(x => x.Id == uid).Messages;

            messages.Add(new Message {
                Subject = Resources.Messages.Success + "!", Body = Resources.Messages.StatusPrepared
            });

            await db.SaveChangesAsync();

            return(RedirectToAction("Manage", "Home"));
        }