-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
151 changed files
with
76,987 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.13" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
using BlueBerry.ToysShop.Web.Models; | ||
using BlueBerry.ToysShop.Web.ViewModels; | ||
using Microsoft.AspNetCore.Authorization; | ||
using System.Data; | ||
using AutoMapper; | ||
using BlueBerry.ToysShop.Web.Database_Settings; | ||
|
||
namespace BlueBerry.ToysShop.Web.Controllers | ||
{ | ||
[Authorize(Roles = "Admin")] | ||
public class AdminsController : Controller | ||
{ | ||
private readonly UserManager<Admin> _userManager; | ||
private readonly SignInManager<Admin> _signInManager; | ||
private readonly IMapper _mapper; | ||
private readonly WebDbContext _context; | ||
|
||
public AdminsController(UserManager<Admin> userManager, SignInManager<Admin> signInManager,IMapper mapper, WebDbContext context) | ||
{ | ||
_userManager = userManager; | ||
_signInManager = signInManager; | ||
_mapper = mapper; | ||
_context = context; | ||
} | ||
[HttpGet] | ||
public IActionResult Register() | ||
{ | ||
return View(); | ||
} | ||
[HttpPost] | ||
public async Task<IActionResult> Register(AdminViewModel model, [FromForm]string First, [FromForm]string Last) | ||
{ | ||
model.FullName = First + "-" + Last; | ||
if (ModelState.IsValid) | ||
{ | ||
var admin = _mapper.Map<Admin>(model); | ||
|
||
_context.Admins.Add(admin); | ||
_context.SaveChanges(); | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
|
||
return View(model); | ||
} | ||
|
||
|
||
[HttpGet] | ||
public IActionResult Login() | ||
{ | ||
return View(); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Login(AdminViewModel model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
var user = await _userManager.FindByEmailAsync(model.Email); | ||
if (user != null) | ||
{ | ||
var result = await _signInManager.PasswordSignInAsync(user, model.Password, false, lockoutOnFailure: false); | ||
if (result.Succeeded) | ||
{ | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
} | ||
|
||
ModelState.AddModelError(string.Empty, "Invalid email or password."); | ||
} | ||
|
||
return View(model); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Logout() | ||
{ | ||
await _signInManager.SignOutAsync(); | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
} | ||
} | ||
|
163 changes: 163 additions & 0 deletions
163
BlueBerry.ToysShop.Web/Controllers/CategoriesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.EntityFrameworkCore; | ||
using BlueBerry.ToysShop.Web.Database_Settings; | ||
using BlueBerry.ToysShop.Web.Models; | ||
|
||
namespace BlueBerry.ToysShop.Web.Controllers | ||
{ | ||
public class CategoriesController : Controller | ||
{ | ||
private readonly WebDbContext _context; | ||
|
||
public CategoriesController(WebDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
// GET: Categories | ||
public async Task<IActionResult> Index() | ||
{ | ||
return _context.Category != null ? | ||
View(await _context.Category.ToListAsync()) : | ||
Problem("Entity set 'WebDbContext.Category' is null."); | ||
} | ||
|
||
// GET: Categories/Details/5 | ||
public async Task<IActionResult> Details(int? id) | ||
{ | ||
if (id == null || _context.Category == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var category = await _context.Category | ||
.FirstOrDefaultAsync(m => m.Id == id); | ||
if (category == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return View(category); | ||
} | ||
|
||
// GET: Categories/Create | ||
public IActionResult Create() | ||
{ | ||
return View(); | ||
} | ||
|
||
// POST: Categories/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. | ||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public async Task<IActionResult> Create([Bind("Id,Name")] Category category) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
_context.Add(category); | ||
await _context.SaveChangesAsync(); | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
return View(category); | ||
} | ||
|
||
// GET: Categories/Edit/5 | ||
public async Task<IActionResult> Edit(int? id) | ||
{ | ||
if (id == null || _context.Category == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var category = await _context.Category.FindAsync(id); | ||
if (category == null) | ||
{ | ||
return NotFound(); | ||
} | ||
return View(category); | ||
} | ||
|
||
// POST: Categories/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. | ||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] Category category) | ||
{ | ||
if (id != category.Id) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
try | ||
{ | ||
_context.Update(category); | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!CategoryExists(category.Id)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
return View(category); | ||
} | ||
|
||
// GET: Categories/Delete/5 | ||
public async Task<IActionResult> Delete(int? id) | ||
{ | ||
if (id == null || _context.Category == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var category = await _context.Category | ||
.FirstOrDefaultAsync(m => m.Id == id); | ||
if (category == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return View(category); | ||
} | ||
|
||
// POST: Categories/Delete/5 | ||
[HttpPost, ActionName("Delete")] | ||
[ValidateAntiForgeryToken] | ||
public async Task<IActionResult> DeleteConfirmed(int id) | ||
{ | ||
if (_context.Category == null) | ||
{ | ||
return Problem("Entity set 'WebDbContext.Category' is null."); | ||
} | ||
var category = await _context.Category.FindAsync(id); | ||
if (category != null) | ||
{ | ||
_context.Category.Remove(category); | ||
} | ||
|
||
await _context.SaveChangesAsync(); | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
|
||
private bool CategoryExists(int id) | ||
{ | ||
return (_context.Category?.Any(e => e.Id == id)).GetValueOrDefault(); | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
BlueBerry.ToysShop.Web/Controllers/CustomersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using AutoMapper; | ||
using BlueBerry.ToysShop.Web.Database_Settings; | ||
using BlueBerry.ToysShop.Web.Models; | ||
using BlueBerry.ToysShop.Web.ViewModels; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Authorization; | ||
using System.Data; | ||
|
||
namespace BlueBerry.ToysShop.Web.Controllers | ||
{ | ||
[Authorize(Roles = "Customer")] | ||
public class CustomersController:Controller | ||
{ | ||
|
||
private readonly WebDbContext _context; | ||
private readonly IMapper _mapper; | ||
public CustomersController(WebDbContext context, IMapper mapper) | ||
{ | ||
_context = context; | ||
_mapper = mapper; | ||
|
||
} | ||
[HttpGet] | ||
public IActionResult SignUp() | ||
{ | ||
return View(); | ||
} | ||
[HttpPost] | ||
public IActionResult SignUp(CustomerViewModel customer, [FromForm] string First, [FromForm] string Last) | ||
{ | ||
customer.FullName = First + "-" + Last; | ||
_context.Customers.Add(_mapper.Map<Customer>(customer)); | ||
_context.SaveChanges(); | ||
return RedirectToAction("Login"); | ||
} | ||
[HttpGet] | ||
public IActionResult Login() | ||
{ | ||
return View(); | ||
} | ||
[HttpPost] | ||
public async Task<IActionResult> Login(CustomerViewModel customer) | ||
{ | ||
var existingCustomer = await _context.Customers.FirstOrDefaultAsync(c => c.Email == customer.Email); | ||
if (existingCustomer != null && existingCustomer.Password == customer.Password) | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, existingCustomer.Email), | ||
new Claim(ClaimTypes.Role, "Customer") | ||
}; | ||
|
||
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); | ||
var principal = new ClaimsPrincipal(identity); | ||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); | ||
|
||
return RedirectToAction("Index", "Home"); | ||
} | ||
else | ||
{ | ||
ModelState.AddModelError(string.Empty, "Invalid email or password."); | ||
return View(customer); | ||
} | ||
} | ||
[HttpGet] | ||
public async Task<IActionResult> Logout() | ||
{ | ||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
[HttpGet] | ||
[Authorize(Roles = "Customer")] | ||
public IActionResult CustomerProfile() | ||
{ | ||
// Get the currently authenticated user's email | ||
var email = User.Identity.Name; | ||
|
||
// Get the customer from the database using the email | ||
var customer = _context.Customers.FirstOrDefault(c => c.Email == email); | ||
|
||
// Map the customer to the view model | ||
var customerViewModel = _mapper.Map<CustomerViewModel>(customer); | ||
|
||
return View(customerViewModel); | ||
} | ||
[HttpPost] | ||
public IActionResult addToCart() | ||
{ | ||
return View(); | ||
} | ||
[HttpGet] | ||
public IActionResult viewCart() { | ||
|
||
|
||
return View(); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.