summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/WikiControllerController.cs
blob: 435e5bd71bf1f61817bd339ba341dbf658815155 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    public class WikiController : Controller
    {
        public ActionResult Index(string id = "")
        {
            var db = new ApplicationDbContext();
            var model = new WikiViewModel();
            var wikicategories = db.WikiCategories.Where(x => x.Parent == "none");
            model.Categories = wikicategories;
            model.Page = db.WikiPages.FirstOrDefault(x => x.Id == id);
            return View(model);
        }

        public ActionResult Random()
        {
            var db = new ApplicationDbContext();
            var rnd = new Random();
            var index = rnd.Next(0, db.WikiPages.Count());
            if (db.WikiPages.Count() == 0)
                return RedirectToAction("Index");

            var wiki = db.WikiPages.ToArray()[index].Id;
            return RedirectToAction("Index", new { id = wiki });
        }

        [Authorize]
        public ActionResult AddPage()
        {
            return View(new AddWikiPageViewModel());
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddPage(AddWikiPageViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            var db = new ApplicationDbContext();
            var page = new WikiPage();
            page.CategoryId = model.ParentId;
            page.Contents = model.Content;
            page.Name = model.Name;

            string allowed = "abcdefghijklmnopqrstuvwxyz1234567890_";
            page.Id = page.Name.ToLower();
            foreach(var c in page.Id.ToCharArray())
            {
                if (!allowed.Contains(c))
                    page.Id = page.Id.Replace(c, '_');
            }

            var edit = new ForumPostEdit();
            edit.Id = Guid.NewGuid().ToString();
            edit.Parent = page.Id;
            edit.EditedAt = DateTime.Now;
            edit.EditReason = "Page created.";
            edit.PreviousState = page.Contents;
            edit.UserId = User.Identity.GetUserId();

            db.ForumPostEdits.Add(edit);
            db.WikiPages.Add(page);
            db.SaveChanges();

            return RedirectToAction("Index", new { id = edit.Id });
        }
    }
}