Newer
Older
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 CourseService : ICourseService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public CourseService(Tsi1Context context, IMapper mapper)
}
public async Task<ServiceResult<Course>> Create(CourseCreateDto newCourse)
{
var result = new ServiceResult<Course>();
var existingCourse = await _context.Courses
.FirstOrDefaultAsync(x => x.Name == newCourse.Name && x.TenantId == newCourse.TenantId);
if (existingCourse != null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.DuplicateCourseName, newCourse.Name);
return result;
}
_context.Courses.Add(course);
await _context.SaveChangesAsync();
result.Data = course;
return result;
}
public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType)
{
var result = new ServiceResult<List<CoursePreviewDto>>();
var courses = new List<Course>();
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;
}
if (userType == UserTypes.Student)
{
courses = await _context.StudentCourses
.Include(x => x.Course)
.Select(x => x.Course)
.ToListAsync();
}
else if (userType == UserTypes.Professor)
{
courses = await _context.ProfessorCourses
.Include(x => x.Course)
.Select(x => x.Course)
.ToListAsync();
}
public async Task<ServiceResult<bool>> Matriculate(int userId, int courseId)
{
var result = new ServiceResult<bool>();
var user = await _context.Users
.Include(x => x.Student)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null || user.Student == null)
{
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
return result;
}
var existingStudentCourse = await _context.StudentCourses
.FirstOrDefaultAsync(x => x.StudentId == user.StudentId && x.CourseId == course.Id);
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.StudentCourseAlreadyExists, user.Username, course.Name);
return result;
}
var studentCourse = new StudentCourse
{
Course = course,
Student = user.Student
};
_context.StudentCourses.Add(studentCourse);
await _context.SaveChangesAsync();
result.Data = true;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
return result;
}
public async Task<ServiceResult<bool>> AddProfessorToCourse(ProfessorCourseDto professorCourseDto)
{
var result = new ServiceResult<bool>();
var user = await _context.Users
.Include(x => x.Professor)
.FirstOrDefaultAsync(x => x.Id == professorCourseDto.UserId);
if (user == null || user.Professor == null)
{
result.Message = string.Format(ErrorMessages.UserDoesNotExist, user.Username);
return result;
}
var course = await _context.Courses
.FirstOrDefaultAsync(x => x.Id == professorCourseDto.CourseId);
if (course == null)
{
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId);
return result;
}
var existingProfessorCourse = await _context.ProfessorCourses
.FirstOrDefaultAsync(x => x.ProfessorId == user.ProfessorId && x.CourseId == course.Id);
if (existingProfessorCourse != null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ProfessorCourseAlreadyExists, user.Username, course.Name);
return result;
}
var professorCourse = new ProfessorCourse
{
Course = course,
Professor = user.Professor
};
_context.ProfessorCourses.Add(professorCourse);
result.Data = true;
184
185
186
187
188
189
190
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
public async Task<ServiceResult<List<CoursePreviewDto>>> GetAll(int tenantId)
{
var result = new ServiceResult<List<CoursePreviewDto>>();
var courses = await _context.Courses
.Where(x => x.TenantId == tenantId)
.ToListAsync();
var coursesDto = _mapper.Map<List<CoursePreviewDto>>(courses);
result.Data = coursesDto;
return result;
}
public async Task<ServiceResult<bool>> Modify(int courseId, CourseCreateDto courseDto)
{
var result = new ServiceResult<bool>();
var course = await _context.Courses
.FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
return result;
}
_mapper.Map(courseDto, course);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
public async Task<ServiceResult<Course>> Delete(int courseId)
var course = await _context.Courses
.FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
return result;
}
_context.Courses.Remove(course);
await _context.SaveChangesAsync();
243
244
245
246
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
public async Task<ServiceResult<bool>> DropOutFromCourse(int userId, int courseId)
{
var result = new ServiceResult<bool>();
var user = await _context.Users
.Include(x => x.Student)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null || user.Student == null)
{
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
return result;
}
var studentCourse = await _context.StudentCourses
.FirstOrDefaultAsync(x => x.StudentId == user.StudentId && x.CourseId == course.Id);
if (studentCourse == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.StudentCourseDoesNotExists, user.Username, course.Name);
return result;
}
_context.StudentCourses.Remove(studentCourse);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
public async Task<ServiceResult<bool>> RemoveProfessorToCourse(ProfessorCourseDto professorCourseDto)
{
var result = new ServiceResult<bool>();
var user = await _context.Users
.Include(x => x.Professor)
.FirstOrDefaultAsync(x => x.Id == professorCourseDto.UserId);
if (user == null || user.Professor == null)
{
result.Message = string.Format(ErrorMessages.UserDoesNotExist, user.Username);
return result;
}
var course = await _context.Courses
.FirstOrDefaultAsync(x => x.Id == professorCourseDto.CourseId);
if (course == null)
{
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId);
return result;
}
var professorCourse = await _context.ProfessorCourses
.FirstOrDefaultAsync(x => x.ProfessorId == user.ProfessorId && x.CourseId == course.Id);
if (professorCourse == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ProfessorCourseDoesNotExists, user.Username, course.Name);
return result;
}
_context.ProfessorCourses.Remove(professorCourse);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
public async Task<ServiceResult<List<UserPreviewDto>>> GetProfessors(int courseId)
{
var result = new ServiceResult<List<UserPreviewDto>>();
var users = await _context.ProfessorCourses
.Include(x => x.Professor)
.ThenInclude(x => x.User)
.Where(x => x.CourseId == courseId)
.Select(x => x.Professor.User)
.ToListAsync();
var userDtos = _mapper.Map<List<UserPreviewDto>>(users);
result.Data = userDtos;
return result;
}