Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
77 lines (67 loc) · 2.11 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
77 lines (67 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Diary.Data;
using Diary.Models;
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
using System.Windows;
namespace Diary
{
public partial class MainWindow : Window
{
private DateTime selectedDate;
public MainWindow()
{
InitializeComponent();
calendar.SelectedDate = DateTime.Today;
selectedDate = DateTime.Today;
LoadEntryForDate(selectedDate);
}
private void Calendar_SelectedDatesChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (calendar.SelectedDate.HasValue)
{
selectedDate = calendar.SelectedDate.Value;
LoadEntryForDate(selectedDate);
}
}
private void LoadEntryForDate(DateTime date)
{
using var db = new DiaryContext();
var entry = db.DiaryEntries.FirstOrDefault(e => e.CreatedAt.Date == date.Date);
if (entry != null)
{
txtTitle.Text = entry.Title;
txtContent.Text = entry.Content;
}
else
{
txtTitle.Text = "";
txtContent.Text = "";
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
using var db = new DiaryContext();
var entry = db.DiaryEntries.FirstOrDefault(e => e.CreatedAt.Date == selectedDate.Date);
if (entry == null)
{
entry = new DiaryEntry
{
Id = Guid.NewGuid().ToString(),
Title = txtTitle.Text,
Content = txtContent.Text,
CreatedAt = selectedDate,
};
db.DiaryEntries.Add(entry);
}
else
{
entry.Title = txtTitle.Text;
entry.Content = txtContent.Text;
}
db.SaveChanges();
MessageBox.Show("Diary saved!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}