Addition of other database components and basic API endpoints

This commit is contained in:
Josh Deck 2024-08-21 13:39:10 -04:00
parent 56a100ad96
commit 2c967f701f
8 changed files with 153 additions and 13 deletions

View File

@ -1,7 +1,9 @@
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using System;
using System.Data; using System.Data;
using Telerik.SvgIcons; using Telerik.SvgIcons;
using TSC2.Components.Layout; using TSC2.Components.Layout;
using static Telerik.Blazor.ThemeConstants;
namespace TSC2.Components.CSharp namespace TSC2.Components.CSharp
{ {
@ -129,5 +131,93 @@ namespace TSC2.Components.CSharp
} }
conn.Close(); 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<string> 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<string> reviews = new();
while (reader.Read())
{
var reviewText = reader.GetString(1);
reviews.Add(reviewText);
}
return reviews;
}
public static List<int> 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<int> scores = new();
while (reader.Read())
{
var reviewScore = Convert.ToInt32(reader.GetInt32(2));
scores.Add(reviewScore);
}
return scores;
}
}
} }

View File

@ -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

View File

@ -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<string> GetReviews(string shopID)
{
var reviews = DatabaseManager.GetShopReviews(shopID).ToArray();
return reviews;
}
[HttpGet("GetShopReviewScores")]
public IEnumerable<int> GetReviewScores(string shopID)
{
var scores = DatabaseManager.GetShopReviewScores(shopID).ToArray();
return scores;
}
}
}

View File

@ -219,13 +219,12 @@
</TelerikButton> </TelerikButton>
@code { @code {
public void VisitClickHandler() public void VisitClickHandler()
{ {
Console.WriteLine("Visiting page" + CurrentSelection); string destination = "/info/" + CurrentSelection + "&" + CurrentUniqueID;
string destination = "/info/" + CurrentSelection; NavManager.NavigateTo(destination);
NavManager.NavigateTo(destination);
} }
} }
</WindowContent> </WindowContent>
<WindowActions> <WindowActions>

View File

@ -44,6 +44,8 @@ namespace TSC2.Components.Pages
var dataItem = args.DataItem as MarkerModel; var dataItem = args.DataItem as MarkerModel;
CurrentSelection = dataItem.Title; CurrentSelection = dataItem.Title;
CurrentBlurb = dataItem.Blurb; CurrentBlurb = dataItem.Blurb;
CurrentUniqueID = dataItem.UniqueID;
ToggleModal(); ToggleModal();
} }
@ -137,6 +139,8 @@ namespace TSC2.Components.Pages
public TelerikPopover? Popover { get; set; } public TelerikPopover? Popover { get; set; }
public string CurrentSelection { get; set; } = "123412341234onetwothreefour"; 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 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 bool isVisible = false;
public void ToggleModal() public void ToggleModal()

View File

@ -1,4 +1,4 @@
@page "/info/{Shop}" @page "/info/{Shop}&{UniqueID}"
@ -121,6 +121,7 @@
<div class="col-lg-8"> <div class="col-lg-8">
<div class="heading-block"> <div class="heading-block">
<h2>Some of your Questions:</h2> <h2>Some of your Questions:</h2>
<TelerikButton OnClick="@LeaveReviewDebug">Leave Review</TelerikButton>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum tempore autem distinctio qui iure aspernatur doloribus porro blanditiis perspiciatis alias.</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Earum tempore autem distinctio qui iure aspernatur doloribus porro blanditiis perspiciatis alias.</p>
</div> </div>

View File

@ -1,5 +1,5 @@
 using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components; using TSC2.Components.CSharp;
namespace TSC2.Components.Pages namespace TSC2.Components.Pages
{ {
@ -7,10 +7,18 @@ namespace TSC2.Components.Pages
{ {
[Parameter] [Parameter]
public string Shop { get; set; } = "DEFAULT_NAME"; 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);
} }
} }
} }

View File

@ -4,10 +4,11 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddRazorComponents(options => options.DetailedErrors = builder.Environment.IsDevelopment()).AddInteractiveServerComponents(); builder.Services.AddRazorComponents(options => options.DetailedErrors = builder.Environment.IsDevelopment()).AddInteractiveServerComponents();
builder.Services.AddControllers();
builder.Services.AddTelerikBlazor(); builder.Services.AddTelerikBlazor();
var app = builder.Build(); var app = builder.Build();
app.MapControllers();
// Note secrets for login auth // Note secrets for login auth
var GoogleClientId = builder.Configuration["Authentication:Google:ClientId"]; var GoogleClientId = builder.Configuration["Authentication:Google:ClientId"];