Compare commits
4 Commits
9c372b31a6
...
eaa06029bb
| Author | SHA1 | Date | |
|---|---|---|---|
| eaa06029bb | |||
| 9e16d6004a | |||
| 0df7449a99 | |||
| 7f61e011ed |
@@ -25,6 +25,7 @@ import {
|
|||||||
throwError
|
throwError
|
||||||
} from 'rxjs';
|
} from 'rxjs';
|
||||||
import { CarCardComponent } from './components/car-card/car-card.component';
|
import { CarCardComponent } from './components/car-card/car-card.component';
|
||||||
|
import { SelectedCarService } from '@vegasco-web/modules/entries/services/selected-car.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-entries',
|
selector: 'app-entries',
|
||||||
@@ -52,6 +53,7 @@ import { CarCardComponent } from './components/car-card/car-card.component';
|
|||||||
export class CarsComponent {
|
export class CarsComponent {
|
||||||
private readonly carClient = inject(CarClient);
|
private readonly carClient = inject(CarClient);
|
||||||
private readonly messageService = inject(MessageService);
|
private readonly messageService = inject(MessageService);
|
||||||
|
private readonly selectedCarService = inject(SelectedCarService);
|
||||||
|
|
||||||
protected readonly nonDeletedCars$: Observable<Car[]>;
|
protected readonly nonDeletedCars$: Observable<Car[]>;
|
||||||
|
|
||||||
@@ -78,13 +80,21 @@ export class CarsComponent {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
onCarDeleted(entry: Car): void {
|
onCarDeleted(car: Car): void {
|
||||||
this.deletedCars$.next([...this.deletedCars$.value, entry.id]);
|
this.deletedCars$.next([...this.deletedCars$.value, car.id]);
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'success',
|
severity: 'success',
|
||||||
summary: 'Auto gelöscht',
|
summary: 'Auto gelöscht',
|
||||||
detail: 'Das Auto wurde erfolgreich gelöscht.',
|
detail: 'Das Auto wurde erfolgreich gelöscht.',
|
||||||
});
|
});
|
||||||
|
this.resetSelectedCarIfDeleted(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetSelectedCarIfDeleted(car: Car) {
|
||||||
|
const selectedCarId = this.selectedCarService.getSelectedCarId();
|
||||||
|
if (selectedCarId === car.id) {
|
||||||
|
this.selectedCarService.setSelectedCarId(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleGetCarsError(error: unknown): Observable<never> {
|
private handleGetCarsError(error: unknown): Observable<never> {
|
||||||
|
|||||||
@@ -201,6 +201,14 @@ export class EditCarComponent implements OnInit {
|
|||||||
'Die Anwendung scheint falsche Daten an den Server zu senden.',
|
'Die Anwendung scheint falsche Daten an den Server zu senden.',
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
case error.status === 409:
|
||||||
|
this.messageService.add({
|
||||||
|
severity: 'warn',
|
||||||
|
summary: 'Konflikt',
|
||||||
|
detail:
|
||||||
|
'Es existiert bereits ein Auto mit diesem Namen. Bitte wähle einen anderen Namen.',
|
||||||
|
});
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.error(error);
|
console.error(error);
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FluentValidation.Results;
|
using FluentValidation.Results;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Vegasco.Server.Api.Authentication;
|
using Vegasco.Server.Api.Authentication;
|
||||||
using Vegasco.Server.Api.Common;
|
using Vegasco.Server.Api.Common;
|
||||||
using Vegasco.Server.Api.Persistence;
|
using Vegasco.Server.Api.Persistence;
|
||||||
@@ -19,7 +20,8 @@ public static class CreateCar
|
|||||||
.WithTags("Cars")
|
.WithTags("Cars")
|
||||||
.WithDescription("Creates a new car")
|
.WithDescription("Creates a new car")
|
||||||
.Produces<Response>(201)
|
.Produces<Response>(201)
|
||||||
.ProducesValidationProblem();
|
.ProducesValidationProblem()
|
||||||
|
.Produces(409);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Validator : AbstractValidator<Request>
|
public class Validator : AbstractValidator<Request>
|
||||||
@@ -59,10 +61,18 @@ public static class CreateCar
|
|||||||
|
|
||||||
Car car = new()
|
Car car = new()
|
||||||
{
|
{
|
||||||
Name = request.Name,
|
Name = request.Name.Trim(),
|
||||||
UserId = userId
|
UserId = userId
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var isDuplicate = await dbContext.Cars
|
||||||
|
.AnyAsync(x => x.Name.ToUpper() == request.Name.ToUpper(), cancellationToken);
|
||||||
|
|
||||||
|
if (isDuplicate)
|
||||||
|
{
|
||||||
|
return TypedResults.Conflict();
|
||||||
|
}
|
||||||
|
|
||||||
await dbContext.Cars.AddAsync(car, cancellationToken);
|
await dbContext.Cars.AddAsync(car, cancellationToken);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FluentValidation.Results;
|
using FluentValidation.Results;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Vegasco.Server.Api.Authentication;
|
using Vegasco.Server.Api.Authentication;
|
||||||
using Vegasco.Server.Api.Common;
|
using Vegasco.Server.Api.Common;
|
||||||
using Vegasco.Server.Api.Persistence;
|
using Vegasco.Server.Api.Persistence;
|
||||||
@@ -19,7 +20,8 @@ public static class UpdateCar
|
|||||||
.WithDescription("Updates a car by ID")
|
.WithDescription("Updates a car by ID")
|
||||||
.Produces<Response>()
|
.Produces<Response>()
|
||||||
.ProducesValidationProblem()
|
.ProducesValidationProblem()
|
||||||
.Produces(404);
|
.Produces(404)
|
||||||
|
.Produces(409);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Validator : AbstractValidator<Request>
|
public class Validator : AbstractValidator<Request>
|
||||||
@@ -53,7 +55,15 @@ public static class UpdateCar
|
|||||||
return TypedResults.NotFound();
|
return TypedResults.NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
car.Name = request.Name;
|
var isDuplicate = await dbContext.Cars
|
||||||
|
.AnyAsync(x => x.Name.ToUpper() == request.Name.ToUpper(), cancellationToken);
|
||||||
|
|
||||||
|
if (isDuplicate)
|
||||||
|
{
|
||||||
|
return TypedResults.Conflict();
|
||||||
|
}
|
||||||
|
|
||||||
|
car.Name = request.Name.Trim();
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
Response response = new(car.Id.Value, car.Name);
|
Response response = new(car.Id.Value, car.Name);
|
||||||
|
|||||||
@@ -51,34 +51,38 @@ public static class GetConsumptions
|
|||||||
ApplicationDbContext dbContext,
|
ApplicationDbContext dbContext,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
List<Consumption> consumptions = await dbContext.Consumptions
|
Dictionary<CarId, List<Consumption>> consumptionsByCar = await dbContext.Consumptions
|
||||||
.OrderByDescending(x => x.DateTime)
|
.OrderByDescending(x => x.DateTime)
|
||||||
.Include(x => x.Car)
|
.Include(x => x.Car)
|
||||||
.ToListAsync(cancellationToken);
|
.GroupBy(x => x.CarId)
|
||||||
|
.ToDictionaryAsync(x => x.Key, x => x.ToList(), cancellationToken);
|
||||||
|
|
||||||
List<ResponseDto> responses = [];
|
List<ResponseDto> responses = [];
|
||||||
|
|
||||||
for (int i = 0; i < consumptions.Count; i++)
|
foreach (var consumptions in consumptionsByCar.Select(x => x.Value))
|
||||||
{
|
{
|
||||||
Consumption consumption = consumptions[i];
|
for (int i = 0; i < consumptions.Count; i++)
|
||||||
|
|
||||||
double? literPer100Km = null;
|
|
||||||
|
|
||||||
bool isLast = i == consumptions.Count - 1;
|
|
||||||
if (!isLast)
|
|
||||||
{
|
{
|
||||||
Consumption previousConsumption = consumptions[i + 1];
|
Consumption consumption = consumptions[i];
|
||||||
double distanceDiff = consumption.Distance - previousConsumption.Distance;
|
|
||||||
literPer100Km = consumption.Amount / (distanceDiff / 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
responses.Add(new ResponseDto(
|
double? literPer100Km = null;
|
||||||
consumption.Id.Value,
|
|
||||||
consumption.DateTime,
|
bool isLast = i == consumptions.Count - 1;
|
||||||
consumption.Distance,
|
if (!isLast)
|
||||||
consumption.Amount,
|
{
|
||||||
CarDto.FromCar(consumption.Car),
|
Consumption previousConsumption = consumptions[i + 1];
|
||||||
literPer100Km));
|
double distanceDiff = consumption.Distance - previousConsumption.Distance;
|
||||||
|
literPer100Km = consumption.Amount / (distanceDiff / 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.Add(new ResponseDto(
|
||||||
|
consumption.Id.Value,
|
||||||
|
consumption.DateTime,
|
||||||
|
consumption.Distance,
|
||||||
|
consumption.Amount,
|
||||||
|
CarDto.FromCar(consumption.Car),
|
||||||
|
literPer100Km));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiResponse apiResponse = new() { Consumptions = responses };
|
ApiResponse apiResponse = new() { Consumptions = responses };
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Vegasco.Server.AppHost.Shared;
|
using Vegasco.Server.AppHost.Shared;
|
||||||
|
|
||||||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);
|
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);
|
||||||
|
|
||||||
IResourceBuilder<PostgresDatabaseResource> postgres = builder.AddPostgres(Constants.Database.ServiceName)
|
IResourceBuilder<PostgresServerResource> postgresBuilder = builder.AddPostgres(Constants.Database.ServiceName)
|
||||||
.WithLifetime(ContainerLifetime.Persistent)
|
.WithLifetime(ContainerLifetime.Persistent)
|
||||||
.WithDataVolume()
|
.WithDataVolume();
|
||||||
|
|
||||||
|
if (builder.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
postgresBuilder = postgresBuilder
|
||||||
|
.WithPgWeb()
|
||||||
|
.WithPgAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
IResourceBuilder<PostgresDatabaseResource> postgres = postgresBuilder
|
||||||
.AddDatabase(Constants.Database.Name);
|
.AddDatabase(Constants.Database.Name);
|
||||||
|
|
||||||
IResourceBuilder<ProjectResource> api = builder
|
IResourceBuilder<ProjectResource> api = builder
|
||||||
|
|||||||
Reference in New Issue
Block a user