Example #1
0
        // HTTP GET
        public IActionResult Create()
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }

            Infant infant = new Infant
            {
                Dob    = DateTime.Now,
                UserId = userManager.GetUserId(User)
            };

            return(View("InfantEditor", InfantViewModelFactory.Create(infant)));
        }
Example #2
0
        // HTTP GET
        public async Task <IActionResult> Edit(long id)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }
            Infant infant = await context.Infants.FindAsync(id);

            if (!IsInfantOwner(infant))
            {
                return(RedirectToPage("/Error/Error404"));
            }
            InfantViewModel model = InfantViewModelFactory.Edit(infant);

            return(View("InfantEditor", model));
        }
Example #3
0
        public async Task <IActionResult> Create([FromForm] Infant infant)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }
            if (ModelState.IsValid)
            {
                infant.InfantId = default;
                context.Infants.Add(infant);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View("InfantEditor", InfantViewModelFactory.Create(infant)));
        }
Example #4
0
        public async Task <IActionResult> Details(int id)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }
            Infant i = await context.Infants.FirstOrDefaultAsync(i => i.InfantId == id);

            if (!IsInfantOwner(i))
            {
                return(RedirectToPage("/Error/Error404"));
            }
            InfantViewModel model = InfantViewModelFactory.Details(i);

            return(View("InfantEditor", model));
        }
Example #5
0
        public async Task <IActionResult> Edit(long id, [FromForm] Infant infant)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }
            Infant preSaveInfant = await context.Infants.AsNoTracking().FirstOrDefaultAsync(i => i.InfantId == id);

            if (!IsInfantOwner(preSaveInfant))
            {
                return(RedirectToPage("/Error/Error404"));
            }
            if (ModelState.IsValid)
            {
                context.Infants.Update(infant);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View("InfantEditor", InfantViewModelFactory.Edit(infant)));
        }