Adding Custom data provider for today

This commit is contained in:
José David Guillén 2022-01-11 20:21:05 +00:00
parent 08a83f72b9
commit 1fd7941cf2
27 changed files with 4561 additions and 4 deletions

View File

@ -17,3 +17,17 @@
APP_ENV=dev
APP_SECRET=8757cce92158b769a2f0ff84b15f43a2
###< symfony/framework-bundle ###
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7"
# DATABASE_URL="postgresql://symfony:ChangeMe@127.0.0.1:5432/app?serverVersion=13&charset=utf8"
DATABASE_URL="mysql://dt2.es_calgasene:92TIqQrLJ7@192.168.1.100/dt2.es_calgasene?serverVersion=5.5"
###< doctrine/doctrine-bundle ###
###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN='^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$'
###< nelmio/cors-bundle ###

View File

@ -1 +1,12 @@
#T
#Step by step
>symfony check:requirements
>symfony new symfony
>symfony server:start
>composer require api
Created src\entity\prices.php
> php bin/console doctrine:schema:update --dump-sql
> php bin/console doctrine:schema:update --force

View File

@ -7,15 +7,31 @@
"php": ">=7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.6",
"composer/package-versions-deprecated": "1.11.99.4",
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.10",
"nelmio/cors-bundle": "^2.2",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.2",
"symfony/asset": "5.4.*",
"symfony/console": "5.4.*",
"symfony/dotenv": "5.4.*",
"symfony/expression-language": "5.4.*",
"symfony/flex": "^1.17|^2",
"symfony/framework-bundle": "5.4.*",
"symfony/property-access": "5.4.*",
"symfony/property-info": "5.4.*",
"symfony/proxy-manager-bridge": "5.4.*",
"symfony/runtime": "5.4.*",
"symfony/security-bundle": "5.4.*",
"symfony/serializer": "5.4.*",
"symfony/twig-bundle": "5.4.*",
"symfony/validator": "5.4.*",
"symfony/yaml": "5.4.*"
},
"require-dev": {
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,

File diff suppressed because it is too large Load Diff

View File

@ -2,4 +2,10 @@
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
];

View File

@ -0,0 +1,7 @@
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
patch_formats:
json: ['application/merge-patch+json']
swagger:
versions: [3]

View File

@ -0,0 +1,17 @@
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App

View File

@ -0,0 +1,6 @@
doctrine_migrations:
migrations_paths:
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
'DoctrineMigrations': '%kernel.project_dir%/migrations'
enable_profiler: '%kernel.debug%'

View File

@ -0,0 +1,10 @@
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null

View File

@ -0,0 +1,17 @@
doctrine:
orm:
auto_generate_proxy_classes: false
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system

View File

@ -0,0 +1,40 @@
security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
users_in_memory: { memory: null }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: users_in_memory
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
when@test:
security:
password_hashers:
# By default, password hashers are resource intensive and take time. This is
# important to generate secure password hashes. In tests however, secure hashes
# are not important, waste resources and increase test times. The following
# reduces the work factor to the lowest possible values.
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
algorithm: auto
cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon

View File

@ -0,0 +1,4 @@
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'

View File

@ -0,0 +1,3 @@
framework:
validation:
not_compromised_password: false

View File

@ -0,0 +1,6 @@
twig:
default_path: '%kernel.project_dir%/templates'
when@test:
twig:
strict_variables: true

View File

@ -0,0 +1,8 @@
framework:
validation:
email_validation_mode: html5
# Enables validator auto-mapping support.
# For instance, basic validation constraints will be inferred from Doctrine's metadata.
#auto_mapping:
# App\Entity\: []

View File

@ -0,0 +1,7 @@
controllers:
resource: ../../src/Controller/
type: annotation
kernel:
resource: ../../src/Kernel.php
type: annotation

View File

@ -0,0 +1,4 @@
api_platform:
resource: .
type: api_platform
prefix: /api

View File

@ -0,0 +1,8 @@
version: '3'
services:
###> doctrine/doctrine-bundle ###
database:
ports:
- "5432"
###< doctrine/doctrine-bundle ###

View File

@ -0,0 +1,21 @@
version: '3'
services:
###> doctrine/doctrine-bundle ###
database:
image: postgres:${POSTGRES_VERSION:-13}-alpine
environment:
POSTGRES_DB: ${POSTGRES_DB:-app}
# You should definitely change the password in production
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-ChangeMe}
POSTGRES_USER: ${POSTGRES_USER:-symfony}
volumes:
- db-data:/var/lib/postgresql/data:rw
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
# - ./docker/db/data:/var/lib/postgresql/data:rw
###< doctrine/doctrine-bundle ###
volumes:
###> doctrine/doctrine-bundle ###
db-data:
###< doctrine/doctrine-bundle ###

