/
NoteController.cs
executable file
·102 lines (83 loc) · 2.92 KB
/
NoteController.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NoteTaker.Models;
using Newtonsoft.Json;
using System.Net;
namespace NoteTaker.Controllers
{
public class NoteController : Controller
{
//
// GET: /Note/
public ActionResult Index()
{
var notes = GetNotes();
var tags = GetTags();
return View(new NoteList()
{
Notes=notes.Items.Select(n=>n.Document),
Tags=tags.Items.ToDictionary(x=>x.Key,x=>Int32.Parse(x.Value))
});
}
public ActionResult Tagged(string id)
{
var notes = GetNotesByTag(id);
var tags = GetTags();
return View("Index",new NoteList()
{
Notes = notes.Items.Select(n => n.Document),
Tags = tags.Items.ToDictionary(x => x.Key, x => Int32.Parse(x.Value))
});
}
public DocumentCollection<Note> GetNotes()
{
var json = Couch.Uri.Get("_design/Notes/_view/all?include_docs=true");
return JsonConvert.DeserializeObject<DocumentCollection<Note>>(json);
}
public DocumentCollection<Note> GetNotesByTag(string tag)
{
var json = Couch.Uri.Get("_design/Tags/_view/all?key=\""+tag+"\"&reduce=false&include_docs=true");
return JsonConvert.DeserializeObject<DocumentCollection<Note>>(json);
}
public DocumentCollection<object> GetTags()
{
var json = Couch.Uri.Get("_design/Tags/_view/all?group_level=1");
return JsonConvert.DeserializeObject<DocumentCollection<object>>(json);
}
public ActionResult Create()
{
var note = new Note() { Id = Guid.NewGuid() };
return View("Edit", note);
}
public ActionResult Edit(Guid id)
{
var json=Couch.Uri.Get(id);
return View(JsonConvert.DeserializeObject<Note>(json));
}
[HttpPost]
public ActionResult Edit(Note note)
{
try
{
Couch.Uri.Put(note.Id, JsonConvert.SerializeObject(note));
return RedirectToAction("Edit", new { id = note.Id });
}
catch (WebException ex)
{
var statusCode = ((HttpWebResponse)ex.Response).StatusCode;
if (statusCode != HttpStatusCode.Conflict)
throw;
ModelState.AddModelError("Revision", "Conflict Detected");
return View(note);
}
}
public ActionResult Delete(Guid id,string rev)
{
var json = Couch.Uri.Delete(id,rev);
return RedirectToAction("Index");
}
}
}