diff --git a/TSC2/Components/CSharp/DatabaseManager.cs b/TSC2/Components/CSharp/DatabaseManager.cs index f221e3e..853c6bd 100644 --- a/TSC2/Components/CSharp/DatabaseManager.cs +++ b/TSC2/Components/CSharp/DatabaseManager.cs @@ -1,7 +1,9 @@ using MySql.Data.MySqlClient; +using System; using System.Data; using Telerik.SvgIcons; using TSC2.Components.Layout; +using static Telerik.Blazor.ThemeConstants; namespace TSC2.Components.CSharp { @@ -129,5 +131,93 @@ namespace TSC2.Components.CSharp } conn.Close(); } - } + + + + /**********************************************/ + // REVIEWS + /**********************************************/ + public async static void AddReview(string shopID) + { + if (MainLayout.Session.Count == 0) // We only want to proceed if the user is signed in + return; + + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); + + // Set review ID to a combination of the user's unique id and the shop's + var reviewID = MainLayout.Session["id"] + "~~" + shopID; + + using (var count_cmd = new MySqlCommand("SELECT COUNT(*) FROM (SELECT * FROM reviews WHERE ReviewID='" + reviewID + "') AS result", conn)) + { + int count = Convert.ToInt32(count_cmd.ExecuteScalar()); + if (count == 0) + { + try + { + + + // Check if a review already exists by this ID + + var insert_cmd = new MySqlCommand(); + + insert_cmd.CommandText = "INSERT INTO reviews(ReviewID,ReviewText,ReviewScore) VALUES(@reviewid,@reviewtext,@reviewscore)"; + insert_cmd.Connection = conn; + + insert_cmd.Parameters.AddWithValue("@reviewid", reviewID); + insert_cmd.Parameters.AddWithValue("@reviewtext", "This is a review! Surely this will work on the Second Try!"); + insert_cmd.Parameters.AddWithValue("@reviewscore", 1); + + insert_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + await Console.Out.WriteLineAsync(ex.Message); + } + } + else + { + await Console.Out.WriteLineAsync("Review already exists for this user on this shop."); + } + } + } + + + + public static List GetShopReviews(string shopID) + { + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); + + var cmd = new MySqlCommand("SELECT * FROM reviews WHERE ReviewID LIKE '%~~" + shopID + "'", conn); + MySqlDataReader reader = cmd.ExecuteReader(); + + List reviews = new(); + while (reader.Read()) + { + var reviewText = reader.GetString(1); + reviews.Add(reviewText); + } + return reviews; + } + + + + public static List GetShopReviewScores(string shopID) + { + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); + + var cmd = new MySqlCommand("SELECT * FROM reviews WHERE ReviewID LIKE '%~~" + shopID + "'", conn); + MySqlDataReader reader = cmd.ExecuteReader(); + + List scores = new(); + while (reader.Read()) + { + var reviewScore = Convert.ToInt32(reader.GetInt32(2)); + scores.Add(reviewScore); + } + return scores; + } + } } diff --git a/TSC2/Components/Controllers/Controller Map.txt b/TSC2/Components/Controllers/Controller Map.txt new file mode 100644 index 0000000..60ec578 --- /dev/null +++ b/TSC2/Components/Controllers/Controller Map.txt @@ -0,0 +1,9 @@ +These are the API endpoints to be exposed for external consumption. + + + +GET endpoints +-------------------- +/api/getshopreviews?shopid={id} - returns a list of all reviews left for the specified shop + +/api/getshopreviewscores?shopid={id} - returns a list of all review scores left for the specified shop \ No newline at end of file diff --git a/TSC2/Components/Controllers/ReviewController.cs b/TSC2/Components/Controllers/ReviewController.cs new file mode 100644 index 0000000..f32a7ce --- /dev/null +++ b/TSC2/Components/Controllers/ReviewController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text.Json; +using TSC2.Components.CSharp; + +namespace TSC2.Components.Controllers +{ + [ApiController] + [Route("api")] + public class ReviewController : Controller + { + [HttpGet("GetShopReviews")] + public IEnumerable GetReviews(string shopID) + { + var reviews = DatabaseManager.GetShopReviews(shopID).ToArray(); + + + return reviews; + } + + + [HttpGet("GetShopReviewScores")] + public IEnumerable GetReviewScores(string shopID) + { + var scores = DatabaseManager.GetShopReviewScores(shopID).ToArray(); + return scores; + } + } +} diff --git a/TSC2/Components/Pages/Home.razor b/TSC2/Components/Pages/Home.razor index 171dbd7..3eb32f3 100644 --- a/TSC2/Components/Pages/Home.razor +++ b/TSC2/Components/Pages/Home.razor @@ -219,13 +219,12 @@ @code { - public void VisitClickHandler() - { - Console.WriteLine("Visiting page" + CurrentSelection); - string destination = "/info/" + CurrentSelection; - NavManager.NavigateTo(destination); + public void VisitClickHandler() + { + string destination = "/info/" + CurrentSelection + "&" + CurrentUniqueID; + NavManager.NavigateTo(destination); - } + } } diff --git a/TSC2/Components/Pages/Home.razor.cs b/TSC2/Components/Pages/Home.razor.cs index a8b8b68..bb5cdfb 100644 --- a/TSC2/Components/Pages/Home.razor.cs +++ b/TSC2/Components/Pages/Home.razor.cs @@ -44,6 +44,8 @@ namespace TSC2.Components.Pages var dataItem = args.DataItem as MarkerModel; CurrentSelection = dataItem.Title; CurrentBlurb = dataItem.Blurb; + CurrentUniqueID = dataItem.UniqueID; + ToggleModal(); } @@ -137,6 +139,8 @@ namespace TSC2.Components.Pages public TelerikPopover? Popover { get; set; } public string CurrentSelection { get; set; } = "123412341234onetwothreefour"; public string CurrentBlurb { get; set; } = "This is a blurb for a shop. I believe it is rather dastardly."; + public string CurrentUniqueID { get; set; } = ""; + public bool isVisible = false; public void ToggleModal() diff --git a/TSC2/Components/Pages/Info.razor b/TSC2/Components/Pages/Info.razor index b294504..f722dc0 100644 --- a/TSC2/Components/Pages/Info.razor +++ b/TSC2/Components/Pages/Info.razor @@ -1,4 +1,4 @@ -@page "/info/{Shop}" +@page "/info/{Shop}&{UniqueID}" @@ -121,6 +121,7 @@

