Skip to content

Commit

Permalink
add example odata API to recreate #3198
Browse files Browse the repository at this point in the history
  • Loading branch information
iress-ljm authored and jeremydmiller committed May 20, 2024
1 parent d1b9e68 commit 47b8f18
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
20 changes: 20 additions & 0 deletions src/AspNetODataWithMarten/AspNetODataWithMarten.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Marten\Marten.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/AspNetODataWithMarten/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Marten;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;

namespace AspNetODataWithMarten.Controllers;

[Route("/weatherforecast")]
public class WeatherForecastController : ODataController
{
[HttpGet]
[EnableQuery]
public IActionResult Get([FromServices] IDocumentSession session )
{
return Ok(session.Query<WeatherForecast>());
}
}
24 changes: 24 additions & 0 deletions src/AspNetODataWithMarten/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 5045

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["AspNetODataWithMarten/AspNetODataWithMarten.csproj", "AspNetODataWithMarten/"]
RUN dotnet restore "./AspNetODataWithMarten/AspNetODataWithMarten.csproj"
COPY . .
WORKDIR "/src/AspNetODataWithMarten"
RUN dotnet build "./AspNetODataWithMarten.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./AspNetODataWithMarten.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AspNetODataWithMarten.dll"]
38 changes: 38 additions & 0 deletions src/AspNetODataWithMarten/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Marten;
using Microsoft.AspNetCore.OData;
using Microsoft.OData.ModelBuilder;
using System.ComponentModel.DataAnnotations;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMarten((options) =>
{
options.Connection(Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING"));
options.RegisterDocumentType<WeatherForecast>();
options.DatabaseSchemaName = "public";
});

builder.Services.AddControllers().AddOData(options =>
{
var entityBuilder = new ODataModelBuilder();

entityBuilder.EntityType<WeatherForecast>().HasKey(t => t.Id);
entityBuilder.EntitySet<WeatherForecast>("WeatherForecast");

options.EnableQueryFeatures().AddRouteComponents("odata", entityBuilder.GetEdmModel());
});

var app = builder.Build();

app.MapControllers();

app.Run();

public record WeatherForecast()
{
public Guid Id { get; init; }
public DateOnly Date { get; init; }
public int Temperature { get; init; }
public string? Summary { get; init; }
public decimal? Humidity { get; init; }
}
14 changes: 14 additions & 0 deletions src/AspNetODataWithMarten/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"profiles": {
"Container (Dockerfile)": {
"commandName": "Docker",
"launchBrowser": fa,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/weatherforecast",
"environmentVariables": {
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true
}
},
"$schema": "http://json.schemastore.org/launchsettings.json"
}
16 changes: 16 additions & 0 deletions src/docker-compose.dcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<DockerPublishLocally>False</DockerPublishLocally>
<ProjectGuid>0f9f373e-a47a-4028-aaeb-04e2fe4e7f24</ProjectGuid>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/weatherforecast</DockerServiceUrl>
<DockerServiceName>aspnetodatawithmarten</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.4'

services:
aspnetodatawithmarten:
build:
context: .
dockerfile: AspNetODataWithMarten/Dockerfile
ports:
- "8080:8080"
environment:
ASPNETCORE_ENVIRONMENT: "Development"
ASPNETCORE_HTTP_PORTS: 8080
POSTGRES_CONNECTION_STRING: "Host=postgres;Port=5432;Database=weatherforecast;Username=postgres;password=Password123!"
depends_on:
- postgres

postgres:
image: postgres
environment:
POSTGRES_DB: "weatherforecast"
POSTGRES_PASSWORD: "Password123!"
ports:
- "5432:5432"
11 changes: 11 additions & 0 deletions src/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Docker Compose": {
"commandName": "DockerCompose",
"commandVersion": "1.0",
"serviceActions": {
"aspnetodatawithmarten": "StartDebugging"
}
}
}
}

0 comments on commit 47b8f18

Please sign in to comment.