Skip to content

Commit

Permalink
Refactor after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
giggio committed Dec 13, 2023
1 parent b6d3775 commit a0531bb
Show file tree
Hide file tree
Showing 63 changed files with 409 additions and 645 deletions.
146 changes: 146 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 100

[*.md]
max_line_length = off
trim_trailing_whitespace = false

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_prefer_collection_expression = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_predefined_type_for_member_access = true:silent
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_readonly_field = true:suggestion
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
dotnet_style_allow_multiple_blank_lines_experimental = true:silent
dotnet_code_quality_unused_parameters = all:suggestion
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_property = false:silent
dotnet_style_qualification_for_event = false:silent
dotnet_style_qualification_for_method = false:silent

[*.cs]
csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = false:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_style_throw_expression = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_space_around_binary_operators = before_and_after
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion
csharp_style_prefer_readonly_struct = true:suggestion
csharp_prefer_static_local_function = true:suggestion
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:silent
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent
csharp_style_conditional_delegate_call = true:suggestion
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_prefer_switch_expression = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_prefer_pattern_matching = true:silent
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion
1 change: 1 addition & 0 deletions PartsUnlimited.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PartsUnlimited.WebJobs.Upda
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{68C951F3-C4F7-42B5-B133-B704D4A9DF43}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
build.ps1 = build.ps1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using Microsoft.EntityFrameworkCore;
using PartsUnlimited.Models;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace PartsUnlimited.Models.Migrations
{
Expand Down
4 changes: 1 addition & 3 deletions src/PartsUnlimited.Models/PartsUnlimitedContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

namespace PartsUnlimited.Models
{
public class PartsUnlimitedContext : IdentityDbContext<ApplicationUser>, IPartsUnlimitedContext
public class PartsUnlimitedContext(DbContextOptions dbContextOptions) : IdentityDbContext<ApplicationUser>(dbContextOptions), IPartsUnlimitedContext
{
public PartsUnlimitedContext(DbContextOptions dbContextOptions) : base(dbContextOptions) { }

public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Category> Categories { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/PartsUnlimited.Models/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Dictionary<string, string> ProductDetailList
{
if (string.IsNullOrWhiteSpace(ProductDetails))
{
return new Dictionary<string, string>();
return [];
}
try
{
Expand All @@ -88,7 +88,7 @@ public Dictionary<string, string> ProductDetailList
/// </summary>
public Product()
{
OrderDetails = new List<OrderDetail>();
OrderDetails = [];
Created = DateTime.UtcNow;
}
}
Expand Down
43 changes: 18 additions & 25 deletions src/PartsUnlimited.Models/ShoppingCart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.AspNetCore.Http;
Expand All @@ -9,16 +9,10 @@

namespace PartsUnlimited.Models
{
public partial class ShoppingCart
public partial class ShoppingCart(IPartsUnlimitedContext db)
{
private readonly IPartsUnlimitedContext _db;
private string ShoppingCartId { get; set; }

public ShoppingCart(IPartsUnlimitedContext db)
{
_db = db;
}

public static ShoppingCart GetCart(IPartsUnlimitedContext db, HttpContext context)
{
var cart = new ShoppingCart(db);
Expand All @@ -29,7 +23,7 @@ public static ShoppingCart GetCart(IPartsUnlimitedContext db, HttpContext contex
public void AddToCart(Product product)
{
// Get the matching cart and product instances
var cartItem = _db.CartItems.SingleOrDefault(
var cartItem = db.CartItems.SingleOrDefault(
c => c.CartId == ShoppingCartId
&& c.ProductId == product.ProductId);

Expand All @@ -44,7 +38,7 @@ public void AddToCart(Product product)
DateCreated = DateTime.Now
};

_db.CartItems.Add(cartItem);
db.CartItems.Add(cartItem);
}
else
{
Expand All @@ -56,11 +50,11 @@ public void AddToCart(Product product)
public int RemoveFromCart(int id)
{
// Get the cart
var cartItem = _db.CartItems.Single(
var cartItem = db.CartItems.Single(
cart => cart.CartId == ShoppingCartId
&& cart.CartItemId == id);

int itemCount = 0;
var itemCount = 0;

if (cartItem != null)
{
Expand All @@ -71,7 +65,7 @@ public int RemoveFromCart(int id)
}
else
{
_db.CartItems.Remove(cartItem);
db.CartItems.Remove(cartItem);
}
}

Expand All @@ -80,28 +74,28 @@ public int RemoveFromCart(int id)

public void EmptyCart()
{
var cartItems = _db.CartItems.Where(cart => cart.CartId == ShoppingCartId);
_db.CartItems.RemoveRange(cartItems.ToArray());
var cartItems = db.CartItems.Where(cart => cart.CartId == ShoppingCartId);
db.CartItems.RemoveRange([.. cartItems]);
}

public List<CartItem> GetCartItems()
{
var cartItems = _db.CartItems.Where(cart => cart.CartId == ShoppingCartId).ToList();
var cartItems = db.CartItems.Where(cart => cart.CartId == ShoppingCartId).ToList();
//TODO: Auto population of the related product data not available until EF feature is lighted up.
foreach (var cartItem in cartItems)
{
cartItem.Product = _db.Products.Single(a => a.ProductId == cartItem.ProductId);
cartItem.Product = db.Products.Single(a => a.ProductId == cartItem.ProductId);
}

return cartItems;
}

public int GetCount()
{
int sum = 0;
var sum = 0;
//https://github.com/aspnet/EntityFramework/issues/557
// Get the count of each item in the cart and sum them up
var cartItemCounts = (from cartItems in _db.CartItems
var cartItemCounts = (from cartItems in db.CartItems
where cartItems.CartId == ShoppingCartId
select (int?)cartItems.Count);

Expand All @@ -125,9 +119,9 @@ public decimal GetTotal()

// TODO Collapse to a single query once EF supports querying related data
decimal total = 0;
foreach (var item in _db.CartItems.Where(c => c.CartId == ShoppingCartId).ToList())
foreach (var item in db.CartItems.Where(c => c.CartId == ShoppingCartId).ToList())
{
var product = _db.Products.First(a => a.ProductId == item.ProductId);
var product = db.Products.First(a => a.ProductId == item.ProductId);
total += item.Count * product.Price;
}

Expand All @@ -143,8 +137,7 @@ public int CreateOrder(Order order)
// Iterate over the items in the cart, adding the order details for each
foreach (var item in cartItems)
{
//var product = _db.Products.Find(item.ProductId);
var product = _db.Products.Single(a => a.ProductId == item.ProductId);
var product = db.Products.Single(a => a.ProductId == item.ProductId);

var orderDetail = new OrderDetail
{
Expand All @@ -157,7 +150,7 @@ public int CreateOrder(Order order)
// Set the order total of the shopping cart
orderTotal += (item.Count * product.Price);

_db.OrderDetails.Add(orderDetail);
db.OrderDetails.Add(orderDetail);
}

// Set the order's total to the orderTotal count
Expand Down Expand Up @@ -192,4 +185,4 @@ public string GetCartId(HttpContext context)
return cartId;
}
}
}
}
1 change: 0 additions & 1 deletion src/PartsUnlimited.WebJobs.ProcessOrder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Threading.Tasks;

namespace PartsUnlimited.WebJobs.ProcessOrder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PartsUnlimited.Models;
using System.Linq;
using System.Threading.Tasks;

namespace PartsUnlimited.Areas.Admin.Controllers
{
public class CustomerController : AdminController
public class CustomerController(IPartsUnlimitedContext context) : AdminController
{
private readonly IPartsUnlimitedContext _context;

public CustomerController(IPartsUnlimitedContext context)
{
_context = context;
}

public async Task<IActionResult> Index(string id)
{
if (string.IsNullOrEmpty(id))
{
return Redirect(nameof(Find));
}

var user = await _context.Users.SingleOrDefaultAsync(u => u.Id == id);
var user = await context.Users.SingleOrDefaultAsync(u => u.Id == id);

if (user == null)
{
Expand All @@ -37,7 +28,7 @@ public async Task<IActionResult> Index(string id)

public async Task<IActionResult> Find(string username, string email, string phoneNumber)
{
IQueryable<ApplicationUser> query = _context.Users;
IQueryable<ApplicationUser> query = context.Users;

if (!string.IsNullOrWhiteSpace(username))
{
Expand Down
Loading

0 comments on commit a0531bb

Please sign in to comment.