Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

23 changes: 6 additions & 17 deletions src/BangazonUserAuth/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ public class AccountController : Controller
private readonly ISmsSender _smsSender;
private readonly ILogger _logger;
private ApplicationDbContext newContext;
private BangazonWebContext context;

public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory,
ApplicationDbContext ctx1,
BangazonWebContext ctx2)
ApplicationDbContext ctx1)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<AccountController>();
newContext = ctx1;
context = ctx2;
}


Expand Down Expand Up @@ -113,22 +110,14 @@ public async Task<IActionResult> Register(RegisterViewModel model, string return
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
//Create a new customer with first and last name properties
Customer customer = new Customer();
customer.FirstName = model.FirstName;
customer.LastName = model.LastName;
//Create a new Customer Controller instance
//Use the new Customer method to post the Customer instance to the old database
//Get the Customer Id back
CustomersController customersController = new CustomersController(_userManager, newContext, context);
int ActiveCustomerId = customersController.New(customer);
//Set the customer Id on the new Application User
//Resume logic that's already written
var user = new ApplicationUser
{
CustomerId = ActiveCustomerId,
UserName = model.Email,
Email = model.Email
Email = model.Email,
FirstName = model.FirstName,
LastName = model.LastName
};

var result = await _userManager.CreateAsync(user, model.Password);
Expand Down Expand Up @@ -159,7 +148,7 @@ public async Task<IActionResult> LogOff()
{
await _signInManager.SignOutAsync();
_logger.LogInformation(4, "User logged out.");
return RedirectToAction(nameof(ProductsController.Index), "Home");
return RedirectToAction(nameof(ProductsController.Index), "");
}

//
Expand Down Expand Up @@ -484,7 +473,7 @@ private IActionResult RedirectToLocal(string returnUrl)
}
else
{
return RedirectToAction(nameof(ProductsController.Index), "Home");
return RedirectToAction(nameof(ProductsController.Index), "");
}
}

Expand Down
118 changes: 70 additions & 48 deletions src/BangazonUserAuth/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Authorization;

