Skip to content

Commit 5f309e4

Browse files
authored
Merge pull request #319 from waylonkenning/feature/training-edit-functionality
feat(training): Add edit functionality for training module
2 parents 6cfee05 + a5d4a8a commit 5f309e4

11 files changed

Lines changed: 1593 additions & 2 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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="ITrainingAttachmentRepository"/>
12+
/// </summary>
13+
public sealed class MockTrainingAttachmentRepository : ITrainingAttachmentRepository
14+
{
15+
private readonly List<TrainingAttachment> _attachments = new List<TrainingAttachment>();
16+
private int _nextId = 1;
17+
18+
public Task<IEnumerable<TrainingAttachment>> GetAllAsync()
19+
=> Task.FromResult<IEnumerable<TrainingAttachment>>(_attachments.ToList());
20+
21+
public Task<TrainingAttachment> GetByIdAsync(object id)
22+
{
23+
var intId = (int)id;
24+
var attachment = _attachments.FirstOrDefault(a => a.TrainingAttachmentId == intId);
25+
return Task.FromResult(attachment);
26+
}
27+
28+
public Task<IEnumerable<TrainingAttachment>> GetAllByDepartmentIdAsync(int departmentId)
29+
=> Task.FromResult<IEnumerable<TrainingAttachment>>(new List<TrainingAttachment>());
30+
31+
public Task<IEnumerable<TrainingAttachment>> GetAllByUserIdAsync(string userId)
32+
=> Task.FromResult<IEnumerable<TrainingAttachment>>(new List<TrainingAttachment>());
33+
34+
public Task<TrainingAttachment> InsertAsync(TrainingAttachment entity, CancellationToken cancellationToken, bool firstLevelOnly = false)
35+
{
36+
entity.TrainingAttachmentId = _nextId++;
37+
_attachments.Add(entity);
38+
return Task.FromResult(entity);
39+
}
40+
41+
public Task<TrainingAttachment> UpdateAsync(TrainingAttachment entity, CancellationToken cancellationToken, bool firstLevelOnly = false)
42+
{
43+
var existing = _attachments.FirstOrDefault(a => a.TrainingAttachmentId == entity.TrainingAttachmentId);
44+
if (existing != null)
45+
{
46+
_attachments.Remove(existing);
47+
}
48+
_attachments.Add(entity);
49+
return Task.FromResult(entity);
50+
}
51+
52+
public Task<bool> DeleteAsync(TrainingAttachment entity, CancellationToken cancellationToken)
53+
{
54+
var existing = _attachments.FirstOrDefault(a => a.TrainingAttachmentId == entity.TrainingAttachmentId);
55+
if (existing != null)
56+
{
57+
_attachments.Remove(existing);
58+
}
59+
return Task.FromResult(true);
60+
}
61+
62+
public Task<TrainingAttachment> SaveOrUpdateAsync(TrainingAttachment entity, CancellationToken cancellationToken, bool firstLevelOnly = false)
63+
{
64+
if (entity.TrainingAttachmentId == 0)
65+
{
66+
entity.TrainingAttachmentId = _nextId++;
67+
_attachments.Add(entity);
68+
}
69+
else
70+
{
71+
var existing = _attachments.FirstOrDefault(a => a.TrainingAttachmentId == entity.TrainingAttachmentId);
72+
if (existing != null)
73+
{
74+
_attachments.Remove(existing);
75+
}
76+
_attachments.Add(entity);
77+
}
78+
return Task.FromResult(entity);
79+
}
80+
81+
public Task<bool> DeleteMultipleAsync(TrainingAttachment entity, string parentKeyName, object parentKeyId, List<object> ids, CancellationToken cancellationToken)
82+
=> Task.FromResult(true);
83+
84+
public Task<IEnumerable<TrainingAttachment>> GetTrainingAttachmentsByTrainingIdAsync(int trainingId)
85+
{
86+
var result = _attachments.Where(a => a.TrainingId == trainingId).ToList();
87+
return Task.FromResult<IEnumerable<TrainingAttachment>>(result);
88+
}
89+
90+
public void SeedAttachment(TrainingAttachment attachment)
91+
{
92+
if (attachment.TrainingAttachmentId == 0)
93+
{
94+
attachment.TrainingAttachmentId = _nextId++;
95+
}
96+
else
97+
{
98+
_nextId = System.Math.Max(_nextId, attachment.TrainingAttachmentId + 1);
99+
}
100+
_attachments.Add(attachment);
101+
}
102+
}
103+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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="ITrainingQuestionRepository"/>
12+
/// </summary>
13+
public sealed class MockTrainingQuestionRepository : ITrainingQuestionRepository
14+
{
15+
private readonly List<TrainingQuestion> _questions = new List<TrainingQuestion>();
16+
private int _nextId = 1;
17+
18+
public Task<IEnumerable<TrainingQuestion>> GetAllAsync()
19+
=> Task.FromResult<IEnumerable<TrainingQuestion>>(_questions.ToList());
20+
21+
public Task<TrainingQuestion> GetByIdAsync(object id)
22+
{
23+
var intId = (int)id;
24+
var question = _questions.FirstOrDefault(q => q.TrainingQuestionId == intId);
25+
return Task.FromResult(question);
26+
}
27+
28+
public Task<IEnumerable<TrainingQuestion>> GetAllByDepartmentIdAsync(int departmentId)
29+
=> Task.FromResult<IEnumerable<TrainingQuestion>>(new List<TrainingQuestion>());
30+
31+
public Task<IEnumerable<TrainingQuestion>> GetAllByUserIdAsync(string userId)
32+
=> Task.FromResult<IEnumerable<TrainingQuestion>>(new List<TrainingQuestion>());
33+
34+
public Task<TrainingQuestion> InsertAsync(TrainingQuestion entity, CancellationToken cancellationToken, bool firstLevelOnly = false)
35+
{
36+
entity.TrainingQuestionId = _nextId++;
37+
_questions.Add(entity);
38+
return Task.FromResult(entity);
39+
}
40+
41+
public Task<TrainingQuestion> UpdateAsync(TrainingQuestion entity, CancellationToken cancellationToken, bool firstLevelOnly = false)
42+
{
43+
var existing = _questions.FirstOrDefault(q => q.TrainingQuestionId == entity.TrainingQuestionId);
44+
if (existing != null)
45+
{
46+
_questions.Remove(existing);
47+
}
48+
_questions.Add(entity);
49+
return Task.FromResult(entity);
50+
}
51+
52+
public Task<bool> DeleteAsync(TrainingQuestion entity, CancellationToken cancellationToken)
53+
{
54+
var existing = _questions.FirstOrDefault(q => q.TrainingQuestionId == entity.TrainingQuestionId);
55+
if (existing != null)
56+
{
57+
_questions.Remove(existing);
58+
}
59+
return Task.FromResult(true);
60+
}
61+
62+
public Task<TrainingQuestion> SaveOrUpdateAsync(TrainingQuestion entity, CancellationToken cancellationToken, bool firstLevelOnly = false)
63+
{
64+
if (entity.TrainingQuestionId == 0)
65+
{
66+
entity.TrainingQuestionId = _nextId++;
67+
_questions.Add(entity);
68+
}
69+
else
70+
{
71+
var existing = _questions.FirstOrDefault(q => q.TrainingQuestionId == entity.TrainingQuestionId);
72+
if (existing != null)
73+
{
74+
_questions.Remove(existing);
75+
}
76+
_questions.Add(entity);
77+
}
78+
return Task.FromResult(entity);
79+
}
80+
81+
public Task<bool> DeleteMultipleAsync(TrainingQuestion entity, string parentKeyName, object parentKeyId, List<object> ids, CancellationToken cancellationToken)
82+
=> Task.FromResult(true);
83+
84+
public Task<IEnumerable<TrainingQuestion>> GetTrainingQuestionsByTrainingIdAsync(int trainingId)
85+
{
86+
var result = _questions.Where(q => q.TrainingId == trainingId).ToList();
87+
return Task.FromResult<IEnumerable<TrainingQuestion>>(result);
88+
}
89+
90+
public void SeedQuestion(TrainingQuestion question)
91+
{
92+
if (question.TrainingQuestionId == 0)
93+
{
94+
question.TrainingQuestionId = _nextId++;
95+
}
96+
else
97+
{
98+
_nextId = System.Math.Max(_nextId, question.TrainingQuestionId + 1);
99+
}
100+
_questions.Add(question);
101+
}
102+
}
103+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)