Skip to content

Commit 5f10200

Browse files
authored
Merge pull request #26 from DiyorMarket/history-endpoints
History endpoints
2 parents 46e64d7 + 97451da commit 5f10200

File tree

57 files changed

+3284
-149
lines changed

Some content is hidden

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

57 files changed

+3284
-149
lines changed

CheckDrive.Api/CheckDrive.Api/CheckDrive.Api.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@
3434
<ProjectReference Include="..\CheckDrive.TestDataCreator\CheckDrive.TestDataCreator.csproj" />
3535
</ItemGroup>
3636

37+
<ItemGroup>
38+
<Folder Include="logs\" />
39+
</ItemGroup>
40+
3741
</Project>

CheckDrive.Api/CheckDrive.Api/Controllers/Auth/AuthController.cs CheckDrive.Api/CheckDrive.Api/Controllers/AuthController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using CheckDrive.Application.Interfaces.Auth;
33
using Microsoft.AspNetCore.Mvc;
44

5-
namespace CheckDrive.Api.Controllers.Auth;
5+
namespace CheckDrive.Api.Controllers;
66

77
[Route("api/auth")]
88
[ApiController]

CheckDrive.Api/CheckDrive.Api/Controllers/CarsController.cs

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CheckDrive.Application.DTOs.Car;
22
using CheckDrive.Application.Interfaces;
3+
using CheckDrive.Application.QueryParameters;
34
using Microsoft.AspNetCore.Mvc;
45

56
namespace CheckDrive.Api.Controllers;
@@ -16,10 +17,47 @@ public CarsController(ICarService carService)
1617
}
1718

1819
[HttpGet]
19-
public async Task<ActionResult<List<CarDto>>> GetAvailableCars()
20+
public async Task<ActionResult<List<CarDto>>> GetAllAsync(CarQueryParameters queryParameters)
2021
{
21-
var cars = await _carService.GetAvailableCarsAsync();
22+
var cars = await _carService.GetAllAsync(queryParameters);
2223

2324
return Ok(cars);
2425
}
26+
27+
[HttpGet("{id:int}", Name = "GetCarByIdAsync")]
28+
public async Task<ActionResult<CarDto>> GetByIdAsync(int id)
29+
{
30+
var car = await _carService.GetByIdAsync(id);
31+
32+
return Ok(car);
33+
}
34+
35+
[HttpPost]
36+
public async Task<ActionResult<CarDto>> CreateAsync(CreateCarDto car)
37+
{
38+
var createdCar = await _carService.CreateAsync(car);
39+
40+
return CreatedAtAction("GetCarByIdAsync", createdCar, new { id = createdCar.Id });
41+
}
42+
43+
[HttpPut("{id:int}")]
44+
public async Task<ActionResult<CarDto>> UpdateAsync(int id, UpdateCarDto car)
45+
{
46+
if (id != car.Id)
47+
{
48+
return BadRequest($"Route parameter id: {id} does not match with body parameter id: {car.Id}.");
49+
}
50+
51+
var updatedCar = await _carService.UpdateAsync(car);
52+
53+
return Ok(updatedCar);
54+
}
55+
56+
[HttpDelete("{id:int}")]
57+
public async Task<ActionResult> DeleteAsync(int id)
58+
{
59+
await _carService.DeleteAsync(id);
60+
61+
return NoContent();
62+
}
2563
}

CheckDrive.Api/CheckDrive.Api/Controllers/CheckPointsController.cs

+8
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ public async Task<ActionResult<CheckPointDto>> GetCurrentCheckPointByDriverIdAsy
3131

3232
return Ok(checkPoint);
3333
}
34+
35+
[HttpPut("{id:int}/cancel")]
36+
public async Task<ActionResult<CheckPointDto>> CancelCheckPoint(int id)
37+
{
38+
await _service.CancelCheckPointAsync(id);
39+
40+
return NoContent();
41+
}
3442
}

CheckDrive.Api/CheckDrive.Api/Controllers/OilMarksController.cs

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CheckDrive.Application.DTOs.OilMark;
22
using CheckDrive.Application.Interfaces;
3+
using CheckDrive.Application.QueryParameters;
34
using Microsoft.AspNetCore.Mvc;
45

