Impl Idea
Impl Idea
md 2024-07-07
Creating an entire project from scratch is a substantial task, but I can provide you with a detailed outline and
some code snippets to help you get started. Here’s a step-by-step guide to building the "Travel Genie" project
in .NET:
Models/User.cs:
using Microsoft.AspNetCore.Identity;
Models/Trip.cs:
1/8
ImplIdea.md 2024-07-07
Data/ApplicationDbContext.cs:
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
Startup.cs:
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<User>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
}
appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=
(localdb)\\mssqllocaldb;Database=TravelGenieDb;Trusted_Connection=True;MultipleAct
iveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
2/8
ImplIdea.md 2024-07-07
"AllowedHosts": "*"
}
Controllers/HomeController.cs:
using Microsoft.AspNetCore.Mvc;
Views/Home/Index.cshtml:
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome to Travel Genie</h1>
<p>Plan your perfect trip with ease!</p>
</div>
Controllers/AccountController.cs:
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new User { UserName = model.Email, Email = model.Email,
FullName = model.FullName };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
return View(model);
}
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email,
model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
}
return View(model);
}
[HttpPost]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
4/8
ImplIdea.md 2024-07-07
Views/Account/Register.cshtml:
@model RegisterViewModel
<form asp-action="Register">
<div class="form-group">
<label asp-for="FullName"></label>
<input asp-for="FullName" class="form-control" />
<span asp-validation-for="FullName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
Views/Account/Login.cshtml:
@model LoginViewModel
<form asp-action="Login">
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<div class="form-check">
5/8
ImplIdea.md 2024-07-07
Controllers/TripsController.cs:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
[Authorize]
public class TripsController : Controller
{
private readonly ApplicationDbContext _context;
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(Trip trip)
{
if (ModelState.IsValid)
{
trip.User = await
_context.Users.FindAsync(User.FindFirstValue(ClaimTypes.NameIdentifier));
_context.Trips.Add(trip);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
6/8
ImplIdea.md 2024-07-07
}
return View(trip);
}
Views/Trips/Index.cshtml:
@model IEnumerable<Trip>
<h1>Your Trips</h1>
<table class="table">
<thead>
<tr>
<th>Destination</th>
<th>Start Date</th>
<th>End Date</th>
<th>Notes</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var trip in Model)
{
<tr>
<td>@trip.Destination</td>
<td>@trip.StartDate.ToShortDateString()</td>
<td>@trip.EndDate.ToShortDateString()</td>
<
td>@trip.Notes</td>
<td>
<a asp-action="Edit" asp-route-id="@trip.Id" class="btn btn-
primary">Edit</a>
<a asp-action="Delete" asp-route-id="@trip.Id" class="btn btn-
danger">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Views/Trips/Create.cshtml:
7/8
ImplIdea.md 2024-07-07
@model Trip
<form asp-action="Create">
<div class="form-group">
<label asp-for="Destination"></label>
<input asp-for="Destination" class="form-control" />
<span asp-validation-for="Destination" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="StartDate"></label>
<input asp-for="StartDate" class="form-control" type="date" />
<span asp-validation-for="StartDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EndDate"></label>
<input asp-for="EndDate" class="form-control" type="date" />
<span asp-validation-for="EndDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Notes"></label>
<textarea asp-for="Notes" class="form-control"></textarea>
<span asp-validation-for="Notes" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
This is a high-level overview and some starter code to get you going. Building a complete project will require
more detailed work, especially for advanced features and integrations. Feel free to ask for further details or
specific implementations!
8/8