Skip to content

Commit 2c254f7

Browse files
committed
fix
1 parent a3f2e36 commit 2c254f7

3 files changed

Lines changed: 219 additions & 259 deletions

File tree

BikeRental.Application/Services/BikeService.cs

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ public BikeService(
2424
_models = models;
2525
}
2626

27+
private static BikeModelDto ToModelDto(BikeModel m) => new()
28+
{
29+
Id = m.Id,
30+
Name = m.Name,
31+
Type = m.Type,
32+
WheelSize = m.WheelSize,
33+
BikeWeight = m.BikeWeight,
34+
MaxRiderWeight = m.MaxRiderWeight,
35+
BrakeType = m.BrakeType,
36+
ModelYear = m.ModelYear,
37+
HourlyRate = m.HourlyRate
38+
};
39+
40+
private static BikeDto ToDto(Bike b, BikeModel m) => new()
41+
{
42+
Id = b.Id,
43+
SerialNumber = b.SerialNumber,
44+
Color = b.Color,
45+
ModelId = b.ModelId,
46+
Model = ToModelDto(m)
47+
};
48+
2749
/// <summary>
2850
/// Retrieves all bikes with their associated models.
2951
/// </summary>
@@ -32,34 +54,23 @@ public async Task<IEnumerable<BikeDto>> GetAllAsync()
3254
var bikes = await _bikes.GetAllAsync();
3355
var models = await _models.GetAllAsync();
3456

35-
return bikes.Select(b =>
57+
var modelMap = models.ToDictionary(m => m.Id);
58+
59+
var result = new List<BikeDto>();
60+
61+
foreach (var bike in bikes)
3662
{
37-
var model = models.First(m => m.Id == b.ModelId);
38-
39-
return new BikeDto
40-
{
41-
Id = b.Id,
42-
SerialNumber = b.SerialNumber,
43-
Color = b.Color,
44-
ModelId = b.ModelId,
45-
Model = new BikeModelDto
46-
{
47-
Id = model.Id,
48-
Name = model.Name,
49-
Type = model.Type,
50-
WheelSize = model.WheelSize,
51-
BikeWeight = model.BikeWeight,
52-
MaxRiderWeight = model.MaxRiderWeight,
53-
BrakeType = model.BrakeType,
54-
ModelYear = model.ModelYear,
55-
HourlyRate = model.HourlyRate
56-
}
57-
};
58-
});
63+
if (!modelMap.TryGetValue(bike.ModelId, out var model))
64+
continue;
65+
66+
result.Add(ToDto(bike, model));
67+
}
68+
69+
return result;
5970
}
6071

6172
/// <summary>
62-
/// Retrieves a bike by its unique identifier, including its model.
73+
/// Retrieves a bike by its unique identifier.
6374
/// </summary>
6475
public async Task<BikeDto?> GetByIdAsync(string id)
6576
{
@@ -69,25 +80,7 @@ public async Task<IEnumerable<BikeDto>> GetAllAsync()
6980
var model = await _models.GetByIdAsync(bike.ModelId);
7081
if (model == null) return null;
7182

72-
return new BikeDto
73-
{
74-
Id = bike.Id,
75-
SerialNumber = bike.SerialNumber,
76-
Color = bike.Color,
77-
ModelId = bike.ModelId,
78-
Model = new BikeModelDto
79-
{
80-
Id = model.Id,
81-
Name = model.Name,
82-
Type = model.Type,
83-
WheelSize = model.WheelSize,
84-
BikeWeight = model.BikeWeight,
85-
MaxRiderWeight = model.MaxRiderWeight,
86-
BrakeType = model.BrakeType,
87-
ModelYear = model.ModelYear,
88-
HourlyRate = model.HourlyRate
89-
}
90-
};
83+
return ToDto(bike, model);
9184
}
9285

9386
/// <summary>
@@ -96,7 +89,7 @@ public async Task<IEnumerable<BikeDto>> GetAllAsync()
9689
public async Task<BikeDto> CreateAsync(BikeCreateDto dto)
9790
{
9891
var model = await _models.GetByIdAsync(dto.ModelId)
99-
?? throw new ArgumentException("Bike model not found");
92+
?? throw new KeyNotFoundException($"Bike model {dto.ModelId} not found");
10093

10194
var bike = new Bike
10295
{
@@ -107,7 +100,7 @@ public async Task<BikeDto> CreateAsync(BikeCreateDto dto)
107100

108101
await _bikes.CreateAsync(bike);
109102

110-
return (await GetByIdAsync(bike.Id))!;
103+
return ToDto(bike, model);
111104
}
112105

113106
/// <summary>
@@ -118,13 +111,16 @@ public async Task<BikeDto> CreateAsync(BikeCreateDto dto)
118111
var bike = await _bikes.GetByIdAsync(dto.Id);
119112
if (bike == null) return null;
120113

114+
var model = await _models.GetByIdAsync(dto.ModelId)
115+
?? throw new KeyNotFoundException($"Bike model {dto.ModelId} not found");
116+
121117
bike.SerialNumber = dto.SerialNumber;
122118
bike.Color = dto.Color;
123119
bike.ModelId = dto.ModelId;
124120

125121
await _bikes.UpdateAsync(bike.Id, bike);
126122

127-
return await GetByIdAsync(bike.Id);
123+
return ToDto(bike, model);
128124
}
129125

130126
/// <summary>

0 commit comments

Comments
 (0)