/
ViewResult.cs
180 lines (164 loc) · 5.54 KB
/
ViewResult.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using LoveSeat;
using LoveSeat.Interfaces;
using LoveSeat.Support;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace LoveSeat
{
public class ViewResult<T> : ViewResult, IViewResult<T> where T:class
{
private readonly IObjectSerializer objectSerializer = null;
private CouchDictionary<T> dict = null;
public ViewResult(CouchResponse response, HttpWebRequest request, IObjectSerializer objectSerializer, bool includeDocs = false)
: base(response, request, includeDocs)
{
this.objectSerializer = objectSerializer;
}
public CouchDictionary<T> Dictionary
{
get
{
if (dict != null) return dict;
dict = new CouchDictionary<T>();
foreach (var row in this.Rows)
{
dict.Add(row.Value<JToken>("key").ToString(Formatting.None), objectSerializer.Deserialize<T>(row.Value<string>("value")));
}
return dict;
}
}
public IEnumerable<T> Items
{
get
{
if (objectSerializer == null)
{
throw new InvalidOperationException("ObjectSerializer must be set in order to use the generic view.");
}
var values = this.IncludeDocs ? this.RawDocs : this.RawValues;
return values.Select(item => objectSerializer.Deserialize<T>(item));
}
}
}
public class ViewResult : IViewResult
{
private readonly CouchResponse response;
private readonly HttpWebRequest request;
private JObject json = null;
public JObject Json { get { return json ?? (json = JObject.Parse(response.ResponseString)); } }
public ViewResult(CouchResponse response, HttpWebRequest request, bool includeDocs = false)
{
this.response = response;
this.request = request;
this.IncludeDocs = includeDocs;
}
/// <summary>
/// Typically won't be needed. Provided for debuging assistance
/// </summary>
public HttpWebRequest Request { get { return request; } }
public HttpStatusCode StatusCode { get { return response.StatusCode; } }
public string Etag { get { return response.ETag; } }
public int TotalRows
{
get
{
if (Json["total_rows"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
return Json["total_rows"].Value<int>();
}
}
public int OffSet
{
get
{
if (Json["offset"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
return Json["offset"].Value<int>();
}
}
public IEnumerable<JToken> Rows
{
get
{
if (Json["rows"] == null) throw new CouchException(request, response, Json["reason"].Value<string>());
return (JArray)Json["rows"];
}
}
/// <summary>
/// Only populated when IncludeDocs is true
/// </summary>
public IEnumerable<JToken> Docs
{
get
{
return this.Rows.Select(x => x["doc"]);
}
}
public bool IncludeDocs { get; private set; }
public JToken[] Keys
{
get
{
var arry = (JArray)Json["rows"];
return arry.Select(item => item["key"]).ToArray();
}
}
/// <summary>
/// An IEnumerable of strings insteda of the IEnumerable of JTokens
/// </summary>
public IEnumerable<string> RawRows
{
get
{
var arry = (JArray)Json["rows"];
return arry == null
? new List<string> ()
: arry.Select(item => item.ToString());
}
}
public IEnumerable<string> RawValues
{
get
{
var arry = (JArray)Json["rows"];
return arry == null
? new List<string>()
: arry.Select(item => item["value"].ToString());
}
}
public IEnumerable<string> RawDocs
{
get
{
var arry = (JArray)Json["rows"];
return arry == null
? new List<string>()
: arry.Select(item => item["doc"].ToString());
}
}
public string RawString
{
get { return response.ResponseString; }
}
public bool Equals(IListResult other)
{
if (string.IsNullOrEmpty(Etag) || string.IsNullOrEmpty(other.Etag)) return false;
return Etag == other.Etag;
}
public override string ToString()
{
return response.ResponseString;
}
/// <summary>
/// Provides a formatted version of the json returned from this Result. (Avoid this method in favor of RawString as it's much more performant)
/// </summary>
public string FormattedResponse { get { return Json.ToString(Formatting.Indented); } }
public HttpWebResponse Response
{
get { throw new NotImplementedException(); }
}
}
}