Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .gitignore
Binary file not shown.
Binary file added .vs/CodingExercise/v17/.wsuo
Binary file not shown.
27 changes: 27 additions & 0 deletions .vs/CodingExercise/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\pc\\source\\repos\\CodingExercise\\",
"Documents": [],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": -1,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:0:0:{56df62a4-05a3-4e5b-aa1a-99371ccfb997}"
},
{
"$type": "Bookmark",
"Name": "ST:0:0:{cce594b6-0c39-4442-ba28-10c64ac7e89f}"
}
]
}
]
}
]
}
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\C:\\Users\\pc\\Source\\Repos\\CodingExercise",
"PreviewInSolutionExplorer": false
}
1 change: 1 addition & 0 deletions InvestmentApi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vs/
38 changes: 38 additions & 0 deletions InvestmentApi/Controllers/InvestmentsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using InvestmentApi.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using InvestmentApi.Models;

namespace InvestmentApi.Controllers;

[ApiController]
[Route("api/users/{userId:int}/[controller]")]
public class InvestmentsController : ControllerBase
{
private readonly AppDbContext _db;
public InvestmentsController(AppDbContext db) => _db = db;

// GET: /api/users/1/investments
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByUser(int userId)
{
var exists = await _db.Users.AnyAsync(u => u.Id == userId);
if (!exists) return NotFound(new { message = $"User {userId} not found" });

var items = await _db.Investments
.Where(i => i.UserId == userId)
.OrderBy ( i => i.Id)
.Select(i => new {
i.Id,
i.Ticker,
i.Units,
i.CostBasis
})
.ToListAsync();

return Ok(items);
}
}

15 changes: 15 additions & 0 deletions InvestmentApi/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Controllers/UsersController.cs
using InvestmentApi.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly AppDbContext _db;
public UsersController(AppDbContext db) => _db = db;

[HttpGet]
public async Task<IActionResult> GetAll() => Ok(await _db.Users.ToListAsync());
}
14 changes: 14 additions & 0 deletions InvestmentApi/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using InvestmentApi.Models;
using Microsoft.EntityFrameworkCore;

namespace InvestmentApi.Data
{
// Fix: Inherit from DbContext to use Entity Framework Core features
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

public DbSet<User> Users => Set<User>();
public DbSet<Investment> Investments => Set<Investment>();
}
}
26 changes: 26 additions & 0 deletions InvestmentApi/InvestmentApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Content Remove="data.json" />
</ItemGroup>

<ItemGroup>
<None Include="data.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.19" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.20" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions InvestmentApi/InvestmentApi.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
</PropertyGroup>
</Project>
25 changes: 25 additions & 0 deletions InvestmentApi/InvestmentApi.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36518.9 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InvestmentApi", "InvestmentApi.csproj", "{E49B2ED4-6579-4D14-9A18-4E3F30A5E89F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E49B2ED4-6579-4D14-9A18-4E3F30A5E89F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E49B2ED4-6579-4D14-9A18-4E3F30A5E89F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E49B2ED4-6579-4D14-9A18-4E3F30A5E89F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E49B2ED4-6579-4D14-9A18-4E3F30A5E89F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0E44436F-5CE3-40DB-988A-52B7D6DB3B04}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions InvestmentApi/Models/CreateInvestmentDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace InvestmentApi.Models
{
public class CreateInvestmentDto
{
public string Ticker { get; set; } = "";
public decimal Units { get; set; }
public decimal CostBasis { get; set; }
}
}
11 changes: 11 additions & 0 deletions InvestmentApi/Models/Investment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace InvestmentApi.Models
{
public class Investment
{
public int Id { get; set; }
public int UserId { get; set; }
public string Ticker { get; set; } = "";
public decimal Units { get; set; }
public decimal CostBasis { get; set; }
}
}
10 changes: 10 additions & 0 deletions InvestmentApi/Models/SeedData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace InvestmentApi.Models
{
public class SeedData
{
public List<User> Users { get; set; } = new();
public List<Investment> Investments { get; set; } = new();
}
}
9 changes: 9 additions & 0 deletions InvestmentApi/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace InvestmentApi.Models
{
public class User
{
public int Id { get; set; }
public string Email { get; set; } = "";
public string Name { get; set; } = "";
}
}
63 changes: 63 additions & 0 deletions InvestmentApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Text.Json;
using InvestmentApi.Data;
using InvestmentApi.Models; // SeedData class
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);


builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseInMemoryDatabase("InvestmentsDb"));
builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// --- Seeding from JSON (controlled by env vars) ---
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();

// --- NEW CONFIG-BASED CODE ---
var cfg = app.Configuration.GetSection("Seed");
var seedOnStartup = cfg.GetValue("OnStartup", true);
var seedFile = cfg.GetValue("File", "data.json");
var path = Path.IsPathRooted(seedFile)
? seedFile
: Path.Combine(app.Environment.ContentRootPath, seedFile);
// --- END CONFIG BLOCK ---

if (seedOnStartup && !db.Users.Any())
{

if (File.Exists(path))
{
var json = await File.ReadAllTextAsync(path);
var seed = JsonSerializer.Deserialize<SeedData>(json,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

if (seed != null)
{
db.Users.AddRange(seed.Users);
db.Investments.AddRange(seed.Investments);
await db.SaveChangesAsync();
}
}
else
{
app.Logger.LogWarning("Seed file not found at {Path}", path);
}
}
}
// --- end seeding ---

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.MapControllers();
app.Run();
52 changes: 52 additions & 0 deletions InvestmentApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:38855",
"sslPort": 44309
}
},
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"SEED_ON_STARTUP": "true",
"SEED_FILE": "data.json"
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5110",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"SEED_ON_STARTUP": "true",
"SEED_FILE": "data.json"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7204;http://localhost:5110",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"SEED_ON_STARTUP": "true",
"SEED_FILE": "data.json"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"SEED_ON_STARTUP": "true",
"SEED_FILE": "data.json"
}
}
}
}
8 changes: 8 additions & 0 deletions InvestmentApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
14 changes: 14 additions & 0 deletions InvestmentApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Seed": {
"OnStartup": true,
"File": "data.json"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

Loading