PetController.cs Causing Error
PetController.cs Causing Error
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using PetShop.Models;
using PetShop.Models.ViewModel;
using PetShop.Services;
namespace PetShop.Controllers
{
[Authorize] // Require authentication for all actions
public class PetsController : Controller
{
private readonly IPetService _petService;
public PetsController(IPetService petService)
{
_petService = petService;
db = new ApplicationDbContext();
}
// GET: Pets
[AllowAnonymous] // Allow anonymous users to access the Index action
public ActionResult Index()
{
var unadoptedPets = db.Pets.Where(p => p.OwnerId == null).ToList();
return View(unadoptedPets);
}
// GET: Pets/Create
public ActionResult Create()
{
var viewModel = new CreatePetViewModel
{
DropDownBreed = new List<SelectListItem>
{
new SelectListItem { Text = "Labrador", Value = "Labrador" },
new SelectListItem { Text = "Poodle", Value = "Poodle" },
new SelectListItem { Text = "Bulldog", Value = "Bulldog" },
// Add more breeds as needed
}
};
return View(viewModel);
}
// POST: Pets/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Name,isMale,Breed,Age")]
CreatePetViewModel viewModel)
{
if (ModelState.IsValid)
{
try
{
var pet = new Pet
{
Name = viewModel.Name,
isMale = viewModel.isMale,
Breed = viewModel.Breed,
Age = viewModel.Age,
OwnerId = null // Ensure the owner is null for new pets
};
db.Pets.Add(pet);
db.SaveChanges();
return Json(new { text = "Invalid data provided. Please check the form
and try again." });
}
// GET: Pets/Edit/5
[Authorize(Roles = "Admin")]
public ActionResult Edit(int id)
{
var pet = db.Pets.Find(id);
if (pet == null) return HttpNotFound();
return View(viewModel);
}
// POST: Pets/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,isMale,Breed,Age")]
EditPetViewModel viewModel)
{
if (ModelState.IsValid)
{
try
{
var pet = db.Pets.Find(viewModel.ID);
if (pet == null)
return Json(new { text = "Pet not found." });
pet.Name = viewModel.Name;
pet.isMale = viewModel.isMale;
pet.Breed = viewModel.Breed;
pet.Age = viewModel.Age;
db.Entry(pet).State = EntityState.Modified;
db.SaveChanges();
return Json(new { text = "Invalid data provided. Please check the form
and try again." });
}
// GET: Pets/Delete/5
[Authorize(Roles = "Admin")]
public ActionResult Delete(int id)
{
var pet = db.Pets.Find(id);
if (pet == null) return HttpNotFound();
return View(pet);
}
// POST: Pets/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Pet pet = db.Pets.Find(id);
db.Pets.Remove(pet);
db.SaveChanges();
return RedirectToAction("Index");
}
[Authorize]
public ActionResult MyPets()
{
var userId = User.Identity.GetUserId();
if (userId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "User ID
is null.");
}
var myPets = db.Pets.Where(p => p.OwnerId == userId).ToList();
if (!myPets.Any())
{
ViewBag.Message = "You do not own any pets.";
}
return View(myPets);
}
[Authorize]
public ActionResult Adopt(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return View(pet);
}
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Adopt(int id)
{
var pet = db.Pets.Find(id);
if (pet == null)
{
return HttpNotFound("Pet not found.");
}
// GET: Pets/Details/5
[Authorize] // Optional: Require authentication
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Pet pet = db.Pets.Find(id);
if (pet == null)
{
return HttpNotFound();
}
return View(pet);
}
// Return the partial view with the list of pets filtered by breed
return PartialView("_PetListPartial", petsByBreed);
}