Added a few controllers and views
This commit is contained in:
69
Controllers/AuthController.cs
Normal file
69
Controllers/AuthController.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace OneForMe.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly UserManager<IdentityUser> _userManager;
|
||||
private readonly SignInManager<IdentityUser> _signInManager;
|
||||
|
||||
public AuthController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a new user
|
||||
/// </summary>
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> Register([FromBody] RegisterRequest request)
|
||||
{
|
||||
var user = new IdentityUser { UserName = request.Email, Email = request.Email };
|
||||
var result = await _userManager.CreateAsync(user, request.Password);
|
||||
|
||||
if (result.Succeeded)
|
||||
return Ok(new { message = "User registered successfully" });
|
||||
|
||||
return BadRequest(new { errors = result.Errors.Select(e => e.Description) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Login user
|
||||
/// </summary>
|
||||
[HttpPost("login")]
|
||||
public async Task<IActionResult> Login([FromBody] LoginRequest request)
|
||||
{
|
||||
var result = await _signInManager.PasswordSignInAsync(request.Email, request.Password, false, false);
|
||||
|
||||
if (result.Succeeded)
|
||||
return Ok(new { message = "Login successful" });
|
||||
|
||||
return Unauthorized(new { message = "Invalid email or password" });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logout user
|
||||
/// </summary>
|
||||
[HttpPost("logout")]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
return Ok(new { message = "Logout successful" });
|
||||
}
|
||||
}
|
||||
|
||||
public class RegisterRequest
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class LoginRequest
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
35
Controllers/AuthViewController.cs
Normal file
35
Controllers/AuthViewController.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.CodeDom.Compiler;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace OneForMe.Controllers;
|
||||
|
||||
[Route("[controller]")]
|
||||
public class AuthViewController : Controller
|
||||
{
|
||||
private readonly SignInManager<IdentityUser> _signInManager;
|
||||
|
||||
public AuthViewController(SignInManager<IdentityUser> signInManager)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
[HttpGet("Login")]
|
||||
public IActionResult Login()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet("Register")]
|
||||
public IActionResult Register()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost("Logout")]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,56 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OneForMe.Data;
|
||||
using OneForMe.Models;
|
||||
|
||||
namespace OneForMe.Controllers;
|
||||
|
||||
[Authorize]
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public HomeController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
if (User.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
return RedirectToAction("Dashboard");
|
||||
}
|
||||
return RedirectToAction("Login", "AuthViewController");
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Dashboard()
|
||||
{
|
||||
var userEmail = User.Identity?.Name;
|
||||
|
||||
var createdOrders = await _context.Orders
|
||||
.Where(o => o.CreatorName == userEmail)
|
||||
.Include(o => o.MenuItems)
|
||||
.Include(o => o.OrderItems)
|
||||
.ThenInclude(oi => oi.MenuItem)
|
||||
.ToListAsync();
|
||||
|
||||
var joinedOrders = await _context.Orders
|
||||
.Where(o => o.OrderItems.Any(oi => oi.ParticipantEmail == userEmail || oi.ParticipantName == userEmail))
|
||||
.Include(o => o.MenuItems)
|
||||
.Include(o => o.OrderItems)
|
||||
.ThenInclude(oi => oi.MenuItem)
|
||||
.ToListAsync();
|
||||
|
||||
var viewModel = new DashboardViewModel
|
||||
{
|
||||
CreatedOrders = createdOrders,
|
||||
JoinedOrders = joinedOrders
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
|
||||
118
Controllers/OrderController.cs
Normal file
118
Controllers/OrderController.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OneForMe.Data;
|
||||
using OneForMe.Models;
|
||||
|
||||
namespace OneForMe.Controllers;
|
||||
|
||||
[Authorize]
|
||||
public class OrderController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public OrderController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: Order/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Order/Create
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create(Order order, string[] itemNames, decimal[] itemPrices)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return View();
|
||||
|
||||
order.OrderCode = GenerateOrderCode();
|
||||
order.CreatorName = User.Identity?.Name ?? "Unknown";
|
||||
|
||||
_context.Orders.Add(order);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Add menu items
|
||||
for (int i = 0; i < itemNames.Length; i++)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(itemNames[i]) && itemPrices[i] > 0)
|
||||
{
|
||||
_context.MenuItems.Add(new MenuItem
|
||||
{
|
||||
OrderId = order.Id,
|
||||
Name = itemNames[i],
|
||||
Price = itemPrices[i]
|
||||
});
|
||||
}
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction("Details", new { code = order.OrderCode });
|
||||
}
|
||||
|
||||
// GET: Order/Join
|
||||
public async Task<IActionResult> Join(string code)
|
||||
{
|
||||
var order = await _context.Orders
|
||||
.Include(o => o.MenuItems)
|
||||
.Include(o => o.OrderItems)
|
||||
.FirstOrDefaultAsync(o => o.OrderCode == code);
|
||||
|
||||
if (order == null)
|
||||
return NotFound("Order not found");
|
||||
|
||||
if (order.IsClosed)
|
||||
return BadRequest("This order is closed");
|
||||
|
||||
return View(order);
|
||||
}
|
||||
|
||||
// POST: Order/AddItem
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddItem(int orderId, int menuItemId, int quantity, string participantName, string? participantEmail)
|
||||
{
|
||||
var order = await _context.Orders.FindAsync(orderId);
|
||||
if (order == null || order.IsClosed)
|
||||
return BadRequest("Order not found or is closed");
|
||||
|
||||
var menuItem = await _context.MenuItems.FindAsync(menuItemId);
|
||||
if (menuItem == null)
|
||||
return BadRequest("Menu item not found");
|
||||
|
||||
var orderItem = new OrderItem
|
||||
{
|
||||
OrderId = orderId,
|
||||
MenuItemId = menuItemId,
|
||||
Quantity = quantity,
|
||||
ParticipantName = participantName,
|
||||
ParticipantEmail = participantEmail
|
||||
};
|
||||
|
||||
_context.OrderItems.Add(orderItem);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction("Join", new { code = order.OrderCode });
|
||||
}
|
||||
|
||||
// GET: Order/Details/{code}
|
||||
public async Task<IActionResult> Details(string code)
|
||||
{
|
||||
var order = await _context.Orders
|
||||
.Include(o => o.MenuItems)
|
||||
.Include(o => o.OrderItems)
|
||||
.FirstOrDefaultAsync(o => o.OrderCode == code);
|
||||
|
||||
if (order == null)
|
||||
return NotFound();
|
||||
|
||||
return View(order);
|
||||
}
|
||||
|
||||
private string GenerateOrderCode()
|
||||
{
|
||||
return Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user