Skip to content

Commit

Permalink
offloading weight from controller to models
Browse files Browse the repository at this point in the history
  • Loading branch information
METrimble committed Mar 3, 2025
1 parent c5cf9e3 commit 15d3666
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 53 deletions.
55 changes: 9 additions & 46 deletions MyCloset/Controllers/MyClosetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ public class MyClosetController : Controller
// My Closet Model used to send multiple parmaters into the view
static ViewModels.MyClosetViewModel MyClosetParam = new ViewModels.MyClosetViewModel();

// Is Base Top
private static bool IsBaseTop(ClothingItem ClothingItem)
{
if(ClothingItem.Type == ClothingType.Top && ClothingItem.IsBase)
return true;
return false;
}

// Is Base Bottom
private static bool IsBaseBottom(ClothingItem ClothingItem)
{
if (ClothingItem.Type == ClothingType.Bottoms && ClothingItem.IsBase)
return true;
return false;
}

// Constructor
public MyClosetController(Context context, BlobServiceClient blobServiceClient)
{
Expand All @@ -53,16 +37,17 @@ public async Task<IActionResult> Closet()

// Error handling
if (Closet == null ||
Closet.Find(IsBaseTop) == null ||
Closet.Find(IsBaseBottom) == null)
Closet.Find(c => c.IsBaseTop()) == null ||
Closet.Find(c => c.IsBaseBottom()) == null)
{
// TODO error
}

// Create the MyCloset View Model
MyClosetParam.ClothingItems = Closet;
MyClosetParam.BlobContainerUri = _containerClient.Uri;
MyClosetParam.Doll = new Doll(Closet.Find(IsBaseTop), Closet.Find(IsBaseBottom));
MyClosetParam.Doll = new Doll(Closet.Find(c => c.IsBaseTop()),
Closet.Find(c => c.IsBaseBottom()));

return View("Closet", MyClosetParam);
}
Expand All @@ -84,39 +69,17 @@ public ActionResult OnSelectClothingItem(int Id)
}

// If clothing exists on the doll, remove it
if (MyClosetParam.Doll.ClothingItems.IndexOf(SelectedClothingItem) != -1)
if (MyClosetParam.Doll.ClothingItemEquipped(SelectedClothingItem))
{
MyClosetParam.Doll.ClothingItems.Remove(SelectedClothingItem);

// If this is a Top or Bottom and removing it left the doll without either,
// Make sure the base clothing is added back
if (SelectedClothingItem.Type == ClothingType.Bottoms &&
MyClosetParam.Doll.ClothingItems.Find(c => c.Type == ClothingType.Bottoms) == null)
MyClosetParam.Doll.ClothingItems.Add(MyClosetParam.ClothingItems.Find(IsBaseBottom));

else if (SelectedClothingItem.Type == ClothingType.Top &&
MyClosetParam.Doll.ClothingItems.Find(c => c.Type == ClothingType.Top) == null)
MyClosetParam.Doll.ClothingItems.Add(MyClosetParam.ClothingItems.Find(IsBaseTop));
MyClosetParam.Doll.RemoveClothingItemFromDoll(SelectedClothingItem,
MyClosetParam.ClothingItems.Find(c => c.IsBaseBottom()),
MyClosetParam.ClothingItems.Find(c => c.IsBaseTop()));
}

// If clothing does not exist on the doll, add it
else
{
MyClosetParam.Doll.ClothingItems.Add(SelectedClothingItem);

// See if base clothing is on the doll
ClothingItem BaseBottom = null;
ClothingItem BaseTop = null;

BaseBottom = MyClosetParam.Doll.ClothingItems.Find(IsBaseBottom);
BaseTop = MyClosetParam.Doll.ClothingItems.Find(IsBaseTop);

// If this is a Top or Bottom, make sure the base clothing is removed after adding new
if (SelectedClothingItem.Type == ClothingType.Bottoms && BaseBottom != null)
MyClosetParam.Doll.ClothingItems.Remove(BaseBottom);

else if (SelectedClothingItem.Type == ClothingType.Top && BaseTop != null)
MyClosetParam.Doll.ClothingItems.Remove(BaseTop);
MyClosetParam.Doll.AddClothingItemToDoll(SelectedClothingItem);
}

