Register
This commit is contained in:
153
RentACar/WebApp/Controllers/CarsController.cs
Normal file
153
RentACar/WebApp/Controllers/CarsController.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
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 Data;
|
||||
using Data.Entities;
|
||||
|
||||
namespace WebApp.Controllers
|
||||
{
|
||||
public class CarsController : Controller
|
||||
{
|
||||
private readonly RentACarDbContext _context;
|
||||
|
||||
public CarsController(RentACarDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: Cars
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return View(await _context.Cars.ToListAsync());
|
||||
}
|
||||
|
||||
// GET: Cars/Details/5
|
||||
public async Task<IActionResult> Details(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var car = await _context.Cars
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if (car == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(car);
|
||||
}
|
||||
|
||||
// GET: Cars/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Cars/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,Brand,Model,Year,CountPassengerSeats,Description,PriceForDay")] Car car)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_context.Add(car);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(car);
|
||||
}
|
||||
|
||||
// GET: Cars/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var car = await _context.Cars.FindAsync(id);
|
||||
if (car == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return View(car);
|
||||
}
|
||||
|
||||
// POST: Cars/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,Brand,Model,Year,CountPassengerSeats,Description,PriceForDay")] Car car)
|
||||
{
|
||||
if (id != car.Id)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(car);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!CarExists(car.Id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(car);
|
||||
}
|
||||
|
||||
// GET: Cars/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var car = await _context.Cars
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if (car == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(car);
|
||||
}
|
||||
|
||||
// POST: Cars/Delete/5
|
||||
[HttpPost, ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
var car = await _context.Cars.FindAsync(id);
|
||||
_context.Cars.Remove(car);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
private bool CarExists(int id)
|
||||
{
|
||||
return _context.Cars.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user