Skip to content

Commit

Permalink
Continued working on #260.
Browse files Browse the repository at this point in the history
  • Loading branch information
uncheckederror committed Feb 3, 2022
1 parent 36c0a55 commit 307e33c
Show file tree
Hide file tree
Showing 20 changed files with 887 additions and 151 deletions.
11 changes: 7 additions & 4 deletions NumberSearch.Ops/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ public async Task<IActionResult> Orders(Guid? orderId)
}
else
{
var order = await _context.Orders.Where(x => x.OrderId == orderId).AsNoTracking().FirstOrDefaultAsync();
var order = await _context.Orders.AsNoTracking().FirstOrDefaultAsync(x => x.OrderId == orderId);
var productOrders = await _context.ProductOrders.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToListAsync();
var purchasedPhoneNumbers = await _context.PurchasedPhoneNumbers.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToListAsync();
var verifiedPhoneNumbers = await _context.VerifiedPhoneNumbers.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToListAsync();
var portedPhoneNumbers = await _context.PortedPhoneNumbers.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToListAsync();
var shipments = await _context.ProductShipments.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToArrayAsync();

// Rather than using a completely generic concept of a product we have two kind of products: phone number and everything else.
// This is done for performance because we have 300k phone numbers where the DialedNumber is the primary key and perhaps 20 products where a guid is the key.
Expand Down Expand Up @@ -160,7 +161,7 @@ public async Task<IActionResult> Orders(Guid? orderId)
PurchasedPhoneNumbers = purchasedPhoneNumbers
};

return View("OrderEdit", new EditOrderResult { Order = order, Cart = cart });
return View("OrderEdit", new EditOrderResult { Order = order, ProductShipments = shipments, Cart = cart });
}
}

Expand Down Expand Up @@ -281,6 +282,7 @@ public async Task<IActionResult> OrderUpdate(Order order)
var purchasedPhoneNumbers = await _context.PurchasedPhoneNumbers.AsNoTracking().Where(x => x.OrderId == order.OrderId).ToListAsync();
var verifiedPhoneNumbers = await _context.VerifiedPhoneNumbers.AsNoTracking().Where(x => x.OrderId == order.OrderId).ToListAsync();
var portedPhoneNumbers = await _context.PortedPhoneNumbers.AsNoTracking().Where(x => x.OrderId == order.OrderId).ToListAsync();
var shipments = await _context.ProductShipments.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToArrayAsync();

// Rather than using a completely generic concept of a product we have two kind of products: phone number and everything else.
// This is done for performance because we have 300k phone numbers where the DialedNumber is the primary key and perhaps 20 products where a guid is the key.
Expand Down Expand Up @@ -319,14 +321,15 @@ public async Task<IActionResult> OrderUpdate(Order order)
PurchasedPhoneNumbers = purchasedPhoneNumbers
};

return View("OrderEdit", new EditOrderResult { Order = order, Cart = cart, Message = "Order updated successfully! 😘", AlertType = "alert-success" });
return View("OrderEdit", new EditOrderResult { Order = order, ProductShipments = shipments, Cart = cart, Message = "Order updated successfully! 😘", AlertType = "alert-success" });
}
catch (Exception ex)
{
var productOrders = await _context.ProductOrders.Where(x => x.OrderId == order.OrderId).ToListAsync();
var purchasedPhoneNumbers = await _context.PurchasedPhoneNumbers.Where(x => x.OrderId == order.OrderId).ToListAsync();
var verifiedPhoneNumbers = await _context.VerifiedPhoneNumbers.Where(x => x.OrderId == order.OrderId).ToListAsync();
var portedPhoneNumbers = await _context.PortedPhoneNumbers.Where(x => x.OrderId == order.OrderId).ToListAsync();
var shipments = await _context.ProductShipments.Where(x => x.OrderId == order.OrderId).AsNoTracking().ToArrayAsync();

// Rather than using a completely generic concept of a product we have two kind of products: phone number and everything else.
// This is done for performance because we have 300k phone numbers where the DialedNumber is the primary key and perhaps 20 products where a guid is the key.
Expand Down Expand Up @@ -365,7 +368,7 @@ public async Task<IActionResult> OrderUpdate(Order order)
PurchasedPhoneNumbers = purchasedPhoneNumbers
};

