
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RestAPI2.Models;
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;


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

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

        public IActionResult OnGet()
        {
            Console.WriteLine("got it");
            return Page();
        }

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

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            var v=await _context.Residents.ToListAsync();
            if(v.Count==0)
                Resident.ID=1;
            else{
                 Resident.ID=v.ToArray()[v.Count-1].ID+1;
            }

            _context.Residents.Add(Resident);
            await _context.SaveChangesAsync();

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