Skip to content

Instantly share code, notes, and snippets.

@jesslilly
Last active August 11, 2023 17:09
Show Gist options
  • Save jesslilly/821c1b97819ae1e883a9781d6ef81577 to your computer and use it in GitHub Desktop.
Save jesslilly/821c1b97819ae1e883a9781d6ef81577 to your computer and use it in GitHub Desktop.
How would you refactor this code?

How would you refactor this code?

  1. What major and minor problems does this code have?
  2. How would you refactor it?

Please copy the code and make all the edits to correct these problems.

Tip: You could paste the code into Visual Studio to get better syntax highlighting and hints.

(Assume the code is .NET 7 in an ASP.NET MVC project.)

using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace MyApplication.Web.Controllers
{
public class ProductControl : Controller
{
public async Task<IActionResult> Edit()
{
//ProductEditViewModel a = new ProductEditViewModel();
ProductEditViewModel a = new ProductEditViewModel();
product_edits model = new product_edits();
a.queryDate = DateTime.Now.ToShortDateString();
var Database = new MyAppDbContext();
var temp = Database.Product.Select(pro => new product_edits
{
ProductId = pro.ProductId.ToString(),
})
.ToList();
a.ProductEdits = new Collection<product_edits>(temp);
foreach (var aProductEdit in a.ProductEdits)
{
aProductEdit.brand = Database.Product
.SingleOrDefault(pro2 => pro2.ProductId == Convert.ToInt16(aProductEdit.ProductId)) .BrandName;
aProductEdit.Color = Database.Product.SingleOrDefault(pro2 => pro2.ProductId == Convert.ToInt16(aProductEdit.ProductId)).Color;
}
foreach (var aProductEdit in a.ProductEdits) {
if (aProductEdit.brand == "Carrier") {
aProductEdit.Sale = "This is 10% off!";
if (aProductEdit.brand == "Mitsubishi")
{
aProductEdit.Sale = "This is 20% off!";
}
}
}
a.queryDate = DateTime.Now.ToShortDateString();
return View("Edit", a);
}
[HttpPost("Edit")]
public IActionResult Edit_Product(ProductEditViewModel Products)
{
var Database = new MyAppDbContext();
var db = Database.Product
.Single(x => x.ProductId.ToString() == Products.ProductEdits[0].ProductId);
//Database.Entry(db).CurrentValues.SetValues(Products);
//await Database.SaveChangesAsync();
// todo I could not get to work.
string productsCmd = @"UPDATE Products"
+ "SET Name = Quantity = {quantity}"
+ string.Format("WHERE Id = {0}", Products.ProductEdits[0].ProductId);
int id = Database.Database.ExecuteSql(FormattableStringFactory.Create(productsCmd));
Console.WriteLine(string.Format("Edited, {0}", Products.ProductEdits[0].ProductId));
return View(Products);
}
}
}
public class ProductEditViewModel
{
public string queryDate { get; set; }
public Collection<product_edits> ProductEdits { get; set; }
}
public class product_edits
{
public string ProductId { get; set; }
public string brand { get; set; }
public string Color { get; set; }
public string Sale { get; set; }
}
public class MyAppDbContext : DbContext
{
public HashSet<Product> Product { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string BrandName { get; set; }
public string Color { get; set; }
public string Quantity { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment