forked from VCDN-2024/prog7311-part-2-Leighche
-
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
410 changed files
with
112,691 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file added
BIN
+42.4 KB
FireBasics/.vs/FireBasics/FileContentIndex/2a4e196b-bdc8-4439-a33a-9cf01f5ab420.vsidx
Binary file not shown.
Binary file added
BIN
+94.7 KB
FireBasics/.vs/FireBasics/FileContentIndex/2a92aee2-3664-423a-99fa-3a9e3981b8be.vsidx
Binary file not shown.
Binary file added
BIN
+39.9 KB
FireBasics/.vs/FireBasics/FileContentIndex/3c38d577-2818-4296-97c2-b4e73e4c1ec3.vsidx
Binary file not shown.
Binary file added
BIN
+43.5 KB
FireBasics/.vs/FireBasics/FileContentIndex/c996533f-69f5-4b6f-acd7-130bffdc9952.vsidx
Binary file not shown.
Binary file added
BIN
+1.61 MB
FireBasics/.vs/FireBasics/FileContentIndex/fb45ba28-cd5d-4740-8df6-73586a854088.vsidx
Binary file not shown.
1,011 changes: 1,011 additions & 0 deletions
1,011
FireBasics/.vs/FireBasics/config/applicationhost.config
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.8.34309.116 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireBasics", "FireBasics\FireBasics.csproj", "{ED638C06-E3D3-4A1B-9EA8-90683E9DDD6B}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{ED638C06-E3D3-4A1B-9EA8-90683E9DDD6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{ED638C06-E3D3-4A1B-9EA8-90683E9DDD6B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{ED638C06-E3D3-4A1B-9EA8-90683E9DDD6B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{ED638C06-E3D3-4A1B-9EA8-90683E9DDD6B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {D49EED48-E148-439A-A26E-D49E88DDC05C} | ||
EndGlobalSection | ||
EndGlobal |
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,12 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace FireBasics.Controllers | ||
{ | ||
public class AboutController : Controller | ||
{ | ||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
} | ||
} |
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,12 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace FireBasics.Controllers | ||
{ | ||
public class AdminController : Controller | ||
{ | ||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
} | ||
} |
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,139 @@ | ||
using Firebase.Auth; | ||
using FireBasics.Logger; | ||
using FireBasics.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Newtonsoft.Json; | ||
using NuGet.Common; | ||
using System.Diagnostics; | ||
|
||
namespace FireBasics.Controllers | ||
{ | ||
public class AuthController : Controller | ||
{ | ||
private ILog iLog; | ||
private readonly AgrienergyContext _context; | ||
|
||
FirebaseAuthProvider auth; | ||
|
||
public AuthController(AgrienergyContext context) // Dependency injection | ||
{ | ||
_context = context; // Ensure _context is initialized | ||
iLog = Log.GetInstance(); | ||
auth = new FirebaseAuthProvider(new FirebaseConfig("AIzaSyBxp6SwUyRGTYZlhZHmCGq4XL8WFMVWQrQ")); | ||
} | ||
|
||
public IActionResult Registration() | ||
{ | ||
if (!string.IsNullOrEmpty(HttpContext.Session.GetString("_UserToken"))) | ||
{ | ||
return RedirectToAction("Index", "Home"); // Redirect to the homepage if logged in | ||
} | ||
return View(); | ||
} | ||
|
||
public IActionResult SignIn() | ||
{ | ||
if (!string.IsNullOrEmpty(HttpContext.Session.GetString("_UserToken"))) | ||
{ | ||
return RedirectToAction("Index", "Home"); // Redirect to the homepage if logged in | ||
} | ||
return View(); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Registration(AuthModel authModel) | ||
{ | ||
try | ||
{ | ||
var fbAuthLink = await auth.CreateUserWithEmailAndPasswordAsync(authModel.Email, authModel.Password); | ||
|
||
if (fbAuthLink?.User == null) | ||
{ | ||
ModelState.AddModelError(string.Empty, "Failed to create user in Firebase."); | ||
return View(authModel); | ||
} | ||
|
||
string firebaseUid = fbAuthLink.User.LocalId; // Get Firebase UID | ||
string token = fbAuthLink.FirebaseToken; // Get Firebase token | ||
|
||
if (token == null) | ||
{ | ||
ModelState.AddModelError(string.Empty, "Failed to retrieve a valid token."); | ||
return View(authModel); | ||
} | ||
|
||
// Set session with Firebase token | ||
HttpContext.Session.SetString("_UserToken", token); | ||
|
||
var newUser = new FireBasics.Models.User | ||
{ | ||
FirebaseUid = firebaseUid, | ||
Role = "FARMER", // Or 'EMPLOYEE', depending on your logic | ||
Name = authModel.Name, // User-provided name | ||
Dob = authModel.Dob, // User-provided date of birth | ||
Bio = authModel.Bio // User-provided bio | ||
}; | ||
|
||
_context.Users.Add(newUser); // Save to the local database | ||
await _context.SaveChangesAsync(); // Persist changes to the database | ||
|
||
// Redirect after successful registration | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
catch (FirebaseAuthException ex) | ||
{ | ||
var firebaseEx = JsonConvert.DeserializeObject<FirebaseError>(ex.ResponseData); | ||
ModelState.AddModelError(string.Empty, "Registration error: " + firebaseEx.error.message); | ||
return View(authModel); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ModelState.AddModelError(string.Empty, "An unexpected error occurred: " + ex.Message); | ||
return View(authModel); | ||
} | ||
} | ||
|
||
[HttpPost] | ||
|
||
public async Task<IActionResult> SignIn(AuthModel authModel) | ||
{ | ||
try | ||
{ | ||
var fbAuthLink = await auth.SignInWithEmailAndPasswordAsync(authModel.Email, authModel.Password); | ||
string token = fbAuthLink.FirebaseToken; | ||
|
||
if (token != null) | ||
{ | ||
HttpContext.Session.SetString("_UserToken", token); // Save token in session | ||
return RedirectToAction("Index", "Home"); | ||
} | ||
else | ||
{ | ||
ModelState.AddModelError(String.Empty, "Invalid login credentials."); | ||
iLog.LogException("User Login Failed - " + authModel.Email); | ||
return View(authModel); // Return the sign-in view with errors | ||
} | ||
} | ||
catch (FirebaseAuthException ex) | ||
{ | ||
var firebaseEx = JsonConvert.DeserializeObject<FirebaseError>(ex.ResponseData); | ||
ModelState.AddModelError(String.Empty, "Login error: " + firebaseEx.error.message); | ||
iLog.LogException("User Login Failed - " + authModel.Email); | ||
return View(authModel); // Return the sign-in view with errors | ||
} | ||
catch (Exception ex) | ||
{ | ||
ModelState.AddModelError(String.Empty, "An unexpected error occurred: " + ex.Message); | ||
iLog.LogException("User Login Failed - " + authModel.Email); | ||
return View(authModel); // Return the sign-in view with errors | ||
} | ||
} | ||
|
||
public IActionResult LogOut() | ||
{ | ||
HttpContext.Session.Remove("_UserToken"); | ||
return RedirectToAction("SignIn"); | ||
} | ||
} | ||
} |
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 FireBasics.Models; | ||
|
||
namespace FireBasics.Controllers | ||
{ | ||
public class CartController : Controller | ||
{ | ||
private readonly AgrienergyContext _context; | ||
|
||
public CartController(AgrienergyContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
// GET: Cart | ||
public async Task<IActionResult> Index() | ||
{ | ||
var agrienergyContext = _context.Carts.Include(c => c.Product); | ||
return View(await agrienergyContext.ToListAsync()); | ||
} | ||
|
||
// GET: Cart/Details/5 | ||
public async Task<IActionResult> Details(int? id) | ||
{ | ||
if (id == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var cart = await _context.Carts | ||
.Include(c => c.Product) | ||
.FirstOrDefaultAsync(m => m.CartId == id); | ||
if (cart == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return View(cart); | ||
} | ||
|
||
// GET: Cart/Create | ||
public IActionResult Create() | ||
{ | ||
ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "ProductId"); | ||
return View(); | ||
} | ||
|
||
// POST: Cart/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("CartId,ProductId")] Cart cart) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
_context.Add(cart); | ||
await _context.SaveChangesAsync(); | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "ProductId", cart.ProductId); | ||
return View(cart); | ||
} | ||
|
||
// GET: Cart/Edit/5 | ||
public async Task<IActionResult> Edit(int? id) | ||
{ | ||
if (id == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var cart = await _context.Carts.FindAsync(id); | ||
if (cart == null) | ||
{ | ||
return NotFound(); | ||
} | ||
ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "ProductId", cart.ProductId); | ||
return View(cart); | ||
} | ||
|
||
// POST: Cart/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("CartId,ProductId")] Cart cart) | ||
{ | ||
if (id != cart.CartId) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
try | ||
{ | ||
_context.Update(cart); | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!CartExists(cart.CartId)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "ProductId", cart.ProductId); | ||
return View(cart); | ||
} | ||
|
||
// GET: Cart/Delete/5 | ||
public async Task<IActionResult> Delete(int? id) | ||
{ | ||
if (id == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
var cart = await _context.Carts | ||
.Include(c => c.Product) | ||
.FirstOrDefaultAsync(m => m.CartId == id); | ||
if (cart == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return View(cart); | ||
} | ||
|
||
// POST: Cart/Delete/5 | ||
[HttpPost, ActionName("Delete")] | ||
[ValidateAntiForgeryToken] | ||
public async Task<IActionResult> DeleteConfirmed(int id) | ||
{ | ||
var cart = await _context.Carts.FindAsync(id); | ||
if (cart != null) | ||
{ | ||
_context.Carts.Remove(cart); | ||
} | ||
|
||
await _context.SaveChangesAsync(); | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
|
||
private bool CartExists(int id) | ||
{ | ||
return _context.Carts.Any(e => e.CartId == id); | ||
} | ||
} | ||
} |
Oops, something went wrong.