Skip to content

Commit 36172f7

Browse files
committed
Added all methods to Admin module
1 parent 0be09af commit 36172f7

7 files changed

Lines changed: 75 additions & 41 deletions

File tree

EA.DesktopApp/Rest/AdminGatewayService.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text;
43
using System.Threading;
54
using System.Threading.Tasks;
65
using EA.DesktopApp.Contracts;
7-
using EA.DesktopApp.Exceptions;
86
using EA.DesktopApp.Models;
9-
using Newtonsoft.Json;
107
using RestSharp;
118

129
namespace EA.DesktopApp.Rest
@@ -31,14 +28,20 @@ public async Task<bool> Login(Credentials credentials, CancellationToken token)
3128
return GetContent<bool>(response);
3229
}
3330

34-
public Task<IReadOnlyList<AdministratorModel>> GetAllAsync(CancellationToken token)
31+
public async Task<IReadOnlyList<AdministratorModel>> GetAllAsync(CancellationToken token)
3532
{
36-
throw new NotImplementedException();
33+
var url = new Uri($"{BaseUrl}/api/Administrator/GetAllAdministrators");
34+
var response = await SendRequestAsync(url, Method.Get, token);
35+
36+
return GetContent<IReadOnlyList<AdministratorModel>>(response);
3737
}
3838

39-
public Task<AdministratorModel> GetByIdAsync(long id, CancellationToken token)
39+
public async Task<AdministratorModel> GetByIdAsync(long id, CancellationToken token)
4040
{
41-
throw new NotImplementedException();
41+
var url = new Uri($"{BaseUrl}/api/Administrator/GetAdministratorById?id={id}");
42+
var response = await SendRequestAsync(url, Method.Get, token);
43+
44+
return GetContent<AdministratorModel>(response);
4245
}
4346

4447
public async Task<bool> ChangeLoginAsync(Credentials credentials, CancellationToken token)
@@ -48,19 +51,25 @@ public async Task<bool> ChangeLoginAsync(Credentials credentials, CancellationTo
4851
return GetContent<bool>(response);
4952
}
5053

51-
public Task CreateAsync(AdministratorModel admin, CancellationToken token)
54+
public async Task CreateAsync(AdministratorModel admin, CancellationToken token)
5255
{
53-
throw new NotImplementedException();
56+
var url = new Uri($"{BaseUrl}/api/Administrator/Create");
57+
var response = await SendRequestAsync(admin, url, Method.Post, token);
58+
CheckResponse(response);
5459
}
5560

56-
public Task UpdateAsync(AdministratorModel admin, CancellationToken token)
61+
public async Task UpdateAsync(AdministratorModel admin, CancellationToken token)
5762
{
58-
throw new NotImplementedException();
63+
var url = new Uri($"{BaseUrl}/api/Administrator/Update");
64+
var response = await SendRequestAsync(admin, url, Method.Put, token);
65+
CheckResponse(response);
5966
}
6067

61-
public Task DeleteAsync(long id, CancellationToken token)
68+
public async Task DeleteAsync(long id, CancellationToken token)
6269
{
63-
throw new NotImplementedException();
70+
var url = new Uri($"{BaseUrl}/api/Administrator/Delete?id={id}");
71+
var response = await SendRequestAsync(url, Method.Delete, token);
72+
CheckResponse(response);
6473
}
6574
}
6675
}

EA.DesktopApp/ViewModels/AdminViewModel.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using System.Threading;
44
using System.Windows.Input;
55
using EA.DesktopApp.Contracts;
6+
using EA.DesktopApp.Contracts.ViewContracts;
67
using EA.DesktopApp.Models;
78
using EA.DesktopApp.Resources.Messages;
89
using EA.DesktopApp.Services;
10+
using EA.DesktopApp.View;
911
using EA.DesktopApp.ViewModels.Commands;
1012
using NLog;
1113

