コード例 #1
0
        public async Task <IActionResult> Send([Bind("ReceiverID", "Header", "Body")] MailboxViewModel mailBox)
        {
            if (ModelState.IsValid)
            {
                var currid    = GetUserID();
                var addThread = new Thread();
                addThread.InitiatorID = currid;
                addThread.ReceiverID  = mailBox.ReceiverID;
                _context.Add(addThread);
                await _context.SaveChangesAsync();

                var addMessage = new Message();
                addMessage.MsgHeader   = mailBox.Header;
                addMessage.MsgBody     = mailBox.Body;
                addMessage.MsgSenderID = currid;
                _context.Add(addMessage);
                await _context.SaveChangesAsync();

                var addConversation = new Conversation();
                addConversation.MessageID = addMessage.MessageID;
                addConversation.ThreadID  = addThread.ThreadID;
                _context.Add(addConversation);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Mail)));
            }

            var testList = _context.Profiles
                           .Select(p => new { p.ProfileID, p.FirstName, p.LastName })
                           .ToList();

            mailBox.ReceiverList = new SelectList(testList, "ProfileID", "FirstName");
            return(View(mailBox));
        }
コード例 #2
0
        public ActionResult <Game> Update(Game game)
        {
            using var gameDataContext = new GameDataContext();
            var gameData = gameDataContext.GameDatas.Where(g => g.Id == game.Id).SingleOrDefault();

            gameData.Date = game.Date;
            gameDataContext.SaveChanges();
            using var matchContext = new MatchContext();
            var matches = matchContext.Matches.Where(m => m.GameId == game.Id);

            matchContext.Matches.RemoveRange(matches);
            foreach (var player in game.Team1)
            {
                matchContext.Add(new Match
                {
                    GameId = game.Id,
                    Player = player,
                    Team   = 1,
                });
            }
            foreach (var player in game.Team2)
            {
                matchContext.Add(new Match
                {
                    GameId = game.Id,
                    Player = player,
                    Team   = 2,
                });
            }
            matchContext.SaveChanges();
            using var scoreContext = new ScoreContext();
            var score = scoreContext.Scores.Where(s => s.GameId == game.Id).Single();

            if (game.Score == '1')
            {
                score.Result = 1f;
            }
            else if (game.Score == '2')
            {
                score.Result = 0f;
            }
            else if (game.Score == 'D')
            {
                score.Result = 0.5f;
            }
            else if (game.Score == 'C')
            {
                score.Result = -1f;
            }
            else
            {
                score.Result = null;
            }
            scoreContext.SaveChanges();
            return(CreatedAtAction(nameof(GetById), new { id = game.Id }, game));
        }
コード例 #3
0
 public async Task<IActionResult> Create([Bind("FirstID,SecondID,Status")] BuddyState buddyState)
 {
     if (ModelState.IsValid)
     {
         _context.Add(buddyState);
         await _context.SaveChangesAsync();
         return RedirectToAction(nameof(Index));
     }
     ViewData["FirstID"] = new SelectList(_context.Profiles, "ProfileID", "FirstName", buddyState.FirstID);
     ViewData["SecondID"] = new SelectList(_context.Profiles, "ProfileID", "FirstName", buddyState.SecondID);
     return View(buddyState);
 }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("FirstName,LastName")] User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(user);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Cannot save new User." +
                                         "Try again, check that your entry information is correct.");
            }
            return(View(user));
        }
コード例 #5
0
        public async Task <IActionResult> CreatePersonal([Bind("RaceType,RaceTimeHours,RaceTimeMinutes,RaceTimeSeconds")] RaceRecordViewModel raceRecordView)
        {
            if (ModelState.IsValid)
            {
                var currid    = GetUserID();
                int totalTime = raceRecordView.RaceTimeHours * 3600 + raceRecordView.RaceTimeMinutes * 60 + raceRecordView.RaceTimeSeconds;
                int mileTime  = Functions.GetMileTime(totalTime, raceRecordView.RaceType);
                if (mileTime > 223)
                {
                    var race = new RaceRecord
                    {
                        ProfileID = currid,
                        RaceTime  = totalTime,
                        RaceType  = raceRecordView.RaceType,
                        MileTime  = mileTime
                    };


                    try
                    {
                        _context.Add(race);
                        await _context.SaveChangesAsync();

                        //return RedirectToAction("Index", "Profiles", new {@show = "myracerecords"});
                        return(RedirectToAction("CreatePersonalSuccess", "RaceRecords", race));
                    }
                    catch (Exception ex)
                    {
                        return(View());
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Wow, you must be fast!  Unfortnately your mile time must be greater than 3:43!");
                }
            }
//            ViewData["UserID"] = new SelectList(_context.Users, "UserID", "FirstName", raceRecord.ProfileID);
            return(View(raceRecordView));
        }
コード例 #6
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Calculate mile time.
                int totalTime = model.FirstTimeEntry.RaceTimeHours * 3600 + model.FirstTimeEntry.RaceTimeMinutes * 60 + model.FirstTimeEntry.RaceTimeSeconds;
                var mileTime  = Functions.GetMileTime(totalTime, model.FirstTimeEntry.RaceType);

                //Check minimum mile time is greater than current world record.
                if (mileTime > 223)
                {
                    var user = new SprintOne.Models.User
                    {
                        UserName = model.Username
                    };

                    using (var registrationTransaction = _context.Database.BeginTransaction())
                    {
                        try
                        {
                            var result = await userManager.CreateAsync(user, model.Password);

                            if (result.Succeeded)
                            {
                                var profile = new SprintOne.Models.Profile
                                {
                                    ProfileID    = user.UserID,
                                    FirstName    = model.Firstname,
                                    LastName     = model.Lastname,
                                    CreationDate = DateTime.Now,
                                    UserID       = user.UserID
                                };

                                var race = new RaceRecord
                                {
                                    RaceType  = model.FirstTimeEntry.RaceType,
                                    RaceTime  = totalTime,
                                    ProfileID = user.UserID,
                                    MileTime  = mileTime
                                };
                                _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Profiles ON");
                                _context.Add(profile);
                                _context.SaveChanges();
                                _context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Profiles OFF");
                                _context.Add(race);
                                _context.SaveChanges();
                                registrationTransaction.Commit();
                                await signInManager.SignInAsync(user, isPersistent : false);

                                return(RedirectToAction("Success", "Home"));
                            }
                            else
                            {
                                foreach (var error in result.Errors)
                                {
                                    ModelState.AddModelError("", error.Description);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            registrationTransaction.Rollback();
                            ModelState.AddModelError("", "Unable to register, please contact your administrator for more details.");
                            return(View(model));
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Wow, you must be fast!  Unfortnately your mile time must be greater than 3:43!");
                }
            }
            return(View(model));
        }