Exemplo n.º 1
0
        public async Task <ToRespond> CreateIndustrialSupervisor(IndustrialSupervisorRequest industrialSupervisorRequest)
        {
            if (industrialSupervisorRequest == null)
            {
                return(new ToRespond()
                {
                    StatusCode = Helpers.ObjectNull,
                    StatusMessage = Helpers.StatusMessageObjectNull
                });
            }

            if (await IndustrialSupervisorExists(industrialSupervisorRequest.EmailAddress))
            {
                return(new ToRespond()
                {
                    StatusCode = Helpers.ObjectExists,
                    StatusMessage = Helpers.StatusMessageObjectExists
                });
            }

            var industrialSupervisor = new IndustrialSupervisor
            {
                EmailAddress   = industrialSupervisorRequest.EmailAddress,
                FirstName      = industrialSupervisorRequest.FirstName,
                LastName       = industrialSupervisorRequest.LastName,
                PhoneNumber    = industrialSupervisorRequest.PhoneNumber,
                CompanyAddress = industrialSupervisorRequest.CompanyAddress,
                CompanyName    = industrialSupervisorRequest.CompanyName,
                SectionOfWork  = industrialSupervisorRequest.SectionOfWork
            };

            if (industrialSupervisorRequest.PictureUrl != null)
            {
                var result = _cloudinaryRepository.UploadFileToCloudinary(industrialSupervisorRequest.PictureUrl);
                var image  = (RawUploadResult)result.ObjectValue;
                industrialSupervisor.PictureUrl = image.Uri.ToString();
            }

            var dbTransaction = await _dataContext.Database.BeginTransactionAsync();

            _globalRepository.Add(industrialSupervisor);
            var saveVal = await _globalRepository.SaveAll();

            if (saveVal != null)
            {
                if (!saveVal.Value)
                {
                    return(new ToRespond()
                    {
                        StatusCode = Helpers.SaveNoRowAffected,
                        StatusMessage = "Can Not Save"
                    });
                }
                var user = new User
                {
                    UserName   = industrialSupervisorRequest.EmailAddress,
                    Email      = industrialSupervisorRequest.EmailAddress,
                    UserTypeId = industrialSupervisor.IndustrialSupervisorId,
                    UserType   = Helpers.Student
                };

                var password = (new Generate()).RandomPassword();

                var result = await _userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    //ASSIGN STUDENT ROLE TO USER (Student)
                    string IndustrialSupervisorTypeRole = Helpers.IndustrialSupervisorRole;


                    var assignmentResult = await _userManager.AddToRoleAsync(user, IndustrialSupervisorTypeRole);

                    if (assignmentResult.Succeeded)
                    {
                        //THEN UPDATE Student TABLE USERID COLUMN WITH NEWLY CREATED USER ID
                        industrialSupervisor.UserId = user.Id;
                        var IndustrialSupervisorUpdateResult = _globalRepository.Update(industrialSupervisor);
                        if (!IndustrialSupervisorUpdateResult)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.NotSucceeded,
                                StatusMessage = "Not Suceeded"
                            });
                        }

                        var IndustrialSupervisorUpdateSaveResult = await _globalRepository.SaveAll();

                        if (!IndustrialSupervisorUpdateSaveResult.HasValue)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.SaveError,
                                StatusMessage = Helpers.StatusMessageSaveError
                            });
                        }

                        if (!IndustrialSupervisorUpdateSaveResult.Value)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.SaveNoRowAffected,
                                StatusMessage = "Can not save Row affected"
                            });
                        }

                        //SEND MAIL TO Student TO CONFIRM EMAIL
                        var userTokenVal = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        string hashedEmail = GetHashedEmail(user.Email);

                        /*var shortToken = userTokenVal.Substring(0, 7);
                         * user.ShortToken = shortToken;
                         * user.LongToken = userTokenVal;
                         * _ = _globalRepository.SaveAll();*/
                        var fullToken = userTokenVal + "#" + hashedEmail;

                        var emailVerificationLink = _authenticationRepository.GetUserEmailVerificationLink(fullToken);
                        if (emailVerificationLink == null)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.ObjectNull,
                                StatusMessage = "Email Verification Link Generation Error"
                            });
                        }
                        else
                        {
                            await dbTransaction.CommitAsync();

                            return(new ToRespond()
                            {
                                StatusCode = Helpers.Success,
                                ObjectValue = _mapper.Map <IndustrialSupervisorResponse>(industrialSupervisor),
                                StatusMessage = "Student Created Successfully!!!"
                            });
                        }
                    }
                }

                return(new ToRespond()
                {
                    StatusCode = Helpers.NotSucceeded,
                    StatusMessage = "Can not Suceed"
                });
            }

            return(new ToRespond()
            {
                StatusCode = Helpers.SaveError,
                StatusMessage = Helpers.StatusMessageSaveError
            });
        }
