using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace RestAPI2.Pages
{
    public class DeleteModel : PageModel
    {
        private readonly RestAPI2.Data.HouseContext _context;

        public DeleteModel(RestAPI2.Data.HouseContext context)
        {
            _context = context;
        }

        [BindProperty]
         public RestAPI2.Models.House House { get; set; }

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            House = await _context.House.FirstOrDefaultAsync(m => m.ID == id);

            if (House == null)
            {
                return NotFound();
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            House = await _context.House.FindAsync(id);

            if (House != null)
            {
                _context.House.Remove(House);
                await _context.SaveChangesAsync();
            }

            return RedirectToPage("./House");
        }
    }
}