0
backend/symfony/migrations/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,45 @@
<?php
namespace App\DataProvider;
use Doctrine\Persistence\ManagerRegistry;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\PriceToday;
use App\Entity\Prices;
class PriceTodayProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
public function getCollection(string $resourceClass, string $operationName = null) : iterable
{
// Get the today's price (from cache)
$em = $this->doctrine->getManager();
$dql = "SELECT hour_start, hour_end, price FROM prices WHERE day = CURRENT_DATE()";
$query = $em->getConnection()->prepare($dql);
$prices = $query->executeQuery()->fetchAllAssociative(); // \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY
if ( count($prices)<=0 ) {
// Get the today's price (from internet)
}
$priceToday = new PriceToday();
$priceToday->id = 1;
$priceToday->min = 0.01;
$priceToday->max = 0.10;
$priceToday->avg = 0.05;
$priceToday->now = 0.03;
return [$priceToday];
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return $resourceClass === PriceToday::class;
}
}

0
backend/symfony/src/Entity/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Action\NotFoundAction;
/**
* @ApiResource(
* itemOperations={
* "get"={
* "method"="GET",
* "controller"=NotFoundAction::class,
* "read"=false,
* "output"=false,
* },
* },
* collectionOperations={"get"}
* )
*
*/
class PriceToday
{
/**
* @ApiProperty(identifier=true)
*/
public ?int $id = null;
public ?int $min = null;
public ?int $max = null;
public ?int $avg = null;
public ?int $now = null;
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayColletion;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="prices")
* @ApiResource(
* itemOperations={"get"},
* collectionOperations={"get"}
* )
*/
class Prices
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\Column(type="date")
*/
public ?\DateTime $day = null;
/**
* @ORM\Column(type="integer")
*/
public ?int $hourStart = null;
/**
* @ORM\Column(type="integer")
*/
public ?int $hourEnd = null;
/**
* @ORM\Column(type="decimal", precision="8", scale="6")
* @Assert\NotBlank()
*/
public float $price = 0.0;
/******** METHODS ********/
public function getId()
{
return $this->id;
}
/**
* Prepersist gets triggered on Insert
* @ORM\PrePersist
*/
public function updatedTimestamps()
{
if ($this->day == null) {
$this->day = new \DateTime('now');
}
}
public function __toString()
{
return $this->name;
}
}

View File

View File

