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

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

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

        [BindProperty]
        public RestAPI2.Models.House House { get; set; }
        private int idS=0;

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

        #region snippet
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            House.ID=House.Number;

            _context.Attach(House).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MovieExists(House.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./House");
        }

        private bool MovieExists(int id)
        {
            return _context.House.Any(e => e.ID == id);
        }
        #endregion
    }
}