Skip to content

Commit

Permalink
Application v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Leighche committed May 30, 2024
1 parent c41f058 commit 08e6c73
Show file tree
Hide file tree
Showing 410 changed files with 112,691 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,011 changes: 1,011 additions & 0 deletions FireBasics/.vs/FireBasics/config/applicationhost.config

Large diffs are not rendered by default.

Binary file added FireBasics/.vs/FireBasics/v17/.futdcache.v2
Binary file not shown.
Binary file added FireBasics/.vs/FireBasics/v17/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
25 changes: 25 additions & 0 deletions FireBasics/FireBasics.sln
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
12 changes: 12 additions & 0 deletions FireBasics/FireBasics/Controllers/AboutController.cs
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();
}
}
}
12 changes: 12 additions & 0 deletions FireBasics/FireBasics/Controllers/AdminController.cs
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();
}
}
}
139 changes: 139 additions & 0 deletions FireBasics/FireBasics/Controllers/AuthController.cs
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");
}
}
}
163 changes: 163 additions & 0 deletions FireBasics/FireBasics/Controllers/CartController.cs
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);
}
}
}
Loading

0 comments on commit 08e6c73

Please sign in to comment.