@@ -17,17 +19,19 @@ internal class AdminViewModel : BaseViewModel
1719
private readonly IAdminGatewayService _adminGatewayService;
1820
private readonly ISoundPlayerService _soundPlayer;
1921
private readonly CancellationToken _token;
22+
private readonly IWindowManager _windowManager;
2023
private string _oldPasswordValue;
2124

2225
private string _userMessage;
2326

2427
public AdminViewModel(ISoundPlayerService soundPlayer,
2528
IAdminGatewayService adminGatewayService,
26-
CancellationToken token)
29+
CancellationToken token, IWindowManager windowManager)
2730
{
2831
_soundPlayer = soundPlayer;
2932
_adminGatewayService = adminGatewayService;
3033
_token = token;
34+
_windowManager = windowManager;
3135
InitializeCommands();
3236
}
3337

@@ -112,14 +116,22 @@ private async void ToggleRegistrationExecute()
112116
{
113117
try
114118
{
119+
_soundPlayer.PlaySound(SoundPlayerService.ButtonSound);
120+
115121
var credentials = SetCredentials(OldPasswordField);
116122
_adminGatewayService.SetCredentials(credentials);
117-
await _adminGatewayService.ChangeLoginAsync(new Credentials
123+
var result = await _adminGatewayService.ChangeLoginAsync(new Credentials
118124
{
119125
UserName = LoginField,
120126
Password = PasswordField,
121127
OldPassword = OldPasswordField
122-
}, _token).ConfigureAwait(false);
128+
}, _token);
129+
130+
if (result)
131+
{
132+
_windowManager.CloseWindow<AdminForm>();
133+
_windowManager.ShowModalWindow("Password has been changed");
134+
}
123135
}
124136
catch (Exception e)
125137
{

EA.DesktopApp/ViewModels/LoginViewModel.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using EA.DesktopApp.Contracts;
66
using EA.DesktopApp.Contracts.ViewContracts;
77
using EA.DesktopApp.Enum;
8-
using EA.DesktopApp.Models;
98
using EA.DesktopApp.Resources.Messages;
109
using EA.DesktopApp.Services;
1110
using EA.DesktopApp.View;
@@ -93,6 +92,8 @@ private async void ToggleLoginExecute()
9392

9493
if (!isLogin)
9594
{
95+
96+
ClearFields();
9697
return;
9798
}
9899

@@ -112,21 +113,27 @@ private async void ToggleLoginExecute()
112113
}
113114
catch (Exception e)
114115
{
116+
ClearFields();
115117
Logger.Error("Login to app failed! {E}", e);
116118
}
117119
}
118120

119121
private void ToggleCancelExecute()
120122
{
121123
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
122-
LoginField = string.Empty;
123-
PasswordField = string.Empty;
124+
ClearFields();
124125
}
125126

126127
private void ToggleAdminWindowShowExecute()
127128
{
128129
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
129130
_windowManager.ShowWindow<AdminForm>();
130131
}
132+
133+
protected override void ClearFields()
134+
{
135+
LoginField = string.Empty;
136+
PasswordField = string.Empty;
137+
}
131138
}
132139
}

EA.DesktopApp/ViewModels/MainViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class MainViewModel : BaseViewModel
3434
private readonly IEigenFaceRecognition _eigenRecognizer;
3535
private readonly IEmployeeGatewayService _employeeGatewayService;
3636
private readonly IFaceDetectionService _faceDetectionService;
37-
private readonly ISoundPlayerService _soundPlayerHelper;
37+
private readonly ISoundPlayerService _soundPlayerService;
3838
private readonly CancellationToken _token;
3939
private readonly IWindowManager _windowManager;
4040

