Example #1
0
 private void Reload(DateTime start, DateTime end)
 {
     if (start < end)
     {
         listHistory.DataSource = null;
         selectTopic.DataSource = null;
         using (var db = new DuoContext()) {
             var his = db.Histories.AsNoTracking().Where(x => x.CreatedDate >= start && x.CreatedDate <= end).ToList();
             totalHistory.Text = his.Count.ToString();
             if (his.Count > 0)
             {
                 var hisSelect = his.Select(x => new HistorySelection()
                 {
                     Id = x.Id, CreatedDate = x.CreatedDate.ToString("yyyy/MM/dd")
                 })
                                 .ToList();
                 listHistory.DataSource    = hisSelect;
                 listHistory.DisplayMember = "CreatedDate";
             }
             var yesterday = end.AddDays(-2.5);
             var topics    = db.Topics.AsNoTracking().Where(x => !x.IsDeleted && !x.IsHide &&
                                                            !x.HistoryDetails.Any(y => y.MyHistory.CreatedDate > yesterday && y.MyHistory.CreatedDate <= end))
                             .OrderBy(x => x.Sort)
                             .Select(x => new SelectionData {
                 Id = x.Id, Name = x.Title
             }).ToList();
             selectTopic.DataSource    = topics;
             selectTopic.DisplayMember = "Name";
         }
         return;
     }
     MessageBox.Show("Start Date and End Date invalid", "Date select not valid");
 }
