-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (96 loc) · 3.54 KB
/
Program.cs
File metadata and controls
112 lines (96 loc) · 3.54 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Linq;
using EFGetStarted;
using Microsoft.EntityFrameworkCore;
using Task = EFGetStarted.Task;
using var db = new BloggingContext();
SeedTasks(db);
PrintIncompleteTasksAndTodos();
seedWorkers(db);
PrintTeamsWithoutTasks();
void SeedTasks(BloggingContext context)
{
var task1 = new Task
{
Name = "Produce software",
};
task1.Todos.Add(new Todo { Description = "Write code", IsDone = false });
task1.Todos.Add(new Todo { Description = "Compile source", IsDone = false });
task1.Todos.Add(new Todo { Description = "Test program", IsDone = false });
var task2 = new Task
{
Name = "Brew coffee",
};
task2.Todos.Add(new Todo { Description = "Pour water", IsDone = false });
task2.Todos.Add(new Todo { Description = "Pour coffee", IsDone = false });
task2.Todos.Add(new Todo { Description = "Turn on", IsDone = false });
context.Tasks.AddRange(task1, task2);
context.SaveChanges();
}
static void PrintIncompleteTasksAndTodos()
{
using (var context = new BloggingContext())
{
var tasks = context.Tasks
.Include(task => task.Todos)
.Where(task => task.Todos.Any(todo => !todo.IsDone));
foreach (var task in tasks)
{
Console.WriteLine($"Task: {task.Name}");
foreach (var todo in task.Todos)
{
if (!todo.IsDone)
{
Console.WriteLine($"-{todo.Description}");
}
}
}
}
}
static void seedWorkers(BloggingContext context)
{
var tasks = context.Tasks.Include(t => t.Todos).ToList();
var todos = tasks.SelectMany(t => t.Todos).ToList();
var frontendTeam = new Team { Name = "Frontend", CurrentTask = tasks[0]};
var backendTeam = new Team { Name = "Backend", CurrentTask = tasks[1] };
var testersTeam = new Team { Name = "Testere", CurrentTask = tasks[2]};
var workers = new List<Worker>
{
new Worker { Name = "Steen Secher", CurrentTodo = todos[0] },
new Worker { Name = "Ejvind Møller", CurrentTodo = todos[1] },
new Worker { Name = "Konrad Sommer", CurrentTodo = todos[2]},
new Worker { Name = "Sofus Lotus" },
new Worker { Name = "Remo Lademann" },
new Worker { Name = "Ella Fanth" },
new Worker { Name = "Anne Dam" }
};
var teamWorkers = new List<TeamWorker>
{
new TeamWorker { Team = frontendTeam, Worker = workers[0] },
new TeamWorker { Team = frontendTeam, Worker = workers[1] },
new TeamWorker { Team = frontendTeam, Worker = workers[2] },
new TeamWorker { Team = backendTeam, Worker = workers[2] },
new TeamWorker { Team = backendTeam, Worker = workers[3] },
new TeamWorker { Team = backendTeam, Worker = workers[4] },
new TeamWorker { Team = testersTeam, Worker = workers[5] },
new TeamWorker { Team = testersTeam, Worker = workers[6] },
new TeamWorker { Team = testersTeam, Worker = workers[0] }
};
context.TeamWorkers.AddRange(teamWorkers);
context.SaveChanges();
}
static void PrintTeamsWithoutTasks()
{
using (var context = new BloggingContext())
{
// Get all teams that have no current task
var teamsWithoutTasks = context.Teams
.Where(t => t.CurrentTask == null)
.ToList();
// Print the names of the teams without tasks
foreach (var team in teamsWithoutTasks)
{
Console.WriteLine(team.Name);
}
}
}