1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using System . Threading ;
4+ using System . Threading . Tasks ;
5+ using Resgrid . Model ;
6+ using Resgrid . Model . Repositories ;
7+
8+ namespace Resgrid . Tests . Mocks
9+ {
10+ /// <summary>
11+ /// In-memory mock for <see cref="ITrainingRepository"/> that stores trainings
12+ /// without requiring a database connection.
13+ /// </summary>
14+ public sealed class MockTrainingRepository : ITrainingRepository
15+ {
16+ private readonly List < Training > _trainings = new List < Training > ( ) ;
17+ private int _nextId = 1 ;
18+
19+ public List < Training > Trainings => _trainings ;
20+
21+ public Task < IEnumerable < Training > > GetAllAsync ( )
22+ => Task . FromResult < IEnumerable < Training > > ( _trainings . ToList ( ) ) ;
23+
24+ public Task < Training > GetByIdAsync ( object id )
25+ {
26+ var intId = ( int ) id ;
27+ var training = _trainings . FirstOrDefault ( t => t . TrainingId == intId ) ;
28+ return Task . FromResult ( training ) ;
29+ }
30+
31+ public Task < IEnumerable < Training > > GetAllByDepartmentIdAsync ( int departmentId )
32+ {
33+ var result = _trainings . Where ( t => t . DepartmentId == departmentId ) . ToList ( ) ;
34+ return Task . FromResult < IEnumerable < Training > > ( result ) ;
35+ }
36+
37+ public Task < IEnumerable < Training > > GetAllByUserIdAsync ( string userId )
38+ => Task . FromResult < IEnumerable < Training > > ( new List < Training > ( ) ) ;
39+
40+ public Task < Training > InsertAsync ( Training entity , CancellationToken cancellationToken , bool firstLevelOnly = false )
41+ {
42+ entity . TrainingId = _nextId ++ ;
43+ _trainings . Add ( entity ) ;
44+ return Task . FromResult ( entity ) ;
45+ }
46+
47+ public Task < Training > UpdateAsync ( Training entity , CancellationToken cancellationToken , bool firstLevelOnly = false )
48+ {
49+ var existing = _trainings . FirstOrDefault ( t => t . TrainingId == entity . TrainingId ) ;
50+ if ( existing != null )
51+ {
52+ _trainings . Remove ( existing ) ;
53+ }
54+ _trainings . Add ( entity ) ;
55+ return Task . FromResult ( entity ) ;
56+ }
57+
58+ public Task < bool > DeleteAsync ( Training entity , CancellationToken cancellationToken )
59+ {
60+ var existing = _trainings . FirstOrDefault ( t => t . TrainingId == entity . TrainingId ) ;
61+ if ( existing != null )
62+ {
63+ _trainings . Remove ( existing ) ;
64+ }
65+ return Task . FromResult ( true ) ;
66+ }
67+
68+ public Task < Training > SaveOrUpdateAsync ( Training entity , CancellationToken cancellationToken , bool firstLevelOnly = false )
69+ {
70+ if ( entity . TrainingId == 0 )
71+ {
72+ entity . TrainingId = _nextId ++ ;
73+ _trainings . Add ( entity ) ;
74+ }
75+ else
76+ {
77+ var existing = _trainings . FirstOrDefault ( t => t . TrainingId == entity . TrainingId ) ;
78+ if ( existing != null )
79+ {
80+ _trainings . Remove ( existing ) ;
81+ }
82+ _trainings . Add ( entity ) ;
83+ }
84+ return Task . FromResult ( entity ) ;
85+ }
86+
87+ public Task < bool > DeleteMultipleAsync ( Training entity , string parentKeyName , object parentKeyId , List < object > ids , CancellationToken cancellationToken )
88+ => Task . FromResult ( true ) ;
89+
90+ public List < Training > GetAllTrainings ( )
91+ => _trainings . ToList ( ) ;
92+
93+ public Task < IEnumerable < Training > > GetTrainingsByDepartmentIdAsync ( int departmentId )
94+ {
95+ var result = _trainings . Where ( t => t . DepartmentId == departmentId ) . ToList ( ) ;
96+ return Task . FromResult < IEnumerable < Training > > ( result ) ;
97+ }
98+
99+ public Task < Training > GetTrainingByTrainingIdAsync ( int trainingId )
100+ {
101+ var training = _trainings . FirstOrDefault ( t => t . TrainingId == trainingId ) ;
102+ return Task . FromResult ( training ) ;
103+ }
104+
105+ /// <summary>
106+ /// Helper method to seed test data
107+ /// </summary>
108+ public void SeedTraining ( Training training )
109+ {
110+ if ( training . TrainingId == 0 )
111+ {
112+ training . TrainingId = _nextId ++ ;
113+ }
114+ else
115+ {
116+ _nextId = System . Math . Max ( _nextId , training . TrainingId + 1 ) ;
117+ }
118+ _trainings . Add ( training ) ;
119+ }
120+ }
121+ }
0 commit comments