Skip to content
This repository was archived by the owner on Apr 23, 2023. It is now read-only.

Commit 932f189

Browse files
odata
1 parent 1cedb6f commit 932f189

File tree

13 files changed

+559
-0
lines changed

13 files changed

+559
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2027
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BooksODataService", "BooksODataService\BooksODataService.csproj", "{B2FB43AC-1F6A-4C43-AD2C-A6069CEEC77E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B2FB43AC-1F6A-4C43-AD2C-A6069CEEC77E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B2FB43AC-1F6A-4C43-AD2C-A6069CEEC77E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B2FB43AC-1F6A-4C43-AD2C-A6069CEEC77E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B2FB43AC-1F6A-4C43-AD2C-A6069CEEC77E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {46C2C8D5-119A-462D-9763-5D1F1ADAF4DB}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
13+
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.0.0-Nightly201802051329" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using BooksODataService.Models;
2+
using Microsoft.AspNet.OData;
3+
using Microsoft.AspNet.OData.Query;
4+
using Microsoft.EntityFrameworkCore;
5+
using System.Linq;
6+
7+
namespace BooksODataService.Controllers
8+
{
9+
public class BooksController : ODataController
10+
{
11+
private readonly BooksContext _booksContext;
12+
public BooksController(BooksContext booksContext)
13+
{
14+
_booksContext = booksContext;
15+
}
16+
17+
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
18+
public IQueryable<Book> Get(ODataQueryOptions options)
19+
{
20+
ODataValidationSettings settings = new ODataValidationSettings()
21+
{
22+
MaxExpansionDepth = 4
23+
};
24+
options.Validate(settings);
25+
var books = _booksContext.Books.Include(b => b.Chapters);
26+
return books;
27+
}
28+
29+
[EnableQuery()]
30+
public SingleResult<Book> Get([FromODataUri] int key)
31+
{
32+
return SingleResult.Create(_booksContext.Books.Where(b => b.Id == key));
33+
}
34+
35+
}
36+
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using BooksODataService.Models;
2+
using Microsoft.AspNet.OData;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.EntityFrameworkCore;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace BooksODataService.Controllers
11+
{
12+
public class ChaptersController : ODataController
13+
{
14+
private readonly BooksContext _booksContext;
15+
public ChaptersController(BooksContext booksContext)
16+
{
17+
_booksContext = booksContext;
18+
}
19+
20+
public IQueryable<BookChapter> Get() =>
21+
_booksContext.Chapters.Include(c => c.Book);
22+
23+
[EnableQuery]
24+
public SingleResult<BookChapter> Get([FromODataUri] int key) =>
25+
SingleResult.Create(_booksContext.Chapters.Where(c => c.Id == key));
26+
27+
}
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace BooksODataService.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
public class ValuesController : Controller
11+
{
12+
// GET api/values
13+
[HttpGet]
14+
public IEnumerable<string> Get()
15+
{
16+
return new string[] { "value1", "value2" };
17+
}
18+
19+
// GET api/values/5
20+
[HttpGet("{id}")]
21+
public string Get(int id)
22+
{
23+
return "value";
24+
}
25+
26+
// POST api/values
27+
[HttpPost]
28+
public void Post([FromBody]string value)
29+
{
30+
}
31+
32+
// PUT api/values/5
33+
[HttpPut("{id}")]
34+
public void Put(int id, [FromBody]string value)
35+
{
36+
}
37+
38+
// DELETE api/values/5
39+
[HttpDelete("{id}")]
40+
public void Delete(int id)
41+
{
42+
}
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace BooksODataService.Models
7+
{
8+
public class Book
9+
{
10+
public Book()
11+
{
12+
Chapters = new List<BookChapter>();
13+
}
14+
public int Id { get; set; }
15+
public string Isbn { get; set; }
16+
public string Title { get; set; }
17+
public string Publisher { get; set; }
18+
public List<BookChapter> Chapters { get; set; }
19+
}
20+
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace BooksODataService.Models
7+
{
8+
public class BookChapter
9+
{
10+
public int Id { get; set; }
11+
public int BookId { get; set; }
12+
public Book Book { get; set; }
13+
public string Title { get; set; }
14+
public int Number { get; set; }
15+
}
16+
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace BooksODataService.Models
9+
{
10+
public class BooksContext : DbContext
11+
{
12+
public BooksContext(DbContextOptions<BooksContext> options)
13+
: base(options)
14+
{
15+
16+
}
17+
18+
public DbSet<Book> Books { get; set; }
19+
public DbSet<BookChapter> Chapters { get; set; }
20+
protected override void OnModelCreating(ModelBuilder modelBuilder)
21+
{
22+
base.OnModelCreating(modelBuilder);
23+
var bookBuilder = modelBuilder.Entity<Book>();
24+
bookBuilder.HasMany(b => b.Chapters)
25+
.WithOne(c => c.Book)
26+
.HasForeignKey(c => c.BookId);
27+
bookBuilder.Property(b => b.Title)
28+
.HasMaxLength(120)
29+
.IsRequired();
30+
bookBuilder.Property(b => b.Publisher)
31+
.HasMaxLength(40)
32+
.IsRequired(false);
33+
bookBuilder.Property(b => b.Isbn)
34+
.HasMaxLength(20)
35+
.IsRequired(false);
36+
var chapterBuilder = modelBuilder.Entity<BookChapter>();
37+
chapterBuilder.Property(c => c.Title)
38+
.HasMaxLength(120);
39+
chapterBuilder.HasOne(c => c.Book)
40+
.WithMany(b => b.Chapters)
41+
.HasForeignKey(c => c.BookId);
42+
}
43+
}
44+
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace BooksODataService
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
23+
.Build();
24+
}
25+
}

0 commit comments

Comments
 (0)