Browse Source

feat: add readme.md

fix: exclude route from built in laravel CSRF validation
main
Tovi Jaeschke-Rogers 3 months ago
parent
commit
c55303af1f
2 changed files with 90 additions and 46 deletions
  1. +87
    -45
      README.md
  2. +3
    -1
      bootstrap/app.php

+ 87
- 45
README.md View File

@ -1,66 +1,108 @@
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel Logo"></a></p>
# Date Duration Calculation
<p align="center">
<a href="https://github.com/laravel/framework/actions"><img src="https://github.com/laravel/framework/workflows/tests/badge.svg" alt="Build Status"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
</p>
This Laravel application calculates the duration between two provided dates,
and outputs the number of days, week days, and weeks.
## About Laravel
A conversion parameter can also be provided to the endpoint to convert the
result to seconds, minutes, hours, or years.
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
## General Decisions made
- [Simple, fast routing engine](https://laravel.com/docs/routing).
- [Powerful dependency injection container](https://laravel.com/docs/container).
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
- [Robust background job processing](https://laravel.com/docs/queues).
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
- I've used Laravel for this, as its PHP, and something I am familiar with.
Laravel is accessible, powerful, and provides tools required for large, robust applications.
- The endpoint is a POST request to `/calculate-duration`, this was done in
order to leverage Laravel's validation rules. This also allows the end user to
specify their data in JSON, which can be easier than managing query parameters.
## Learning Laravel
- Due to the specifications detailing that the duration should be initially
calculated in days, week days, and weeks, and then convert it to the another
value, the timezone requirement seemed redundant. This is because the most
granular interval the specifications allows is a day. If, after more
discussion, the requirement changed to needing the seconds between two
_datetimes_, I would have changed the CalculateDuration service class to
calculate the duration with the granularity of a second.
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
- The weeks are calculated using `$start->diff($end)`, as opposed to using the
PHP `DateInterval` and `DatePeriod` classes. This is primarily due to the
rounding issues I experienced during dev, i.e 2024-08-01 2024-08-17 equals 4
weeks, as the duration exists _over_ 3 weeks, but does not span the _whole_ of
the 3 weeks. The duration of the interval is then also added `$this->end`
property during the calculation (in order to include the final day in the
calculation when using P1D for the `DateInterval`).
You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch.
If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
## General Code Structure
## Laravel Sponsors
1. `routes/web.php`
- Defines the POST request route
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com).
2. `app/Http/Controllers/CalculateDurationController.php`
- Handles the POST request
### Premium Partners
3. `app/Http/Requests/CalculateDurationRequest.php`
- Performs validation on the POST request data
- **[Vehikl](https://vehikl.com/)**
- **[Tighten Co.](https://tighten.co)**
- **[WebReinvent](https://webreinvent.com/)**
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
- **[64 Robots](https://64robots.com)**
- **[Curotec](https://www.curotec.com/services/technologies/laravel/)**
- **[Cyber-Duck](https://cyber-duck.co.uk)**
- **[DevSquad](https://devsquad.com/hire-laravel-developers)**
- **[Jump24](https://jump24.co.uk)**
- **[Redberry](https://redberry.international/laravel/)**
- **[Active Logic](https://activelogic.com)**
- **[byte5](https://byte5.de)**
- **[OP.GG](https://op.gg)**
4. `app/Http/Resources/CalculateDurationResource.php`
- Returns the calculated durations
## Contributing
5. `app/Services/CalculateDuration.php`
- Calculates the duration, and converts to the `DurationModifier` if required
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
6. `app/Enums/DurationModifier.php`
- Enum for the possible conversions, also used to validated the `convert_to`
POST request parameter
## Code of Conduct
7. `app/DTO/DurationResult.php`
- Data transfer object, so we aren't parsing
around arrays, and needing to assume the array keys exist.
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
8. `tests/*`
Unit and integration tests.
## Security Vulnerabilities
## Room for improvement
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed.
There are two things I would probably improve, if this was intended for production
## License
1. I'd like to avoid the ternary on CalculateDuration.php:56, although I
couldn't think of a quick way to simplify it. Its been a long day.
2. Tests relating to timezones are lacking, due to the aforementioned redundancy.
## Running the code
All is well, as all is dockerized.
```sh
composer install
./vendor/bin/sail up
```
```sh
./vendor/bin/sail test
```
## Testing manually
You can the convert_to value to `second`, `minute`, `hour`, `year`, or omit the key entirely.
```sh
read -r -d '' VAR << EOM
{
"start": {
"date": "2024-08-01"
},
"end": {
"date": "2024-08-21"
},
"convert_to": null
}
EOM
curl -v \
--data "${VAR}" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
"http://localhost:80/calculate-duration"
```
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

+ 3
- 1
bootstrap/app.php View File

@ -11,7 +11,9 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
$middleware->validateCsrfTokens(except: [
'calculate-duration',
]);
})
->withExceptions(function (Exceptions $exceptions) {
//


Loading…
Cancel
Save