Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ktnishide/part1 dotnet #3

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bubblenet/bin/Debug/net6.0/AppDotNet.dll",
"args": [],
"cwd": "${workspaceFolder}/bubblenet",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/bubblenet/AppDotNet.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/bubblenet/AppDotNet.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/bubblenet/AppDotNet.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
5 changes: 5 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# https://dart.dev/guides/libraries/private-files
.dart_tool/
.packages
pubspec.lock
*orders.json
27 changes: 27 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Official Dart image: https://hub.docker.com/_/dart
# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.12)
FROM dart:stable AS build

# Resolve app dependencies.
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get

# Copy app source code and AOT compile it.
COPY . .
# Ensure packages are still up-to-date if anything has changed
RUN dart pub get --offline
RUN dart compile exe lib/server.dart -o lib/server

# Build minimal serving image from AOT-compiled `/server` and required system
# libraries and configuration files stored in `/runtime/` from the build stage.
FROM scratch
COPY --from=build /runtime/ /
COPY --from=build /app/lib/ /app/lib/

# Include files in the /public directory to enable static asset handling
# COPY --from=build /app/public/ /public

# Start server.
EXPOSE 8000
CMD ["/app/lib/server"]
52 changes: 52 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
A simple Dart HTTP server using [package:shelf](https://pub.dev/packages/shelf).

- Listens on "any IP" (0.0.0.0) instead of loop-back (localhost, 127.0.0.1) to
allow remote connections.
- Defaults to listening on port `8000`, but this can be configured by setting
the `PORT` environment variable. (This is also the convention used by
[Cloud Run](https://cloud.google.com/run).)
- Includes `Dockerfile` for easy containerization

To deploy on [Cloud Run](https://cloud.google.com/run), follow
[these instructions](https://cloud.google.com/run/docs/quickstarts/build-and-deploy/other).

To run this server locally, run as follows:

```bash
$ dart run lib/server.dart
```


To run server tests locally, run as follows:

```bash
$ dart test
```

To build docker image, run as follows:

```bash
$ docker build -t <image name> .
```

To run docker image, run as follows:

```bash
$ docker run --publish 8000:8000 bubble
```

Curl call examples:

post order
```bash
$ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "store_number=1&order_number=1&flavours=brown sugar&toppings=tapioca pearls&amount_of_ice=Full&total_order_price=50.99" http://localhost:8000/order/
```

get report
```bash
$ curl -X GET http://localhost:8000/order/report?monthYear=2021-09
```


Interesting...
![image_size](image_size.png)
7 changes: 7 additions & 0 deletions app/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include: package:lints/recommended.yaml

# linter:
# rules:
# I'm curious the reason of this rule....
# for map is ok but list not
# avoid_function_literals_in_foreach_calls: false
3 changes: 3 additions & 0 deletions app/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tags:
presubmit-only:
skip: "Should only be run during presubmit"
Binary file added app/image_size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions app/lib/controllers/order_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'dart:convert';
import 'dart:developer';

import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

import '../models/order.dart';
import '../models/orders_report.dart';
import '../repositories/orders_repository.dart';
import '../shared/helpers.dart';

class OrderController {
OrderController(this.orderRepository);
final IOrdersRepository orderRepository;

Router get router {
final router = Router();

router.post('/', (Request request) async {
try {
final order = Order.fromMap(await request.parse());
order.validate();
orderRepository.saveOrder(order);
return Response.ok(order.toJson());
} catch (e, stack) {
return handleError(e, stack);
}
});

router.get('/', (request) {
try {
return Response.ok(json.encode(orderRepository.getOrders()),
headers: {'Content-Type': 'application/json'});
} catch (e, stack) {
return handleError(e, stack);
}
});

router.get('/report<monthYear|.*>', (Request request) async {
try {
final monthYear = request.url.queryParameters['monthYear'] ?? '';
final orders = orderRepository.getOrders();

OrdersReport report = OrdersReport();

return Response.ok(
json.encode(report.generateReport(orders, monthYear).toJson()),
headers: {'Content-Type': 'application/json'});
} catch (e, stack) {
return handleError(e, stack);
}
});

return router;
}

Response handleError(e, stack) {
log(e.toString());
log(stack.toString());
return Response.internalServerError(body: e.toString());
}
}
Empty file added app/lib/data/.gitkeep
Empty file.
Loading