using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc.Rendering; using PostgreSqlDotnetCore.Models; using System; using System.Net; namespace PostgreSqlDotnetCore.Controllers { public class PetCaresController : BaseController { public PetCaresController(UserManager userManager) : base(userManager) { } [HttpGet] /* public async Task Create() { var vetCenters = await db.VetCentersObj.ToListAsync(); ViewBag.VetCenters = new SelectList(vetCenters, "id", "name"); return View(); }*/ public async Task Create() { UsersClass customerClass = await getCrrentUser(); ViewBag.isAuthenticated = customerClass; var vetCenters = await db.VetCentersObj.ToListAsync(); ViewBag.VetCenters = new SelectList(vetCenters, "id", "name"); // check if the user is authenticated so we can take only his pets if (customerClass != null) { var queryPetsByUser = from st in db.PetsObj where st.usersid == customerClass.id select st; var userPets = await queryPetsByUser.ToListAsync(); ViewBag.Pets = new SelectList(userPets, "id", "name"); } return View(); } // GET: Customer public async Task IndexAsync() { // check for permission UsersClass customerClass = await getCrrentUser(); // set if is authenticated ViewBag.isAuthenticated = customerClass; if (customerClass == null) { return RedirectToAction("AccessDenied", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); if (customerClass.role_id == RoleConstants.Standard) { // query var query = from st in db.PetCaresObj where st.usersid == customerClass.id select st; var userPetCares = await query.ToListAsync(); return View(userPetCares); } else { return View(db.PetCaresObj.ToList()); } } // GET: Customer/Details/5 public async Task Details(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } UsersClass customerClass = await getCrrentUser(); ViewBag.isAuthenticated = customerClass; Pet_CaresClass peClass = db.PetCaresObj.Find(id); if (peClass == null) { return RedirectToAction("NotExist", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(peClass); } // GET: Customer/Create //public ActionResult Create() //{ // return View(); //} /*public ActionResult Create() { return View(); }*/ // POST: Customer/Create // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid, pet_id")] Pet_CaresClass peClass) { bool isAuthenticated = User.Identity.IsAuthenticated; if (!isAuthenticated) { return RedirectToAction("AccessDenied", "Error"); } ViewBag.isAuthenticated = new UsersClass(); // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); UsersClass customerClass = null; if (ModelState.IsValid) { ViewBag.isAuthenticated = new UsersClass(); peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc); peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc); var user = await _userManager.GetUserAsync(User); customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email); peClass.usersid = customerClass.id; db.PetCaresObj.Add(peClass); db.SaveChanges(); return RedirectToAction("Index"); } var vetCenters = await db.VetCentersObj.ToListAsync(); ViewBag.VetCenters = new SelectList(vetCenters, "id", "name"); var queryPetsByUser = from st in db.PetsObj where st.usersid == customerClass.id select st; var userPets =await queryPetsByUser.ToListAsync(); ViewBag.Pets= new SelectList(userPets, "id", "name"); return View(peClass); } // GET: Customer/Edit/5 /* public ActionResult Edit(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } Pet_CaresClass peClass = db.PetCaresObj.Find(id); if (peClass == null) { return RedirectToAction("NotExist", "Error"); } return View(peClass); }*/ // GET: Customer/Edit/5 /* public ActionResult Edit(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } Pet_CaresClass peClass = db.PetCaresObj.Find(id); if (peClass == null) { return RedirectToAction("NotExist", "Error"); } return View(peClass); }*/ public async Task Edit(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id); if (peClass == null) { return RedirectToAction("NotExist", "Error"); } var vetCenters = await db.VetCentersObj.ToListAsync(); ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid); // dodadeno na 22.08 UsersClass customerClass = await getCrrentUser(); // check if the user is authenticated so we can take only his pets if (customerClass != null) { var queryPetsByUser = from st in db.PetsObj where st.usersid == customerClass.id select st; var userPets = await queryPetsByUser.ToListAsync(); ViewBag.Pets = new SelectList(userPets, "id", "name"); } ViewBag.isAuthenticated = customerClass; return View(peClass); } // POST: Customer/Edit/5 // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass) { bool isAuthenticated = User.Identity.IsAuthenticated; if (!isAuthenticated) { return RedirectToAction("AccessDenied", "Error"); } ViewBag.isAuthenticated = await getCrrentUser(); // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); if (ModelState.IsValid) { peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc); var user = await _userManager.GetUserAsync(User); var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email); peClass.usersid = customerClass.id; db.Entry(peClass).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(peClass); } // GET: Customer/Delete/5 public async Task Delete(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот ViewBag.isAuthenticated = customerClass; Pet_CaresClass peClass = db.PetCaresObj.Find(id); if (peClass == null) { return RedirectToAction("NotExist", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(peClass); } // POST: Customer/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Pet_CaresClass peClass = db.PetCaresObj.Find(id); db.PetCaresObj.Remove(peClass); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }