Example #1
0
 public TestRecord(TestMatch match, ApplicationUser user)
 {
     Id    = Guid.NewGuid();
     Match = match;
     User  = user;
     Score = "";
 }
Example #2
0
        public bool ApplyMatch(TestMatch match, ApplicationUser user)
        {
            using (var db = new ApplicationDbContext())
            {
                //获取实体对象
                var contextmatch = db.TestMatches.Find(match.Id);
                var contextuser  = db.Users.Find(user.Id);
                var record       = new TestRecord(contextmatch, contextuser);
                if (contextmatch == null || contextuser == null)
                {
                    return(false);
                }

                //判断是否可以继续操作
                if (contextmatch.Count >= contextmatch.Limit || !contextmatch.Enabled || contextuser.Applied || !contextuser.Enabled)
                {
                    return(false);
                }

                db.TestRecords.Add(record);
                contextuser.Applied = true;
                db.OperationRecords.Add(new OperationRecord(user.StudentNumber, "Apply", match.Name));

                //保存,采取乐观并发
                bool succeed;
                do
                {
                    succeed = true;
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        succeed = false;
                        ex.Entries.Single().Reload();
                        if (contextmatch.Count >= contextmatch.Limit)
                        {
                            return(false);
                        }
                    }
                } while (!succeed);

                //检查操作成功
                if (!contextmatch.RecordsCollection.Contains(record))
                {
                    return(false);
                }
            }
            return(true);
        }
Example #3
0
        public bool QuitMatch(TestMatch match, ApplicationUser user)
        {
            if (user.Applied == false)
            {
                return(false);
            }
            using (var db = new ApplicationDbContext())
            {
                var contextmatch = db.TestMatches.Find(match.Id);
                var contextuser  = db.Users.Find(user.Id);
                var record       = contextuser.RecordsCollection.Where(r => r.Match.StartTime <= DateTime.Now && r.Match.EndTime >= DateTime.Now).SingleOrDefault();
                if (contextmatch == null || contextuser == null || record == null)
                {
                    return(false);
                }

                if (!contextmatch.RecordsCollection.Contains(record) || !contextmatch.Enabled)
                {
                    return(false);
                }

                contextmatch.RecordsCollection.Remove(record);
                contextuser.RecordsCollection.Remove(record);
                db.TestRecords.Remove(record);
                contextuser.Applied = false;
                db.OperationRecords.Add(new OperationRecord(user.StudentNumber, "Quit", contextmatch.Name));

                bool succeed;
                do
                {
                    succeed = true;
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        succeed = false;
                        ex.Entries.Single().Reload();
                    }
                } while (!succeed);

                if (contextmatch.RecordsCollection.Contains(record))
                {
                    return(false);
                }
            }
            return(true);
        }