Exemplo n.º 2
0
        public async Task <ToRespond> CreateLecturer(LecturerRequest lecturerRequest)
        {
            if (lecturerRequest == null)
            {
                return(new ToRespond()
                {
                    StatusCode = Helpers.ObjectNull,
                    StatusMessage = Helpers.StatusMessageObjectNull
                });
            }

            if (await LecturerExists(lecturerRequest.EmailAddress))
            {
                return(new ToRespond()
                {
                    StatusCode = Helpers.ObjectExists,
                    StatusMessage = Helpers.StatusMessageObjectExists
                });
            }

            var lecturer = new Lecturer
            {
                EmailAddress = lecturerRequest.EmailAddress,
                FirstName    = lecturerRequest.FirstName,
                LastName     = lecturerRequest.LastName,
                PhoneNumber  = lecturerRequest.PhoneNumber,
                Department   = lecturerRequest.Department,
            };

            if (lecturerRequest.PictureUrl != null)
            {
                var result = _cloudinaryRepository.UploadFileToCloudinary(lecturerRequest.PictureUrl);
                var image  = (RawUploadResult)result.ObjectValue;
                lecturer.PictureUrl = image.Uri.ToString();
            }

            var dbTransaction = await _dataContext.Database.BeginTransactionAsync();

            _globalRepository.Add(lecturer);
            var saveVal = await _globalRepository.SaveAll();

            if (saveVal != null)
            {
                if (!saveVal.Value)
                {
                    return(new ToRespond()
                    {
                        StatusCode = Helpers.SaveNoRowAffected,
                        StatusMessage = "Can Not Save"
                    });
                }
                var user = new User
                {
                    UserName   = lecturerRequest.EmailAddress,
                    Email      = lecturerRequest.EmailAddress,
                    UserTypeId = lecturer.LecturerId,
                    UserType   = Helpers.Student
                };

                var password = (new Generate()).RandomPassword();

                var result = await _userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    //ASSIGN STUDENT ROLE TO USER (Student)
                    string LecturerTypeRole = Helpers.LecturerRole;


                    var assignmentResult = await _userManager.AddToRoleAsync(user, LecturerTypeRole);

                    if (assignmentResult.Succeeded)
                    {
                        //THEN UPDATE Student TABLE USERID COLUMN WITH NEWLY CREATED USER ID
                        lecturer.UserId = user.Id;
                        var LecturerUpdateResult = _globalRepository.Update(lecturer);
                        if (!LecturerUpdateResult)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.NotSucceeded,
                                StatusMessage = "Not Suceeded"
                            });
                        }

                        var LecturerUpdateSaveResult = await _globalRepository.SaveAll();

                        if (!LecturerUpdateSaveResult.HasValue)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.SaveError,
                                StatusMessage = Helpers.StatusMessageSaveError
                            });
                        }

                        if (!LecturerUpdateSaveResult.Value)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.SaveNoRowAffected,
                                StatusMessage = "Can not save Row affected"
                            });
                        }

                        var userTokenVal = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        string hashedEmail = GetHashedEmail(user.Email);

                        var fullToken = userTokenVal + "#" + hashedEmail;

                        var emailVerificationLink = _authenticationRepository.GetUserEmailVerificationLink(fullToken);
                        if (emailVerificationLink == null)
                        {
                            return(new ToRespond()
                            {
                                StatusCode = Helpers.ObjectNull,
                                StatusMessage = "Email Verification Link Generation Error"
                            });
                        }
                        else
                        {
                            await dbTransaction.CommitAsync();

                            return(new ToRespond()
                            {
                                StatusCode = Helpers.Success,
                                ObjectValue = _mapper.Map <LecturerResponse>(lecturer),
                                StatusMessage = "Lecturer Created Successfully!!!"
                            });
                        }
                    }
                }

                return(new ToRespond()
                {
                    StatusCode = Helpers.NotSucceeded,
                    StatusMessage = "Can not Suceed"
                });
            }

            return(new ToRespond()
            {
                StatusCode = Helpers.SaveError,
                StatusMessage = Helpers.StatusMessageSaveError
            });
        }