return PartialView("_Doll", new Tuple<Doll, Uri>(MyClosetParam.Doll, MyClosetParam.BlobContainerUri));
Expand Down
20 changes: 19 additions & 1 deletion MyCloset/Models/ClothingItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,23 @@ public class ClothingItem
public bool IsBase { get; set; }
public string ImageName { get; set; }
public string CroppedImageName { get; set; }
}

// Is Base Top
public bool IsBaseTop()
{
if (this.Type == ClothingType.Top && this.IsBase)
return true;

return false;
}

// Is Base Bottom
public bool IsBaseBottom()
{
if (this.Type == ClothingType.Bottoms && this.IsBase)
return true;

return false;
}
}
}
52 changes: 49 additions & 3 deletions MyCloset/Models/Doll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Doll
public List<ClothingItem> ClothingItems { get; set; }

// Default Constructor
public Doll(ClothingItem BaseTop,
ClothingItem BaseBottom)
public Doll(ClothingItem BaseTop,
ClothingItem BaseBottom)
{
ClothingItems = new List<ClothingItem>();

Expand All @@ -18,6 +18,52 @@ public Doll(ClothingItem BaseTop,
ClothingItems.Add(BaseBottom);
}

// Function to select / replace clothing items
// Remove clothing item from doll
public void RemoveClothingItemFromDoll(ClothingItem SelectedClothingItem,
ClothingItem BaseBottom,
ClothingItem BaseTop)
{
this.ClothingItems.Remove(SelectedClothingItem);

// If this is a Top or Bottom and removing it left the doll without either,
// add the base clothing is added back
if (SelectedClothingItem.Type == ClothingType.Bottoms &&
this.ClothingItems.Find(c => c.Type == ClothingType.Bottoms) == null)
{
this.ClothingItems.Add(BaseBottom);
}

else if (SelectedClothingItem.Type == ClothingType.Top &&
this.ClothingItems.Find(c => c.Type == ClothingType.Top) == null)
{
this.ClothingItems.Add(BaseTop);
}
}

// Add clothing item to doll
public void AddClothingItemToDoll(ClothingItem SelectedClothingItem)
{
this.ClothingItems.Add(SelectedClothingItem);

// See if base clothing is on the doll
ClothingItem BaseBottom = this.ClothingItems.Find(c => c.IsBaseBottom());
ClothingItem BaseTop = this.ClothingItems.Find(c => c.IsBaseTop());

// If this is a Top or Bottom, make sure the base clothing is removed after adding new
if (SelectedClothingItem.Type == ClothingType.Bottoms && BaseBottom != null)
this.ClothingItems.Remove(BaseBottom);

else if (SelectedClothingItem.Type == ClothingType.Top && BaseTop != null)
this.ClothingItems.Remove(BaseTop);
}

// Clothing Item currently equipped on doll
public bool ClothingItemEquipped(ClothingItem SelectedClothingItem)
{
if (this.ClothingItems.IndexOf(SelectedClothingItem) == -1)
return false;

return true;
}
}
}
1 change: 0 additions & 1 deletion MyCloset/MyCloset.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.2">
Expand Down
2 changes: 0 additions & 2 deletions MyCloset/Views/MyCloset/Closet.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@using MyCloset.ViewModels
@using System.Web.Mvc.Ajax

@model MyCloset.ViewModels.MyClosetViewModel
@{
Expand Down Expand Up @@ -28,7 +27,6 @@
type: "POST",
url: "MyCloset/OnSelectClothingItem",
data: {Id: clicked.val()},
async: false,
success: function(res)
{
console.log("successful");
Expand Down

0 comments on commit 15d3666

Please sign in to comment.