public void AddRequirementsToStudent(Student student)
 {
     //var listOfId = _context.RequirementStudents
     //    .Where(c => c.StudentId == student.Id)
     //    .Select(c => c.RequirementId);
     //var listRequirement = _context.Requirements
     //    .Where(c => !listOfId.Contains(c.Id));
     //if (!listRequirement.Any())
     //{
     //    return;
     //}
     //foreach (var req in listRequirement)
     //{
     //    var newReq = new RequirementStudents
     //    {
     //        RequirementId = req.Id,
     //        StudentId = student.Id,
     //        Status = false,
     //    };
     //    _context.RequirementStudents.Add(newReq);
     //}
     //_context.SaveChangesAsync();
     using (var context = new MorenoContext())
     {
         var listOfId = context.RequirementStudents
                        .Where(c => c.StudentId == student.Id)
                        .Select(c => c.RequirementId);
         var listRequirement = context.Requirements
                               .Where(c => !listOfId.Contains(c.Id));
         if (!listRequirement.Any())
         {
             return;
         }
         foreach (var req in listRequirement)
         {
             var newReq = new RequirementStudents
             {
                 RequirementId = req.Id,
                 StudentId     = student.Id,
                 Status        = false,
             };
             context.RequirementStudents.Add(newReq);
         }
         context.SaveChangesAsync();
     }
 }
 private async void DoSubmit()
 {
     bool result = false;
     await DialogHost.Show(new PleaseWaitView(), "RootDialog",
                           delegate(object sender, DialogOpenedEventArgs args)
     {
         Task.Run(() =>
         {
             //try
             //{
             foreach (var sv in StudentVotes)
             {
                 //_context.Entry(sv).State = EntityState.Added;
                 _context.StudentVotes.Add(sv);
             }
             _context.SaveChangesAsync();
             result = true;        //}
             //catch (Exception e)
             //{
             //    Console.WriteLine(e);
             //    result = false;
             //}
             Thread.Sleep(500);
         }).ContinueWith((t, _) =>
         {
             Messenger.Default.Send(new SubmitVoteMessage());
             Messenger.Default.Send(new SetNavigation()
             {
                 Content = new VoterProfileView()
                 {
                     DataContext = new VoterProfileViewModel(ref _context, CurrentStudent)
                 }
             });
             args.Session.UpdateContent(new OkMessageDialog()
             {
                 DataContext = result ? "Vote Success" : "Vote Failed, Please inform your teacher"
             });
         }, null, TaskScheduler.FromCurrentSynchronizationContext());
     });
 }
        private async void DoEnd()
        {
            await DialogHost.Show(new OkCancelMessageDialog()
            {
                DataContext = "Do you want to end the election?"
            },
                                  "RootDialog", delegate(object sender, DialogClosingEventArgs args)
            {
                if (Equals(args.Parameter, false))
                {
                    return;
                }
                if (Equals(args.Parameter, "Ok") && IsEndDialog)
                {
                    IsEndDialog = false;
                    return;
                }

                if (Equals(args.Parameter, "Ok") && !IsEndDialog)
                {
                    IsEndDialog = true;
                    args.Cancel();
                    args.Session.UpdateContent(new PleaseWaitView());
                    bool result = false;
                    Task.Run(() =>
                    {
                        try
                        {
                            int votes   = 0;
                            int cmvotes = 0;
                            //var cmMember = new CouncilMembers();
                            List <ElectionHistory> electHistory = new List <ElectionHistory>();
                            var positions    = _context.CouncilPositions.ToList();
                            var members      = _context.CouncilMembers.ToList();
                            var hasVotes     = _context.StudentVotes.FirstOrDefault();
                            bool resultVotes = hasVotes != null;
                            //if (!positions.Any() && !members.Any())
                            if (!resultVotes)
                            {
                                IsEndDialog = true;
                                args.Session.UpdateContent(new OkMessageDialog()
                                {
                                    DataContext = "Failed to End"
                                });
                                return;
                            }
                            //int highestVote = 0;
                            foreach (var position in positions)
                            {
                                var cmMember = new CouncilMembers();
                                foreach (var member in members)
                                {
                                    if (member.CouncilPosition == position)
                                    {
                                        votes   = CalculateVotes(member.Student);
                                        cmvotes = CalculateVotes(cmMember?.Student);
                                        if (votes > cmvotes)
                                        {
                                            //highestVote = votes;
                                            cmMember = member;
                                        }
                                    }
                                }

                                var eHistory = new ElectionHistory()
                                {
                                    Position  = cmMember.CouncilPosition.Position,
                                    Name      = cmMember.Student.FullName,
                                    DateYear  = DateTime.Now,
                                    VoteCount = CalculateVotes(cmMember?.Student)
                                };
                                electHistory.Add(eHistory);
                            }
                            _context.ElectionHistory.AddRange(electHistory);
                            _context.CouncilMembers.RemoveRange(_context.CouncilMembers);
                            _context.PartyLists.RemoveRange(_context.PartyLists);
                            var list = _context.StudentVotes.ToList();
                            _context.StudentVotes.RemoveRange(list);
                            _context.ElectionStatus.RemoveRange(_context.ElectionStatus);
                            _context.SaveChangesAsync();
                            result = true;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            result = false;
                        }
                    }).ContinueWith((t, _) =>
                    {
                        LoadData();
                        //args.Session.Close(false);
                        Messenger.Default.Send(new EndElectionMessage());
                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = result ? "End Successfull, Check Election History for results." : "Failed to end"
                        });
                    }, null, TaskScheduler.FromCurrentSynchronizationContext());
                }
            });

            //int votes = 0;
            //int cmvotes = 0;
            ////var cmMember = new CouncilMembers();
            //List<ElectionHistory> electHistory = new List<ElectionHistory>();
            //var positions = _context.CouncilPositions.ToList();
            //var members = _context.CouncilMembers.ToList();
            //if (!positions.Any() && !members.Any())
            //{
            //    await DialogHost.Show(new OkMessageDialog() {DataContext = "Failed to End"}, "RootDialog");
            //    return;
            //}
            //foreach (var position in positions)
            //{
            //    var cmMember = new CouncilMembers();
            //    foreach (var member in members)
            //    {
            //        if (member.CouncilPosition == position)
            //        {
            //            votes = CalculateVotes(member.Student);
            //            cmvotes = CalculateVotes(cmMember?.Student);
            //            if (votes > cmvotes)
            //            {
            //                cmMember = member;
            //            }
            //        }
            //    }

            //    var eHistory = new ElectionHistory()
            //    {
            //        Position = cmMember.CouncilPosition.Position,
            //        Name = cmMember.Student.FullName,
            //        DateYear = DateTime.Now,
            //        VoteCount = cmvotes
            //    };
            //    electHistory.Add(eHistory);
            //}
            //_context.ElectionHistory.AddRange(electHistory);
            //_context.CouncilMembers.RemoveRange(_context.CouncilMembers);
            //_context.PartyLists.RemoveRange(_context.PartyLists);
            //_context.StudentVotes.RemoveRange(_context.StudentVotes);
            //_context.ElectionStatus.RemoveRange(_context.ElectionStatus);
            //_context.SaveChanges();
            //LoadData();
            ////RaisePropertyChanged(() => StartCommand);
            ////RaisePropertyChanged(() => CanStart);
        }
