Skip to content

Commit 80415ff

Browse files
committed
Fix GetSubjectInCurriculumByCurriculumId
1 parent d0f98f2 commit 80415ff

3 files changed

Lines changed: 53 additions & 15 deletions

File tree

FPTStella/FPTStella.Application/Services/SubjectInCurriculumService.cs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,46 @@ public class SubjectInCurriculumService : ISubjectInCurriculumService
1515
{
1616
private readonly IUnitOfWork _unitOfWork;
1717
private readonly ISubjectInCurriculumRepository _subjectInCurriculumRepository;
18-
/// <summary>
18+
private readonly ISubjectRepository _subjectRepository;
1919
/// Initializes a new instance of the SubjectInCurriculumService class.
2020
/// </summary>
2121
/// <param name="unitOfWork">The unit of work</param>
2222
/// <param name="subjectInCurriculumRepository">The subject in curriculum repository</param>
23-
public SubjectInCurriculumService(IUnitOfWork unitOfWork, ISubjectInCurriculumRepository subjectInCurriculumRepository)
23+
/// <param name="subjectRepository">The subject repository</param>
24+
public SubjectInCurriculumService(
25+
IUnitOfWork unitOfWork,
26+
ISubjectInCurriculumRepository subjectInCurriculumRepository,
27+
ISubjectRepository subjectRepository)
2428
{
2529
_unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
2630
_subjectInCurriculumRepository = subjectInCurriculumRepository ?? throw new ArgumentNullException(nameof(subjectInCurriculumRepository));
31+
_subjectRepository = subjectRepository ?? throw new ArgumentNullException(nameof(subjectRepository));
2732
}
2833

2934
/// <summary>
3035
/// Maps a SubjectInCurriculum entity to its DTO representation.
3136
/// </summary>
3237
/// <param name="entity">The SubjectInCurriculum entity</param>
3338
/// <returns>A mapped SubjectInCurriculumDto object</returns>
34-
private static SubjectInCurriculumDto MapToDto(SubjectInCurriculum entity) => new()
39+
private async Task<SubjectInCurriculumDto> MapToDtoAsync(SubjectInCurriculum entity)
3540
{
36-
Id = entity.Id,
37-
CurriculumId = entity.CurriculumId,
38-
SubjectId = entity.SubjectId
39-
};
41+
var dto = new SubjectInCurriculumDto
42+
{
43+
Id = entity.Id,
44+
CurriculumId = entity.CurriculumId,
45+
SubjectId = entity.SubjectId
46+
};
47+
48+
// Fetch the subject details
49+
var subject = await _subjectRepository.GetByIdAsync(entity.SubjectId.ToString());
50+
if (subject != null && !subject.DelFlg)
51+
{
52+
dto.SubjectCode = subject.SubjectCode;
53+
dto.SubjectName = subject.SubjectName;
54+
}
55+
56+
return dto;
57+
}
4058

4159
/// <summary>
4260
/// Creates a new mapping between a subject and a curriculum.
@@ -62,7 +80,7 @@ public async Task<SubjectInCurriculumDto> CreateMappingAsync(CreateSubjectInCurr
6280
await _subjectInCurriculumRepository.InsertAsync(mapping);
6381
await _unitOfWork.SaveAsync();
6482

65-
return MapToDto(mapping);
83+
return await MapToDtoAsync(mapping);
6684
}
6785

6886
/// <summary>
@@ -125,7 +143,7 @@ public async Task<SubjectInCurriculumDto> GetMappingByIdAsync(Guid id)
125143
throw new KeyNotFoundException($"Subject-Curriculum mapping with ID {id} not found.");
126144
}
127145

128-
return MapToDto(mapping);
146+
return await MapToDtoAsync(mapping);
129147
}
130148

