This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeController.cs
297 lines (242 loc) · 9.82 KB
/
HomeController.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MorrisPhotos.Web.DataModels;
using MorrisPhotos.Web.DataModels.Comparers;
using MorrisPhotos.Web.ViewModels;
using MorrisPhotos.Web.ViewModels.Home;
using ServiceStack;
using ServiceStack.Mvc;
using ServiceStack.OrmLite;
namespace MorrisPhotos.Web.Controllers
{
public class HomeController : ServiceStackController
{
private static List<Photo> allPhotos;
private static List<Person> allPeople;
private List<Photo> AllPhotos
{
get
{
if (allPhotos == null)
{
allPhotos = Db.LoadSelect<Photo>();
foreach (var photo in allPhotos)
{
Db.LoadReferences(photo.PhotoEvent);
}
}
return allPhotos;
}
}
private List<Person> AllPeople => allPeople ?? (allPeople = Db.Select<Person>());
public IActionResult Index()
{
return View();
}
[Microsoft.AspNetCore.Mvc.Route("school-year/{schoolYearName}")]
public IActionResult SchoolYearDetails(string schoolYearName)
{
var schoolYear = Db.Single<SchoolYear>(x => x.Name == schoolYearName);
var photoEvents = Db.LoadSelect<PhotoEvent>(x => x.SchoolYearId == schoolYear.Id);
var categories = photoEvents.Select(x => x.Category).Distinct().ToList();
var vm = new SchoolYearDetailsViewModel
{
SchoolYear = schoolYear,
Categories = categories
};
return View(vm);
}
[Microsoft.AspNetCore.Mvc.Route("school-year/{schoolYearName}/category/{categoryName}")]
public IActionResult SchoolYearAndCategoryDetails(string schoolYearName, string categoryName)
{
var schoolYear = Db.Single<SchoolYear>(x => x.Name == schoolYearName);
var category = Db.Single<Category>(x => x.UrlName == categoryName);
var photoEvents = Db.Select<PhotoEvent>(x => x.SchoolYearId == schoolYear.Id && x.CategoryId == category.Id);
var vm = new SchoolYearAndCategoryDetailsViewModel
{
SchoolYear = schoolYear,
Category = category,
PhotoEvents = photoEvents
};
return View(vm);
}
[Microsoft.AspNetCore.Mvc.Route("school-year/{schoolYearName}/category/{categoryName}/event/{eventName}")]
public IActionResult SchoolYearAndCategoryAndEventDetails(string schoolYearName, string categoryName, string eventName)
{
if (schoolYearName.IsNullOrEmpty() || categoryName.IsNullOrEmpty() || eventName.IsNullOrEmpty())
{
return BadRequest();
}
var schoolYear = Db.Single<SchoolYear>(x => x.Name == schoolYearName);
if (schoolYear == null)
{
return NotFound(schoolYearName);
}
var category = Db.Single<Category>(x => x.UrlName == categoryName);
if (category == null)
{
return NotFound(categoryName);
}
var photoEvent = Db.Single<PhotoEvent>(x => x.UrlName == eventName && x.SchoolYearId == schoolYear.Id && x.CategoryId == category.Id);
if (photoEvent == null)
{
return NotFound(eventName);
}
var photos = Db.Select<Photo>(x => x.PhotoEventId == photoEvent.Id);
foreach (var photo in photos)
{
var peoplePhotos = Db.Select<PersonPhoto>(x => x.PhotoId == photo.Id);
foreach (var personPhoto in peoplePhotos)
{
var person = Db.Single<Person>(x => x.Id == personPhoto.PersonId);
photo.People.Add(person);
}
}
var vm = new SchoolYearAndCategoryAndEventDetailsViewModel
{
SchoolYear = schoolYear,
Category = category,
PhotoEvent = photoEvent,
Photos = photos
};
return View(vm);
}
[Microsoft.AspNetCore.Mvc.Route("category/{categoryName}")]
public IActionResult CategoryDetails(string categoryName)
{
var category = Db.Single<Category>(x => x.UrlName == categoryName);
var photoEvent = Db.LoadSelect<PhotoEvent>(x => x.CategoryId == category.Id);
var schoolYears = photoEvent.Select(x => x.SchoolYear).Distinct().ToList();
var vm = new CategoryDetailsViewModel
{
Category = category,
SchoolYears = schoolYears
};
return View(vm);
}
[Microsoft.AspNetCore.Mvc.Route("events")]
public IActionResult Events()
{
var output = new Dictionary<SchoolYear, Dictionary<Category, List<PhotoEvent>>>();
var photoEvents = Db.LoadSelect<PhotoEvent>();
var events = photoEvents.GroupBy(x => x.SchoolYear).ToDictionary(x => x.Key, x => x.ToList());
foreach (var group in events)
{
var categoryGrouping = group.Value.GroupBy(x => x.Category).ToDictionary(x => x.Key, x => x.ToList());
output[group.Key] = categoryGrouping;
}
var vm = new EventsViewModel
{
Events = output
};
return View(vm);
}
[Microsoft.AspNetCore.Mvc.Route("people")]
public IActionResult People()
{
var query = Db.From<PersonPhoto>().Select(x => x.PersonId);
var peopleIds = Db.ColumnDistinct<int>(query);
var people = new List<Person>();
foreach (var personId in peopleIds)
{
people.Add(Db.Single<Person>(x => x.Id == personId));
}
people.Sort(new PersonComparer());
var vm = new PeopleViewModel
{
People = people
};
return View(vm);
}
[Microsoft.AspNetCore.Mvc.Route("people/{personId}/{personName}")]
public IActionResult PersonDetails(int personId, string personName)
{
var person = Db.Single<Person>(x => x.Id == personId);
var personPhotos = Db.Select<PersonPhoto>(x => x.PersonId == personId);
var photos = new List<Photo>();
foreach (var personPhoto in personPhotos)
{
photos.Add(Db.Single<Photo>(x => x.Id == personPhoto.PhotoId));
}
foreach (var photo in photos)
{
var peoplePhotos = Db.Select<PersonPhoto>(x => x.PhotoId == photo.Id);
foreach (var personPhoto in peoplePhotos)
{
var p = Db.Single<Person>(x => x.Id == personPhoto.PersonId);
photo.People.Add(p);
}
}
var vm = new PersonDetailsViewModel
{
Person = person,
Photos = photos
};
return View(vm);
}
[HttpGet]
[Microsoft.AspNetCore.Mvc.Route("phototag")]
public IActionResult PhotoTag()
{
var people = this.AllPeople;
var photos = this.AllPhotos;
var vm = new PhotoTagViewModel
{
People = people,
Photos = photos
};
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
[Microsoft.AspNetCore.Mvc.Route("phototag")]
public IActionResult PhotoTag(PhotoTagViewModel viewModel)
{
var personPhoto = new PersonPhoto
{
PersonId = viewModel.PersonId,
PhotoId = viewModel.PhotoId
};
Db.Insert(personPhoto);
viewModel.People = this.AllPeople;
viewModel.Photos = this.AllPhotos;
viewModel.PersonId = 0;
viewModel.PhotoId = 0;
return View(viewModel);
}
[Microsoft.AspNetCore.Mvc.Route("photolist")]
public IActionResult PhotoList()
{
var photos = this.AllPhotos;
var data = new Dictionary<SchoolYear, Dictionary<Category, Dictionary<PhotoEvent, List<Photo>>>>();
var schoolYearGroups = photos.GroupBy(x => x.PhotoEvent.SchoolYear).ToDictionary(x => x.Key, x => x.ToList());
foreach (var schoolYearGroup in schoolYearGroups)
{
var schoolYearData = new Dictionary<Category, Dictionary<PhotoEvent, List<Photo>>>();
var categoryGroups = schoolYearGroup.Value.GroupBy(x => x.PhotoEvent.Category).ToDictionary(x => x.Key, x => x.ToList());
foreach (var categoryGroup in categoryGroups)
{
var categoryData = new Dictionary<PhotoEvent, List<Photo>>();
var photoEventGroups = categoryGroup.Value.GroupBy(x => x.PhotoEvent).ToDictionary(x => x.Key, x => x.ToList());
foreach (var photoEventGroup in photoEventGroups)
{
categoryData[photoEventGroup.Key] = photoEventGroup.Value;
}
schoolYearData[categoryGroup.Key] = categoryData;
}
data[schoolYearGroup.Key] = schoolYearData;
}
var vm = new PhotoListViewModel
{
Photos = data
};
return View(vm);
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}