Esempio n. 4
0
        private void OnBorrowClosing(object sender, DialogClosingEventArgs args)
        {
            if (Equals(args.Parameter, false))
            {
                return;
            }

            if (args.Parameter is TextBox)
            {
                args.Session.UpdateContent(new PleaseWaitView());
                TextBox txtName = (TextBox)args.Parameter;
                int     number;
                var     name     = txtName.Text.Trim();
                var     tryParse = Int32.TryParse(name, out number);
                if (tryParse)
                {
                    if (number > SelectedBook.AvailableQuantity)
                    {
                        args.Cancel();
                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = "Borrow quantity can't exceed available quantity."
                        });
                        return;
                    }
                    if (number < 1)
                    {
                        args.Cancel();
                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = "Minimum quantity is 1."
                        });
                        return;
                    }
                }
                Task.Run(() =>
                {
                    if (tryParse)
                    {
                        _oldSelectedTeacher = SelectedTeacher;
                        var bookSearch      =
                            _context.TeacherBorrowedBooks.FirstOrDefault(c => c.Teacher.Id == SelectedTeacher.Id &&
                                                                         c.Book.Id == SelectedBook.Id);
                        bool isFind = bookSearch != null;

                        if (!isFind)
                        {
                            var newBorrowedbook = new TeacherBorrowedBook
                            {
                                Teacher          = SelectedTeacher,
                                Book             = SelectedBook,
                                QuantityBorrowed = number,
                                DateBorrowed     = DateTime.Now.Date,
                            };

                            //SelectedBook.AvailableQuantity =
                            _context.TeacherBorrowedBooks.Add(newBorrowedbook);
                            _context.SaveChanges();
                        }
                        else
                        {
                            var borBook = _context.TeacherBorrowedBooks.FirstOrDefault(c => c.Teacher.Id == SelectedTeacher.Id &&
                                                                                       c.Book.Id == SelectedBook.Id);
                            if (borBook != null)
                            {
                                borBook.QuantityBorrowed = borBook.QuantityBorrowed + number;
                                //SelectedBook.AvailableQuantity -= number;
                                //_unitOfWork.TeacherBorrowedBook.ModifyBorrowedBook(borBook);
                            }
                        }
                        var newBorrowedBook = new TeacherBorrowedBook();
                        //var borrewdBook = _context.TeacherBorrowedBooks.


                        var bookList         = _context.TeacherBorrowedBooks.Where(c => c.Book.Id == SelectedBook.Id).ToList();
                        int quantityBorrowed = 0;
                        foreach (var book in bookList)
                        {
                            quantityBorrowed += book.QuantityBorrowed;
                        }

                        SelectedBook.AvailableQuantity = SelectedBook.Quantity -
                                                         (quantityBorrowed + SelectedBook.Damaged +
                                                          SelectedBook.Outdated);

                        _context.SaveChangesAsync();
                    }
                }).ContinueWith((t, _) =>
                {
                    LoadData();
                    Messenger.Default.Send(new NewBookUpdate());
                    SelectedTeacher = _oldSelectedTeacher;
                }, null, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }