Skip to content

Commit 4b5a2c4

Browse files
Implemented MVC
1 parent 6142f60 commit 4b5a2c4

29 files changed

+486
-187
lines changed

ProjectB.sln.DotSettings.user

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;
3+
&lt;Assembly Path="/Users/marcus.talbot/school/y1/ProjectB/ProjectB/ProjectB/bin/Debug/net8.0/System.Diagnostics.EventLog.dll" /&gt;
4+
&lt;/AssemblyExplorer&gt;</s:String>
25
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=19fd1edb_002D1417_002D4038_002Da919_002D0c34f0f5114d/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="TestAdditionCalculator" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
36
&lt;TestAncestor&gt;
47
&lt;TestId&gt;MSTest::30DFCAE0-5221-440C-8C75-963D08834F19::net7.0::ProjectBTest.UnitTest1&lt;/TestId&gt;
@@ -9,5 +12,6 @@
912
&lt;TestId&gt;MSTest::30DFCAE0-5221-440C-8C75-963D08834F19::net8.0::ProjectBTest.Repositories.UserRepositoryTest.FindAllShouldFindNEntities&lt;/TestId&gt;
1013
&lt;TestId&gt;MSTest::30DFCAE0-5221-440C-8C75-963D08834F19::net8.0::ProjectBTest.Repositories.UserRepositoryTest.PersistShouldSaveNewUsersToFile&lt;/TestId&gt;
1114
&lt;TestId&gt;MSTest::30DFCAE0-5221-440C-8C75-963D08834F19::net8.0::ProjectBTest.Repositories.UserRepositoryTest&lt;/TestId&gt;
15+
&lt;TestId&gt;MSTest::30DFCAE0-5221-440C-8C75-963D08834F19::net8.0::ProjectBTest.Repositories.TourRepositoryTest.Test&lt;/TestId&gt;
1216
&lt;/TestAncestor&gt;
1317
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ProjectB.Exceptions;
2+
3+
public class EntityNotFoundException(string message) : Exception(message)
4+
{
5+
6+
}

ProjectB/Models/Guest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public class Guest : AbstractUser
1717
public Guest(string username, DateOnly validForDate) : base(username, UserRole.Guest)
1818
{
1919
_validForDate = validForDate;
20-
this.Username = username;
20+
Username = username;
2121
}
2222

23-
public bool IsValid() => _validForDate.CompareTo(DateTime.Today) == 0;
23+
public bool IsValid() => _validForDate.CompareTo(DateOnly.FromDateTime(DateTime.Today)) == 0;
2424

