Skip to content

Commit 018d51c

Browse files
committed
revert samples changes
1 parent b864fe8 commit 018d51c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+6918
-0
lines changed

samples/01-carapp/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Car Application
2+
3+
This is just a quick demonstration of ellar web framework. Modifications will be made on this project as the Ellar project
4+
evolves to become better.
5+
6+
## Installation
7+
having setup up your virtual environment,
8+
```shell
9+
$(venv) poetry
10+
```
11+
## Project Setup
12+
```shell
13+
$(venv) poetry install
14+
```
15+
16+
## Start Development Server
17+
Start the dev server by running the code below:
18+
19+
```bash
20+
ellar runserver --reload
21+
```
22+
23+
Once the server is up, you can visit [SwaggerDoc](http://localhost:8000/docs#) or [ReDoc](http://localhost:8000/redoc#)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Define endpoints routes in python class-based fashion
3+
example:
4+
5+
@Controller("/dogs", tag="Dogs", description="Dogs Resources")
6+
class MyController(ControllerBase):
7+
@get('/')
8+
def index(self):
9+
return {'detail': "Welcome Dog's Resources"}
10+
"""
11+
12+
import typing_extensions as types
13+
from ellar.common import (
14+
Body,
15+
Controller,
16+
ControllerBase,
17+
Query,
18+
Version,
19+
get,
20+
post,
21+
render,
22+
)
23+
from ellar.openapi import ApiTags
24+
25+
from .schemas import CarListFilter, CreateCarSerializer
26+
from .services import CarRepository
27+
28+
29+
@Controller("/car")
30+
@ApiTags(
31+
description="Example of Car Resource with <strong>Controller</strong>",
32+
name="CarController",
33+
)
34+
class CarController(ControllerBase):
35+
def __init__(self, repo: CarRepository):
36+
self.repo = repo
37+
38+
@get("/list-html")
39+
@render()
40+
async def list(self):
41+
return self.repo.get_all()
42+
43+
@post()
44+
async def create(self, payload: types.Annotated[CreateCarSerializer, Body()]):
45+
result = self.repo.create_car(payload)
46+
result.update(message="This action adds a new car")
47+
return result
48+
49+
@get("/{car_id:str}")
50+
async def get_one(self, car_id: str):
51+
return f"This action returns a #{car_id} car"
52+
53+
@get()
54+
async def get_all(self, query: CarListFilter = Query()):
55+
res = {
56+
"cars": self.repo.get_all(),
57+
"message": f"This action returns all cars at limit={query.limit}, offset={query.offset}",
58+
}
59+
return res
60+
61+
@post("/", name="v2_create")
62+
@Version("v2")
63+
async def create_v2(self, payload: Body[CreateCarSerializer]):
64+
result = self.repo.create_car(payload)
65+
result.update(message="This action adds a new car - version 2")
66+
return result
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
@Module(
3+
controllers=[MyController],
4+
providers=[
5+
YourService,
6+
ProviderConfig(IService, use_class=AService),
7+
ProviderConfig(IFoo, use_value=FooService()),
8+
],
9+
routers=(routerA, routerB)
10+
statics='statics',
11+
template='template_folder',
12+
# base_directory -> default is the `car` folder
13+
)
14+
class MyModule(ModuleBase):
15+
def register_providers(self, container: Container) -> None:
16+
# for more complicated provider registrations
17+
pass
18+
19+
"""
20+
21+
from ellar.common import Module
22+
from ellar.core import ModuleBase
23+
from ellar.di import Container
24+
25+
from .controllers import CarController
26+
from .routers import router
27+
from .services import CarRepository
28+
29+
30+
@Module(
31+
controllers=[CarController],
32+
providers=[CarRepository],
33+
routers=[router],
34+
template_folder="views",
35+
static_folder="statics",
36+
)
37+
class CarModule(ModuleBase):
38+
"""
39+
Car Module
40+
"""
41+
42+
def register_providers(self, container: Container) -> None:
43+
"""for more complicated provider registrations, use container.register_instance(...)"""
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Define endpoints routes in python function fashion
3+
example:
4+
5+
my_router = ModuleRouter("/cats", tag="Cats", description="Cats Resource description")
6+
7+
@my_router.get('/')
8+
def index(request: Request):
9+
return {'detail': 'Welcome to Cats Resource'}
10+
"""
11+
12+
from dataclasses import dataclass
13+
from typing import List
14+
15+
from ellar.common import (
16+
Inject,
17+
ModuleRouter,
18+
render,
19+
render_template,
20+
)
21+
from ellar.core import Request
22+
from ellar.openapi import ApiTags
23+
24+
from .services import CarRepository
25+
26+
router = ModuleRouter("/car-as-router")
27+
ApiTags(
28+
name="Router",
29+
description="Example of Car Resource from a <strong>ModuleRouter</strong>",
30+
)(router)
31+
32+
33+
@dataclass
34+
class CarObject:
35+
name: str
36+
model: str
37+
38+
39+
@router.get(response={200: List[CarObject]})
40+
async def get_cars(repo: Inject[CarRepository]):
41+
return repo.get_all()
42+
43+
44+
@router.http_route("/html", methods=["get", "post"])
45+
@render("index.html")
46+
async def get_car_html(repo: Inject[CarRepository]):
47+
return repo.get_all()
48+
49+
50+
@router.get("/html/outside")
51+
async def get_car_html_with_render(
52+
repo: Inject[CarRepository], request: Inject[Request]
53+
):
54+
return render_template("car/list.html", request=request, model=repo.get_all())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Define Serializers/DTOs
3+
Example:
4+
5+
class ASampleDTO(Serializer):
6+
name: str
7+
age: t.Optional[int] = None
8+
9+
for dataclasses, Inherit from DataclassSerializer
10+
11+
@dataclass
12+
class ASampleDTO(DataclassSerializer):
13+
name: str
14+
age: t.Optional[int] = None
15+
"""
16+
17+
from ellar.common.serializer import Serializer
18+
from pydantic import Field
19+
20+
21+
class CreateCarSerializer(Serializer):
22+
name: str
23+
year: int = Field(..., gt=0)
24+
model: str
25+
26+
27+
class CarListFilter(Serializer):
28+
offset: int = 1
29+
limit: int = 10
30+
31+
32+
class CarSerializer(Serializer):
33+
id: int
34+
name: str
35+
year: int
36+
model: str
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Create a provider and declare its scope
3+
4+
@injectable
5+
class AProvider
6+
pass
7+
8+
@injectable(scope=transient_scope)
9+
class BProvider
10+
pass
11+
"""
12+
13+
import typing as t
14+
15+
from ellar.di import injectable, singleton_scope
16+
17+
from .schemas import CarSerializer, CreateCarSerializer
18+
19+
20+
@injectable(scope=singleton_scope)
21+
class CarRepository:
22+
def __init__(self):
23+
self._cars: t.List[CarSerializer] = []
24+
25+
def create_car(self, data: CreateCarSerializer) -> dict:
26+
data = CarSerializer(id=len(self._cars) + 1, **data.dict())
27+
self._cars.append(data)
28+
return data.dict()
29+
30+
def get_all(self) -> t.List[CarSerializer]:
31+
return self._cars
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* stylelint-disable selector-list-comma-newline-after */
2+
3+
.blog-header {
4+
line-height: 1;
5+
border-bottom: 1px solid #e5e5e5;
6+
}
7+
8+
.blog-header-logo {
9+
font-family: "Playfair Display", Georgia, "Times New Roman", serif/*rtl:Amiri, Georgia, "Times New Roman", serif*/;
10+
font-size: 2.25rem;
11+
}
12+
13+
.blog-header-logo:hover {
14+
text-decoration: none;
15+
}
16+
17+
h1, h2, h3, h4, h5, h6 {
18+
font-family: "Playfair Display", Georgia, "Times New Roman", serif/*rtl:Amiri, Georgia, "Times New Roman", serif*/;
19+
}
20+
21+
.display-4 {
22+
font-size: 2.5rem;
23+
}
24+
@media (min-width: 768px) {
25+
.display-4 {
26+
font-size: 3rem;
27+
}
28+
}
29+
30+
.nav-scroller {
31+
position: relative;
32+
z-index: 2;
33+
height: 2.75rem;
34+
overflow-y: hidden;
35+
}
36+
37+
.nav-scroller .nav {
38+
display: flex;
39+
flex-wrap: nowrap;
40+
padding-bottom: 1rem;
41+
margin-top: -1px;
42+
overflow-x: auto;
43+
text-align: center;
44+
white-space: nowrap;
45+
-webkit-overflow-scrolling: touch;
46+
}
47+
48+
.nav-scroller .nav-link {
49+
padding-top: .75rem;
50+
padding-bottom: .75rem;
51+
font-size: .875rem;
52+
}
53+
54+
.card-img-right {
55+
height: 100%;
56+
border-radius: 0 3px 3px 0;
57+
}
58+
59+
.flex-auto {
60+
flex: 0 0 auto;
61+
}
62+
63+
.h-250 { height: 250px; }
64+
@media (min-width: 768px) {
65+
.h-md-250 { height: 250px; }
66+
}
67+
68+
/* Pagination */
69+
.blog-pagination {
70+
margin-bottom: 4rem;
71+
}
72+
.blog-pagination > .btn {
73+
border-radius: 2rem;
74+
}
75+
76+
/*
77+
* Blog posts
78+
*/
79+
.blog-post {
80+
margin-bottom: 4rem;
81+
}
82+
.blog-post-title {
83+
margin-bottom: .25rem;
84+
font-size: 2.5rem;
85+
}
86+
.blog-post-meta {
87+
margin-bottom: 1.25rem;
88+
color: #727272;
89+
}
90+
91+
/*
92+
* Footer
93+
*/
94+
.blog-footer {
95+
padding: 2.5rem 0;
96+
color: #727272;
97+
text-align: center;
98+
background-color: #f9f9f9;
99+
border-top: .05rem solid #e5e5e5;
100+
}
101+
.blog-footer p:last-child {
102+
margin-bottom: 0;
103+
}

0 commit comments

Comments
 (0)