131149
/// <summary>
@@ -135,7 +153,14 @@ public async Task<SubjectInCurriculumDto> GetMappingByIdAsync(Guid id)
135153
public async Task<List<SubjectInCurriculumDto>> GetAllMappingsAsync()
136154
{
137155
var mappings = await _subjectInCurriculumRepository.FilterByAsync(m => !m.DelFlg);
138-
return mappings.Select(MapToDto).ToList();
156+
var result = new List<SubjectInCurriculumDto>();
157+
158+
foreach (var mapping in mappings)
159+
{
160+
result.Add(await MapToDtoAsync(mapping));
161+
}
162+
163+
return result;
139164
}
140165

141166
/// <summary>
@@ -146,7 +171,14 @@ public async Task<List<SubjectInCurriculumDto>> GetAllMappingsAsync()
146171
public async Task<List<SubjectInCurriculumDto>> GetMappingsByCurriculumIdAsync(Guid curriculumId)
147172
{
148173
var mappings = await _subjectInCurriculumRepository.GetByCurriculumIdAsync(curriculumId);
149-
return mappings.Select(MapToDto).ToList();
174+
var result = new List<SubjectInCurriculumDto>();
175+
176+
foreach (var mapping in mappings)
177+
{
178+
result.Add(await MapToDtoAsync(mapping));
179+
}
180+
181+
return result;
150182
}
151183

152184
/// <summary>
@@ -157,7 +189,14 @@ public async Task<List<SubjectInCurriculumDto>> GetMappingsByCurriculumIdAsync(G
157189
public async Task<List<SubjectInCurriculumDto>> GetMappingsBySubjectIdAsync(Guid subjectId)
158190
{
159191
var mappings = await _subjectInCurriculumRepository.GetBySubjectIdAsync(subjectId);
160-
return mappings.Select(MapToDto).ToList();
192+
var result = new List<SubjectInCurriculumDto>();
193+
194+
foreach (var mapping in mappings)
195+
{
196+
result.Add(await MapToDtoAsync(mapping));
197+
}
198+
199+
return result;
161200
}
162201

163202
/// <summary>

FPTStella/FPTStella.Application/Services/SubjectToolService.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ public async Task<UpdateSubjectToolResultDto> UpdateMappingsAsync(UpdateSubjectT
235235
var updatedMappings = new List<UpdateSubjectToolDto>();
236236
var failedMappings = new List<(UpdateSubjectToolDto Mapping, string Reason)>();
237237

238-
// Validate Subject and Tool existence in batch
239238
var allSubjectIds = updateMappingBatchDto.Mappings
240239
.SelectMany(m => new[] { m.SubjectId, m.NewSubjectId })
241240
.Where(id => id.HasValue && id.Value != Guid.Empty)
@@ -272,15 +271,13 @@ public async Task<UpdateSubjectToolResultDto> UpdateMappingsAsync(UpdateSubjectT
272271
{
273272
try
274273
{
275-
// Validate the existing mapping
276274
var existingMapping = await _subjectToolRepository.GetMappingAsync(mappingDto.SubjectId, mappingDto.ToolId);
277275
if (existingMapping == null)
278276
{
279277
failedMappings.Add((mappingDto, $"Mapping between Subject ID {mappingDto.SubjectId} and Tool ID {mappingDto.ToolId} does not exist."));
280278
continue;
281279
}
282280

283-
// Validate new Subject ID if provided
284281
if (mappingDto.NewSubjectId.HasValue && mappingDto.NewSubjectId.Value != Guid.Empty)
285282
{
286283
if (!existingSubjects.TryGetValue(mappingDto.NewSubjectId.Value, out var subjectExists) || !subjectExists)

FPTStella/FPTStella.Contracts/DTOs/SubjectInCurriculums/SubjectInCurriculumDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ public class SubjectInCurriculumDto
2222
/// Gets or sets the subject ID.
2323
/// </summary>
2424
public Guid SubjectId { get; set; }
25+
public string SubjectCode { get; set; } = string.Empty;
26+
public string SubjectName { get; set; } = string.Empty;
2527
}
2628
}

0 commit comments

Comments
 (0)