Skip to content

Commit ec60c07

Browse files
committed
update API, and AzureSQL, Migrate Table
1 parent da1eea0 commit ec60c07

File tree

57 files changed

+386
-1248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+386
-1248
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ dist-ssr
2222
*.njsproj
2323
*.sln
2424
*.sw?
25+
26+
.vs/
Binary file not shown.
Binary file not shown.
-176 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

.vs/TradingWebApp/config/applicationhost.config

Lines changed: 0 additions & 1016 deletions
This file was deleted.

.vs/TradingWebApp/v17/.futdcache.v2

-308 Bytes
Binary file not shown.

.vs/TradingWebApp/v17/DocumentLayout.json

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
22
using System;
33
using System.Collections.Generic;
4-
using System.Net;
4+
using System.Net.Http;
55
using System.Threading.Tasks;
6+
using Newtonsoft.Json.Linq;
67

78
namespace TradingWebApp.Server.Controllers
89
{
910
[Route("api/[controller]")]
1011
[ApiController]
1112
public class QuoteController : ControllerBase
1213
{
13-
// GetCompanyName method moved here
14-
private string GetCompanyName(string ticker)
14+
private readonly HttpClient _httpClient;
15+
16+
public QuoteController()
1517
{
16-
string url = $"https://finance.google.com/finance?q={ticker}";
17-
WebClient client = new WebClient();
18-
string content = client.DownloadString(url);
19-
int startLocation = content.IndexOf("<title>") + "<title>".Length;
20-
int endLocation = content.IndexOf("</title>");
21-
string title = content.Substring(startLocation, endLocation - startLocation);
22-
string companyName = title.Split(':')[0];
23-
int positionRemove = companyName.IndexOf('(');
24-
companyName = companyName.Substring(0, positionRemove);
25-
return companyName.Trim();
18+
_httpClient = new HttpClient();
19+
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0"); // Prevent blocking
20+
}
21+
22+
private async Task<string> GetCompanyName(string symbol)
23+
{
24+
Console.WriteLine($"Testing Get Company Name11");
25+
try
26+
{
27+
string url = $"https://query2.finance.yahoo.com/v1/finance/search?q={symbol}";
28+
HttpResponseMessage response = await _httpClient.GetAsync(url);
29+
response.EnsureSuccessStatusCode();
30+
31+
string jsonResponse = await response.Content.ReadAsStringAsync();
32+
JObject json = JObject.Parse(jsonResponse);
33+
34+
string companyName = json["quotes"]?[0]?["shortname"]?.ToString();
35+
Console.WriteLine($"Testing Get Company Name: {companyName}");
36+
return !string.IsNullOrEmpty(companyName) ? companyName : "Unknown Company";
37+
}
38+
catch
39+
{
40+
return "Unknown Company";
41+
}
42+
}
43+
44+
private async Task<double?> GetStockPrice(string symbol)
45+
{
46+
Console.WriteLine($"Testing Get Stock Price");
47+
string apiKey = "cuemgvpr01qkqnpfilcgcuemgvpr01qkqnpfild0";
48+
string url = $"https://finnhub.io/api/v1/quote?symbol={symbol}&token={apiKey}";
49+
50+
HttpResponseMessage response = await _httpClient.GetAsync(url);
51+
if (!response.IsSuccessStatusCode)
52+
return null;
53+
54+
string jsonResponse = await response.Content.ReadAsStringAsync();
55+
JObject json = JObject.Parse(jsonResponse);
56+
57+
double? price = json["c"]?.ToObject<double>();
58+
return price;
59+
60+
61+
2662
}
2763

28-
//api/Quote/Lookup
2964
[HttpPost("Lookup")]
3065
public async Task<IActionResult> LookupStock([FromBody] string symbol)
3166
{
@@ -34,44 +69,25 @@ public async Task<IActionResult> LookupStock([FromBody] string symbol)
3469
return BadRequest("Symbol cannot be empty");
3570
}
3671

37-
Console.WriteLine($"Symbol: {symbol}");
38-
39-
symbol = symbol.ToUpper();
40-
DateTime end = DateTime.Now;
41-
DateTime start = end.AddDays(-5);
72+
Console.WriteLine($"Looking up stock: {symbol}");
4273

43-
string url = $"https://query1.finance.yahoo.com/v7/finance/download/{Uri.EscapeDataString(symbol)}" +
44-
$"?period1={(Int32)(start.Subtract(new DateTime(1970, 1, 1))).TotalSeconds}" +
45-
$"&period2={(Int32)(end.Subtract(new DateTime(1970, 1, 1))).TotalSeconds}" +
46-
$"&interval=1d&events=history&includeAdjustedClose=true";
74+
string companyName = await GetCompanyName(symbol);
75+
double? price = await GetStockPrice(symbol);
4776

48-
try
77+
if (price == null)
4978
{
50-
WebClient client = new WebClient();
51-
client.Headers["User-Agent"] = "C# WebClient";
52-
string response = client.DownloadString(url);
53-
string[] lines = response.Split('\n');
54-
Array.Reverse(lines);
55-
string[] headers = lines[0].Split(',');
56-
string[] data = lines[1].Split(',');
57-
double price = Math.Round(Double.Parse(data[4]), 2); // Getting current price instead of adjusted close
58-
string name = GetCompanyName(symbol);
59-
60-
var stockInfo = new Dictionary<string, dynamic>()
61-
{
62-
{"name", name},
63-
{"price", price},
64-
{"symbol", symbol}
65-
};
66-
67-
Console.WriteLine($"Stock Information: {stockInfo}");
68-
return Ok(stockInfo);
79+
return NotFound($"No price information found for symbol: {symbol}");
6980
}
70-
catch (WebException)
81+
82+
var stockInfo = new Dictionary<string, object>()
7183
{
72-
Console.WriteLine($"No information found for symbol: {symbol}");
73-
return NotFound($"No information found for symbol: {symbol}");
74-
}
84+
{"name", companyName},
85+
{"price", price},
86+
{"symbol", symbol.ToUpper()}
87+
};
88+
89+
Console.WriteLine($"Stock Information: {stockInfo}");
90+
return Ok(stockInfo);
7591
}
7692
}
7793
}

