Skip to content

Commit

Permalink
Blob images working
Browse files Browse the repository at this point in the history
SQL database connected
app service published
  • Loading branch information
METrimble committed Mar 1, 2025
1 parent 653f73b commit fe3d77f
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 41 deletions.
31 changes: 23 additions & 8 deletions MyCloset/Controllers/MyClosetController.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
using Microsoft.AspNetCore.Mvc;
using MyCloset.Data;
using Microsoft.EntityFrameworkCore;
using Azure.Storage.Blobs;
using MyCloset.Models;
using MyCloset.ViewModels;

namespace MyCloset.Controllers
{
public class MyClosetController : Controller
{
// Database context
private readonly Context _context;

// Database context
public MyClosetController(Context context)
// Blob Account Container and Stoage
private const string ContainerName = "mycloset";
private readonly BlobServiceClient _blobServiceClient;
private readonly BlobContainerClient _containerClient;

// Constructor
public MyClosetController(Context context, BlobServiceClient blobServiceClient)
{
_context = context;

_blobServiceClient = blobServiceClient;
_containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
}

// Retrieve all clothing items from the database
// Use asynchronous action to retrieve data from the database
// Build the default closet view
public async Task<IActionResult> Closet()
{
// Clothing Items database set
var ClothingItems = await _context.ClothingItems.ToListAsync();

// Wait until we get data back before returning the view
return View(ClothingItems);
var Closet = await _context.ClothingItems.ToListAsync();

// Create the MyCloset View Model
var MyCloset = new ViewModels.MyClosetViewModel();
MyCloset.ClothingItems = Closet;
MyCloset.BlobContainerUri = _containerClient.Uri;

return View(MyCloset);
}
}
}
1 change: 0 additions & 1 deletion MyCloset/Data/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace MyCloset.Data
{
public class Context : DbContext
{
public Context() { }
public Context(DbContextOptions<Context> options)
: base(options)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions MyCloset/Migrations/20250227182612_CroppedImageUrl-New-Col.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MyCloset.Migrations
{
/// <inheritdoc />
public partial class CroppedImageUrlNewCol : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CroppedImageUrl",
table: "ClothingItems",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CroppedImageUrl",
table: "ClothingItems");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions MyCloset/Migrations/20250301024623_Removing URL Columns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MyCloset.Migrations
{
/// <inheritdoc />
public partial class RemovingURLColumns : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CroppedImageUrl",
table: "ClothingItems");

migrationBuilder.DropColumn(
name: "ImageUrl",
table: "ClothingItems");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CroppedImageUrl",
table: "ClothingItems",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");

migrationBuilder.AddColumn<string>(
name: "ImageUrl",
table: "ClothingItems",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
}
}
}
4 changes: 0 additions & 4 deletions MyCloset/Migrations/ContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<string>("ImageUrl")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
Expand Down
1 change: 0 additions & 1 deletion MyCloset/Models/ClothingItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public class ClothingItem
public string Name { get; set; }
public string Type { get; set; }
public int StackType { get; set; }
public string ImageUrl { get; set; }
}
}
19 changes: 19 additions & 0 deletions MyCloset/Models/Doll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace MyCloset.Models
{
// There is only one instance of the doll object
public class Doll
{
// List of the clothing items currently displayed on the doll
// only one of each clothing type will be selected at each time, enforced by the selector functions
public List<ClothingItem> ClothingItems { get; set; }

// Default Constructor
public Doll()
{
// TODO: add default clothing
ClothingItems = new List<ClothingItem>();
}

// Function to select / replace clothing items
}
}
3 changes: 3 additions & 0 deletions MyCloset/MyCloset.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.2">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -17,6 +19,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.10.0" />
</ItemGroup>

</Project>
23 changes: 18 additions & 5 deletions MyCloset/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Azure.Identity;
using Microsoft.EntityFrameworkCore;
using MyCloset.Data;
using Microsoft.Extensions.Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

/************************************
* Documentation:
Expand All @@ -11,28 +15,38 @@
// Add services to the container.
builder.Services.AddControllersWithViews();

var connection = String.Empty;
var Connection = String.Empty;
if (builder.Environment.IsDevelopment())
{
// Set the Azure SQL Database connection string
builder.Configuration.AddEnvironmentVariables().AddJsonFile("appsettings.Development.json");
connection = builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING");
Connection = builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING");
}
else
{
connection = Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING");
// Uses Environment variable set in the Azure App Service
Connection = Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING");
}

// Use the connection string to register the EF Core DbContext class
builder.Services.AddDbContext<Context>(options =>
options.UseSqlServer(connection));
options.UseSqlServer(Connection));

// Connect to Azure Blob Storage Account using default azure credential
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddBlobServiceClient(
new Uri("https://mycloset.blob.core.windows.net"));
clientBuilder.UseCredential(new DefaultAzureCredential());
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");

// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
Expand All @@ -48,5 +62,4 @@
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");


app.Run();
Loading

0 comments on commit fe3d77f

Please sign in to comment.