-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBannerRepository.cs
97 lines (83 loc) · 2.86 KB
/
BannerRepository.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BannerFlow.Models;
using MongoDB.Driver;
using System.Configuration;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
using MongoDB.Driver.Linq;
using System.Diagnostics;
namespace BannerFlow.Repositories
{
public class BannerRepository : IBannerRepository
{
private MongoClient client = null;
private MongoServer server = null;
private MongoDatabase db = null;
private MongoCollection<Banner> banners = null;
public BannerRepository()
{
client = new MongoClient(ConfigurationManager.AppSettings["MongoDBConnectionString"]);
server = client.GetServer();
db = server.GetDatabase(ConfigurationManager.AppSettings["MongoDBDatabaseName"]);
banners = db.GetCollection<Banner>(ConfigurationManager.AppSettings["MongoDBCollection"]);
}
public int GetLastKey()
{
List<Banner> list = new List<Banner>();
if (this.banners.Count() == 0)
return 0;
list = this.banners.FindAll().ToList();
return list[list.Count()-1].Id;
}
public Banner Add(Banner item)
{
var result = this.banners.Save(item);
if(result.DocumentsAffected == 0 && result.HasLastErrorMessage)
{
Trace.TraceError(result.LastErrorMessage);
}
return item;
}
public Banner Get(int id)
{
Banner result = null;
var partialResult = this.banners.AsQueryable<Banner>()
.Where(p => p.Id == id)
.ToList();
result = partialResult.Count > 0
? partialResult[0]
: null;
return result;
}
public IEnumerable<Banner> GetAll()
{
List<Banner> result = new List<Banner>();
result = this.banners.FindAll().ToList();
return result;
}
public void Delete(int id)
{
var query = Query<Banner>.EQ(p => p.Id, id);
var result = this.banners.Remove(query);
if(result.DocumentsAffected == 0 && result.HasLastErrorMessage)
{
Trace.TraceError(result.LastErrorMessage);
}
}
public Banner Update(int id, Banner item)
{
var query = Query<Banner>.EQ(p => p.Id, id);
var update = Update<Banner>.Set(p => p.Html, item.Html)
.Set(p => p.Modified, item.Modified);
var result = this.banners.Update(query, update);
if(result.DocumentsAffected == 0 && result.HasLastErrorMessage)
{
Trace.TraceError(result.LastErrorMessage);
}
return item;
}
}
}