56
namespace CheckDrive.Api.Controllers;
@@ -16,10 +17,47 @@ public OilMarksController(IOilMarkService oilMarkService)
1617
}
1718

1819
[HttpGet]
19-
public async Task<ActionResult<List<OilMarkDto>>> GetAsync()
20+
public async Task<ActionResult<List<OilMarkDto>>> GetAsync(OilMarkQueryParameters queryParameters)
2021
{
21-
var oilMarks = await _oilMarkService.GetAllAsync();
22+
var oilMarks = await _oilMarkService.GetAllAsync(queryParameters);
2223

2324
return Ok(oilMarks);
2425
}
26+
27+
[HttpGet("{id:int}", Name = "GetOilMarkById")]
28+
public async Task<ActionResult<OilMarkDto>> GetByIdAsync(int id)
29+
{
30+
var oilMark = await _oilMarkService.GetByIdAsync(id);
31+
32+
return oilMark;
33+
}
34+
35+
[HttpPost]
36+
public async Task<ActionResult<OilMarkDto>> CreateAsync(CreateOilMarkDto oilMark)
37+
{
38+
var createdOilMark = await _oilMarkService.CreateAsync(oilMark);
39+
40+
return CreatedAtAction("GetOilMarkById", oilMark, new { id = createdOilMark.Id });
41+
}
42+
43+
[HttpPut("{id:int}")]
44+
public async Task<ActionResult<OilMarkDto>> UpdateAsync(int id, UpdateOilMarkDto oilMark)
45+
{
46+
if (id != oilMark.Id)
47+
{
48+
return BadRequest($"Route parameter id: {id} does not match with body parameter id: {oilMark.Id}.");
49+
}
50+
51+
var updatedOilMark = await _oilMarkService.UpdateAsync(oilMark);
52+
53+
return Ok(updatedOilMark);
54+
}
55+
56+
[HttpDelete("{id:int}")]
57+
public async Task<ActionResult> DeleteAsync(int id)
58+
{
59+
await _oilMarkService.DeleteAsync(id);
60+
61+
return NoContent();
62+
}
2563
}

CheckDrive.Api/CheckDrive.Api/Controllers/Reviews/DispatcherReviewsController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CheckDrive.Api.Controllers.Reviews;
66

7-
[Route("api/reviews/dispatcher/{dispatcherId:int}")]
7+
[Route("api/reviews/dispatchers/{dispatcherId:int}")]
88
[ApiController]
99
public class DispatcherReviewsController : ControllerBase
1010
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using CheckDrive.Application.DTOs.CheckPoint;
3+
using CheckDrive.Application.DTOs.DoctorReview;
4+
using CheckDrive.Application.DTOs.OperatorReview;
5+
using CheckDrive.Application.DTOs.Review;
6+
using CheckDrive.Application.Interfaces.Review;
7+
8+
namespace CheckDrive.Api.Controllers.Reviews;
9+
10+
[Route("api/reviews/histories")]
11+
[ApiController]
12+
public class ReviewHistoriesController : ControllerBase
13+
{
14+
private readonly IReviewHistoryService _historyService;
15+
16+
public ReviewHistoriesController(IReviewHistoryService historyService)
17+
{
18+
_historyService = historyService ?? throw new ArgumentNullException(nameof(historyService));
19+
}
20+
21+
[HttpGet("drivers/{driverId:int}")]
22+
public async Task<ActionResult<List<CheckPointDto>>> GetDriverHistoriesAsync(int driverId)
23+
{
24+
var reviews = await _historyService.GetDriverHistoriesAsync(driverId);
25+
26+
return Ok(reviews);
27+
}
28+
29+
[HttpGet("doctors/{doctorId:int}")]
30+
public async Task<ActionResult<List<DoctorReviewDto>>> GetDoctorHistoriesAsync(int doctorId)
31+
{
32+
var reviews = await _historyService.GetDoctorHistoriesAsync(doctorId);
33+
34+
return Ok(reviews);
35+
}
36+
37+
[HttpGet("mechanics/{mechanicId:int}")]
38+
public async Task<ActionResult<List<MechanicReviewHistoryDto>>> GetMechanicHistoriesAsync(int mechanicId)
39+
{
40+
var reviews = await _historyService.GetMechanicHistoriesAsync(mechanicId);
41+
42+
return Ok(reviews);
43+
}
44+
45+
[HttpGet("operators/{operatorId:int}")]
46+
public async Task<ActionResult<List<OperatorReviewDto>>> GetOperatorHistoriesAsync(int operatorId)
47+
{
48+
var reviews = await _historyService.GetOperatorHistoriesAsync(operatorId);
49+
50+
return Ok(reviews);
51+
}
52+
}