namespace BangazonUserAuth.Controllers
{
Expand All @@ -20,78 +22,78 @@ public class CustomersController : Controller
private readonly UserManager<ApplicationUser> _userManager;

private ApplicationDbContext newContext;
private BangazonWebContext context;
public CustomersController(UserManager<ApplicationUser> userManager, ApplicationDbContext ctx1, BangazonWebContext ctx2)
public CustomersController(UserManager<ApplicationUser> userManager, ApplicationDbContext ctx1)
{
_userManager = userManager;
newContext = ctx1;
context = ctx2;
}

// This task retrieves the currently authenticated user
private Task<ApplicationUser> GetCurrentUserAsync() => _userManager.GetUserAsync(HttpContext.User);

//Method Name: Overloaded New
//Purpose of the Method: Will take an optional parameter of the type "customer." Takes form data then checks its validity. Post to customer table in database then redirects to home page.
//Arguments in Method: A new customer object taken from the form of Customer/New.cshtml.
[HttpPost]
[ValidateAntiForgeryTokenAttribute]
public int New(Customer customer)
{
if (ModelState.IsValid)
{
context.Add(customer);
context.SaveChangesAsync();
Customer activeCustomer = context.Customer.Single(c => c.FirstName == customer.FirstName && c.LastName == customer.LastName);
if (activeCustomer != null)
{
return activeCustomer.CustomerId;

} else
{
return 0;
}
}
return 0;
}


//Method Name: ShoppingCart
//Purpose of the Method:
//Gets all LineItems on active order and give data to the returned View.
//Gets all PaymentTypes of selected Customer and give data to the returned View.
//This method returns the Customer/ShoppingCart view.
//Arguments in Method: None.
[HttpGet]
[Authorize]
public async Task<IActionResult> ShoppingCart()
{
var user = await GetCurrentUserAsync();
var CustomerId = user.CustomerId;
var activeOrder = await context.Order.Where(o => o.IsCompleted == false && o.CustomerId==CustomerId).SingleOrDefaultAsync();
var activeOrder = await newContext.Order.Where(o => o.IsCompleted == false && o.User==user).SingleOrDefaultAsync();

ShoppingCartViewModel model = new ShoppingCartViewModel(_userManager, newContext, context);
ShoppingCartViewModel model = new ShoppingCartViewModel(_userManager, newContext);

if (activeOrder == null)
{
var product = new Product(){Description="You have no products in your cart!", Name=""};
model.Products = new List<Product>();
model.Products.Add(product);
model.ActiveCustomerId = CustomerId;
model.ListOfPaymentTypes = newContext.PaymentType
.Where(pt => pt.User == user)
.AsEnumerable()
.Select(pt => new SelectListItem
{
Text = $"{pt.FirstName} {pt.LastName} {pt.Processor} {pt.ExpirationDate}",
Value = pt.PaymentTypeId.ToString()
}).ToList();

model.ListOfPaymentTypes.Insert(0, new SelectListItem

{
Text = "Choose Payment Type"
});
return View(model);
}

List<LineItem> LineItemsOnActiveOrder = context.LineItem.Where(li => li.OrderId == activeOrder.OrderId).ToList();
List<LineItem> LineItemsOnActiveOrder = newContext.LineItem.Where(li => li.OrderId == activeOrder.OrderId).ToList();

List<Product> ListOfProducts = new List<Product>();

double CartTotal = 0;

for(var i = 0; i < LineItemsOnActiveOrder.Count(); i++)
{
ListOfProducts.Add(context.Product.Where(p => p.ProductId == LineItemsOnActiveOrder[i].ProductId).SingleOrDefault());
CartTotal += context.Product.Where(p => p.ProductId == LineItemsOnActiveOrder[i].ProductId).SingleOrDefault().Price;
ListOfProducts.Add(newContext.Product.Where(p => p.ProductId == LineItemsOnActiveOrder[i].ProductId).SingleOrDefault());
CartTotal += newContext.Product.Where(p => p.ProductId == LineItemsOnActiveOrder[i].ProductId).SingleOrDefault().Price;
}

model.ListOfPaymentTypes = newContext.PaymentType
.Where(pt => pt.User == user)
.AsEnumerable()
.Select(pt => new SelectListItem
{
Text = $"{pt.FirstName} {pt.LastName} {pt.Processor} {pt.ExpirationDate}",
Value = pt.PaymentTypeId.ToString()
}).ToList();

model.ListOfPaymentTypes.Insert(0, new SelectListItem

{
Text = "Choose Payment Type"
});
model.CartTotal = CartTotal;
model.Products = ListOfProducts;

Expand All @@ -103,10 +105,12 @@ public async Task<IActionResult> ShoppingCart()
//Purpose of the Method: Method should take you to the Payment view with form to add Payment.
//Arguments in Method: None.
[HttpGet]
public IActionResult Payment()
[Authorize]
public async Task<IActionResult> Payment()
{
var user = await GetCurrentUserAsync();

PaymentTypeViewModel model = new PaymentTypeViewModel(_userManager, newContext, context);
PaymentTypeViewModel model = new PaymentTypeViewModel(_userManager, newContext);

return View(model);
}
Expand All @@ -115,43 +119,61 @@ public IActionResult Payment()
//Purpose of the Method: This is the Overloaded method that actually adds the payments to the Db.
//Arguments in Method: Takes a new PaymentType object from the form provided and posts it to the database.
[HttpPost]
[Authorize]
public async Task<IActionResult> Payment([FromForm]PaymentType paymentType)
{
if (ModelState.IsValid)
{
context.Add(paymentType);
await context.SaveChangesAsync();
return RedirectToAction("ShoppingCart");
ModelState.Remove("paymentType.User");

if (ModelState.IsValid)
{
/*
If all other properties validation, then grab the
currently authenticated user and assign it to the
product before adding it to the db context
*/
var user = await GetCurrentUserAsync();
paymentType.User = user;

newContext.Add(paymentType);

await newContext.SaveChangesAsync();
return RedirectToAction("ShoppingCart");
}

return BadRequest();
}

//Method Name: OrderCompleted
//Purpose of the Method: To change the isCompleted bool from false to true on the active order for this customer and direct the user to the OrderCompleted view.
//Arguments in Method: None.
[HttpGet]
[Authorize]
public IActionResult OrderCompleted()
{

BaseViewModel model = new BaseViewModel(_userManager, newContext, context);
BaseViewModel model = new BaseViewModel(_userManager, newContext);

return View(model);
}
//Method Name: SubmitOrder
//Purpose of the Method: To change the isCompleted bool from false to true on the active order for this customer and direct the user to the OrderCompleted view.
//Arguments in Method: None.
[HttpPatch]
[Authorize]
public async Task<IActionResult> SubmitOrder([FromRoute]int id)
{
if (id == 0)
{
return NotFound();
}
var user = await GetCurrentUserAsync();
var CustomerId = user.CustomerId;
var activeOrder = await context.Order.Where(o => o.IsCompleted == false && o.CustomerId==CustomerId)
var activeOrder = await newContext.Order.Where(o => o.IsCompleted == false && o.User==user)
.SingleOrDefaultAsync();
activeOrder.PaymentType = context.PaymentType.Where(pt => pt.PaymentTypeId == id).SingleOrDefault();
activeOrder.PaymentType = newContext.PaymentType.Where(pt => pt.PaymentTypeId == id).SingleOrDefault();
activeOrder.DateCompleted = DateTime.Today;
activeOrder.IsCompleted = true;
context.Update(activeOrder);
await context.SaveChangesAsync();
newContext.Update(activeOrder);
await newContext.SaveChangesAsync();

return Json(id);
}
Expand Down
Loading