diff --git a/FlightJournal.Tests/FlightJournal.Tests.csproj b/FlightJournal.Tests/FlightJournal.Tests.csproj index f7651eb0..d007d786 100644 --- a/FlightJournal.Tests/FlightJournal.Tests.csproj +++ b/FlightJournal.Tests/FlightJournal.Tests.csproj @@ -23,7 +23,7 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG;CFG_DEBUG prompt 4 @@ -57,8 +57,8 @@ ..\packages\Microsoft.Data.OData.5.6.3\lib\net40\Microsoft.Data.OData.dll - - ..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll False diff --git a/FlightJournal.Tests/RouteFilters.cs b/FlightJournal.Tests/RouteFilters.cs index 54e189b4..8e222999 100644 --- a/FlightJournal.Tests/RouteFilters.cs +++ b/FlightJournal.Tests/RouteFilters.cs @@ -13,6 +13,8 @@ using FakeItEasy; using FlightJournal.Web; using FlightJournal.Web.Controllers; +using FlightJournal.Web.Models; +using FlightJournal.Web.Repositories; using FlightJournal.Web.Validators; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -21,11 +23,25 @@ namespace FlightJournal.Tests [TestClass] public class RouteFilters { + public class TestClubRepository : IClubRepository + { + public bool ClubExists(string shortName) + { + return shortName == "ØSF" || shortName == "AASVK"; + } + + public Club CloneClubByShortName(string shortName) + { + return new Club() { ShortName = shortName }; + } + } + [TestInitialize] public void init() { var folder = System.IO.Directory.GetDirectoryRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\FlightJournal.Web\\App_Data")); AppDomain.CurrentDomain.SetData("DataDirectory", folder.ToString()); + } [TestMethod] @@ -111,10 +127,26 @@ public void Root_and_a_non_existing_club_should_set_currentclub_to_non() [TestMethod] [TestCategory("Routes")] - public void Root_and_a_non_existing_club_should_send_to_non_existing_controller_and_index_action() + public void Root_and_a_non_existing_club_should_send_to_pagenotfound_error_controller_and_index_action() { // Will send through the route pipeline and land on the controller default route (missingclub does not exist and will result in a 404) - should_return_expected_controller_and_action("~/MISSINGCLUB", "missingclub", "Index"); + should_return_expected_controller_and_action("~/MISSINGCLUB", "error", "pagenotfound"); + } + + [TestMethod] + [TestCategory("Routes")] + public void Root_valid_controller_should_send_to_pagenotfound_error_controller_and_index_action() + { + should_return_expected_controller_and_action("~/admin", "error", "pagenotfound"); + should_return_expected_controller_and_action("~/logbook", "logbook", "index"); + } + + [TestMethod] + [TestCategory("Routes")] + public void Root_club_valid_controller_should_send_to_pagenotfound_error_controller_and_index_action() + { + should_return_expected_controller_and_action("~/ØSF/admin", "error", "pagenotfound"); + should_return_expected_controller_and_action("~/ØSF/logbook", "logbook", "index"); } [TestMethod] @@ -130,7 +162,7 @@ public void Root_club_and_any_date_part_should_match_currentclub() [TestCategory("Routes")] public void RouteConstrint_Club_validator_test() { - var validator = new ClubValidator(); + var validator = new ClubValidator(new TestClubRepository()); // Valid Country Assert.IsTrue(validator.IsValid("ØSF"), "ØSF should be valid"); @@ -216,7 +248,7 @@ public RouteData GetRouteData(string path) { // attempting with https://gist.github.com/ExploreMqt/3719348 for getting access to route var collection = new RouteCollection(); - RouteConfig.RegisterRoutes(collection, true); + RouteConfig.RegisterRoutes(collection, new TestClubRepository(), true); var httpContext = A.Fake(); A.CallTo(() => httpContext.Request.AppRelativeCurrentExecutionFilePath).Returns(path); @@ -228,7 +260,7 @@ public RouteData GetRouteData(string path) public void should_return_expected_controller_and_action(string path, string expectedController, string expectedAction) { var collection = new RouteCollection(); - RouteConfig.RegisterRoutes(collection, true); + RouteConfig.RegisterRoutes(collection, new TestClubRepository(), true); var httpContext = A.Fake(); A.CallTo(() => httpContext.Request.AppRelativeCurrentExecutionFilePath).Returns(path); @@ -241,14 +273,14 @@ public void should_return_expected_controller_and_action(string path, string exp public void should_return_expected_club(string path, string expectedClub) { var collection = new RouteCollection(); - RouteConfig.RegisterRoutes(collection, true); + RouteConfig.RegisterRoutes(collection, new TestClubRepository(), true); var httpContext = A.Fake(); A.CallTo(() => httpContext.Request.AppRelativeCurrentExecutionFilePath).Returns(path); A.CallTo(() => httpContext.Request.Url).Returns(new Uri("http://localhost/" + path.Replace("~/", string.Empty))); var routeData = collection.GetRouteData(httpContext); Assert.IsNotNull(routeData); - var currentClub = ClubController.GetCurrentClub(httpContext, routeData); + var currentClub = ClubController.GetCurrentClub(httpContext, routeData, new TestClubRepository()); Assert.IsNotNull(currentClub); Assert.AreEqual(expectedClub, currentClub.ShortName); } diff --git a/FlightJournal.Tests/packages.config b/FlightJournal.Tests/packages.config index f04db8b3..76609358 100644 --- a/FlightJournal.Tests/packages.config +++ b/FlightJournal.Tests/packages.config @@ -18,7 +18,7 @@ - + diff --git a/FlightJournal.Web/App_Start/DbCommandInterceptor.cs b/FlightJournal.Web/App_Start/DbCommandInterceptor.cs index f3dc1c07..41f6a264 100644 --- a/FlightJournal.Web/App_Start/DbCommandInterceptor.cs +++ b/FlightJournal.Web/App_Start/DbCommandInterceptor.cs @@ -76,7 +76,8 @@ private void LogEndOfExec(DbCommand command, DbCommandInterceptionConte System.Diagnostics.Trace.TraceError($"Command {command.CommandText} failed with exception {interceptionContext.Exception}"); #if (DEBUG) // Note that EF __Migration exception with CreatedOn is related to https://stackoverflow.com/a/20670134 and is being throwned often but captured - throw new Exception($"Command {command.CommandText} failed with exception {interceptionContext.Exception}"); + if (command.CommandText != "SELECT TOP (1) \r\n [c].[CreatedOn] AS [CreatedOn]\r\n FROM [dbo].[__MigrationHistory] AS [c]") + throw new Exception($"Command {command.CommandText} failed with exception {interceptionContext.Exception}"); #endif } else diff --git a/FlightJournal.Web/App_Start/RouteConfig.cs b/FlightJournal.Web/App_Start/RouteConfig.cs index f124cabb..71b97595 100644 --- a/FlightJournal.Web/App_Start/RouteConfig.cs +++ b/FlightJournal.Web/App_Start/RouteConfig.cs @@ -1,78 +1,90 @@ -using System.Linq.Expressions; -using System.Security.Policy; +using FlightJournal.Web.Constraints; +using FlightJournal.Web.Repositories; +using FlightJournal.Web.Validators; using System.Web.Mvc; using System.Web.Routing; -using FlightJournal.Web.Constraints; -using FlightJournal.Web.Validators; -namespace FlightJournal.Web{ +namespace FlightJournal.Web +{ public class RouteConfig { - public static void RegisterRoutes(RouteCollection routes, bool unittest = false) { + public static void RegisterRoutes(RouteCollection routes, IClubRepository clubRepository, bool unittest = false) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); if (!unittest) routes.MapMvcAttributeRoutes(); - //// Custom club Urls - //routes.MapRoute( - // name: "Root", // Route name - // url: "", // URL with parameters - // defaults: new { controller = "Home", action = "Index" } - //); + // Specific routes to avoid HttpException with The controller for path e.g. '/.well-known/traffic-advice' was not found or does not implement IController. + // Array of URLs to handle as 404 + string[] notFoundUrls = { + "NaN-NaN-NaN/Index", + "sql/Index", + ".well-known/traffic-advice", + ".well-known/apple-app-site-association", + "app/Index", + "actuator;/env;", + "Admin/ashx", + "old/Index", + "root/Index", + "'123/Index", + "includ/Index", + "wordpress/Index", + "actuator/heapdump", + "apple-app-site-association/Index" // Assuming no Apple app association + }; + foreach (var url in notFoundUrls) // Generate routes for known 404 URLs - avoiding validation in ClubRouteConstraint + { + routes.MapRoute( + name: url.Replace("/", "").Replace(";", ""), // Generate a unique name for each route + url: url, + defaults: new { controller = "Error", action = "PageNotFound" } + ); + } // Custom club Urls routes.MapRoute( - "RootClub", // Route name - "{club}", // URL with parameters - new { controller = "Report", action = "Index" }, - new { club = new ClubRouteConstraint(new ClubValidator()) } + name: "GetReportByClub", + url: "{club}", + defaults: new { controller = "Report", action = "Index" }, + constraints: new { club = new ClubRouteConstraint(new ClubValidator(clubRepository)), controller = "Report" } ); // Custom report url /{yyyy} or /{yyyy-MM} or startlist on date {yyyy-MM-dd} routes.MapRoute( - "ReportingDate", // Route name - "{date}", // URL with parameters - new { controller = "Report", action = "Index" }, - new { date = new DateRouteConstraint(new DatePathValidator()) } + name: "GetReportByDate", + url: "{date}", + defaults: new { controller = "Report", action = "Index" }, + constraints: new { date = new DateRouteConstraint(new DatePathValidator()), controller = "Report" } ); // Custom club Urls with custom Date routes.MapRoute( - "ReportingClubDate", // Route name - "{club}/{date}", // URL with parameters - new { controller = "Report", action = "Index" }, - new { club = new ClubRouteConstraint(new ClubValidator()) - , date = new DateRouteConstraint(new DatePathValidator()) } + name: "GetReportByClubByDate", + url: "{club}/{date}", + defaults: new { controller = "Report", action = "Index" }, + constraints: new { club = new ClubRouteConstraint(new ClubValidator(clubRepository)), date = new DateRouteConstraint(new DatePathValidator()), controller = "Report" } ); - //// Custom club Urls with default behavior - //routes.MapRoute( - // "LogbookWithClubFlavor", // Route name - // "{club}/logbook/{year}", // URL with parameters - // new { club = UrlParameter.Optional, controller = "Logbook", action = "Index", year = UrlParameter.Optional }, - // new { club = new ClubRouteConstraint(new ClubValidator()) } - //); - //routes.MapRoute( - // "LogbookWithClubFlavor2", // Route name - // "{club}/logbook", // URL with parameters - // new { club = UrlParameter.Optional, controller = "Logbook", action = "Index"}, - // new { club = new ClubRouteConstraint(new ClubValidator()) } - //); - - // Custom club Urls with default behavior routes.MapRoute( - "DefaultWithClubFlavor", // Route name - "{club}/{controller}/{action}/{id}", // URL with parameters - new { club = UrlParameter.Optional, controller = "Report", action = "Index", id = UrlParameter.Optional }, - new { club = new ClubRouteConstraint(new ClubValidator()) } + name: "GetDefaultByClub", // Route name + url: "{club}/{controller}/{action}/{id}", // URL with parameters + defaults: new { club = UrlParameter.Optional, controller = "Report", action = "Index", id = UrlParameter.Optional }, + constraints: new { club = new ClubRouteConstraint(new ClubValidator(clubRepository)), controller = "Report|About|Account|Club|CodePlayGround|CommentaryAdmin|CommentaryTypeAdmin|Error|Flight|GradingAdmin|Import|Language|Location|Logbook|Manage|ManouvreAdmin|Pilot|PilotStatus|Plane|Report|RolesAdmin|StartType|TrainingExerciseAdmin|TrainingLessonAdmin|TrainingLogAdmin|TrainingLogHistoryAdmin|TrainingProgramAdmin|TrainingStatus|UsersAdmin" } // Constraint to club and all known controllers ); // Default behaviour routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", - defaults: new { controller = "Report", action = "Index", id = UrlParameter.Optional } + defaults: new { controller = "Report", action = "Index", id = UrlParameter.Optional }, + constraints: new { controller = "Report|About|Account|Club|CodePlayGround|CommentaryAdmin|CommentaryTypeAdmin|Error|Flight|GradingAdmin|Import|Language|Location|Logbook|Manage|ManouvreAdmin|Pilot|PilotStatus|Plane|Report|RolesAdmin|StartType|TrainingExerciseAdmin|TrainingLessonAdmin|TrainingLogAdmin|TrainingLogHistoryAdmin|TrainingProgramAdmin|TrainingStatus|UsersAdmin" } // Constraint to all known controllers + ); + + // Catch-all route for unmatched URLs + routes.MapRoute( + name: "NotFound", + url: "{*url}", // Matches any unmatched URL + defaults: new { controller = "Error", action = "PageNotFound" } ); } } diff --git a/FlightJournal.Web/Controllers/ClubController.cs b/FlightJournal.Web/Controllers/ClubController.cs index 2ba8ab84..af62b9ae 100644 --- a/FlightJournal.Web/Controllers/ClubController.cs +++ b/FlightJournal.Web/Controllers/ClubController.cs @@ -1,13 +1,11 @@ -using System; +using FlightJournal.Web.Models; +using FlightJournal.Web.Repositories; using System.Data; using System.Data.Entity; -using System.IO; using System.Linq; -using System.Runtime.Remoting.Channels; using System.Web; using System.Web.Mvc; using System.Web.Routing; -using FlightJournal.Web.Models; namespace FlightJournal.Web.Controllers { @@ -29,9 +27,8 @@ public static Club CurrentClub /// /// /// - public static Club GetCurrentClub(HttpContextBase context, RouteData routeData) + public static Club GetCurrentClub(HttpContextBase context, RouteData routeData, IClubRepository clubRepository) { - // Fetch from URL var urlClubFilter = routeData.Values["club"] as string; @@ -60,36 +57,14 @@ public static Club GetCurrentClub(HttpContextBase context, RouteData routeData) // Read Url if (urlClubFilter != null && !string.IsNullOrWhiteSpace(urlClubFilter)) { - Club ghost = new Club(); - using (var shortDb = new FlightContext()) - { - var club = shortDb.Clubs.SingleOrDefault(d => d.ShortName == urlClubFilter); - if (club != null) - { - ghost = new Club(); - ghost.LocationId = club.LocationId; - ghost.Location = club.Location; // for allowing country - ghost.ContactInformation = club.ContactInformation; - ghost.ShortName = club.ShortName; - ghost.Name = club.Name; - if (club.Website != null && (club.Website.StartsWith("http://") || club.Website.StartsWith("https://"))) - { - ghost.Website = club.Website; - } - else if (club.Website != null) - { - ghost.Website = "http://" + club.Website; - } - ghost.ClubId = club.ClubId; - } - } + var clubCopy = clubRepository.CloneClubByShortName(urlClubFilter); // Set Session Cache context.Items.Remove("CurrentClub"); - context.Items.Add("CurrentClub", ghost); + context.Items.Add("CurrentClub", clubCopy); // Return Current Club - return ghost; + return clubCopy; } return new Club(); } @@ -104,7 +79,7 @@ public static Club GetCurrentClub() return new Club(); } - return GetCurrentClub(context, routeData); + return GetCurrentClub(context, routeData, new ClubRepository(() => new FlightJournal.Web.Models.FlightContext())); } /// diff --git a/FlightJournal.Web/Controllers/ErrorController.cs b/FlightJournal.Web/Controllers/ErrorController.cs index 8156d838..1ed61e11 100644 --- a/FlightJournal.Web/Controllers/ErrorController.cs +++ b/FlightJournal.Web/Controllers/ErrorController.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; +using System.Web.Mvc; namespace FlightJournal.Web.Controllers { @@ -10,8 +6,8 @@ public class ErrorController : Controller { public ActionResult PageNotFound() { - Response.StatusCode = 404; - return View(); + Response.StatusCode = 404; // Set the 404 status code + return new ContentResult { Content = "
 ________________________________________ \r\n
< 404 Page Not Found - Udelanding! >\r\n
---------------------------------------- \r\n
\\ ^__^\r\n
\\ (oo)\\_______\r\n
(__)\\ )\\/\\\r\n
||----w |\r\n
|| ||\r\n
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⣽⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠁⠀⢀⣾⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⣠⣾⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⣠⣾⣿⣿⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣻⣾⡍⠻⣿⣿⣿⠟⠁⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⡙⠁⠀⠈⠻⣧⡀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠈⠻⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠉⠻⣦⡀⠀⠀⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⣠⣾⣿⣷⣤⡀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿\r\n⣿⣿⣿⣿⣿⣿⡿⠋⠁⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣦⡀⢈⣿⠟⠉⠉⢻⣿⣿⣿\r\n⣿⣿⣿⣿⣿⠏⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⢀⣴⣿⣿⣿⣿\r\n⣿⣿⣿⠟⠁⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⢀⣴⣿⣿⣿⣿⣿⣿\r\n⣿⣟⠁⠀⣠⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣿⣿⣿⣿⣿⣿⣿⣿\r\n⣿⣿⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
" }; // Return an empty ContentResult } public ActionResult PilotNotFound() diff --git a/FlightJournal.Web/FlightJournal.Web.csproj b/FlightJournal.Web/FlightJournal.Web.csproj index 3530f3dd..d844e7b6 100644 --- a/FlightJournal.Web/FlightJournal.Web.csproj +++ b/FlightJournal.Web/FlightJournal.Web.csproj @@ -568,6 +568,7 @@ + diff --git a/FlightJournal.Web/Global.asax.cs b/FlightJournal.Web/Global.asax.cs index aae7826d..f226baf5 100644 --- a/FlightJournal.Web/Global.asax.cs +++ b/FlightJournal.Web/Global.asax.cs @@ -1,9 +1,11 @@ -using System.Web.Http; +using System.Web.Helpers; +using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using Boerman.AprsClient; using FlightJournal.Web.App_Start; +using FlightJournal.Web.Repositories; using WebGrease.Configuration; namespace FlightJournal.Web @@ -27,7 +29,7 @@ protected void Application_Start() AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); - RouteConfig.RegisterRoutes(RouteTable.Routes); + RouteConfig.RegisterRoutes(RouteTable.Routes, new ClubRepository(() => new FlightJournal.Web.Models.FlightContext())); BundleConfig.RegisterBundles(BundleTable.Bundles); } } diff --git a/FlightJournal.Web/Repositories/ClubRepository.cs b/FlightJournal.Web/Repositories/ClubRepository.cs new file mode 100644 index 00000000..91321415 --- /dev/null +++ b/FlightJournal.Web/Repositories/ClubRepository.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using System.Windows.Forms; +using FlightJournal.Web.Models; + +namespace FlightJournal.Web.Repositories +{ + public interface IClubRepository + { + bool ClubExists(string shortName); + /// + /// Retrieving a club by its short name and creating a new Club object (a clone) and populating its properties with the values from the original club found in the database + /// + /// + /// + Club CloneClubByShortName(string shortName); + } + + public class ClubRepository : IClubRepository + { + private readonly Func _dbContextFactory; // Factory function to create FlightContext instances + + public ClubRepository(Func dbContextFactory) + { + _dbContextFactory = dbContextFactory; + } + + public bool ClubExists(string shortName) + { + using (var dbContext = _dbContextFactory()) // Create and dispose of DbContext within 'using' + { + return dbContext.Clubs.AsNoTracking().Any(c => c.ShortName == shortName); + } + } + + public Club CloneClubByShortName(string shortName) + { + using (var dbContext = _dbContextFactory()) + { + var originalClub = dbContext.Clubs.AsNoTracking().SingleOrDefault(d => d.ShortName == shortName); + if (originalClub == null) return null; + + var clubCopy = new Club + { + ClubId = originalClub.ClubId, + Name = originalClub.Name, + ShortName = originalClub.ShortName, + LocationId = originalClub.LocationId, + Location = originalClub.Location, + ContactInformation = originalClub.ContactInformation, + Website = originalClub.Website + }; + + return clubCopy; + } + } + } +} \ No newline at end of file diff --git a/FlightJournal.Web/Startup.cs b/FlightJournal.Web/Startup.cs index 0fd29691..fac854bc 100644 --- a/FlightJournal.Web/Startup.cs +++ b/FlightJournal.Web/Startup.cs @@ -13,7 +13,7 @@ public void Configuration(IAppBuilder app) // will look for SQL connection strings that contain Authentication=Active Directory Interactive. When found, they will use the AzureServiceTokenProvider to fetch an access token to authenticate with Azure SQL Database. // https://www.pluralsight.com/guides/how-to-use-managed-identity-with-azure-sql-database System.Data.SqlClient.SqlAuthenticationProvider.SetProvider(System.Data.SqlClient.SqlAuthenticationMethod.ActiveDirectoryInteractive, new SqlAppAuthenticationProvider()); - + ConfigureAuth(app); //ConfigureOpenGliderNetworkFlightLogMonitor(app); ConfigureAprsDataListener(); diff --git a/FlightJournal.Web/Validators/ClubValidator.cs b/FlightJournal.Web/Validators/ClubValidator.cs index 2718d05d..bb95be66 100644 --- a/FlightJournal.Web/Validators/ClubValidator.cs +++ b/FlightJournal.Web/Validators/ClubValidator.cs @@ -1,6 +1,5 @@ -using System.Linq; +using FlightJournal.Web.Repositories; using System.Web; -using FlightJournal.Web.Models; namespace FlightJournal.Web.Validators { @@ -11,6 +10,12 @@ public interface IClubValidator public class ClubValidator : IClubValidator { + private readonly IClubRepository _clubRepository; + public ClubValidator(IClubRepository clubRepository) + { + _clubRepository = clubRepository; + } + public bool IsValid(string club) { if (System.Web.HttpContext.Current == null) @@ -31,10 +36,7 @@ public bool IsValid(string club) bool Any(string s) { - using (var shortDb = new FlightContext()) - { - return (shortDb.Clubs.Any(d => d.ShortName == s)); - } + return _clubRepository.ClubExists(s); } } }