public async Task <ApplicationResponse <CourseDto> > GetByIdAsync(string courseId)
        {
            Guid courseGuid = new Guid();

            if (Guid.TryParse(courseId, out courseGuid))
            {
                Course course = await _courseRepository.GetByIdAsync(courseGuid);

                var appResp = new ApplicationResponse <CourseDto>();

                if (course != null)
                {
                    CourseDto dto = _mapper.Map <CourseDto>(course);
                    appResp.Response = dto;
                    appResp.Message  = "OK";
                    return(appResp);
                }
                else
                {
                    appResp.Message = "Not Found";
                    appResp.AddError($"Course Id '{courseId}' Not Found.");
                    return(appResp);
                }
            }
            else
            {
                var response = new ApplicationResponse <CourseDto>();
                response.Response = new CourseDto();
                response.AddError("Identifier is in a wrong format.");
                return(response);
            }
        }
        public async Task <ApplicationResponse <LecturerDto> > GetByIdAsync(string lecturerId)
        {
            Guid lecturerGuid = new Guid();

            if (Guid.TryParse(lecturerId, out lecturerGuid))
            {
                Lecturer lecturer = await _lecturerRepository.GetByIdAsync(lecturerGuid);

                var appResp = new ApplicationResponse <LecturerDto>();

                if (lecturer != null)
                {
                    LecturerDto dto = _mapper.Map <LecturerDto>(lecturer);
                    appResp.Response = dto;
                    appResp.Message  = "OK";
                    return(appResp);
                }
                else
                {
                    appResp.AddError("Not Found");
                    return(appResp);
                }
            }
            else
            {
                var response = new ApplicationResponse <LecturerDto>();
                response.Response = new LecturerDto();
                response.AddError("Identifier is in a wrong format.");
                return(response);
            }
        }
        public async Task <ApplicationResponse <IEnumerable <CourseDto> > > GetAllAsync()
        {
            var courses = await _courseRepository.GetAllAsync();

            var coursesDto = _mapper.Map <IEnumerable <CourseDto> >(courses);

            var appResp = new ApplicationResponse <IEnumerable <CourseDto> >
            {
                Response = coursesDto
            };

            return(appResp);
        }
Пример #4
0
        public async Task <ApplicationResponse <bool> > SignUpStudentToCourseAsync(Guid courseId, StudentDto student)
        {
            var appResp = new ApplicationResponse <bool>();
            var command = _mapper.Map <SignUpToCourseCommand>(student);

            command.CourseId = courseId;
            var signUpToCourseCommandResponse = await _signUpToCourseHandler.Handle(command);

            if (signUpToCourseCommandResponse.Succeeded)
            {
                var updCourseStatisticsCommand = new UpdateCourseStatisticsCommand
                {
                    Course  = signUpToCourseCommandResponse.Course,
                    Student = signUpToCourseCommandResponse.Student
                };

                var updateCourseStatisticsCommandResponse = await _updateCourseStatisticsHandler.Handle(updCourseStatisticsCommand);

                if (!updateCourseStatisticsCommandResponse.Succeeded)
                {
                    appResp.Message = "Oops, Something went wrong updating statistics.";
                    appResp.AddErrorsRange(updateCourseStatisticsCommandResponse.Errors);
                }
                else
                {
                    var email = new Email();
                    email.To   = student.Email;
                    email.Body = "Contratulation, You have been enrolled to a new Course";
                    await _emailSender.SendEmailAsync(email);

                    appResp.Message  = "OK - Success";
                    appResp.Response = true;
                }

                return(appResp);
            }
            else
            {
                appResp.Message = "Sorry, Something went wrong";
                appResp.AddErrorsRange(signUpToCourseCommandResponse.Errors);
            }

            return(appResp);
        }
        public async Task <ApplicationResponse <bool> > CreateAsync(LecturerDto lecturer)
        {
            Lecturer domainObj = _mapper.Map <Lecturer>(lecturer);
            bool     created   = await _lecturerRepository.CreateAsync(domainObj);

            lecturer.Id = domainObj.Id;

            if (created)
            {
                return(new ApplicationResponse <bool> {
                    Message = "OK"
                });
            }
            else
            {
                var response = new ApplicationResponse <bool>();
                response.AddError("Sorry, Something went wrong");
                return(response);
            }
        }
        public async Task <ApplicationResponse <bool> > CreateAsync(CourseDto course)
        {
            Course domainObj = _mapper.Map <Course>(course);
            bool   created   = await _courseRepository.CreateAsync(domainObj);

            course.Id          = domainObj.Id;
            course.Lecturer.Id = domainObj.Lecturer.Id;

            if (created)
            {
                return(new ApplicationResponse <bool> {
                    Message = "OK"
                });
            }
            else
            {
                var response = new ApplicationResponse <bool>();
                response.AddError("Sorry, Something went wrong");
                return(response);
            }
        }