@@ -64,7 +64,7 @@ public MainViewModel(
6464
IEigenFaceRecognition eigenRecognizer,
6565
IEmployeeGatewayService employeeGatewayService,
6666
IWindowManager windowManager,
67-
ISoundPlayerService soundPlayerHelper,
67+
ISoundPlayerService soundPlayerService,
6868
CancellationToken token)
6969
{
7070
_faceDetectionService = faceDetectionService;
@@ -75,7 +75,7 @@ public MainViewModel(
7575
LoadAvailableCameras();
7676
InitializeCommands();
7777
TimeTicker();
78-
_soundPlayerHelper = soundPlayerHelper;
78+
_soundPlayerService = soundPlayerService;
7979
_token = token;
8080
DetectionHint = ProgramResources.StartDetectorTooltipMessage;
8181
}
@@ -249,7 +249,7 @@ private void FaceDetectionServiceExecute()
249249
? ProgramResources.StartDetectorTooltipMessage
250250
: ProgramResources.StopDetectorTooltipMessage;
251251

252-
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
252+
_soundPlayerService.PlaySound(SoundPlayerService.ButtonSound);
253253

254254
if (_faceDetectionService != null && !_faceDetectionService.IsRunning)
255255
{
@@ -280,7 +280,7 @@ private void StopFaceDetectionService()
280280
/// </summary>
281281
private void TogglePhotoShootServiceExecute()
282282
{
283-
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
283+
_soundPlayerService.PlaySound(SoundPlayerService.ButtonSound);
284284

285285
// True - button is pushed - Working!
286286
IsRunning = false;

EA.DesktopApp/ViewModels/RedactorViewModel.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6-
using System.Windows;
76
using System.Windows.Input;
87
using EA.DesktopApp.Contracts;
98
using EA.DesktopApp.Contracts.ViewContracts;
109
using EA.DesktopApp.Models;
1110
using EA.DesktopApp.Resources.Messages;
11+
using EA.DesktopApp.Services;
1212
using EA.DesktopApp.ViewModels.Commands;
1313
using NLog;
1414

@@ -22,12 +22,15 @@ public class RedactorViewModel : BaseViewModel, IAsyncInitializer
2222
private readonly IWindowManager _windowManager;
2323
private ObservableCollection<EmployeeModel> _employees;
2424
private EmployeeModel _selectedEmployee;
25+
private readonly ISoundPlayerService _soundPlayerHelper;
2526

26-
public RedactorViewModel(IWindowManager windowManager, IEmployeeGatewayService employeeService, CancellationToken token)
27+
public RedactorViewModel(IWindowManager windowManager, IEmployeeGatewayService employeeService,
28+
CancellationToken token, ISoundPlayerService soundPlayerHelper)
2729
{
2830
_windowManager = windowManager;
2931
_employeeService = employeeService;
3032
_token = token;
33+
_soundPlayerHelper = soundPlayerHelper;
3134
InitializeCommands();
3235
}
3336

@@ -100,6 +103,7 @@ private async Task ToggleDeleteExecute()
100103
{
101104
try
102105
{
106+
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
103107
await ExecuteAsync(() => _employeeService.DeleteAsync(SelectedEmployee.Id, _token));
104108
await LoadData();
105109
}
@@ -114,14 +118,16 @@ private async Task ToggleUpdateExecute()
114118
{
115119
try
116120
{
121+
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
122+
117123
var updatedEmployeeData = new EmployeeModel
118124
{
119125
Id = SelectedEmployee.Id,
120126
DateTime = DateTimeOffset.UtcNow,
121127
Name = PersonName,
122128
LastName = PersonLastName,
123129
Department = PersonDepartment,
124-
PhotoName = string.Format(ProgramResources.FileName, PersonName, PersonLastName, DateTime.UtcNow),
130+
PhotoName = string.Format(ProgramResources.FileName, PersonName, PersonLastName, DateTime.UtcNow)
125131
};
126132

127133
await ExecuteAsync(() => _employeeService.UpdateAsync(updatedEmployeeData, _token));
@@ -134,9 +140,10 @@ private async Task ToggleUpdateExecute()
134140
_windowManager.ShowModalWindow("Failed to update employee data");
135141
}
136142
}
137-
143+
138144
private void ToggleClearFields()
139145
{
146+
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
140147
ClearFields();
141148
}
142149
}

EA.DesktopApp/ViewModels/RegistrationViewModel.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66
using System.Windows.Input;
7-
using EA.DesktopApp.Constants;
87
using EA.DesktopApp.Contracts;
98
using EA.DesktopApp.Contracts.ViewContracts;
109
using EA.DesktopApp.Helpers;
@@ -13,7 +12,6 @@
1312
using EA.DesktopApp.Services;
1413
using EA.DesktopApp.ViewModels.Commands;
1514
using Emgu.CV;
16-
using Emgu.CV.CvEnum;
1715
using Emgu.CV.Structure;
1816
using NLog;
1917

@@ -29,7 +27,7 @@ public class RegistrationViewModel : BaseViewModel
2927
private readonly IEmployeeGatewayService _employeeGatewayService;
3028

3129
private readonly IPhotoShootService _photoShootService;
32-
private readonly ISoundPlayerService _soundPlayerService;
30+
private readonly ISoundPlayerService _soundPlayerHelper;
3331
private readonly CancellationToken _token;
3432
private readonly IWindowManager _windowManager;
3533

@@ -40,14 +38,14 @@ public class RegistrationViewModel : BaseViewModel
4038
/// <summary>
4139
/// .ctor
4240
/// </summary>
43-
public RegistrationViewModel(IPhotoShootService photoShootService,
44-
ISoundPlayerService soundPlayerService,
45-
IEmployeeGatewayService employeeGatewayService,
46-
IWindowManager windowManager,
41+
public RegistrationViewModel(IPhotoShootService photoShootService,
42+
ISoundPlayerService soundPlayerHelper,
43+
IEmployeeGatewayService employeeGatewayService,
44+
IWindowManager windowManager,
4745
CancellationToken token)
4846
{
4947
_photoShootService = photoShootService;
50-
_soundPlayerService = soundPlayerService;
48+
_soundPlayerHelper = soundPlayerHelper;
5149
_employeeGatewayService = employeeGatewayService;
5250
_windowManager = windowManager;
5351
_token = token;
@@ -126,6 +124,8 @@ public Bitmap PhotoShootFrame
126124
/// </summary>
127125
public ICommand ToggleClearFormCommand { get; private set; }
128126

127+
private Image<Bgr, byte> CapturedImage { get; set; }
128+
129129
private void OnWindowClosingBehavior(object sender, EventArgs e)
130130
{
131131
_photoShootService?.CancelServiceAsync();
@@ -167,6 +167,8 @@ private void ToggleClearFields()
167167

168168
protected override void ClearFields()
169169
{
170+
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
171+
170172
PersonName = string.Empty;
171173
PersonLastName = string.Empty;
172174
PersonDepartment = string.Empty;
@@ -178,7 +180,7 @@ protected override void ClearFields()
178180
/// </summary>
179181
private async Task ToggleAddImageToDataBase()
180182
{
181-
_soundPlayerService.PlaySound(SoundPlayerService.ButtonSound);
183+
_soundPlayerHelper.PlaySound(SoundPlayerService.ButtonSound);
182184

183185
var converter = new ImageConverter();
184186
var imageArray = (byte[])converter.ConvertTo(GrayScaleImage, typeof(byte[]));
@@ -219,14 +221,12 @@ private async Task ToggleAddImageToDataBase()
219221
}
220222
}
221223

222-
private Image<Bgr, byte> CapturedImage { get; set; }
223-
224224
/// <summary>
225225
/// Get grayscale image method
226226
/// </summary>
227227
private void ToggleGetImageExecute()
228228
{
229-
_soundPlayerService.PlaySound(SoundPlayerService.CameraSound);
229+
_soundPlayerHelper.PlaySound(SoundPlayerService.CameraSound);
230230
GrayScaleImage = _photoShootService.CropFaceFromImage(CapturedImage).ToBitmap();
231231
}
232232
}

EA.Services/Configuration/ServiceKeysConfig.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ public class ServiceKeysConfig
88
public class ServiceKeys
99
{
1010
public string? FirstAdminPass { get; set; }
11-
public string? JwtSecretKey { get; set; }
1211
}

0 commit comments

Comments
 (0)