-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeroesController.cs
115 lines (89 loc) · 3.47 KB
/
HeroesController.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
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbASPNetWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
namespace MongoDbASPNetWebAPI
{
public class OurHeroesController : ApiController
{
MongoClient client = null;
IMongoDatabase database = null;
IMongoCollection<BsonDocument> collection = null;
public OurHeroesController()
{
client = new MongoClient(ConfigurationManager.AppSettings["mongodburl"]);
database = client.GetDatabase(ConfigurationManager.AppSettings["mongodbname"]);
collection = database.GetCollection<BsonDocument>(ConfigurationManager.AppSettings["mongodbcollname"]);
}
// GET api/<controller>
public HttpResponseMessage Get()
{
List<HeroItem> list = new List<HeroItem>();
var documents = collection.Find(new BsonDocument()).ToList();
foreach (var document in documents)
{
HeroItem item = new HeroItem();
var str = document.Elements.Where(e => e.Name == "id").FirstOrDefault().Value.ToString();
int id = 0;
int.TryParse(str, out id);
item.id = id;
item.name = document.Elements.Where(e => e.Name == "name").FirstOrDefault().Value.ToString();
list.Add(item);
}
OurResponse response= new OurResponse();
response.message ="";
response.data = list;
return Request.CreateResponse(HttpStatusCode.OK, response);
}
// GET api/<controller>/5
public HeroItem Get(int id)
{
List<HeroItem> list = new List<HeroItem>();
var myquery = "{\"id\":" + id + "}";
var documents = collection.Find(myquery).ToList();
foreach (var document in documents)
{
HeroItem item = new HeroItem();
var str = document.Elements.ElementAt(1).Value.ToString();
id = 0;
if (!int.TryParse(str, out id))
{
continue;
}
item.id = id;
item.name = document.Elements.ElementAt(2).Value.ToString();
list.Add(item);
}
return list.FirstOrDefault();
}
// POST api/<controller>
[AcceptVerbs("OPTIONS", "POST")]
[HttpPost]
public HttpResponseMessage Post(HeroItem value)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("id", new Random().Next(1000));
dict.Add("name", value.name);
BsonDocument doc = new BsonDocument(dict);
collection.InsertOne(doc);
return Request.CreateResponse(HttpStatusCode.OK, "0");
}
// PUT api/<controller>/5
[AcceptVerbs("OPTIONS", "PUT")]
[HttpPut]
public HttpResponseMessage Put(HeroItem value)
{
var filter = Builders<BsonDocument>.Filter.Eq<int>("id", value.id);
var update = Builders<BsonDocument>.Update.Set("name", value.name);
var result = collection.UpdateOne(filter, update);
return Request.CreateResponse(HttpStatusCode.OK, result.ModifiedCount);
}
}
}