Exemplo n.º 1
0
        public async Task <IActionResult> Create(long id, [FromForm] Diaper diaper)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }

            Infant preSaveInfant = context.Infants.AsNoTracking().FirstOrDefault(i => i.InfantId == id);

            if (!IsInfantOwner(preSaveInfant))
            {
                return(RedirectToPage("/Error/Error404"));
            }

            if (ModelState.IsValid)
            {
                diaper.DiaperId = default;
                diaper.Infant   = default;
                context.Diapers.Add(diaper);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index", "Dashboard", new { id = diaper.InfantId }));
            }
            return(View("DiaperEditor", DiaperViewModelFactory.Create(diaper, preSaveInfant)));
        }
Exemplo n.º 2
0
        // HTTP GEt
        public async Task <IActionResult> Delete(long id)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }

            Diaper diaper = await context.Diapers.Include(f => f.Infant).FirstOrDefaultAsync(f => f.DiaperId == id);

            if (!IsDiaperOwner(diaper))
            {
                return(RedirectToPage("/Error/Error404"));
            }

            return(View("DiaperEditor", DiaperViewModelFactory.Delete(diaper, diaper.Infant)));
        }
Exemplo n.º 3
0
        // HTTP Get
        public IActionResult Create(long id)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }
            Infant infant = context.Infants.FirstOrDefault(i => i.InfantId == id);

            if (!IsInfantOwner(infant))
            {
                return(RedirectToPage("/Error/Error404"));
            }

            Diaper diaper = new Diaper
            {
                Time     = DateTime.Now,
                InfantId = id
            };

            return(View("DiaperEditor", DiaperViewModelFactory.Create(diaper, infant)));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(long id, [FromForm] Diaper diaper)
        {
            if (!IsLoggedIn())
            {
                return(RedirectToPage("/Account/Login"));
            }

            Diaper preSaveDiaper = context.Diapers.AsNoTracking().Include(f => f.Infant).FirstOrDefault(d => d.DiaperId == id);

            if (!IsDiaperOwner(preSaveDiaper))
            {
                return(RedirectToPage("/Error/Error404"));
            }

            if (ModelState.IsValid)
            {
                context.Diapers.Update(diaper);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index", "Dashboard", new { id = diaper.InfantId }));
            }
            return(View("DiaperEditor", DiaperViewModelFactory.Edit(diaper, preSaveDiaper.Infant)));
        }