-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathValuesController.cs
More file actions
203 lines (188 loc) · 8.85 KB
/
ValuesController.cs
File metadata and controls
203 lines (188 loc) · 8.85 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Arch.EntityFrameworkCore.UnitOfWork.Collections;
using Arch.EntityFrameworkCore.UnitOfWork.Host.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using UnitOfWork.Host.Models;
namespace Arch.EntityFrameworkCore.UnitOfWork.Host.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private ILogger<ValuesController> _logger;
// 1. IRepositoryFactory used for readonly scenario;
// 2. IUnitOfWork used for read/write scenario;
// 3. IUnitOfWork<TContext> used for multiple databases scenario;
public ValuesController(IUnitOfWork unitOfWork, ILogger<ValuesController> logger)
{
_unitOfWork = unitOfWork;
_logger = logger;
// seeding
var repo = _unitOfWork.GetRepository<Blog>(hasCustomRepository: true);
if (repo.Count() == 0)
{
repo.Insert(new Blog
{
Id = 1,
Url = "/a/" + 1,
Title = $"a{1}",
Posts = new List<Post>{
new Post
{
Id = 1,
Title = "A",
Content = "A's content",
Comments = new List<Comment>
{
new Comment
{
Id = 1,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 2,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 3,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 2,
Title = "B",
Content = "B's content",
Comments = new List<Comment>
{
new Comment
{
Id = 4,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 5,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 6,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 3,
Title = "C",
Content = "C's content",
Comments = new List<Comment>
{
new Comment
{
Id = 7,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 8,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 9,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 4,
Title = "D",
Content = "D's content",
Comments = new List<Comment>
{
new Comment
{
Id = 10,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 11,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 12,
Title = "c",
Content = "c's content",
}
},
}
},
});
_unitOfWork.SaveChanges();
}
}
// GET api/values
[HttpGet]
public async Task<IList<Blog>> Get()
{
return await _unitOfWork.GetRepository<Blog>().GetAllAsync(include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));
}
// GET api/values/Page/5/10
[HttpGet("Page/{pageIndex}/{pageSize}")]
public async Task<IPagedList<Blog>> Get(int pageIndex, int pageSize)
{
// projection
var items = _unitOfWork.GetRepository<Blog>().GetPagedList(b => new { Name = b.Title, Link = b.Url });
return await _unitOfWork.GetRepository<Blog>().GetPagedListAsync(pageIndex: pageIndex, pageSize: pageSize);
}
// GET api/values/Search/a1
[HttpGet("Search/{term}")]
public async Task<IPagedList<Blog>> Get(string term)
{
_logger.LogInformation("demo about first or default with include");
var item = _unitOfWork.GetRepository<Blog>().GetFirstOrDefault(predicate: x => x.Title.Contains(term), include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));
_logger.LogInformation("demo about first or default without include");
item = _unitOfWork.GetRepository<Blog>().GetFirstOrDefault(predicate: x => x.Title.Contains(term), orderBy: source => source.OrderByDescending(b => b.Id));
_logger.LogInformation("demo about first or default with projection");
var projection = _unitOfWork.GetRepository<Blog>().GetFirstOrDefault(b => new { Name = b.Title, Link = b.Url }, predicate: x => x.Title.Contains(term));
return await _unitOfWork.GetRepository<Blog>().GetPagedListAsync(predicate: x => x.Title.Contains(term));
}
// GET api/values/4
[HttpGet("{id}")]
public async Task<Blog> Get(int id)
{
return await _unitOfWork.GetRepository<Blog>().FindAsync(id);
}
// POST api/values
[HttpPost]
public async Task Post([FromBody]Blog value)
{
var repo = _unitOfWork.GetRepository<Blog>();
repo.Insert(value);
await _unitOfWork.SaveChangesAsync();
}
}
}