Beispiel #1
0
        public async Task <BLSingleResponse <LoginRespDto> > LoginAsync(LoginDto model)
        {
            var response = new BLSingleResponse <LoginRespDto>();

            //new PasswordHasher<UserDto>().HashPassword(model.Username, pPassword);

            try
            {
                var signInResul = await _accountManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe);

                if (signInResul == SignInResult.Failed)
                {
                    response.Data = new LoginRespDto("Username or Password is invalid.");
                }
                else if (signInResul == SignInResult.TwoFactorRequired)
                {
                    response.Data = new LoginRespDto("This User does not confirmed email or phone.");
                }
                else if (signInResul == SignInResult.LockedOut)
                {
                    response.Data = new LoginRespDto("This User is currently locked out.");
                }
                else
                {
                    response.Data = TokenBuilder(model.Username, "basic");
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #2
0
        public async Task <BLSingleResponse <bool> > DeleteAsync(GradeDto pDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                //var entity = await _uow.GetRepo<Grade>().GetByIdAsync(pDto.Id);
                await _uow.GetRepository <Grade>().DeleteAsync(pDto);

                if (await _uow.CommitAsync())
                {
                    response.Data = true;
                }
                else
                {
                    HandleSVCException(response, "This action couldn't be performed.");
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #3
0
        public async Task <BLSingleResponse <bool> > SaveAttendsAsync(int subjectId, IList <StudentDto> studentDtos)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                foreach (StudentDto stud in studentDtos)
                {
                    var attend = new Attend
                    {
                        SubjectId  = subjectId,
                        StudentId  = stud.Id,
                        Attendance = stud.TodayAttend.Attendance,
                        Obs        = stud.TodayAttend.Obs,
                        Date       = DateTime.Now
                    };

                    await _uow.GetRepository <Attend>().InsertAsync(attend);
                }
                response.Data = await _uow.CommitAsync();
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #4
0
        public async Task <BLSingleResponse <bool> > EnrollStudentAsync(int subjectId, int userId)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                //TODO: applay transaction capability
                //using (var trx = await _uow.StartTransactionAsync())
                //{
                //    trx.Commit();
                //}

                var subjEntity = await _uow.GetRepository <Subject>().GetByIdAsync(subjectId);

                if (subjEntity == null)
                {
                    throw new Exception($"Subject Id = {subjectId} didn't match any result.");
                }

                var studentResult = await _uow.GetRepository <Student>().GetByExpressionAsync(s => s.ApplicationUserId == userId);

                StateManager stateManager = await StateManager.GetStateManagerAsync(_studentStateSvc, studentResult.Id, subjectId);

                response.Data = await stateManager.EnrollStudentAsync(
                    _mapperExplicit.MapEntity <Student, StudentDto>(studentResult), _mapperExplicit.MapEntity <Subject, SubjectDto>(subjEntity));
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #5
0
        public async Task <BLSingleResponse <AuthenticationProperties> > SignInWithGoogleAsync(string redirectUrl)
        {
            var response = new BLSingleResponse <AuthenticationProperties>();

            await Task.Run(() =>
            {
                response.Data = _accountManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);
            });

            return(response);
        }
Beispiel #6
0
        public async Task <BLSingleResponse <bool> > UpdateAsync(GradeDto pDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #7
0
        public async Task <BLSingleResponse <LoginRespDto> > LoginAsync(LoginDto model)
        {
            var response = new BLSingleResponse <LoginRespDto>();

            //new PasswordHasher<UserDto>().HashPassword(model.Username, pPassword);

            try
            {
                var signInResul = await _accountManager.SignInAsync(model.Username, model.Password, model.RememberMe);

                if (signInResul == SignInResult.Failed)
                {
                    response.Data = new LoginRespDto("Username or Password is invalid.");
                }
                else if (signInResul == SignInResult.TwoFactorRequired)
                {
                    response.Data = new LoginRespDto("This User does not confirmed email or phone.");
                }
                else if (signInResul == SignInResult.LockedOut)
                {
                    response.Data = new LoginRespDto("This User is currently locked out.");
                }
                else
                {
                    var user = await _accountManager.UserManager.FindByNameAsync(model.Username);

                    var role = await _accountManager.GetRoleByUserAsync(user);

                    if (role != null)
                    {
                        var tokenResul = _tokenProvider.GenerateToken(user, role);
                        response.Data = new LoginRespDto {
                            Token = tokenResul.Token, ExpirationDate = tokenResul.ExpirationDate
                        };
                    }
                    else
                    {
                        response.Data = new LoginRespDto("This User has no role assigned.");
                        await _accountManager.SignOutAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #8
0
        public async Task <BLSingleResponse <bool> > TryEnrollAsync(int subjectId, int studentId)
        {
            var response = new BLSingleResponse <bool>()
            {
                Data = true
            };

            try
            {
                var subjResponse = await _uow.GetRepository <Subject>().FindAsync(s => s.Id == subjectId, x => x.Include(s => s.PreReqSubject));

                if (subjResponse == null)
                {
                    throw new Exception($"Subject Id = {subjectId} didn't match any result.");
                }

                //check if he is already enrolled
                var isAlreadyEnrolled = await _uow.GetRepository <Student>().ExistAsync(s => s.ApplicationUserId == studentId && s.StudentStates
                                                                                        .Any(ss => ss.SubjectId == subjectId && ss.AcademicState == StudentStateEnum.ENROLLED));

                if (isAlreadyEnrolled)
                {
                    response.Errors.Add("You're already enrolled to this subject.");
                    response.Data = false;
                    return(response);
                }

                //check if the subject has a prerequired subject
                if (subjResponse.PreReqSubject != null)
                {
                    var hasNotPreReq = await _uow.GetRepository <Student>()
                                       .ExistAsync(s => s.ApplicationUserId == studentId && s.StudentStates
                                                   .Any(ss => ss.SubjectId == subjResponse.PreReqSubject.Id && ss.AcademicState == StudentStateEnum.ACHIEVED));

                    if (!hasNotPreReq)
                    {
                        response.Errors.Add("You cannot enroll to this subject due to previous requirement.");
                    }
                    response.Data = hasNotPreReq;
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #9
0
        public async Task <BLSingleResponse <UserDto> > GetUserByIdAsync(int pId)
        {
            var response = new BLSingleResponse <UserDto>();

            try
            {
                var result = await _uow.Users.FindByIdAsync(pId);

                response.Data = _mapper.MapEntity <ApplicationUser, UserDto>(result);
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }
            return(response);
        }
Beispiel #10
0
        public async Task <BLSingleResponse <SubjectDto> > GetByIdAsync(int pId)
        {
            var response = new BLSingleResponse <SubjectDto>();

            try
            {
                var entityResult = await _uow.GetRepository <Subject>().GetByIdAsync(pId);

                response.Data = _mapper.MapEntity(entityResult);
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #11
0
        public async Task <BLSingleResponse <ClassDto> > GetByIdAsync(int pId)
        {
            var response = new BLSingleResponse <ClassDto>();

            try
            {
                var entity = await _uow.GetRepository <Class>().FindAsync(c => c.Id == pId, x => x.Include(c => c.Grade));

                response.Data = _mapper.MapEntity(entity);
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #12
0
        public async Task <BLSingleResponse <bool> > ConfirmEmailAsync(UserDto user, string token)
        {
            var response = new BLSingleResponse <bool>();

            var confirmResult = await _accountManager.ConfirmEmailAsync(user, token);

            if (confirmResult.Succeeded)
            {
                response.Data = true;
            }
            else
            {
                HandleSVCException(response, confirmResult.Errors.ToList().ConvertAll(x => x.Description).ToArray());
            }

            return(response);
        }
Beispiel #13
0
        public async Task <BLSingleResponse <bool> > UpdateAsync(TableDto pDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                var entity = _mapper.MapToEntity(pDto);
                await _uow.GetEfRepository <Table>().UpdateAsync(entity);

                response.Data = await _uow.CommitAsync();
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #14
0
        public async Task <BLSingleResponse <StudentStateDto> > GetLastValidStateAsync(int studentId, int subjectId)
        {
            var response = new BLSingleResponse <StudentStateDto>();

            try
            {
                var entityResult = await _uow.GetRepository <StudentState>().FindAsync(
                    s => !s.DateTo.HasValue && s.StudentId == studentId && s.SubjectId == subjectId, null);

                response.Data = _mapper.MapEntity(entityResult);
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #15
0
        public async Task <BLSingleResponse <bool> > SendEmailAsync(string email, string callbackUrl)
        {
            var response = new BLSingleResponse <bool>();

            var client = new SendGridClient(_settings.SendGridApiKey);
            var msg    = MailHelper.CreateSingleEmail(
                new EmailAddress(_settings.SendGridFrom, _settings.SendGridUserSender),
                new EmailAddress(email),
                _settings.SendGridSubject,
                "Thank you for register.",
                $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

            // Disable click tracking.
            // See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
            msg.SetClickTracking(false, false);
            var sentResult = await client.SendEmailAsync(msg);

            response.Data = sentResult.StatusCode == System.Net.HttpStatusCode.Accepted;
            return(response);
        }
Beispiel #16
0
        public async Task <BLSingleResponse <GradeDto> > CreateAsync(GradeDto pDto)
        {
            var response = new BLSingleResponse <GradeDto>();

            try
            {
                var entity = await _uow.GetRepository <Grade>().InsertAsync(pDto);

                if (!await _uow.CommitAsync())
                {
                    HandleSVCException(response, "This action couldn't be performed.");
                }
                response.Data = _mapper.MapEntity(entity);
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #17
0
        public async Task <BLSingleResponse <ClassDto> > CreateAsync(ClassDto pDto)
        {
            var response = new BLSingleResponse <ClassDto>();

            try
            {
                var entity = await _uow.GetRepository <Class>().InsertAsync(pDto);

                if (!await _uow.CommitAsync())
                {
                    HandleSVCException(response, "Class could not be created");
                }
                response.Data = _mapper.MapEntity(entity);
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #18
0
        public async Task <BLSingleResponse <bool> > DeleteAsync(ClassDto pDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                await _uow.GetRepository <Class>().DeleteAsync(pDto);

                if (!await _uow.CommitAsync())
                {
                    HandleSVCException(response, "Class could not be deleted");
                }
                response.Data = true;
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #19
0
        public async Task <BLSingleResponse <UserDto> > RegisterAsync(RegisterDto model)
        {
            var response = new BLSingleResponse <UserDto>();

            var user = _mapper.MapToBaseClass <UserDto, ApplicationUser>(new UserDto(model.Username, model.Email, model.FirstName, model.LastName));

            var signUpResul = await _accountManager.SignUpAsync(user, model.Password);

            if (signUpResul.Succeeded)
            {
                var entity = await _accountManager.UserManager.FindByNameAsync(model.Username);

                response.Data = _mapper.MapEntity <ApplicationUser, UserDto>(entity);
            }
            else
            {
                HandleSVCException(response, signUpResul.Errors.ToList().ConvertAll(x => x.Description).ToArray());
            }

            return(response);
        }
Beispiel #20
0
        public async Task <BLSingleResponse <bool> > SetInitialTableArrangementAsync(IEnumerable <TableDto> tablesDtos)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                foreach (TableDto table in tablesDtos)
                {
                    var entityReult = _mapper.MapToEntity(table);
                    await _uow.GetEfRepository <Table>().InsertAsync(entityReult);
                }

                response.Data = await _uow.CommitAsync();
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #21
0
        public async Task <BLSingleResponse <bool> > SaveOrderAsync(OrderDto consumeDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                TableStateManager stateManager = await TableStateManager.GetTableStateManagerAsync(tableSvc, consumeDto);

                if (await stateManager.SaveAsync(consumeDto))
                {
                    var resp = await _uow.GetEfRepository <Order>().InsertAsync(consumeDto.BaseEntity);

                    await _uow.CommitAsync();
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #22
0
        public async Task <BLSingleResponse <bool> > SaveExamResultAsync(int subjectId, StudentDto studentDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                foreach (Exam exam in studentDto.Exams)
                {
                    exam.Date = DateTime.Now;

                    var entityExam = await _uow.GetRepository <Exam>()
                                     .GetByExpressionAsync(e => e.SubjectId == subjectId && e.StudentId == studentDto.Id && e.ExamType == exam.ExamType);

                    if (entityExam != null)
                    {
                        if (entityExam.Score != exam.Score)
                        {
                            entityExam.Score = exam.Score;
                            entityExam.Date  = exam.Date;
                            await _uow.GetRepository <Exam>().UpdateAsync(entityExam);
                        }
                        continue;
                    }

                    exam.SubjectId = subjectId;
                    exam.StudentId = studentDto.Id;
                    await _uow.GetRepository <Exam>().InsertAsync(exam);
                }
                response.Data = await _uow.CommitAsync();
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #23
0
        public async Task <BLSingleResponse <bool> > SaveSubjectExams(SubjectDto pSubject)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                await _uow.GetRepository <Subject>().UpdateAsync(pSubject);

                if (await _uow.CommitAsync())
                {
                    response.Data = true;
                }
                else
                {
                    HandleSVCException(response, "This action couldn't be performed.");
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #24
0
        public async Task <BLSingleResponse <bool> > SaveWaiterExams(WaiterDto pWaiter)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                await _uow.GetEfRepository <Waiter>().UpdateAsync(pWaiter.BaseEntity);

                if (await _uow.CommitAsync())
                {
                    response.Data = true;
                }
                else
                {
                    HandleSVCException(response, "This action couldn't be performed.");
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #25
0
        public async Task <BLSingleResponse <bool> > UpdateAsync(StudentStateDto pDto)
        {
            var response = new BLSingleResponse <bool>();

            try
            {
                await _uow.GetRepository <StudentState>().UpdateAsync(pDto);

                if (await _uow.CommitAsync())
                {
                    response.Data = true;
                }
                else
                {
                    HandleSVCException(response, "This action couldn't be performed.");
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #26
0
        public async Task <BLSingleResponse <WaiterDto> > CreateAsync(WaiterDto pDto)
        {
            var response = new BLSingleResponse <WaiterDto>();

            try
            {
                var entityResult = await _uow.GetEfRepository <Waiter>().InsertAsync(pDto.BaseEntity);

                if (await _uow.CommitAsync())
                {
                    response.Data = _mapper.MapFromEntity(entityResult);
                }
                else
                {
                    HandleSVCException(response, "This action couldn't be performed.");
                }
            }
            catch (Exception ex)
            {
                HandleSVCException(response, ex);
            }

            return(response);
        }
Beispiel #27
0
        public async Task <BLSingleResponse <string> > HandleExternalLoginAsync()
        {
            var response = new BLSingleResponse <string>();

            try
            {
                //retrieves user information stored in a cookie
                var extLoginInfo = await _accountManager.GetExternalLoginInfoAsync();

                //check if the user has previously logged in using the external login provider
                var result = await _accountManager.ExternalLoginSignInAsync(extLoginInfo.LoginProvider, extLoginInfo.ProviderKey, isPersistent : false);

                if (result.Succeeded) //user does already exist
                {
                    var user = await _accountManager.UserManager.FindByNameAsync(_accountManager.Context.User.Identity.Name);

                    var role = await _accountManager.GetRoleByUserAsync(user);

                    response.Data = _tokenProvider.GenerateToken(user, role).Token;
                }
                else //user does not exist yet
                {
                    var defaultRole = new ApplicationRole(RolesEnum.STUDENT.GetDescription());
                    var email       = extLoginInfo.Principal.FindFirstValue(ClaimTypes.Email);

                    ApplicationUser newUser = _userManager.Users.SingleOrDefault(u => u.Email == email);

                    if (newUser == null)
                    {
                        newUser = new ApplicationUser(email.Split('@')[0], email,
                                                      extLoginInfo.Principal.FindFirstValue(ClaimTypes.GivenName),
                                                      extLoginInfo.Principal.FindFirstValue(ClaimTypes.Surname))
                        {
                            EmailConfirmed = true
                        };

                        var createResult = await _userManager.CreateAsync(newUser, new List <ApplicationRole> {
                            defaultRole
                        });

                        if (!createResult.Succeeded)
                        {
                            throw new Exception(createResult.Errors.Select(e => e.Description)
                                                .Aggregate((errors, error) => $"{errors}, {error}"));
                        }
                    }

                    //associate the new user with the external login provider
                    await _userManager.AddLoginAsync(newUser, extLoginInfo);

                    var newUserClaims = extLoginInfo.Principal.Claims;
                    newUserClaims.Append(new Claim("userid", newUser.Id.ToString()));
                    newUserClaims.Append(new Claim(ClaimTypes.Role, defaultRole.Name));

                    await _userManager.AddClaimsAsync(newUser, newUserClaims);

                    await _accountManager.SignInAsync(newUser.UserName, newUser.PasswordHash, false);

                    response.Data = _tokenProvider.GenerateToken(newUser, defaultRole).Token;
                }
            }

            catch (Exception ex)
            {
                HandleSVCException(ex);
            }

            return(response);
        }