Example #2
0
        private void addHistory_Click(object sender, EventArgs e)
        {
            var now     = DateTime.Now.Date;
            var nextDay = now.AddDays(1);
            var topic   = (SelectionData)selectTopic.SelectedItem;

            using (var db = new DuoContext()) {
                History his;
                if (!db.Histories.Any(x => x.CreatedDate >= now && x.CreatedDate < nextDay))
                {
                    his = new History()
                    {
                        CreatedDate = now
                    };
                    db.Histories.Add(his);
                    db.SaveChanges();
                    var start = startDate.Value;
                    var end   = endDate.Value;
                    Reload(start, end);
                }
                else
                {
                    his = db.Histories.FirstOrDefault(x => x.CreatedDate >= now && x.CreatedDate < nextDay &&
                                                      !x.HistoryDetails.Any(y => y.TopicId == topic.Id));
                }
                if (his == null)
                {
                    MessageBox.Show("Chủ đề đã kiểm tra và thêm lịch sử", "Trùng lập dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                var hisDetail = new HistoryDetail()
                {
                    HistoryId = his.Id,
                    TopicId   = topic.Id,
                    DataTest  = new byte[0]
                };
                db.HistoryDetails.Add(hisDetail);
                db.SaveChanges();
                listTopic.DataSource = null;
                var hisDetails = db.HistoryDetails.AsNoTracking()
                                 .Where(x => x.MyHistory.Id == his.Id)
                                 .OrderBy(x => x.Id)
                                 .Select(x => new { Id = x.Id, Title = x.MyTopic.Title }).ToList();
                totalTopic.Text         = hisDetails.Count.ToString();
                listTopic.DataSource    = hisDetails;
                listTopic.DisplayMember = "Title";
                var topics = (List <SelectionData>)selectTopic.DataSource;
                var index  = topics.FindIndex(x => x.Id == topic.Id);
                topics.RemoveAt(index);
                selectTopic.DataSource    = null;
                selectTopic.DataSource    = topics;
                selectTopic.DisplayMember = "Name";
            }
            this.Refresh();
        }
Example #3
0
        private void exportAllData_Click(object sender, EventArgs e)
        {
            var    fod  = new FileOutData();
            string data = "";

            using (var db = new DuoContext()) {
                fod.Topics         = db.Topics.AsNoTracking().AsQueryable().ToList();
                fod.Histories      = db.Histories.AsNoTracking().AsQueryable().ToList();
                fod.HistoryDetails = db.HistoryDetails.AsNoTracking().AsQueryable().ToList();
                data = JsonConvert.SerializeObject(fod);
            }
            FileService.WriteNewFile("DuoData.json", data);
        }
Example #4
0
 private void deleteTopic_Click(object sender, EventArgs e)
 {
     if (_topic != null && !string.IsNullOrWhiteSpace(topicTitle.Text))
     {
         using (var db = new DuoContext()) {
             _topic.IsDeleted = true;
             var chk = db.Update <Topic>(_topic);
         }
         LoadData();
         return;
     }
     MessageBox.Show("Vui lòng chọn một chủ đề hay nhập thông tin chủ đề", "Dữ liệu chưa được chọn"
                     , MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
Example #5
0
        private void listTopic_SelectedIndexChanged(object sender, EventArgs e)
        {
            var value = (string)listTopic.SelectedItem;

            if (!string.IsNullOrEmpty(value))
            {
                var id = value.Split(':')[0];
                using (var db = new DuoContext()) {
                    var idCompare = long.Parse(id);
                    _topic              = db.Topics.AsNoTracking().FirstOrDefault(x => x.Id == idCompare);
                    topicTitle.Text     = _topic.Title;
                    numberQuestion.Text = _topic.Questions.Count().ToString();
                    isHide.Checked      = _topic.IsHide;
                }
            }
        }
Example #6
0
        private void ReloadData()
        {
            string data      = "";
            var    startDate = DateTime.Now.Date;
            var    endDate   = startDate.AddDays(1);

            using (var db = new DuoContext()) {
                data += "Number topic: " + db.Topics.Count(x => !x.IsDeleted) + "\r\n";
                data += "Number question: " + db.Questions.Count(x => x.IsActivated) + "\r\n";
                data += "Number topic test today: " + db.HistoryDetails.Count(x => x.MyHistory.CreatedDate >= startDate && x.MyHistory.CreatedDate < endDate);
            }
            if (databaseInfo.Text != data)
            {
                databaseInfo.Text = data;
                this.Refresh();
            }
        }
Example #7
0
        private void listHistory_SelectedIndexChanged(object sender, EventArgs e)
        {
            listTopic.DataSource = null;
            var item = (HistorySelection)listHistory.SelectedItem;

            if (item != null && item.Id > 0)
            {
                using (var db = new DuoContext()) {
                    var hisDetail = db.HistoryDetails.AsNoTracking()
                                    .Where(x => x.MyHistory.Id == item.Id)
                                    .Select(x => new { Id = x.Id, Title = x.MyTopic.Title })
                                    .OrderBy(x => x.Id).ToList();
                    totalTopic.Text         = hisDetail.Count.ToString();
                    listTopic.DataSource    = hisDetail;
                    listTopic.DisplayMember = "Title";
                }
            }
        }
Example #8
0
 private void saveChangeTopic_Click(object sender, EventArgs e)
 {
     if (_topic == null && !string.IsNullOrWhiteSpace(topicTitle.Text))
     {
         var hashData = HashService.ComputeSha256Hash(topicTitle.Text);
         using (var db = new DuoContext()) {
             if (db.Topics.AsNoTracking().Any(x => !x.IsDeleted && x.HashCompare.Equals(hashData)))
             {
                 MessageBox.Show("Tiêu đề của chủ đề đã tồn tại trong Database", "Cảnh báo trùng lập dữ liệu"
                                 , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 return;
             }
             var count = db.Topics.AsNoTracking().Count();
             var topic = new Topic()
             {
                 Title       = topicTitle.Text,
                 HashCompare = hashData,
                 IsDeleted   = false,
                 IsHide      = isHide.Checked,
                 Sort        = count + 1
             };
             db.Topics.Add(topic);
             db.SaveChanges();
         }
         LoadData();
         return;
     }
     if (_topic != null && !string.IsNullOrWhiteSpace(topicTitle.Text))
     {
         using (var db = new DuoContext()) {
             _topic.Title       = topicTitle.Text;
             _topic.HashCompare = HashService.ComputeSha256Hash(topicTitle.Text);
             _topic.IsHide      = isHide.Checked;
             var chk = db.Update <Topic>(_topic);
         }
         LoadData();
         return;
     }
     MessageBox.Show("Vui lòng chọn một chủ đề hay nhập thông tin chủ đề", "Dữ liệu chưa có"
                     , MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
Example #9
0
 private void LoadData()
 {
     using (var db = new DuoContext()) {
         var topics = db.Topics.Where(x => !x.IsDeleted)
                      .OrderBy(x => x.Sort)
                      .Select(x => new { Id = x.Id, Title = x.Title })
                      .ToList();
         var count = topics.Count;
         totalTopic.Text = count.ToString();
         listTopic.Items.Clear();
         foreach (var item in topics)
         {
             string data = item.Id + ": " + item.Title;
             listTopic.Items.Add(data);
         }
     }
     _topic          = null;
     topicTitle.Text = "";
     isHide.Checked  = false;
     this.Refresh();
 }
Example #10
0
 private void downTopic_Click(object sender, EventArgs e)
 {
     if (_topic != null && !string.IsNullOrWhiteSpace(topicTitle.Text))
     {
         int sort = _topic.Sort;
         using (var db = new DuoContext()) {
             var upTopic = db.Topics.FirstOrDefault(x => !x.IsDeleted && x.Sort > sort);
             if (upTopic == null)
             {
                 MessageBox.Show("Không thể đưa chủ đề xuống dưới vì nó đã ở dưới cùng", "Dữ liệu không thể thay đổi"
                                 , MessageBoxButtons.OK, MessageBoxIcon.Information);
                 return;
             }
             _topic.Sort  = upTopic.Sort;
             upTopic.Sort = sort;
             db.Update <Topic>(_topic);
             db.Update <Topic>(upTopic);
         }
         LoadData();
         return;
     }
     MessageBox.Show("Vui lòng chọn một chủ đề hay nhập thông tin chủ đề", "Dữ liệu chưa được chọn"
                     , MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
Example #11
0
        public static bool CreateOrCheckHistory()
        {
            var now = DateTime.Now;

            try {
                using (var db = new DuoContext()) {
                    var chk = db.Histories.AsNoTracking().Any(x => x.CreatedDate.Date == now.Date);
                    if (chk)
                    {
                        return(true);
                    }
                    var his = new History()
                    {
                        CreatedDate = now
                    };
                    db.Histories.Add(his);
                    db.SaveChanges();
                    return(true);
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Function " + typeof(HistoryService).Name);
                return(false);
            }
        }