0% found this document useful (0 votes)
4 views

PetController.cs Causing Error

Uploaded by

kisanrai739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PetController.cs Causing Error

Uploaded by

kisanrai739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

using System;

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();
}

private ApplicationDbContext 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 = "Pet created successfully!" });


}
catch (Exception ex)
{
return Json(new { text = $"Error: {ex.Message}" });
}
}

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();

var viewModel = new EditPetViewModel


{
ID = pet.ID,
Name = pet.Name,
isMale = pet.isMale,
Age = pet.Age,
Breed = pet.Breed, // Set the current breed as the default
DropDownBreed = db.Pets.Select(p => p.Breed).Distinct().Select(b =>
new SelectListItem
{
Text = b,
Value = b,
Selected = b == pet.Breed // Set the selected item to the
current pet's breed
}).ToList()
};

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 = "Pet updated successfully!" });


}
catch (Exception ex)
{
return Json(new { text = $"Error: {ex.Message}" });
}
}

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);
}

Pet pet = db.Pets.Find(id);


if (pet == null)
{
return HttpNotFound();
}

var userId = User.Identity.GetUserId();


if (pet.OwnerId == userId)
{
return RedirectToAction("MyPets");
}

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.");
}

var userId = User.Identity.GetUserId();


if (userId == null)
{
ModelState.AddModelError("", "You need to be logged in to adopt a
pet.");
return View(pet); // Rerender the pet view with error message
}

// Check if the user is old enough


var userManager =
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = userManager.FindById(userId);
var dateOfBirthClaim = user.Claims.FirstOrDefault(c => c.ClaimType ==
"DateOfBirth")?.ClaimValue;
if (string.IsNullOrEmpty(dateOfBirthClaim) || !
DateTime.TryParse(dateOfBirthClaim, out DateTime dateOfBirth))
{
ModelState.AddModelError("", "Please provide your date of birth to
adopt a pet.");
return View(pet); // Show the error on the adoption page
}

int age = DateTime.Now.Year - dateOfBirth.Year;


if (dateOfBirth > DateTime.Now.AddYears(-age)) age--;

if (age < 18)


{
return new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Only
users aged 18 or older can adopt a pet.");
}

// Assign the owner


pet.OwnerId = userId;
db.Entry(pet).State = EntityState.Modified;
db.SaveChanges();

TempData["SuccessMessage"] = $"Congratulations! You've adopted


{pet.Name}.";
return RedirectToAction("MyPets");
}

// 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);
}

[HttpGet] // Specify that this action will respond to GET requests


public ActionResult GetPetsByBreed(string breed)
{
// Use the injected _petService to get pets by breed
var petsByBreed = _petService.GetPetsByBreed(breed);

// Return the partial view with the list of pets filtered by breed
return PartialView("_PetListPartial", petsByBreed);
}

protected override void Dispose(bool disposing)


{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

You might also like