Newer
Older
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class UserService : IUserService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public UserService(Tsi1Context context, IMapper mapper)
public async Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId)
var user = await _context.Users
.Include(x => x.UserType)
.FirstOrDefaultAsync(x => x.Username == username && x.TenantId == tenantId);
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, username);
return result;
result.HasError = true;
result.Message = ErrorMessages.IncorrectPassword;
return result;
}
result.Data = user;
return result;
}
public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId)
if (type == UserTypes.Student)
{
user.Student = new Student()
{
IdentityCard = dto.IdentityCard,
};
}
if (type == UserTypes.Professor)
{
user.Professor = new Professor()
{
_context.Users.Add(user);
await _context.SaveChangesAsync();
result.Data = user;
return result;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
public async Task<ServiceResult<bool>> Modify(UserModifyDto dto, string type, int userId)
{
var result = new ServiceResult<bool>();
var user = await _context.Users
.Include(x => x.Student)
.Include(x => x.Professor)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null)
{
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
if (dto.Username != user.Username)
{
var existingUser = await _context.Users
.FirstOrDefaultAsync(x => x.Username == dto.Username
&& x.TenantId == user.TenantId);
if (existingUser != null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.DuplicateUsername, dto.Username);
return result;
}
}
_mapper.Map(dto, user);
if (type == UserTypes.Student)
{
user.Student.IdentityCard = dto.IdentityCard;
user.Student.Age = dto.Age;
}
if (type == UserTypes.Professor)
{
user.Professor.IdentityCard = dto.IdentityCard;
}
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
public async Task<ServiceResult<List<UserDetailDto>>> GetAll(int tenantId)
var result = new ServiceResult<List<UserDetailDto>>();
.Where(x => x.UserType.Name != UserTypes.FacultyAdmin && x.TenantId == tenantId)
var usersDto = _mapper.Map<List<UserDetailDto>>(users);
result.Data = usersDto;
return result;
}
public async Task<ServiceResult<UserDetailDto>> GetById(int userId)
var result = new ServiceResult<UserDetailDto>();
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var user = await _context.Users
.Include(x => x.UserType)
.Include(x => x.Student)
.Include(x => x.Professor)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
var userType = user.UserType.Name;
if (userType == UserTypes.Student && user.Student == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.StudentDoesNotExist, userId);
return result;
}
else if(userType == UserTypes.Professor && user.Professor == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, userId);
return result;
}
var userDto = _mapper.Map<UserDetailDto>(user);
result.Data = userDto;
return result;
}
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
public async Task<ServiceResult<User>> GetByUsername(string username, int tenantId)
{
var result = new ServiceResult<User>();
var user = await _context.Users
.Include(x => x.UserType)
.Include(x => x.Student)
.Include(x => x.Professor)
.Where(x => x.TenantId == tenantId
&& x.Username == username)
.FirstOrDefaultAsync();
if (user == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, username);
return result;
}
var userType = user.UserType.Name;
if (userType == UserTypes.Student && user.Student == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.StudentDoesNotExist, username);
return result;
}
else if (userType == UserTypes.Professor && user.Professor == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, username);
return result;
}
result.Data = user;
return result;
}
public async Task<ServiceResult<bool>> UpdatePassword(int userId, string password)
{
var result = new ServiceResult<bool>();
var user = await _context.Users.FirstOrDefaultAsync();
if (user == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
user.Password = password;
await _context.SaveChangesAsync();
return result;
}
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
public async Task<ServiceResult<int>> GetTenant(int userId)
{
var result = new ServiceResult<int>();
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
result.Data = user.TenantId;
return result;
}
public async Task<ServiceResult<bool>> Delete(int userId)
{
var result = new ServiceResult<bool>();
var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null)
{
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
_context.Users.Remove(user);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
public async Task<ServiceResult<UserTypeDto>> GetUserType(int userId)
{
var result = new ServiceResult<UserTypeDto>();
var user = await _context.Users
.Include(x => x.UserType)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
}
result.Data = _mapper.Map<UserTypeDto>(user.UserType);
return result;
}