Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abw stretch #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Bangazon/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public async Task<ActionResult> Search(string searchBar)

return View(products);
}


return View();
}
Expand Down
42 changes: 39 additions & 3 deletions Bangazon/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,31 @@ public async Task<ActionResult> Index()
return View(viewModel);

}

// GET: Orders/Details/5
public ActionResult Details(int id)
public async Task<ActionResult> Details(int id)
{
return View();
var user = await GetCurrentUserAsync();

var order = await _context.OrderProduct
.Where(o => o.Order.UserId == user.Id)
.Where(op => op.OrderId == id)
.Include(o => o.Order)
.Include(p => p.Product)
.Include(op => op.Order.OrderProducts)
.ToListAsync();

var orderId = await _context.Order.FirstOrDefaultAsync(o => o.UserId == user.Id && o.PaymentTypeId != null);


var viewModel = new ShoppingCartViewModel()
{
OrderId = orderId.OrderId,
Products = order
};



return View(viewModel);
}

// GET: Orders/Create
Expand Down Expand Up @@ -205,6 +225,22 @@ public async Task<ActionResult> Delete(int id, OrderProduct orderProduct)
}
}

public async Task<ActionResult> ViewOrder(int id)
{
var user = await GetCurrentUserAsync();

var viewModel = new ViewOrderViewModel();

var order = await _context.Order
.Include(o => o.OrderProducts)
.ThenInclude(op => op.Product)
.FirstOrDefaultAsync(o => o.OrderId == id);

viewModel.Order = order;


return View(viewModel);
}

// delete for the entire order and all it's corresponding products

Expand Down
24 changes: 23 additions & 1 deletion Bangazon/Controllers/PaymentTypesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public async Task<ActionResult> Index()

}


// GET: PaymentTypes/Create
public async Task<ActionResult> Create()
{
Expand Down Expand Up @@ -70,6 +71,25 @@ public async Task<IActionResult> Create(PaymentType paymentType)
return View();
}
}

public async Task<ActionResult> Details(int id)
{
var paymentTypes = await _context.PaymentType.FirstOrDefaultAsync(pt => pt.PaymentTypeId == id);
var ordersList = await _context.Order.Where(p => p.PaymentTypeId == id).Include(p => p.OrderProducts).ToListAsync();



var viewModel = new PaymentType()
{
AccountNumber = paymentTypes.AccountNumber,
Description = paymentTypes.Description,
DateCreated = paymentTypes.DateCreated,
Orders = ordersList,


};
return View(viewModel);
}
public async Task<ActionResult> Delete(int id)
{
var paymentType = await _context.PaymentType.FirstOrDefaultAsync(pt => pt.PaymentTypeId == id);
Expand All @@ -82,6 +102,8 @@ public async Task<ActionResult> Delete(int id)
}

return View(paymentType);


}

// POST: PaymentTypes/Delete/5
Expand All @@ -95,7 +117,7 @@ public async Task<ActionResult> Delete(int id, PaymentType paymentType)
_context.PaymentType.Remove(paymentType);
await _context.SaveChangesAsync();

return RedirectToAction(nameof(Index));
return RedirectToAction("Index", "Profile", new { id = paymentType.PaymentTypeId });
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Bangazon/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task<ActionResult> Delete(int id, Product product)
{
try
{
product.ProductId = id;
product.ProductId = id;
_context.Product.Remove(product);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
Expand Down
156 changes: 156 additions & 0 deletions Bangazon/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Bangazon.Data;
using Bangazon.Models;
using Bangazon.Models.ProfileViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

namespace Bangazon.Controllers
{
[Authorize]
public class ProfileController : Controller
{
private readonly ApplicationDbContext _context;

private readonly UserManager<ApplicationUser> _userManager;

public ProfileController(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
{

_context = context;
_userManager = userManager;
}

private Task<ApplicationUser> GetCurrentUserAsync() => _userManager.GetUserAsync(HttpContext.User);

// GET: Profile
public async Task<ActionResult> Index()
{
var user = await GetCurrentUserAsync();

var userInfo = await _context.ApplicationUsers
.Where(o => o.Id == user.Id)
.Include(o => o.Orders)
.Include(p => p.Products)
.Include(pt => pt.PaymentTypes)
.ToListAsync();

var userId = await _context.ApplicationUsers.FirstOrDefaultAsync(o => o.Id == user.Id);

var viewModel = new ProfileDetailsViewModel()
{
User = user,
UserId = userId.Id,
Products = userId.Products.ToList(),
Orders = userId.Orders.ToList(),
PaymentTypes = userId.PaymentTypes.ToList(),

};



return View(viewModel);

}
// GET: Profile/Details/5
public ActionResult Details(int id)
{
return View();
}

// GET: Profile/Create
public ActionResult Create()
{
return View();
}

// POST: Profile/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
{
// TODO: Add insert logic here

return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}

public async Task<ActionResult> Edit(int id)
{
var user = await GetCurrentUserAsync();
var viewModel = new ProfileFormViewModel
{
UserId = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
StreetAddress = user.StreetAddress
};
return View(viewModel);
}

// POST: Profiles/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(int id, ProfileFormViewModel profile)
{
try
{
var profileData = await GetCurrentUserAsync();

profileData.FirstName = profile.FirstName;
profileData.LastName = profile.LastName;
profileData.StreetAddress = profile.StreetAddress;




_context.ApplicationUsers.Update(profileData);

await _context.SaveChangesAsync();

return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}

// GET: Profile/Delete/5
public ActionResult Delete(int id)
{
return View();
}

// POST: Profile/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
{
// TODO: Add delete logic here

return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
}
}
Loading