2525
public override bool Equals(AbstractUser? other)
2626
{

ProjectB/Models/Tour.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,36 @@ namespace ProjectB.Models;
77
public class Tour : IEntity<TourCompositeKey>
88
{
99

10-
[JsonProperty] private readonly ICollection<string> _participants;
10+
[JsonProperty] private readonly ICollection<Guest> _participants;
1111
[JsonProperty] private readonly int _capacity;
1212
[JsonProperty] private readonly TourCompositeKey _key;
1313

14-
public Tour(TourCompositeKey key, int capacity, ICollection<string> participants)
14+
public Tour(TourCompositeKey key, int capacity, ICollection<Guest> participants)
1515
{
1616
_key = key;
1717
_capacity = capacity;
1818
_participants = participants;
1919
}
2020

21-
public int RemainingCapacity => _capacity - _participants.Count;
22-
2321
public DateTime GetTourTime() {
2422
return _key.Time;
2523
}
2624

25+
public int GetRemainingCapacity()
26+
{
27+
return _capacity - _participants.Count;
28+
}
29+
2730
public string GetGuide() {
2831
return _key.Guide;
2932
}
3033

31-
public void SetGuide(Employee guide) {
34+
public void SetGuide(Employee guide)
35+
{
3236
_key.Guide = guide.GetId();
3337
}
3438

35-
public ICollection<string> GetParticipants() {
39+
public ICollection<Guest> GetParticipants() {
3640
return _participants;
3741
}
3842

ProjectB/Models/TourCompositeKey.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using Newtonsoft.Json;
2-
using ProjectB.Exceptions;
32

43
namespace ProjectB.Models;
54

65
public class TourCompositeKey {
76

87
[JsonProperty] public readonly DateTime Time;
9-
[JsonProperty] public string Guide;
8+
[JsonProperty] public string Guide { get; set; }
109

11-
public TourCompositeKey(DateTime time, string guide) {
10+
public TourCompositeKey(DateTime time, string guide)
11+
{
1212
Guide = guide;
1313
Time = time;
1414
}

ProjectB/Program.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
using ProjectB.login;
2-
using ProjectB.Models;
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
33
using ProjectB.Repositories;
44
using ProjectB.Services;
5-
using ProjectB.settings;
5+
using ProjectB.Views.Admin;
6+
using ProjectB.Views.Debug;
7+
using ProjectB.Views.Login;
8+
using ProjectB.Views.Main;
69
using ProjectB.Views.Reservation;
710

8-
var guest = new Guest("1234", DateOnly.FromDateTime(DateTime.Now));
11+
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
12+
// Repostitories
13+
builder.Services.AddSingleton<GuestRepository>();
14+
builder.Services.AddSingleton<EmployeeRepository>();
15+
builder.Services.AddSingleton<TourRepository>();
916

10-
var repo = new TourRepository();
11-
var guestRepo = new GuestRepository();
12-
guestRepo.Save(guest);
13-
Settings.CurrentSession = new Session(guest.GetId(), guest.GetUserRole());
14-
var guestService = new GuestService(guestRepo);
15-
var service = new TourService(repo);
16-
var createView = new CreateReservationView(service, guestService);
17-
var view = new ReservationView(createView);
17+
// Services
18+
builder.Services.AddSingleton<GuestService>();
19+
builder.Services.AddSingleton<EmployeeService>();
20+
builder.Services.AddSingleton<TourService>();
1821

19-
view.Output();
22+
// Views
23+
builder.Services.AddSingleton<CreateReservationView>();
24+
builder.Services.AddSingleton<EditReservationView>();
25+
builder.Services.AddSingleton<DeleteReservationView>();
26+
builder.Services.AddSingleton<ReservationView>();
27+
builder.Services.AddSingleton<EmployeeLoginView>();
28+
builder.Services.AddSingleton<GuestLoginView>();
29+
builder.Services.AddSingleton<CreateGuestView>();
30+
builder.Services.AddSingleton<CreateEmployeeView>();
31+
builder.Services.AddSingleton<DebugView>();
32+
builder.Services.AddSingleton<MainMenuView>();
33+
34+
using IHost host = builder.Build();
35+
36+
host.Services.GetService<MainMenuView>()!.Output();

ProjectB/ProjectB.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<ItemGroup>
1111
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
1212
<PackageReference Include="Common.Logging" Version="3.4.1" />
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-preview.3.24172.9" />
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
1315
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
1416
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1517
</ItemGroup>

ProjectB/Repositories/AbstractRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void Refresh()
4343

4444
public void Persist()
4545
{
46+
File.CreateText(this.GetFileLocation()).Close();
4647
JsonFileWriter<TEntity> writer = new();
4748
writer.WriteObjects(GetFileLocation(), Repository.Values);
4849
}

ProjectB/Repositories/GuestRepository.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ namespace ProjectB.Repositories;
44

55
public class GuestRepository : AbstractRepository<Guest, string>
66
{
7-
7+
public Guest? FindValidGuestById(string id)
8+
{
9+
return Repository.Values
10+
.FirstOrDefault(guest => guest.GetId() == id && guest.IsValid());
11+
}
812
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using ProjectB.Models;
2+
using System;
23

3-
namespace ProjectB.Repositories;
4-
5-
public class TourRepository : AbstractRepository<Tour, TourCompositeKey>
4+
namespace ProjectB.Repositories
65
{
6+
public class TourRepository : AbstractRepository<Tour, TourCompositeKey>
7+
{
78

8-
public IEnumerable<Tour> GetAllToursTodayAfterNow() =>
9-
from tour in Repository.Values
10-
where tour.GetTourTime().Date == DateTime.Today
11-
&& tour.GetTourTime().CompareTo(DateTime.Now) > 0
12-
&& tour.RemainingCapacity > 0
13-
select tour;
9+
public IEnumerable<Tour> GetAllToursTodayAfterNow() =>
10+
from tour in Repository.Values
11+
where tour.GetTourTime().Date == DateTime.Today && tour.GetTourTime().CompareTo(DateTime.Now) > 0
12+
select tour;
1413

15-
}
14+
}
15+
}

ProjectB/Services/EmployeeService.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ProjectB.Models;
2+
3+
namespace ProjectB.Services;
4+
5+
public class EmployeeService : IService<Employee, string>
6+
{
7+
public void Create(Employee entity)
8+
{
9+
throw new NotImplementedException();
10+
}
11+
12+
public void Update(Employee entity, string id)
13+
{
14+
throw new NotImplementedException();
15+
}
16+
17+
public void Delete(string id)
18+
{
19+
throw new NotImplementedException();
20+
}
21+
}

ProjectB/Services/GuestService.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
using ProjectB.Exceptions;
12
using ProjectB.Models;
23
using ProjectB.Repositories;
34

45
namespace ProjectB.Services;
56

67
public class GuestService(GuestRepository repository) : IService<Guest, string>
78
{
8-
99
public void Create(Guest entity)
1010
{
11-
if (repository.FindById(entity.GetId()) == null)
11+
if (!repository.Exists(entity))
1212
{
1313
repository.Save(entity);
1414
}
@@ -24,7 +24,15 @@ public void Delete(string id)
2424
throw new NotImplementedException();
2525
}
2626

27-
public Guest? GetGuest(string id) {
28-
return repository.FindById(id);
27+
public Guest FindValidGuestById(string id)
28+
{
29+
Guest? guest = repository.FindValidGuestById(id);
30+
31+
if (guest == null)
32+
{
33+
throw new EntityNotFoundException($"Could not find Guest with id: {id}");
34+
}
35+
36+
return guest;
2937
}
3038
}

ProjectB/Services/TourService.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ public IEnumerable<Tour> GetAllToursTodayAfterNow() {
2828
return repository.GetAllToursTodayAfterNow();
2929
}
3030

31-
public bool RegisterUserForTour(Tour tour, Guest guest) {
32-
if (tour.RemainingCapacity == 0 || guest.IsGuestInTour)
31+
public bool RegisterGuestForTour(Guest guest, Tour tour)
32+
{
33+
if (tour.GetRemainingCapacity() == 0)
3334
{
3435
return false;
3536
}
36-
37-
guest.Tour = tour.GetId();
38-
tour.GetParticipants().Add(guest.GetId());
37+
38+
tour.GetParticipants().Add(guest);
3939
repository.Save(tour);
40-
4140
return true;
4241
}
4342
}

ProjectB/Views/AbstractView.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace ProjectB.Views;
2+
3+
public abstract class AbstractView : IView
4+
{
5+
6+
private static DateTime _timeSinceLastInput = DateTime.Now;
7+
8+
protected int ReadUserChoice(int min, int max, string invalidChoiceMessage)
9+
{
10+
int option;
11+
_timeSinceLastInput = DateTime.Now;
12+
while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out option) || (option < min || option > max))
13+
{
14+
_timeSinceLastInput = DateTime.Now;
15+
Console.Clear();
16+
Console.WriteLine(invalidChoiceMessage);
17+
}
18+
return option;
19+
}
20+
21+
protected bool HasSessionExpired()
22+
{
23+
return (DateTime.Now - _timeSinceLastInput).TotalSeconds > 120;
24+
}
25+
26+
public abstract void Output();
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ProjectB.Views.Admin;
2+
3+
public class CreateEmployeeView : IView
4+
{
5+
public void Output()
6+
{
7+
throw new NotImplementedException();
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using ProjectB.Models;
2+
using ProjectB.Services;
3+
4+
namespace ProjectB.Views.Admin;
5+
6+
public class CreateGuestView(GuestService service) : AbstractView
7+
{
8+
public override void Output()
9+
{
10+
Console.WriteLine("Geef het ticketnummer van de gast in:");
11+
string ticketNummer = Console.ReadLine()!;
12+
Console.WriteLine("Geef de geldigheidsdatum van het ticket in (yyyy-mm-dd): ");
13+
DateOnly validForDate = DateOnly.Parse(Console.ReadLine()!);
14+
15+
service.Create(new Guest(ticketNummer, validForDate));
16+
}
17+
}

ProjectB/Views/Debug/DebugView.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using ProjectB.Views.Admin;
2+
3+
namespace ProjectB.Views.Debug;
4+
5+
public class DebugView(CreateGuestView createGuestView, CreateEmployeeView createEmployeeView) : AbstractView
6+
{
7+
private const string DEBUG_VIEW = """
8+
Welcome to the DebugView. To continue, please select one of the options in the list below.
9+
1. Create Guest
10+
2. Create Employee
11+
3. Exit
12+
""";
13+
14+
public override void Output()
15+
{
16+
while (true)
17+
{
18+
Console.WriteLine(DEBUG_VIEW);
19+
int option = ReadUserChoice(1, 3, DEBUG_VIEW);
20+
switch (option)
21+
{
22+
case 1:
23+
createGuestView.Output();
24+
break;
25+
case 2:
26+
createEmployeeView.Output();
27+
break;
28+
case 3:
29+
return;
30+
}
31+
}
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ProjectB.Views.Login;
2+
3+
public class EmployeeLoginView() : AbstractView
4+
{
5+
public override void Output()
6+
{
7+
throw new NotImplementedException();
8+
}
9+
}

0 commit comments

Comments
 (0)