Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**/.git
**/.vs
**/.vscode
**/bin
**/obj
**/TestResults
**/*.user
**/*.suo
README.md
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.{json,yml,yaml}]
indent_size = 2

[*.cs]
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Bb]in/
[Oo]bj/
.vs/
.vscode/
.idea/
*.user
*.suo
*.swp
*.cache
TestResults/
coverage/
8 changes: 8 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project>
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# syntax=docker/dockerfile:1

FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

COPY PowerplantProductionPlan.sln ./
COPY Directory.Build.props ./
COPY global.json ./
COPY src/Powerplant.Api/Powerplant.Api.csproj src/Powerplant.Api/
COPY tests/Powerplant.Api.Tests/Powerplant.Api.Tests.csproj tests/Powerplant.Api.Tests/

RUN dotnet restore PowerplantProductionPlan.sln

COPY . .
RUN dotnet publish src/Powerplant.Api/Powerplant.Api.csproj \
--configuration Release \
--output /app/publish \
/p:UseAppHost=false

FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS runtime
WORKDIR /app

ENV ASPNETCORE_URLS=http://+:8888 \
DOTNET_RUNNING_IN_CONTAINER=true

EXPOSE 8888

COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "Powerplant.Api.dll"]
30 changes: 30 additions & 0 deletions PowerplantProductionPlan.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 18
VisualStudioVersion = 18.6.11819.183
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Powerplant.Api", "src\Powerplant.Api\Powerplant.Api.csproj", "{916AB86E-B84F-4A61-A719-82C889D357CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Powerplant.Api.Tests", "tests\Powerplant.Api.Tests\Powerplant.Api.Tests.csproj", "{C58E130D-DB33-4932-ABCA-59BD77995162}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{916AB86E-B84F-4A61-A719-82C889D357CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{916AB86E-B84F-4A61-A719-82C889D357CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{916AB86E-B84F-4A61-A719-82C889D357CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{916AB86E-B84F-4A61-A719-82C889D357CF}.Release|Any CPU.Build.0 = Release|Any CPU
{C58E130D-DB33-4932-ABCA-59BD77995162}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C58E130D-DB33-4932-ABCA-59BD77995162}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C58E130D-DB33-4932-ABCA-59BD77995162}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C58E130D-DB33-4932-ABCA-59BD77995162}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61019972-2ADA-4956-AFF8-501E3C16F430}
EndGlobalSection
EndGlobal
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
# powerplant-coding-challenge
# Powerplant Coding Challenge

REST API for calculating a powerplant production plan.

## Start the API

The API listens on port `8888` when started with Docker.

Swagger UI is available at:

```text
http://localhost:8888/swagger
```

The production-plan endpoint is:

```text
POST http://localhost:8888/productionplan
```

## Using Docker

Run these commands from the repository root, where the `Dockerfile` is located:

```bash
docker build -t powerplant-coding-challenge .
docker run --rm -p 8888:8888 --name powerplant-coding-challenge powerplant-coding-challenge
```

The container may log something like:

```text
Now listening on: http://[::]:8888
```

That is normal. It means the API is listening inside the container on port `8888`.
From your host machine, open:

```text
http://localhost:8888/swagger
```

## Without Docker

1. Open the solution in Visual Studio or JetBrains Rider.
2. Build the solution.
3. Select the API project as the startup project:

```text
powerplant-coding-challenge/src/Powerplant.Api/Powerplant.Api.csproj
```

4. Run the project.

## Requirements

- .NET 9 SDK, when running without Docker
- Docker Desktop, when running with Docker



## Welcome !
Expand Down
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "9.0.100",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Powerplant.Api.Domain;
using Powerplant.Api.Generated;

namespace Powerplant.Api.Application.Abstractions
{
public interface IProductionPlanService
{
IReadOnlyCollection<ProductionPlanEntry> Calculate(
decimal Load,
FuelPrices fuelPrices,
List<PowerPlant> powerPlants);
}
}
38 changes: 38 additions & 0 deletions src/Powerplant.Api/Application/Services/ProductionPlanService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Powerplant.Api.Application.Abstractions;
using Powerplant.Api.Domain;
using Powerplant.Api.Generated;

namespace Powerplant.Api.Application.Services
{
public class ProductionPlanService : IProductionPlanService
{
public IReadOnlyCollection<ProductionPlanEntry> Calculate(
decimal Load,
FuelPrices fuelPrices,
List<PowerPlant> powerPlants)
{

var result = new List<ProductionPlanEntry>();
var remainingLoad = Load;

var sortedPowerPlants = powerPlants
.OrderBy(p => p.CostPerMWh(fuelPrices))
.ToList();

foreach (PowerPlant powerPlant in sortedPowerPlants)
{
var possibleLoad = powerPlant.GetPossibleLoad(remainingLoad, fuelPrices);
result.Add(new ProductionPlanEntry
{
Name = powerPlant.Name,
Power = possibleLoad
});
remainingLoad -= possibleLoad;

}
return result;

}

}
}
158 changes: 158 additions & 0 deletions src/Powerplant.Api/Contracts/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
openapi: 3.0.3
info:
title: Powerplant Production Plan API
version: 1.0.0
description: API for the powerplant production-plan.
servers:
- url: /
paths:
/productionplan:
post:
operationId: calculateProductionPlan
tags:
- ProductionPlan
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ProductionPlanRequest'
responses:
'200':
description: Production plan
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ProductionPlanResponseItem'
'400':
description: Invalid request payload.
content:
application/problem+json:
schema:
$ref: '#/components/schemas/ProblemDetails'
'422':
description: The request is syntactically valid but no feasible plan can satisfy the load.
content:
application/problem+json:
schema:
$ref: '#/components/schemas/ProblemDetails'
'500':
description: Unexpected server error.
content:
application/problem+json:
schema:
$ref: '#/components/schemas/ProblemDetails'
'501':
description: Production-plan algorithm is not implemented yet in this scaffold.
content:
application/problem+json:
schema:
$ref: '#/components/schemas/ProblemDetails'
components:
schemas:
ProductionPlanRequest:
type: object
required:
- load
- fuels
- powerplants
properties:
load:
type: number
format: decimal
example: 480
fuels:
$ref: '#/components/schemas/Fuels'
powerplants:
type: array
items:
$ref: '#/components/schemas/PowerPlantDto'

Fuels:
type: object
required:
- gas(euro/MWh)
- kerosine(euro/MWh)
- co2(euro/ton)
- wind(%)
additionalProperties:
type: number
format: decimal
example:
gas(euro/MWh): 13.4
kerosine(euro/MWh): 50.8
co2(euro/ton): 20
wind(%): 60

PowerPlantDto:
type: object
required:
- name
- type
- efficiency
- pmin
- pmax
properties:
name:
type: string
example: gasfiredbig1
type:
type: string
enum:
- gasfired
- turbojet
- windturbine
example: gasfired
efficiency:
type: number
format: decimal
example: 0.53
pmin:
type: number
format: decimal
example: 100
pmax:
type: number
format: decimal
example: 460

ProductionPlanResponseItem:
type: object
required:
- name
- p
properties:
name:
type: string
example: windpark1
p:
type: number
format: decimal
example: 90.0
ProblemDetails:
type: object
properties:
type:
type: string
nullable: true
title:
type: string
nullable: true
example: Bad Request
status:
type: integer
format: int32
nullable: true
example: 400
detail:
type: string
nullable: true
example: The request payload is invalid.
instance:
type: string
nullable: true
example: /productionplan
additionalProperties: true

Loading