@ -1,4 +1,134 @@
{
"api-platform/core": {
"version": "2.6",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "2.5",
"ref": "05b57782a78c21a664a42055dc11cf1954ca36bb"
},
"files": [
"config/packages/api_platform.yaml",
"config/routes/api_platform.yaml",
"src/Entity/.gitignore"
]
},
"composer/package-versions-deprecated": {
"version": "1.11.99.4"
},
"doctrine/annotations": {
"version": "1.13",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.0",
"ref": "a2759dd6123694c8d901d0ec80006e044c2e6457"
},
"files": [
"config/routes/annotations.yaml"
]
},
"doctrine/cache": {
"version": "2.1.1"
},
"doctrine/collections": {
"version": "1.6.8"
},
"doctrine/common": {
"version": "3.2.1"
},
"doctrine/dbal": {
"version": "3.2.1"
},
"doctrine/deprecations": {
"version": "v0.5.3"
},
"doctrine/doctrine-bundle": {
"version": "2.5",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "2.4",
"ref": "f98f1affe028f8153a459d15f220ada3826b5aa2"
},
"files": [
"config/packages/doctrine.yaml",
"config/packages/prod/doctrine.yaml",
"config/packages/test/doctrine.yaml",
"src/Entity/.gitignore",
"src/Repository/.gitignore"
]
},
"doctrine/doctrine-migrations-bundle": {
"version": "3.2",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "3.1",
"ref": "ee609429c9ee23e22d6fa5728211768f51ed2818"
},
"files": [
"config/packages/doctrine_migrations.yaml",
"migrations/.gitignore"
]
},
"doctrine/event-manager": {
"version": "1.1.1"
},
"doctrine/inflector": {
"version": "2.0.4"
},
"doctrine/instantiator": {
"version": "1.4.0"
},
"doctrine/lexer": {
"version": "1.2.1"
},
"doctrine/migrations": {
"version": "3.3.2"
},
"doctrine/orm": {
"version": "2.10.4"
},
"doctrine/persistence": {
"version": "2.2.3"
},
"doctrine/sql-formatter": {
"version": "1.1.2"
},
"fig/link-util": {
"version": "1.1.2"
},
"friendsofphp/proxy-manager-lts": {
"version": "v1.0.5"
},
"laminas/laminas-code": {
"version": "4.5.1"
},
"nelmio/cors-bundle": {
"version": "2.2",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.5",
"ref": "6bea22e6c564fba3a1391615cada1437d0bde39c"
},
"files": [
"config/packages/nelmio_cors.yaml"
]
},
"phpdocumentor/reflection-common": {
"version": "2.2.0"
},
"phpdocumentor/reflection-docblock": {
"version": "5.3.0"
},
"phpdocumentor/type-resolver": {
"version": "1.6.0"
},
"phpstan/phpdoc-parser": {
"version": "1.2.0"
},
"psr/cache": {
"version": "1.0.1"
},
@ -8,9 +138,15 @@
"psr/event-dispatcher": {
"version": "1.0.0"
},
"psr/link": {
"version": "1.0.0"
},
"psr/log": {
"version": "1.1.4"
},
"symfony/asset": {
"version": "v5.4.0"
},
"symfony/cache": {
"version": "v5.4.2"
},
@ -38,6 +174,9 @@
"symfony/deprecation-contracts": {
"version": "v2.5.0"
},
"symfony/doctrine-bridge": {
"version": "v5.4.2"
},
"symfony/dotenv": {
"version": "v5.4.2"
},
@ -50,6 +189,9 @@
"symfony/event-dispatcher-contracts": {
"version": "v2.5.0"
},
"symfony/expression-language": {
"version": "v5.4.0"
},
"symfony/filesystem": {
"version": "v5.4.0"
},
@ -93,6 +235,9 @@
"symfony/http-kernel": {
"version": "v5.4.2"
},
"symfony/password-hasher": {
"version": "v5.4.2"
},
"symfony/polyfill-intl-grapheme": {
"version": "v1.24.0"
},
@ -111,6 +256,15 @@
"symfony/polyfill-php81": {
"version": "v1.24.0"
},
"symfony/property-access": {
"version": "v5.4.2"
},
"symfony/property-info": {
"version": "v5.4.2"
},
"symfony/proxy-manager-bridge": {
"version": "v5.4.2"
},
"symfony/routing": {
"version": "5.4",
"recipe": {
@ -127,19 +281,93 @@
"symfony/runtime": {
"version": "v5.4.1"
},
"symfony/security-bundle": {
"version": "5.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "5.3",
"ref": "09b5e809bd7a992061febd05b797c64a2d93b5cd"
},
"files": [
"config/packages/security.yaml"
]
},
"symfony/security-core": {
"version": "v5.4.2"
},
"symfony/security-csrf": {
"version": "v5.4.0"
},
"symfony/security-guard": {
"version": "v5.4.0"
},
"symfony/security-http": {
"version": "v5.4.2"
},
"symfony/serializer": {
"version": "v5.4.2"
},
"symfony/service-contracts": {
"version": "v2.5.0"
},
"symfony/stopwatch": {
"version": "v5.4.0"
},
"symfony/string": {
"version": "v5.4.2"
},
"symfony/translation-contracts": {
"version": "v2.5.0"
},
"symfony/twig-bridge": {
"version": "v5.4.0"
},
"symfony/twig-bundle": {
"version": "5.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "5.4",
"ref": "bffbb8f1a849736e64006735afae730cb428b6ff"
},
"files": [
"config/packages/twig.yaml",
"templates/base.html.twig"
]
},
"symfony/validator": {
"version": "5.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "4.3",
"ref": "3eb8df139ec05414489d55b97603c5f6ca0c44cb"
},
"files": [
"config/packages/test/validator.yaml",
"config/packages/validator.yaml"
]
},
"symfony/var-dumper": {
"version": "v5.4.2"
},
"symfony/var-exporter": {
"version": "v5.4.2"
},
"symfony/web-link": {
"version": "v5.4.0"
},
"symfony/yaml": {
"version": "v5.4.2"
},
"twig/twig": {
"version": "v3.3.7"
},
"webmozart/assert": {
"version": "1.10.0"
},
"willdurand/negotiation": {
"version": "3.0.0"
}
}

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text></svg>">
{# Run `composer require symfony/webpack-encore-bundle` to start using Symfony UX #}
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>