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