forked from Lombiq/Helpful-Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinqToDbSamplesController.cs
78 lines (67 loc) · 3.25 KB
/
LinqToDbSamplesController.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
using LinqToDB;
using Lombiq.HelpfulLibraries.LinqToDb;
using Lombiq.HelpfulLibraries.Samples.Models;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.ContentManagement.Records;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YesSql;
namespace Lombiq.HelpfulLibraries.Samples.Controllers;
// Some examples of querying the database with Lombiq.HelpfulLibraries.LinqToDb.
public class LinqToDbSamplesController : Controller
{
private readonly ISession _session;
public LinqToDbSamplesController(ISession session) => _session = session;
// A simple query on AutoroutePartIndex. Open this from under
// /Lombiq.HelpfulLibraries.Samples/LinqToDbSamples/SimpleQuery
public async Task<IActionResult> SimpleQuery()
{
var result = await _session.LinqQueryAsync(
accessor => accessor
.GetTable<AutoroutePartIndex>()
.Where(index => index.Path.Contains('a', StringComparison.OrdinalIgnoreCase))
.OrderByDescending(index => index.Path)
.ToListAsync(HttpContext.RequestAborted));
return Ok(result);
}
// A more complex query with an SQL JOIN. Open this from under
// /Lombiq.HelpfulLibraries.Samples/LinqToDbSamples/JoinQuery
public async Task<IActionResult> JoinQuery()
{
// This will fetch all items under the "blog/" path. If you used the Blog recipe then these will be all blog
// posts.
var result = await _session.LinqQueryAsync(
accessor =>
(from contentItemIndex in accessor.GetTable<ContentItemIndex>()
join autoroutePartIndex in accessor.GetTable<AutoroutePartIndex>()
on contentItemIndex.ContentItemId equals autoroutePartIndex.ContentItemId
where autoroutePartIndex.Path.StartsWith("blog/", StringComparison.OrdinalIgnoreCase)
select contentItemIndex.DisplayText)
.ToListAsync(HttpContext.RequestAborted));
return Ok(result);
}
// CRUD operations. Or rather, it's CUD but you've seen enough read operations above. Open this from under
// /Lombiq.HelpfulLibraries.Samples/LinqToDbSamples/Crud
public async Task<IActionResult> Crud()
{
var insertedCount = await _session.LinqTableQueryAsync<BookRecord, int>(table => table
.InsertAsync(
() => new BookRecord
{
Title = "Twenty Thousand Leagues Under the Seas",
Author = "Jules Verne",
},
HttpContext.RequestAborted));
var modifiedCount = await _session.LinqTableQueryAsync<BookRecord, int>(table => table
.Where(record => record.Title == "Twenty Thousand Leagues Under the Seas")
.Set(record => record.Title, "Around the World in Eighty Days")
.UpdateAsync(HttpContext.RequestAborted));
var deletedCount = await _session.LinqTableQueryAsync<BookRecord, int>(table => table
.Where(record => record.Author == "Jules Verne")
.DeleteAsync(HttpContext.RequestAborted));
return Ok(FormattableString.Invariant(
$"Inserted: {insertedCount}, modified: {modifiedCount}, deleted: {deletedCount}."));
}
}