Some of your Questions:

+ Leave Review

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum tempore autem distinctio qui iure aspernatur doloribus porro blanditiis perspiciatis alias.

diff --git a/TSC2/Components/Pages/Info.razor.cs b/TSC2/Components/Pages/Info.razor.cs index 3de1bf8..81c3b93 100644 --- a/TSC2/Components/Pages/Info.razor.cs +++ b/TSC2/Components/Pages/Info.razor.cs @@ -1,5 +1,5 @@ - -using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components; +using TSC2.Components.CSharp; namespace TSC2.Components.Pages { @@ -7,10 +7,18 @@ namespace TSC2.Components.Pages { [Parameter] public string Shop { get; set; } = "DEFAULT_NAME"; + [Parameter] public string UniqueID { get; set; } = "-1"; - public void DoAThing() + protected override void OnInitialized() // Executes every page load; run inexpensive rendering code here { - Console.WriteLine("This is a mimic of a sql query... DO NOT LOOK AT THIS PLEASE"); + //DatabaseManager + } + + + + private void LeaveReviewDebug() + { + DatabaseManager.AddReview(UniqueID); } } } \ No newline at end of file diff --git a/TSC2/Program.cs b/TSC2/Program.cs index 16e0757..cea1eff 100644 --- a/TSC2/Program.cs +++ b/TSC2/Program.cs @@ -4,10 +4,11 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents(options => options.DetailedErrors = builder.Environment.IsDevelopment()).AddInteractiveServerComponents(); - +builder.Services.AddControllers(); builder.Services.AddTelerikBlazor(); var app = builder.Build(); +app.MapControllers(); // Note secrets for login auth var GoogleClientId = builder.Configuration["Authentication:Google:ClientId"];