summaryrefslogtreecommitdiff
path: root/CoreWiki/Pages/Create.cshtml.cs
blob: d89b97b44c3fbc219dc68906949db99e1f8f1c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 CoreWiki.Models;
using NodaTime;

namespace CoreWiki.Pages
{
    public class CreateModel : PageModel
    {
        private readonly CoreWiki.Models.ApplicationDbContext _context;
        private readonly IClock _clock;

        public CreateModel(CoreWiki.Models.ApplicationDbContext context, IClock clock)
        {
            _context = context;
            _clock = clock;
        }

        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        public Article Article { get; set; } = default!;


        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid || _context.Articles == null || Article == null)
            {
                return Page();
            }

            Article.Published = _clock.GetCurrentInstant();
            _context.Articles.Add(Article);
            await _context.SaveChangesAsync();

            return Redirect($"/{Article.Topic}");
        }
    }
}