TradingWebApp.Server/Data/AppDbContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
2020
protected override void OnModelCreating(ModelBuilder modelBuilder)
2121
{
2222
// Configure entity relationships, constraints, etc. here
23+
modelBuilder.Entity<Stock>()
24+
.HasKey(s => s.StockID); // Explicitly set the primary key to StockID
2325

26+
modelBuilder.Entity<Stock>()
27+
.Property(s => s.StockID)
28+
.ValueGeneratedOnAdd(); // Ensure it's auto-generated
2429

2530
modelBuilder.Entity<Transaction>()
2631
.Property(t => t.ShareValue)

TradingWebApp.Server/Migrations/20240305182012_AddStockIDColumn.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

TradingWebApp.Server/Migrations/20240305182012_AddStockIDColumn.Designer.cs renamed to TradingWebApp.Server/Migrations/20250131230922_FixUserUniqueIndex.Designer.cs

Lines changed: 2 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace TradingWebApp.Server.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class FixUserUniqueIndex : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.DropForeignKey(
14+
name: "FK_Stocks_Users_UserId",
15+
table: "Stocks");
16+
17+
migrationBuilder.DropForeignKey(
18+
name: "FK_Transactions_Users_UserId",
19+
table: "Transactions");
20+
21+
migrationBuilder.DropIndex(
22+
name: "IX_Transactions_UserId",
23+
table: "Transactions");
24+
25+
migrationBuilder.DropPrimaryKey(
26+
name: "PK_Stocks",
27+
table: "Stocks");
28+
29+
migrationBuilder.DropIndex(
30+
name: "IX_Stocks_UserId",
31+
table: "Stocks");
32+
33+
migrationBuilder.AddColumn<int>(
34+
name: "StockID",
35+
table: "Stocks",
36+
type: "int",
37+
nullable: false,
38+
defaultValue: 0)
39+
.Annotation("SqlServer:Identity", "1, 1");
40+
41+
migrationBuilder.AddPrimaryKey(
42+
name: "PK_Stocks",
43+
table: "Stocks",
44+
column: "StockID");
45+
}
46+
47+
/// <inheritdoc />
48+
protected override void Down(MigrationBuilder migrationBuilder)
49+
{
50+
migrationBuilder.DropPrimaryKey(
51+
name: "PK_Stocks",
52+
table: "Stocks");
53+
54+
migrationBuilder.DropColumn(
55+
name: "StockID",
56+
table: "Stocks");
57+
58+
migrationBuilder.AddPrimaryKey(
59+
name: "PK_Stocks",
60+
table: "Stocks",
61+
column: "Symbol");
62+
63+
migrationBuilder.CreateIndex(
64+
name: "IX_Transactions_UserId",
65+
table: "Transactions",
66+
column: "UserId");
67+
68+
migrationBuilder.CreateIndex(
69+
name: "IX_Stocks_UserId",
70+
table: "Stocks",
71+
column: "UserId");
72+
73+
migrationBuilder.AddForeignKey(
74+
name: "FK_Stocks_Users_UserId",
75+
table: "Stocks",
76+
column: "UserId",
77+
principalTable: "Users",
78+
principalColumn: "Id",
79+
onDelete: ReferentialAction.Cascade);
80+
81+
migrationBuilder.AddForeignKey(
82+
name: "FK_Transactions_Users_UserId",
83+
table: "Transactions",
84+
column: "UserId",
85+
principalTable: "Users",
86+
principalColumn: "Id",
87+
onDelete: ReferentialAction.Cascade);
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)