CheckDrive.Api/CheckDrive.Api/Extensions/StartupExtensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using CheckDrive.Api.Helpers;
22
using CheckDrive.Api.Middlewares;
3-
using CheckDrive.Application.Constants;
43
using CheckDrive.Domain.Interfaces;
54
using CheckDrive.TestDataCreator.Configurations;
65
using Microsoft.AspNetCore.Identity;
@@ -28,4 +27,7 @@ public static IApplicationBuilder UseDatabaseSeeder(this WebApplication app)
2827

2928
return app;
3029
}
30+
31+
public static bool IsTesting(this IHostEnvironment environment)
32+
=> environment.IsEnvironment("Testing");
3133
}

CheckDrive.Api/CheckDrive.Api/Helpers/DatabaseSeeder.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Bogus;
2-
using CheckDrive.Domain.Entities;
1+
using CheckDrive.Domain.Entities;
32
using CheckDrive.Domain.Interfaces;
43
using CheckDrive.TestDataCreator;
54
using CheckDrive.TestDataCreator.Configurations;
@@ -9,8 +8,6 @@ namespace CheckDrive.Api.Helpers;
98

109
public static class DatabaseSeeder
1110
{
12-
private static Faker _faker = new Faker();
13-
1411
public static void SeedDatabase(
1512
ICheckDriveDbContext context,
1613
UserManager<IdentityUser> userManager,
@@ -289,6 +286,7 @@ private static void CreateManagers(ICheckDriveDbContext context, UserManager<Ide
289286

290287
context.SaveChanges();
291288
}
289+
292290
//private static void CreateEmployees(ICheckDriveDbContext context, UserManager<IdentityUser> userManager, DataSeedOptions options)
293291
//{
294292
// if (context.Users.Any())

CheckDrive.Api/CheckDrive.Api/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
var app = builder.Build();
2222

23-
if (!app.Environment.IsProduction())
23+
if (app.Environment.IsTesting())
2424
{
2525
app.UseDatabaseSeeder();
2626
}

CheckDrive.Api/CheckDrive.Application/BackgroundJobs/CarMonthlyMileageResetService.cs CheckDrive.Api/CheckDrive.Application/BackgroundJobs/CarMileageResetService.cs

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
namespace CheckDrive.Application.BackgroundJobs;
77

8-
internal sealed class CarMonthlyMileageResetService : BackgroundService
8+
internal sealed class CarMileageResetService : BackgroundService
99
{
1010
private readonly IServiceProvider _serviceProvider;
1111

12-
public CarMonthlyMileageResetService(IServiceProvider serviceProvider)
12+
public CarMileageResetService(IServiceProvider serviceProvider)
1313
{
1414
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
1515
}
@@ -24,6 +24,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2424
if (now.Day == 1 && now.Hour == 0)
2525
{
2626
await ResetCarMonthlyMileage(stoppingToken);
27+
28+
// Check if it's the first month of the year
29+
if (now.Month == 1)
30+
{
31+
await ResetCarYearlyMileage(stoppingToken);
32+
}
2733
}
2834

2935
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
@@ -38,5 +44,23 @@ private async Task ResetCarMonthlyMileage(CancellationToken stoppingToken)
3844
await context.Cars.ExecuteUpdateAsync(
3945
x => x.SetProperty(x => x.CurrentMonthMileage, 0),
4046
stoppingToken);
47+
48+
await context.Cars.ExecuteUpdateAsync(
49+
x => x.SetProperty(x => x.CurrentMonthFuelConsumption, 0),
50+
stoppingToken);
51+
}
52+
53+
private async Task ResetCarYearlyMileage(CancellationToken stoppingToken)
54+
{
55+
using var scope = _serviceProvider.CreateScope();
56+
var context = scope.ServiceProvider.GetRequiredService<ICheckDriveDbContext>();
57+
58+
await context.Cars.ExecuteUpdateAsync(
59+
x => x.SetProperty(x => x.CurrentYearMileage, 0),
60+
stoppingToken);
61+
62+
await context.Cars.ExecuteUpdateAsync(
63+
x => x.SetProperty(x => x.CurrentYearFuelConsumption, 0),
64+
stoppingToken);
4165
}
4266
}

CheckDrive.Api/CheckDrive.Application/DTOs/Car/CarDto.cs

+6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ public sealed record CarDto(
1010
int ManufacturedYear,
1111
int Mileage,
1212
int CurrentMonthMileage,
13+
int CurrentYearMileage,
14+
int MonthlyDistanceLimit,
1315
int YearlyDistanceLimit,
16+
decimal CurrentMonthFuelConsumption,
17+
decimal CurrentYearFuelConsumption,
18+
decimal MonthlyFuelConsumptionLimit,
19+
decimal YearlyFuelConsumptionLimit,
1420
decimal AverageFuelConsumption,
1521
decimal FuelCapacity,
1622
decimal RemainingFuel,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using CheckDrive.Domain.Enums;
2+
3+
namespace CheckDrive.Application.DTOs.Car;
4+
5+
public record CreateCarDto(
6+
string Model,
7+
string Color,
8+
string Number,
9+
int ManufacturedYear,
10+
int Mileage,
11+
int CurrentMonthMileage,
12+
int CurrentYearMileage,
13+
int MonthlyDistanceLimit,
14+
int YearlyDistanceLimit,
15+
decimal CurrentMonthFuelConsumption,
16+
decimal CurrentYearFuelConsumption,
17+
decimal MonthlyFuelConsumptionLimit,
18+
decimal YearlyFuelConsumptionLimit,
19+
decimal AverageFuelConsumption,
20+
decimal FuelCapacity,
21+
decimal RemainingFuel,
22+
CarStatus Status);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using CheckDrive.Domain.Enums;
2+
3+
namespace CheckDrive.Application.DTOs.Car;
4+
5+
public record UpdateCarDto(
6+
int Id,
7+
string Model,
8+
string Color,
9+
string Number,
10+
int ManufacturedYear,
11+
int Mileage,
12+
int CurrentMonthMileage,
13+
int CurrentYearMileage,
14+
int MonthlyDistanceLimit,
15+
int YearlyDistanceLimit,
16+
decimal CurrentMonthFuelConsumption,
17+
decimal CurrentYearFuelConsumption,
18+
decimal MonthlyFuelConsumptionLimit,
19+
decimal YearlyFuelConsumptionLimit,
20+
decimal AverageFuelConsumption,
21+
decimal FuelCapacity,
22+
decimal RemainingFuel,
23+
CarStatus Status);

CheckDrive.Api/CheckDrive.Application/DTOs/DispatcherReview/CreateDispatcherReviewDto.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public sealed record CreateDispatcherReviewDto(
88
string? Notes,
99
bool IsApprovedByReviewer,
1010
decimal? FuelConsumptionAdjustment,
11-
int? DistanceTravelledAdjustment)
11+
int? FinalMileageAdjustment)
1212
: CreateReviewDtoBase(
1313
ReviewerId: ReviewerId,
1414
Notes: Notes,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace CheckDrive.Application.DTOs.OilMark;
2+
3+
public sealed record CreateOilMarkDto(string Name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace CheckDrive.Application.DTOs.OilMark;
2+
3+
public sealed record UpdateOilMarkDto(int Id, string Name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CheckDrive.Domain.Enums;
2+
3+
namespace CheckDrive.Application.DTOs.Review;
4+
5+
public sealed record MechanicReviewHistoryDto(
6+
int ReviewId,
7+
int DriverId,
8+
string DriverName,
9+
string? Notes,
10+
DateTime Date,
11+
ReviewStatus Status);

0 commit comments

Comments
 (0)