commit b2ee3b430a5757146b1d49008f5e80da202cba98 Author: quentin Date: Mon Apr 15 13:34:57 2024 -0500 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32797af --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/website/bin/ +/website/obj/ +/unitTests/bin/ +/unitTests/obj/ diff --git a/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/.gitignore b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/.gitignore new file mode 100644 index 0000000..ccb5529 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/.idea.sanAntonioSeniorGolf.iml +/contentModel.xml +/projectSettingsUpdater.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/dataSources.xml b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/dataSources.xml new file mode 100644 index 0000000..2ceede1 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/dataSources.xml @@ -0,0 +1,16 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://192.168.1.52:5618 + $ProjectFileDir$ + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/encodings.xml b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/indexLayout.xml b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/misc.xml b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/misc.xml new file mode 100644 index 0000000..e35f54e --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf.dir/.idea/misc.xml @@ -0,0 +1,25 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/.gitignore b/.idea/.idea.sanAntonioSeniorGolf/.idea/.gitignore new file mode 100644 index 0000000..893807f --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/projectSettingsUpdater.xml +/modules.xml +/.idea.sanAntonioSeniorGolf.iml +/contentModel.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/dataSources.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/dataSources.xml new file mode 100644 index 0000000..d0298f3 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/dataSources.xml @@ -0,0 +1,23 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://192.168.1.52:5618 + $ProjectFileDir$ + + + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$USER_HOME$/.local/share/scoreSphere/core.sqlite + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/deployment.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/deployment.xml new file mode 100644 index 0000000..f0c7784 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/encodings.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/indexLayout.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/inspectionProfiles/Project_Default.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8b7b7ce --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/misc.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/misc.xml new file mode 100644 index 0000000..e35f54e --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/misc.xml @@ -0,0 +1,25 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/sshConfigs.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/sshConfigs.xml new file mode 100644 index 0000000..936ba6a --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/sshConfigs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.sanAntonioSeniorGolf/.idea/vcs.xml b/.idea/.idea.sanAntonioSeniorGolf/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.sanAntonioSeniorGolf/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sanAntonioSeniorGolf.sln b/sanAntonioSeniorGolf.sln new file mode 100644 index 0000000..5018b72 --- /dev/null +++ b/sanAntonioSeniorGolf.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "website", "website\website.csproj", "{C7F3B4B2-2224-40CA-B12F-E56905F7A59A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "unitTests", "unitTests\unitTests.csproj", "{089ACCAD-8117-4C24-9429-56A394AC4734}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C7F3B4B2-2224-40CA-B12F-E56905F7A59A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C7F3B4B2-2224-40CA-B12F-E56905F7A59A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C7F3B4B2-2224-40CA-B12F-E56905F7A59A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C7F3B4B2-2224-40CA-B12F-E56905F7A59A}.Release|Any CPU.Build.0 = Release|Any CPU + {089ACCAD-8117-4C24-9429-56A394AC4734}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {089ACCAD-8117-4C24-9429-56A394AC4734}.Debug|Any CPU.Build.0 = Debug|Any CPU + {089ACCAD-8117-4C24-9429-56A394AC4734}.Release|Any CPU.ActiveCfg = Release|Any CPU + {089ACCAD-8117-4C24-9429-56A394AC4734}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/sanAntonioSeniorGolf.sln.DotSettings.user b/sanAntonioSeniorGolf.sln.DotSettings.user new file mode 100644 index 0000000..13ee6b4 --- /dev/null +++ b/sanAntonioSeniorGolf.sln.DotSettings.user @@ -0,0 +1,6 @@ + + <SessionState ContinuousTestingMode="0" IsActive="True" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>NUnit3x::089ACCAD-8117-4C24-9429-56A394AC4734::net5.0::unitTests.Tests.Test1</TestId> + </TestAncestor> +</SessionState> \ No newline at end of file diff --git a/unitTests/EventTests.cs b/unitTests/EventTests.cs new file mode 100644 index 0000000..6a08f38 --- /dev/null +++ b/unitTests/EventTests.cs @@ -0,0 +1,14 @@ +using NUnit.Framework; +using website.Controllers; + +namespace unitTests +{ + public class Tests + { + [Test] + public void getAll() { + EventController eventController = new(); + eventController.get(); + } + } +} \ No newline at end of file diff --git a/unitTests/unitTests.csproj b/unitTests/unitTests.csproj new file mode 100644 index 0000000..9eb8b43 --- /dev/null +++ b/unitTests/unitTests.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + + false + + + + + + + + + + + + + diff --git a/website/Controllers/EventController.cs b/website/Controllers/EventController.cs new file mode 100644 index 0000000..d35959c --- /dev/null +++ b/website/Controllers/EventController.cs @@ -0,0 +1,682 @@ +#region + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using MySql.Data.MySqlClient; +using website.Models; +using website.Models.events; + +#endregion + +namespace website.Controllers { + [ApiController] + [Route("[controller]")] + public class EventController : ControllerBase { // TODO add docs to methods + + [HttpGet("~/event/get")] + public ActionResult get(int eventId) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM events where eventId=@eventId", cnn); + cmd.Parameters.AddWithValue("@eventId", eventId); + MySqlDataReader rdr = cmd.ExecuteReader(); + + Event evnt = null; + while (rdr.Read()) + evnt = new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }; + + cnn.Close(); + if (evnt == null) + return StatusCode(204, null); + return StatusCode(200, evnt); + } + + + [HttpGet("~/event/getAll")] + public ActionResult> get() { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM events", cnn); + MySqlDataReader rdr = cmd.ExecuteReader(); + List events = new(); + + while (rdr.Read()) + events.Add(new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }); + + cnn.Close(); + if (events.Count == 0) + return StatusCode(204, null); + return StatusCode(200, events); + } + + [HttpPost("~/admin/event/postCsv.csv")] + public IActionResult postCsv(IFormFile file) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd; + + Stream stream = file.OpenReadStream(); + + byte[] byteFile = new byte[stream.Length]; + stream.Read(byteFile); + + string result = Encoding.Default.GetString(byteFile); + string[] results = result.Split('\n'); + + for (uint i = 1; i < results.Length-1; i++) { + string[] items = results[i].Split(','); + cmd = new( + "INSERT INTO san_antonio_senior_golf.events (location, date, time, color, textColor, imageUri, description) VALUES (@location, @date, @time, @color, @textColor, @imageUri, @description);", + cnn); + + cmd.Parameters.AddWithValue("@location", items[0]); + cmd.Parameters.AddWithValue("@date", items[1]); + cmd.Parameters.AddWithValue("@time", items[2]); + cmd.Parameters.AddWithValue("@color", items[3]); + cmd.Parameters.AddWithValue("@textColor", items[4]); + cmd.Parameters.AddWithValue("@imageUri", items[5]); + cmd.Parameters.AddWithValue("@description", items[6]); + + cmd.ExecuteReader().Close(); + } + + return NoContent(); + } + + [HttpGet("~/event/getBetween")] + public ActionResult> get(string start, string end) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM events WHERE date BETWEEN @start AND @end;", cnn); + cmd.Parameters.AddWithValue("@start", start); + cmd.Parameters.AddWithValue("@end", end); + MySqlDataReader rdr = cmd.ExecuteReader(); + List events = new(); + + while (rdr.Read()) + events.Add(new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }); + + cnn.Close(); + if (events.Count == 0) + return StatusCode(204, null); + return StatusCode(200, events); + } + + [HttpGet("~/event/getYear")] + public ActionResult> getYear() { + MySqlConnection cnn = Utilities.getConnection(); + cnn.Open(); + MySqlCommand cmd = new("SELECT * FROM events WHERE date BETWEEN @start AND @end;", cnn); + cmd.Parameters.AddWithValue("@start", DateTime.Now.AddYears(-1)); + cmd.Parameters.AddWithValue("@end", DateTime.Now.AddYears(1)); + MySqlDataReader rdr = cmd.ExecuteReader(); + List events = new(); + + while (rdr.Read()) + events.Add(new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }); + + cnn.Close(); + if (events.Count == 0) + return StatusCode(204, null); + return StatusCode(200, events); + } + + [HttpGet("~/event/getTodays")] + public ActionResult> getTodays() { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM events WHERE date=curdate()", cnn); + MySqlDataReader rdr = cmd.ExecuteReader(); + List events = new(); + + while (rdr.Read()) + events.Add(new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }); + + cnn.Close(); + if (events.Count == 0) + return StatusCode(204, null); + return StatusCode(200, events); + } + + [HttpGet("~/event/getLocation")] + public ActionResult getLocation(int eventId) { + ObjectResult result = get(eventId).Result as ObjectResult; + return StatusCode((int)result.StatusCode, ((Event)result.Value).date); + } + + [HttpGet("~/event/getDate")] + public ActionResult getDate(int eventId) { + ObjectResult result = get(eventId).Result as ObjectResult; + return StatusCode((int)result.StatusCode, ((Event)result.Value).date); + } + + [HttpGet("~/event/getTime")] + public ActionResult? getTime(int eventId) { + ObjectResult result = get(eventId).Result as ObjectResult; + return StatusCode((int)result.StatusCode, ((Event)result.Value).date); + } + + [HttpGet("~/event/getColor")] + public ActionResult getColor(int eventId) { + ObjectResult result = get(eventId).Result as ObjectResult; + return StatusCode((int)result.StatusCode, ((Event)result.Value).date); + } + + [HttpGet("~/event/getDescription")] + public ActionResult getDescription(int eventId) { + ObjectResult result = get(eventId).Result as ObjectResult; + return StatusCode((int)result.StatusCode, ((Event)result.Value).date); + } + + [HttpGet("~/event/getEventsAt")] + public ActionResult> getEventsAt(string location) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM events WHERE location=@location", cnn); + cmd.Parameters.AddWithValue("@location", location); + MySqlDataReader rdr = cmd.ExecuteReader(); + List events = new(); + + while (rdr.Read()) + events.Add( + new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }); + + cnn.Close(); + if (events.Count == 0) + return StatusCode(204, null); + return StatusCode(200, events); + } + + [HttpGet("~/event/getEventsOn")] + public ActionResult> GetEventsOn(string date) { + Regex regex = new("^[0-9]{4}/[0-9]{2}/[0-9]{2}$", RegexOptions.IgnoreCase); + if (!regex.IsMatch(date)) return StatusCode(432, null); + + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM events WHERE date=@date", cnn); + cmd.Parameters.AddWithValue("@date", date); + MySqlDataReader rdr = cmd.ExecuteReader(); + List events = new(); + + while (rdr.Read()) + events.Add( + new Event { + eventId = rdr.GetInt32("eventId"), + location = rdr.GetString("location"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + color = rdr.IsDBNull(4) ? null : rdr.GetString("color"), + textColor = rdr.IsDBNull(5) ? null : rdr.GetString("textColor"), + imageUri = rdr.IsDBNull(6) ? null : rdr.GetString("imageUri"), + description = rdr.IsDBNull(7) ? null : rdr.GetString("description") + }); + + cnn.Close(); + if (events.Count == 0) + return StatusCode(204, null); + return StatusCode(200, events); + } + + [HttpPost("~/admin/event/create")] + public ActionResult create([FromBody] PostEvent evnt) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new( + "INSERT INTO san_antonio_senior_golf.events (location, date, time, color, textColor, imageUri, description) VALUES (@location, @date, @time, @color, @textColor, @imageUri, @description);" + , cnn); + cmd.Parameters.AddWithValue("@location", evnt.location); + cmd.Parameters.AddWithValue("@date", evnt.date); + cmd.Parameters.AddWithValue("@time", evnt.time); + cmd.Parameters.AddWithValue("@color", evnt.color); + cmd.Parameters.AddWithValue("@textColor", evnt.textColor); + cmd.Parameters.AddWithValue("@imageUri", evnt.imageUri); + cmd.Parameters.AddWithValue("@description", evnt.description); + + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException e) { + Console.WriteLine(e); + cnn.Close(); + return StatusCode(435); + } + + cnn.Close(); + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/location")] + public ActionResult editLocation([FromBody] EditEventLocation editEventLocation) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new("UPDATE san_antonio_senior_golf.events t SET t.location = @location WHERE t.eventId = @eventId;", + cnn); + cmd.Parameters.AddWithValue("@location", editEventLocation.location); + cmd.Parameters.AddWithValue("@eventId", editEventLocation.eventId); + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException e) { + Console.WriteLine(e); + cnn.Close(); + return StatusCode(435); + } + + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/date")] + public ActionResult editDate([FromBody] EditEventDate editEventDate) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new("UPDATE san_antonio_senior_golf.events t SET t.date = @date WHERE t.eventId = @eventId;", cnn); + cmd.Parameters.AddWithValue("@date", editEventDate.date); + cmd.Parameters.AddWithValue("@eventId", editEventDate.eventId); + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException e) { + Console.WriteLine(e); + cnn.Close(); + return StatusCode(435); + } + + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/time")] + public ActionResult editTime([FromBody] EditEventTime editEventTime) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new("UPDATE san_antonio_senior_golf.events t SET t.time = @time WHERE t.eventId = @eventId;", cnn); + cmd.Parameters.AddWithValue("@time", editEventTime.time); + cmd.Parameters.AddWithValue("@eventId", editEventTime.eventId); + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException e) { + Console.WriteLine(e); + cnn.Close(); + return StatusCode(435); + } + + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/color")] + public ActionResult editColor([FromBody] EditEventColor editEventColor) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new("UPDATE san_antonio_senior_golf.events t SET t.color = @color WHERE t.eventId=@eventId;", cnn); + cmd.Parameters.AddWithValue("@color", editEventColor.color); + cmd.Parameters.AddWithValue("@eventId", editEventColor.eventId); + cmd.ExecuteNonQuery(); + + cnn.Close(); + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/textColor")] + public ActionResult editTextColor([FromBody] EditEventTextColor editEventTextColor) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new("UPDATE san_antonio_senior_golf.events t SET t.textColor = @textColor WHERE t.eventId=@eventId;", + cnn); + cmd.Parameters.AddWithValue("@textColor", editEventTextColor.textColor); + cmd.Parameters.AddWithValue("@eventId", editEventTextColor.eventId); + cmd.ExecuteNonQuery(); + + cnn.Close(); + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/imageUri")] + public ActionResult editImageUri([FromBody] EditEventImageUri editEventImageUri) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new("UPDATE san_antonio_senior_golf.events t SET t.imageUri = @imageUri WHERE t.eventId=@eventId;", + cnn); + cmd.Parameters.AddWithValue("@imageUri", editEventImageUri.imageUri); + cmd.Parameters.AddWithValue("@eventId", editEventImageUri.eventId); + cmd.ExecuteNonQuery(); + + cnn.Close(); + return StatusCode(204); + } + + [HttpPut("~/admin/event/edit/description")] + public ActionResult editDescription([FromBody] EditEventDescription editEventDescription) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new( + "UPDATE san_antonio_senior_golf.events t SET t.description = @description WHERE t.eventId=@eventId;", + cnn); + cmd.Parameters.AddWithValue("@description", editEventDescription.description); + cmd.Parameters.AddWithValue("@eventId", editEventDescription.eventId); + cmd.ExecuteNonQuery(); + + cnn.Close(); + return StatusCode(204); + } + + [HttpDelete("~/admin/event/delete")] + public ActionResult delete(int eventId) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = new("DELETE FROM san_antonio_senior_golf.events WHERE eventId=@eventId;", cnn); + cmd.Parameters.AddWithValue("@eventId", eventId); + cmd.ExecuteNonQuery(); + + cnn.Close(); + return StatusCode(204); + } + } +} \ No newline at end of file diff --git a/website/Controllers/SignupController.cs b/website/Controllers/SignupController.cs new file mode 100644 index 0000000..59efd9d --- /dev/null +++ b/website/Controllers/SignupController.cs @@ -0,0 +1,313 @@ +#region + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using MySql.Data.MySqlClient; +using website.Models.signups; + +#endregion + +namespace website.Controllers { + [ApiController] + [Route("[controller]")] + public class SignupController : ControllerBase { + [HttpGet("~/admin/signup/get")] + public ActionResult get(int signupId) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM san_antonio_senior_golf.signups WHERE signupId=@signupId", cnn); + cmd.Parameters.AddWithValue("@signupId", signupId); + MySqlDataReader rdr = cmd.ExecuteReader(); + + Signup signup = null; + while (rdr.Read()) + signup = new Signup { + signupId = rdr.GetInt32("signupId"), + eventId = rdr.GetInt32("eventId"), + firstName = rdr.GetString("firstName"), + lastName = rdr.GetString("lastName") + }; + + cnn.Close(); + return signup == null ? StatusCode(204, null) : StatusCode(200, signup); + } + + [HttpGet("~/admin/signup/getAll")] + public ActionResult> getAll() { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM san_antonio_senior_golf.signups", cnn); + MySqlDataReader rdr = cmd.ExecuteReader(); + + List signups = new(); + while (rdr.Read()) + signups.Add(new Signup { + signupId = rdr.GetInt32("signupId"), + eventId = rdr.GetInt32("eventId"), + firstName = rdr.GetString("firstName"), + lastName = rdr.GetString("lastName") + }); + + cnn.Close(); + return signups.Count == 0 ? StatusCode(204, null) : StatusCode(200, signups); + } + + [HttpGet("~/admin/signup/getAllCsv.csv")] + public string getAllCsv() { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return null; + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return null; + } + + MySqlCommand cmd = + new( + "select signupId, e.eventId, date, time, location, firstName, lastName from signups join events e on e.eventId = signups.eventId ", + cnn); + MySqlDataReader rdr = cmd.ExecuteReader(); + + List signups = new(); + while (rdr.Read()) + signups.Add(new SignupCsv { + signupId = rdr.GetInt32("signupId"), + eventId = rdr.GetInt32("eventId"), + date = rdr.GetDateTime("date"), + time = rdr.IsDBNull(3) ? null : rdr.GetTimeSpan("time"), + location = rdr.GetString("location"), + firstName = rdr.GetString("firstName"), + lastName = rdr.GetString("lastName") + }); + + cnn.Close(); + string result = "signupId,eventId,date,time,location,firstName,lastName\n" + + string.Join("\n", signups.Select(x => x.ToString()).ToArray()) + "\n"; + return result; + } + + [HttpGet("~/admin/signup/getForEvent")] + public ActionResult> getForEvent(int eventId) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = new("SELECT * FROM san_antonio_senior_golf.signups WHERE eventId=@eventId", cnn); + cmd.Parameters.AddWithValue("@eventId", eventId); + MySqlDataReader rdr = cmd.ExecuteReader(); + + List signups = new(); + while (rdr.Read()) + signups.Add(new Signup { + signupId = rdr.GetInt32("signupId"), + eventId = rdr.GetInt32("eventId"), + firstName = rdr.GetString("firstName"), + lastName = rdr.GetString("lastName") + }); + + cnn.Close(); + return signups.Count == 0 ? StatusCode(204, null) : StatusCode(200, signups); + } + + [HttpGet("~/admin/signup/getForPeople")] + public ActionResult> getForPeople(string firstName, string lastName) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531, null); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532, null); + } + + MySqlCommand cmd = + new("SELECT * FROM san_antonio_senior_golf.signups WHERE firstName=@firstName AND lastName=@lastName", + cnn); + cmd.Parameters.AddWithValue("@firstName", firstName); + cmd.Parameters.AddWithValue("@lastName", lastName); + MySqlDataReader rdr = cmd.ExecuteReader(); + + List signups = new(); + while (rdr.Read()) + signups.Add(new Signup { + signupId = rdr.GetInt32("signupId"), + eventId = rdr.GetInt32("eventId"), + firstName = rdr.GetString("firstName"), + lastName = rdr.GetString("lastName") + }); + + cnn.Close(); + return signups.Count == 0 ? StatusCode(204, null) : StatusCode(200, signups); + } + + [HttpPost("~/signup/create")] + public ActionResult create([FromBody] Signup signup) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new( + "INSERT INTO san_antonio_senior_golf.signups (eventId, firstName, lastName)VALUES (@eventId, @firstName, @lastName);" + , cnn); + cmd.Parameters.AddWithValue("@eventId", signup.eventId); + cmd.Parameters.AddWithValue("@firstName", signup.firstName); + cmd.Parameters.AddWithValue("@lastName", signup.lastName); + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException) { + return StatusCode(434); + } + + cnn.Close(); + return NoContent(); + } + + [HttpDelete("~/signup/delete")] + public ActionResult delete([FromBody] Signup signup) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new( + "DELETE FROM san_antonio_senior_golf.signups WHERE eventId=@eventId AND firstName=@firstName AND lastName=@lastName;" + , cnn); + cmd.Parameters.AddWithValue("@eventId", signup.eventId); + cmd.Parameters.AddWithValue("@firstName", signup.firstName); + cmd.Parameters.AddWithValue("@lastName", signup.lastName); + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException) { + return BadRequest(); + } + + cnn.Close(); + return NoContent(); + } + + [HttpDelete("~/admin/signup/delete")] + public IActionResult delete(int signupId) { + MySqlConnection cnn; + try { + cnn = Utilities.getConnection(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(531); + } + + try { + cnn.Open(); + } + catch (MySqlException e) { + Console.WriteLine(e); + return StatusCode(532); + } + + MySqlCommand cmd = + new( + "DELETE FROM san_antonio_senior_golf.signups WHERE signupId=@signupId;", cnn); + cmd.Parameters.AddWithValue("@signupId", signupId); + try { + cmd.ExecuteNonQuery(); + } + catch (MySqlException) { + return BadRequest(); + } + + cnn.Close(); + return NoContent(); + } + } +} \ No newline at end of file diff --git a/website/Folder.DotSettings.user b/website/Folder.DotSettings.user new file mode 100644 index 0000000..8033d53 --- /dev/null +++ b/website/Folder.DotSettings.user @@ -0,0 +1,13 @@ + + INFO + ThisComputer + <SessionState ContinuousTestingMode="0" Name="test" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>NUnit2x::FB4DF0DF-57BA-9BB7-32BA-1A5AB9438AF7::net5.0::tests.Tests.test</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="passingTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>NUnit2x::FB4DF0DF-57BA-9BB7-32BA-1A5AB9438AF7::net5.0::sanAntonioSeniorGolf.Tests.Tests.passingTest</TestId> + </TestAncestor> +</SessionState> \ No newline at end of file diff --git a/website/Models/events/EditEventColor.cs b/website/Models/events/EditEventColor.cs new file mode 100644 index 0000000..d187357 --- /dev/null +++ b/website/Models/events/EditEventColor.cs @@ -0,0 +1,6 @@ +namespace website.Models.events { + public class EditEventColor { + public int? eventId { get; set; } + public string? color { get; set; } + } +} \ No newline at end of file diff --git a/website/Models/events/EditEventDate.cs b/website/Models/events/EditEventDate.cs new file mode 100644 index 0000000..2907f82 --- /dev/null +++ b/website/Models/events/EditEventDate.cs @@ -0,0 +1,21 @@ +#region + +using System; + +#endregion + +namespace website.Models.events { + public class EditEventDate { + private string _date; + public int eventId { get; set; } + + public string date { + get => _date; + set { + if (!Utilities.verifyDate(value)) + throw new ArgumentException("Date in incorrect format or null"); + _date = value; + } + } + } +} \ No newline at end of file diff --git a/website/Models/events/EditEventDescription.cs b/website/Models/events/EditEventDescription.cs new file mode 100644 index 0000000..15d9202 --- /dev/null +++ b/website/Models/events/EditEventDescription.cs @@ -0,0 +1,6 @@ +namespace website.Models.events { + public class EditEventDescription { + public int? eventId { get; set; } + public string? description { get; set; } + } +} \ No newline at end of file diff --git a/website/Models/events/EditEventImageUri.cs b/website/Models/events/EditEventImageUri.cs new file mode 100644 index 0000000..22899a3 --- /dev/null +++ b/website/Models/events/EditEventImageUri.cs @@ -0,0 +1,6 @@ +namespace website.Models.events { + public class EditEventImageUri { + public int? eventId { get; set; } + public string? imageUri { get; set; } + } +} \ No newline at end of file diff --git a/website/Models/events/EditEventLocation.cs b/website/Models/events/EditEventLocation.cs new file mode 100644 index 0000000..0bbc651 --- /dev/null +++ b/website/Models/events/EditEventLocation.cs @@ -0,0 +1,6 @@ +namespace website.Models.events { + public class EditEventLocation { + public int? eventId { get; set; } + public string? location { get; set; } + } +} \ No newline at end of file diff --git a/website/Models/events/EditEventTextColor.cs b/website/Models/events/EditEventTextColor.cs new file mode 100644 index 0000000..059d741 --- /dev/null +++ b/website/Models/events/EditEventTextColor.cs @@ -0,0 +1,6 @@ +namespace website.Models.events { + public class EditEventTextColor { + public int? eventId { get; set; } + public string? textColor { get; set; } + } +} \ No newline at end of file diff --git a/website/Models/events/EditEventTime.cs b/website/Models/events/EditEventTime.cs new file mode 100644 index 0000000..d9631f5 --- /dev/null +++ b/website/Models/events/EditEventTime.cs @@ -0,0 +1,20 @@ +using System; +using System.Text.RegularExpressions; +using Google.Protobuf.WellKnownTypes; +using MySql.Data.Types; + +namespace website.Models.events { + + public class EditEventTime { + private string _time; + public int eventId { get; set; } + public string? time { + get => _time; + set { + if (!Utilities.verifyTime(value)) + throw new ArgumentException("Time is malformed or is null"); + _time = value; + } + } + } +} \ No newline at end of file diff --git a/website/Models/events/Event.cs b/website/Models/events/Event.cs new file mode 100644 index 0000000..835be51 --- /dev/null +++ b/website/Models/events/Event.cs @@ -0,0 +1,17 @@ +#nullable enable + +using System; +using MySql.Data.Types; + +namespace website.Models.events { + public class Event { + public int? eventId { get; set; } + public string? location { get; set; } + public DateTime date { get; set; } + public TimeSpan? time { get; set; } + public string? color { get; set; } + public string? textColor { get; set; } + public string? imageUri { get; set; } + public string? description { get; set; } + } +} \ No newline at end of file diff --git a/website/Models/events/PostEvent.cs b/website/Models/events/PostEvent.cs new file mode 100644 index 0000000..8e2919a --- /dev/null +++ b/website/Models/events/PostEvent.cs @@ -0,0 +1,84 @@ +#nullable enable + +#region + +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using System.Text.RegularExpressions; +using website.Models.events; + +#endregion + +namespace website.Models { + /// + /// This method is used due to DateTime and TimeSpan + /// being difficult to send in a post request. + /// + public class PostEvent { + private string _date; + private string? _time; + public int? eventId { get; set; } + [NotNull] public string? location { get; set; } + + /// + /// YYYY/MM/DD + /// + [NotNull] + [Required] + public string date { + get => _date; + set { + if (Utilities.verifyDate(value)) { + _date = value; + return; + } + + throw new ArgumentException("Date in incorrect format or null"); + } + } + + /// + /// HH:MM:SS + /// + public string? time { + get => _time; + set { + if (value == null) { + _time = null; + return; + } + + if (Utilities.verifyTime(value)) ; + _time = value; + } + } + + public string? color { get; set; } + public string? textColor { get; set; } + public string? imageUri { get; set; } + public string? description { get; set; } + + public static Event postEventToEvent(PostEvent postEvent) { + if (postEvent.time == null) + return new Event { + eventId = postEvent.eventId, + location = postEvent.location, + date = Utilities.stringDateToDateTime(postEvent.date), + time = null + }; + + Match match = new Regex("^[0-9]{2}$").Match(postEvent.time); + int hours = int.Parse(match.Value); + int minutes = int.Parse(match.NextMatch().Value); + int seconds = int.Parse(match.NextMatch().Value); + + return new Event { + eventId = postEvent.eventId, + location = postEvent.location, + date = Utilities.stringDateToDateTime(postEvent.date), + time = Utilities.stringTimeToTimeSpan(postEvent.time) + }; + } + } +} \ No newline at end of file diff --git a/website/Models/signups/Signup.cs b/website/Models/signups/Signup.cs new file mode 100644 index 0000000..1265de5 --- /dev/null +++ b/website/Models/signups/Signup.cs @@ -0,0 +1,18 @@ +namespace website.Models.signups { + public class Signup { + private string _firstName; + private string _lastName; + public int? signupId { get; set; } + public int? eventId { get; init; } + + public string firstName { + get => _firstName; + set => _firstName = value.ToUpper().Trim(); + } + + public string lastName { + get => _lastName; + set => _lastName = value.ToUpper().Trim(); + } + } +} \ No newline at end of file diff --git a/website/Models/signups/SignupCsv.cs b/website/Models/signups/SignupCsv.cs new file mode 100644 index 0000000..9a8f98f --- /dev/null +++ b/website/Models/signups/SignupCsv.cs @@ -0,0 +1,33 @@ +#region + +using System; + +#endregion + +namespace website.Models.signups { + public class SignupCsv { + private string _firstName; + private string _lastName; + public int? signupId { get; set; } + public int? eventId { get; init; } + public DateTime date { get; set; } + public TimeSpan? time { get; set; } + public string? location { get; set; } + + + public string firstName { + get => _firstName; + set => _firstName = value.ToUpper().Trim(); + } + + public string lastName { + get => _lastName; + set => _lastName = value.ToUpper().Trim(); + } + + public override string ToString() { + return signupId + "," + eventId + "," + date.ToString("yyyy-MM-dd") + "," + time + "," + location + "," + + _firstName + "," + _lastName; + } + } +} \ No newline at end of file diff --git a/website/Program.cs b/website/Program.cs new file mode 100644 index 0000000..ae33d9a --- /dev/null +++ b/website/Program.cs @@ -0,0 +1,19 @@ +#region + +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +#endregion + +namespace website { + public class Program { + public static void Main(string[] args) { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) { + return Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); + } + } +} \ No newline at end of file diff --git a/website/Properties/launchSettings.json b/website/Properties/launchSettings.json new file mode 100644 index 0000000..f80024f --- /dev/null +++ b/website/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:18853", + "sslPort": 44359 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "sanAntonioSeniorGolf": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "launchUrl": "api/website/index.html", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/website/Startup.cs b/website/Startup.cs new file mode 100644 index 0000000..aab0ee9 --- /dev/null +++ b/website/Startup.cs @@ -0,0 +1,53 @@ +#region + +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi.Models; + +#endregion + +namespace website { + public class Startup { + public Startup(IConfiguration configuration) { + this.configuration = configuration; + } + + public IConfiguration configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void configureServices(IServiceCollection services) { + services.AddControllers(); + services.AddCors(); + services.AddSwaggerGen(c => { + c.SwaggerDoc("v1", new OpenApiInfo {Title = "website", Version = "v1"}); + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void configure(IApplicationBuilder app, IWebHostEnvironment env) { + if (env.IsDevelopment()) { + app.UseDeveloperExceptionPage(); + app.UseSwagger(c => c.RouteTemplate = "/api/{documentname}/swagger.json"); + app.UseSwaggerUI(c => { + c.SwaggerEndpoint("/api/v1/swagger.json", "website v1"); + c.RoutePrefix = "api/website"; + }); + } + + app.UseCors(builder => { + builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); + }); + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + } + } +} diff --git a/website/Utilities.cs b/website/Utilities.cs new file mode 100644 index 0000000..1bdc797 --- /dev/null +++ b/website/Utilities.cs @@ -0,0 +1,80 @@ +#nullable enable + +#region + +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Text.RegularExpressions; +using MySql.Data.MySqlClient; + +#endregion + +namespace website { + public static class Utilities { + private static readonly string MySqlPass = + File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "mysql.pass"); + + private static readonly string MySqlHost = + File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "mysql.host"); + + private static readonly string MySqlUser = + File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "mysql.user"); + + public static MySqlConnection getConnection() { + return new MySqlConnection("server=" + MySqlHost + ";pwd=" + MySqlPass + ";uid=" + MySqlUser + + ";database=san_antonio_senior_golf;Port=5618;Convert Zero Datetime=True;"); + } + + /// + /// Verify if string date can be converted to DateTime + /// + /// + /// True if date matches "^[0-9]{4}/[0-9]{2}/[0-9]{2}$" + public static bool verifyDate(string? date) { + if (date == null) + return false; + Regex regex = new("^[0-9]{4}/[0-9]{2}/[0-9]{2}$", RegexOptions.IgnoreCase); + return regex.IsMatch(date); + } + + /// + /// Check if date param is applicable with verifyDate + /// + /// Not null string + /// DateTime equivalent of date param + // TODO Creating 3 regex's is inefficient + public static DateTime stringDateToDateTime([NotNull] string date) { + int year = int.Parse(new Regex("^[0-9]{4}$").Match(date).Value); + int month = int.Parse(new Regex("/[0-9]{2}/$").Match(date).Value); + int day = int.Parse(new Regex("/[0-9]{2}$").Match(date).NextMatch().Value); + return new DateTime(year, month, day); + } + + /// + /// Verify if string time can be converted to TimeSpan using stringTimeToTimeSpan + /// + /// + /// True if time matches "^[0-9]{2}:[0-9]{2}:[0-9]{2}$" + public static bool verifyTime(string? time) { + if (time == null) + return true; + Regex regex = new("^[0-9]{2}:[0-9]{2}:[0-9]{2}$", RegexOptions.IgnoreCase); + return regex.IsMatch(time); + } + + /// + /// Check if time param is applicable with verifyTime + /// + /// Not null string + /// TimeSpan equivalent of time string + public static TimeSpan stringTimeToTimeSpan([NotNull] string time) { + Match match = new Regex("^[0-9]{2}$").Match(time); + int hours = int.Parse(match.Value); + int minutes = int.Parse(match.NextMatch().Value); + int seconds = int.Parse(match.NextMatch().Value); + + return new TimeSpan(hours, minutes, seconds); + } + } +} \ No newline at end of file diff --git a/website/appsettings.Development.json b/website/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/website/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/website/appsettings.json b/website/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/website/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/website/website.csproj b/website/website.csproj new file mode 100644 index 0000000..7f195cb --- /dev/null +++ b/website/website.csproj @@ -0,0 +1,21 @@ + + + + net5.0 + false + sanAntonioSeniorGolf + + + + + + + + + + + + + + +