return View("OrderEdit", new EditOrderResult { Order = order, Cart = cart, Message = $"Failed to update this order! 😠\r\n{ex.Message}\r\n{ex.StackTrace}", AlertType = "alert-danger" });
return View("OrderEdit", new EditOrderResult { Order = order, ProductShipments = shipments, Cart = cart, Message = $"Failed to update this order! 😠\r\n{ex.Message}\r\n{ex.StackTrace}", AlertType = "alert-danger" });
}
}

Expand Down
205 changes: 205 additions & 0 deletions NumberSearch.Ops/Controllers/ProductItemsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using AccelerateNetworks.Operations;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

using System;
using System.Linq;
using System.Threading.Tasks;

namespace NumberSearch.Ops.Controllers
{
public class ProductItemsController : Controller
{
private readonly numberSearchContext _context;
private readonly UserManager<IdentityUser> _userManager;
private readonly IConfiguration _configuration;
private readonly string _postgresql;

public ProductItemsController(numberSearchContext context, UserManager<IdentityUser> userManager, IConfiguration config)
{
_context = context;
_userManager = userManager;
_configuration = config;
_postgresql = _configuration.GetConnectionString("PostgresqlProd");
}

// GET: ProductItems
[Authorize]
[HttpGet("ProductItems")]
public async Task<IActionResult> Index()
{
return View(await _context.ProductItems.ToListAsync());
}

// GET: ProductItems/Details/5
[Authorize]
[HttpGet("ProductItems/Details/{id}")]
public async Task<IActionResult> Details(Guid? id)
{
if (id == null)
{
return NotFound();
}

var productItem = await _context.ProductItems
.FirstOrDefaultAsync(m => m.ProductItemId == id);
if (productItem == null)
{
return NotFound();
}

return View(productItem);
}

// GET: ProductItems/Create
[Authorize]
[HttpGet("ProductItems/Create")]
[HttpGet("ProductItems/Create/{shipmentId}")]
public async Task<IActionResult> Create(Guid? shipmentId)
{
if (shipmentId is not null)
{
var shipment = await _context.ProductShipments.FirstOrDefaultAsync(x => x.ProductShipmentId == shipmentId);
var countExistingItems = await _context.ProductItems.Where(x => x.ProductShipmentId == shipment.ProductShipmentId).CountAsync();
var itemsToCreate = countExistingItems != shipment.Quantity ? shipment.Quantity - countExistingItems : 0;

if (itemsToCreate > 0)
{
for (var i = 0; i < shipment.Quantity; i++)
{
var item = new ProductItem
{
ProductShipmentId = shipment.ProductShipmentId,
OrderId = shipment.OrderId,
ProductId = shipment?.ProductId ?? Guid.Empty,
ProductItemId = Guid.NewGuid(),
};

_context.ProductItems.Add(item);
}

_context.SaveChanges();
}

return Redirect($"/ProductShipments/Edit/{shipment.ProductShipmentId}");
}
else
{
return View();
}
}

// POST: ProductItems/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[Authorize]
[HttpPost("ProductItems")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ProductItemId,ProductId,ProductShipmentId,OrderId,SerialNumber,MACAddress,Condition,DateCreated,DateUpdated")] ProductItem productItem)
{
if (ModelState.IsValid)
{
productItem.ProductItemId = Guid.NewGuid();
_context.Add(productItem);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(productItem);
}

// GET: ProductItems/Edit/5
[Authorize]
[HttpGet("ProductItems/Edit/{id}")]
public async Task<IActionResult> Edit(Guid? id)
{
if (id == null)
{
return NotFound();
}

var productItem = await _context.ProductItems.FindAsync(id);
if (productItem == null)
{
return NotFound();
}
return View(productItem);
}

// POST: ProductItems/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[Authorize]
[HttpPost("ProductItems/Edit/{id}")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("ProductItemId,ProductId,ProductShipmentId,OrderId,SerialNumber,MACAddress,Condition,DateCreated,DateUpdated")] ProductItem productItem)
{
if (id != productItem.ProductItemId)
{
return NotFound();
}

if (ModelState.IsValid)
{
try
{
_context.Update(productItem);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductItemExists(productItem.ProductItemId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction($"/ProductItems/Details/{productItem.ProductItemId}");
}
return View(productItem);
}

// GET: ProductItems/Delete/5
[Authorize]
[HttpGet("ProductItems/Delete/{id}")]
public async Task<IActionResult> Delete(Guid? id)
{
if (id == null)
{
return NotFound();
}

var productItem = await _context.ProductItems
.FirstOrDefaultAsync(m => m.ProductItemId == id);
if (productItem == null)
{
return NotFound();
}

return View(productItem);
}

// POST: ProductItems/Delete/5
[Authorize]
[HttpPost("ProductItems/Delete/{id}")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(Guid id)
{
var productItem = await _context.ProductItems.FindAsync(id);
_context.ProductItems.Remove(productItem);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}

private bool ProductItemExists(Guid id)
{
return _context.ProductItems.Any(e => e.ProductItemId == id);
}
}
}
36 changes: 29 additions & 7 deletions NumberSearch.Ops/Controllers/ProductShipmentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,33 @@ public async Task<IActionResult> Details(Guid? id)
// GET: ProductShipments/Create
[Authorize]
[HttpGet("ProductShipments/Create")]
public async Task<IActionResult> Create()
public async Task<IActionResult> Create(Guid? orderId)
{
var products = await _context.Products.ToListAsync();
var create = new Ops.Models.CreateProductShipment

if (orderId is not null)
{
var order = await _context.Orders.FirstOrDefaultAsync(m => m.OrderId == orderId);

return View(new Ops.Models.CreateProductShipment
{
Products = products,
Shipment = new ProductShipment
{
OrderId = order.OrderId,
BillingClientId = order.BillingClientId,
ShipmentType = "Assigned"
}
});
}
else
{
Products = products
};
return View(create);
return View(new Ops.Models.CreateProductShipment
{
Products = products
});
}

}

// POST: ProductShipments/Create
Expand Down Expand Up @@ -114,7 +133,8 @@ public async Task<IActionResult> Edit(Guid? id)
{
return NotFound();
}
return View(productShipment);
var productItems = await _context.ProductItems.Where(x => x.ProductShipmentId == productShipment.ProductShipmentId).ToListAsync();
return View(new EditProductShipment { ProductItems = productItems, Shipment = productShipment });
}

// POST: ProductShipments/Edit/5
Expand Down Expand Up @@ -168,7 +188,9 @@ public async Task<IActionResult> Edit(Guid id, [Bind("ProductShipmentId,ProductI
}
return RedirectToAction(nameof(Index));
}
return View(productShipment);

var productItems = await _context.ProductItems.Where(x => x.ProductShipmentId == productShipment.ProductShipmentId).ToListAsync();
return View(new EditProductShipment { ProductItems = productItems, Shipment = productShipment });
}

// GET: ProductShipments/Delete/5
Expand Down
1 change: 1 addition & 0 deletions NumberSearch.Ops/Models/EditOrderResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class EditOrderResult
{
public Order Order { get; set; }
public Cart Cart { get; set; }
public ProductShipment[] ProductShipments { get; set; }
public string Message { get; set; }
public string AlertType { get; set; }
}
Expand Down
11 changes: 11 additions & 0 deletions NumberSearch.Ops/Models/EditProductShipment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using AccelerateNetworks.Operations;

using System.Collections.Generic;

namespace NumberSearch.Ops.Models;

public class EditProductShipment
{
public IEnumerable<ProductItem> ProductItems { get; set; }
public ProductShipment Shipment { get; set; }
}
6 changes: 6 additions & 0 deletions NumberSearch.Ops/NumberSearch.Ops.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" />
<PackageReference Include="prometheus-net.AspNetCore" Version="5.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
Expand Down
1 change: 0 additions & 1 deletion NumberSearch.Ops/PostgreSQL/Product.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;

namespace AccelerateNetworks.Operations
{
Expand Down
17 changes: 17 additions & 0 deletions NumberSearch.Ops/PostgreSQL/ProductItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace AccelerateNetworks.Operations
{
public class ProductItem
{
public Guid ProductItemId { get; set; }
public Guid ProductId { get; set; }
public Guid? ProductShipmentId { get; set; }
public Guid? OrderId { get; set; }
public string SerialNumber { get; set; }
public string MACAddress { get; set; }
public string Condition { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
}
}
Loading

0 comments on commit 307e33c

Please sign in to comment.