예제 #1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            ViewModels.LoginInput oLoginInput =
                new ViewModels.LoginInput()
            {
                Password = Passwordtxt.Text,
                Username = Usernametxt.Text,
            };

            FluentValidation.Results.ValidationResult validationResult = Utility.GeneralViewModelValidator
                                                                         <ViewModels.LoginInput, Validations.LoginInputValidator>(oLoginInput);
            if (!validationResult.IsValid)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show(error_message, "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Domain.Entities.User oUser =
                db.Users.Where(current => current.Username == oLoginInput.Username.Trim().ToLower())
                .FirstOrDefault();
            if (oUser == null)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Your Login Information is Not Correct! ☹️ Try Again!", "Incorrect Login Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Your Login Information is Not Correct! ☹️ Try Again!", "Incorrect Login Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string hashPassword = Hashing.GetSha256(oLoginInput.Password);

            if (oUser.Password != hashPassword)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Your Login Information is Not Correct! ☹️ Try Again!", "Incorrect Login Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AuthenicatedUser oAuthenicatedUser = new AuthenicatedUser(oUser);

            //Domain.Entities.User oUsertest =
            //	db.Users.Where(current => current.Id == oAuthenicatedUser.Id)
            //	.FirstOrDefault();
            //if (oUsertest == null)
            //{

            //}
            //if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            //{

            //}

            MainPage oLibraryFirstPageForm = new MainPage();

            oLibraryFirstPageForm.SetAuthenicatedUser(oAuthenicatedUser);
            oLibraryFirstPageForm.Show();

            this.Close();
        }
예제 #2
0
        private void AddClick_Click(object sender, EventArgs e)
        {
            int    PulishDate = 0;
            double Price      = 0;

            if (!int.TryParse(PublishedDatetxt.Text, out PulishDate))
            {
                MessageBox.Show("You Shuold Enter PublishDate In Number Format!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!double.TryParse(Pricetxt.Text, out Price))
            {
                MessageBox.Show("You Shuold Enter Price In Number Format!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ViewModels.BookInput oBookInput =
                new ViewModels.BookInput()
            {
                Name          = Nametxt.Text,
                Author        = Authoretxt.Text,
                Translator    = Translatortxt.Text,
                Publisher     = Publishertxt.Text,
                PublishedDate = PulishDate,
                Circulation   = Circulationtxt.Text,
                Price         = Price,
                ISBN          = ISBNtxt.Text,
            };
            FluentValidation.Results.ValidationResult validationResult = Utility.GeneralViewModelValidator
                                                                         <ViewModels.BookInput, Validations.BookInoutValidator>(oBookInput);
            if (!validationResult.IsValid)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show(error_message, "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (db.Books.Where(current => current.Name == oBookInput.Name.Trim().ToLower()).Any())
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("A book registerd with this 'Name' Before, Please choose a new one!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Domain.Entities.User oUser =
                db.Users.Where(current => current.Id == authenicatedUser.Id)
                .FirstOrDefault();
            if (oUser == null)
            {
                MessageBox.Show("An Error accrued in Your Authenication", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            {
                MessageBox.Show("An Error accrued in Your Authenication", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Book oBook = new Book()
            {
                Name           = oBookInput.Name,
                Author         = oBookInput.Author,
                Translator     = oBookInput.Translator,
                Publisher      = oBookInput.Publisher,
                PublishedDate  = oBookInput.PublishedDate,
                Circulation    = oBookInput.Circulation,
                Price          = oBookInput.Price,
                ISBN           = oBookInput.ISBN,
                UploaderUser   = oUser,
                UploaderUserId = oUser.Id,
            };


            string BookText = BookTextRichBox.Text.ToString();
            string TextPath = "c:\\BookLibraryData\\Book-" + oBook.Id.ToString();

            try
            {
                bool exists = Directory.Exists(TextPath);
                if (exists)
                {
                    Directory.Delete(TextPath, true);
                }
                Directory.CreateDirectory(TextPath);
                StreamWriter Save = new StreamWriter(TextPath + "\\" + "Text.txt", true);
                Save.WriteLine(BookText);
                Save.Close();
                if (!string.IsNullOrEmpty(ImagePath))
                {
                    Image oImage = Image.FromFile(ImagePath);
                    if (oImage == null)
                    {
                        MessageBox.Show("This Image is not Exists !", "Error In Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (oImage.Height < 100 || oImage.Width < 100)
                    {
                        MessageBox.Show("Image Size is very small !", "Error Image Size", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (oImage.Height > 2000 || oImage.Width > 2000)
                    {
                        MessageBox.Show("Image Size is very big !", "Error Image Size", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    oImage.Save(TextPath + "\\" + "Image.jpg");
                    oBook.HasImage = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An Unknown error has occurred : \n \n" + ex.Message, "File Error!", MessageBoxButtons.OK);
                return;
            }


            db.Books.Add(oBook);
            db.SaveChanges();

            var resault = MessageBox.Show("The Book Successfuly Added To Library!", "Book Added", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (resault == DialogResult.OK)
            {
                this.Close();
            }
        }
예제 #3
0
        private void Button4_Click(object sender, EventArgs e)
        {
            ViewModels.SignUpInput oSignUpInput =
                new ViewModels.SignUpInput()
            {
                FirstName          = Nametxt.Text,
                LastName           = LastNametxt.Text,
                EmailAddress       = Emailtxt.Text,
                PhoneNumberIsoCode = comboBoxIsoCode.SelectedValue.ToString(),
                PhoneNumber        = PhoneNumbertxt.Text,
                Password           = Passwordtxt.Text,
                Username           = Usernametxt.Text,
            };

            FluentValidation.Results.ValidationResult validationResult = Utility.GeneralViewModelValidator
                                                                         <ViewModels.SignUpInput, Validations.SignUpInputValidator>(oSignUpInput);
            if (!validationResult.IsValid)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show(error_message, "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (db.Users.Where(current => current.Username == oSignUpInput.Username.Trim().ToLower()).Any())
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Someone registerd with this 'Username' Before, Please choose a new one!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(oSignUpInput.PhoneNumber))
            {
                oSignUpInput.PhoneNumberIsoCode = string.Empty;
            }
            else
            {
                string strRawPhoneNumber     = oSignUpInput.PhoneNumber.Trim().ToLower();
                string strRawCountryIso2Code = oSignUpInput.PhoneNumberIsoCode.Trim();
                PhoneNumbers.PhoneNumberUtil phoneNumberUtil;
                phoneNumberUtil = PhoneNumbers.PhoneNumberUtil.GetInstance();
                PhoneNumbers.PhoneNumber phoneNumber = phoneNumberUtil.Parse(strRawPhoneNumber, strRawCountryIso2Code);
                string strPhoneNumber = phoneNumberUtil.FormatOutOfCountryCallingNumber(phoneNumber, "IR");
                strPhoneNumber           = strPhoneNumber.Replace("  ", " ");
                strPhoneNumber           = strPhoneNumber.Replace(" ", "");
                oSignUpInput.PhoneNumber = strPhoneNumber;
            }

            if (!string.IsNullOrEmpty(oSignUpInput.EmailAddress))
            {
                oSignUpInput.EmailAddress = oSignUpInput.EmailAddress.Trim().ToLower();
            }

            oSignUpInput.Password = Hashing.GetSha256(oSignUpInput.Password);
            oSignUpInput.Username = oSignUpInput.Username.Trim().ToLower();

            User oUser = new User()
            {
                FirstName          = oSignUpInput.FirstName,
                LastName           = oSignUpInput.LastName,
                Username           = oSignUpInput.Username,
                Password           = oSignUpInput.Password,
                PhoneNumberIsoCode = oSignUpInput.PhoneNumberIsoCode,
                PhoneNumber        = oSignUpInput.PhoneNumber,
                EmailAddress       = oSignUpInput.EmailAddress,
            };

            if (!LibraryManagmentConnectSDK.ConnectionSetting.ConnectedToServer)
            {
                DialogResult result = MessageBox.Show("Couldnt Connect To The Library Managment System \n For The Signup Process You Should Connect To WS Server, Try Agin!", "Connection Error!", MessageBoxButtons.OK);
                return;
            }
            else
            {
                LibraryManagmentConnectSDK.ServerConnection oServerConnection = new LibraryManagmentConnectSDK.ServerConnection();
                LibraryManagmentConnectSDK.AddUserRequest   oAddUserRequest   =
                    new LibraryManagmentConnectSDK.AddUserRequest()
                {
                    FirstName          = oUser.FirstName,
                    LastName           = oUser.LastName,
                    Username           = oUser.Username,
                    EmailAddress       = oUser.EmailAddress,
                    PhoneNumber        = oUser.PhoneNumber,
                    Password           = oUser.Password,
                    PhoneNumberIsoCode = oUser.PhoneNumberIsoCode,
                    Role     = (int)oUser.Role,
                    AuthCode = Utility.GetApplicationSetting().LibraryAuthCode,
                };

                LibraryManagmentConnectSDK.AddUserReply oRegistreationReply =
                    oServerConnection.AddUser(oAddUserRequest);
                if (oRegistreationReply == null || oRegistreationReply.IsSuccessfull == false)
                {
                    DialogResult result = MessageBox.Show("Couldnt Register This User To The Library Managment System \n For The Signup Process You Should Connect To WS Server, Try Agin! \n With Error : "
                                                          + oRegistreationReply == null ? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oRegistreationReply.ErrorType).ToString(), "Registration Error!", MessageBoxButtons.OK);
                    return;
                }

                oUser.ConnectedToWS = true;
                oUser.WSAuthCode    = oRegistreationReply.UserAuthCode;
            }

            db.Users.Add(oUser);
            db.SaveChanges();

            AuthenicatedUser oAuthenicatedUser = new AuthenicatedUser(oUser);

            MainPage oLibraryFirstPageForm = new MainPage();

            oLibraryFirstPageForm.SetAuthenicatedUser(oAuthenicatedUser);
            oLibraryFirstPageForm.Show();

            this.Close();
        }
        private void EditClick_Click(object sender, EventArgs e)
        {
            int    PulishDate = 0;
            double Price      = 0;

            if (!int.TryParse(PublishedDatetxt.Text, out PulishDate))
            {
                MessageBox.Show("You Shuold Enter PublishDate In Number Format!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!double.TryParse(Pricetxt.Text, out Price))
            {
                MessageBox.Show("You Shuold Enter Price In Number Format!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ViewModels.BookInput oBookInput =
                new ViewModels.BookInput()
            {
                Name          = Nametxt.Text,
                Author        = Authoretxt.Text,
                Translator    = Translatortxt.Text,
                Publisher     = Publishertxt.Text,
                PublishedDate = PulishDate,
                Circulation   = Circulationtxt.Text,
                Price         = Price,
                ISBN          = ISBNtxt.Text,
            };
            FluentValidation.Results.ValidationResult validationResult = Utility.GeneralViewModelValidator
                                                                         <ViewModels.BookInput, Validations.BookInoutValidator>(oBookInput);
            if (!validationResult.IsValid)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show(error_message, "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (db.Books.Where(current => current.Name == oBookInput.Name.Trim().ToLower() && current.Id != Book.Id).Any())
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("A book registerd with this 'Name' Before, Please choose a new one!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Domain.Entities.Book oBookInNewContext =
                db.Books.Find(Book.Id);
            if (oBookInNewContext == null)
            {
                MessageBox.Show("The Book is not Avaiable For Editing!", "Book Load Error!", MessageBoxButtons.OK);
                this.Close();
            }
            if (oBookInNewContext.IsDeleted)
            {
                if (authenicatedUser.Role < Domain.Enums.Role.Administrator)
                {
                    this.Close();
                }
            }


            oBookInNewContext.Name          = oBookInput.Name;
            oBookInNewContext.Author        = oBookInput.Author;
            oBookInNewContext.Translator    = oBookInput.Translator;
            oBookInNewContext.Publisher     = oBookInput.Publisher;
            oBookInNewContext.PublishedDate = oBookInput.PublishedDate;
            oBookInNewContext.Circulation   = oBookInput.Circulation;
            oBookInNewContext.Price         = oBookInput.Price;
            oBookInNewContext.ISBN          = oBookInput.ISBN;
            oBookInNewContext.IsEdited      = true;
            oBookInNewContext.LastEditDate  = System.DateTime.Now;
            oBookInNewContext.EditerUserId  = authenicatedUser.Id;

            string BookText = BookTextRichBox.Text.ToString();
            string TextPath = "c:\\BookLibraryData\\Book-" + oBookInNewContext.Id.ToString();

            try
            {
                bool exists = Directory.Exists(TextPath);
                if (!exists)
                {
                    Directory.CreateDirectory(TextPath);
                }

                string TextFullPath = TextPath + "\\" + "Text.txt";
                if (!File.Exists(TextFullPath))
                {
                    File.CreateText(TextFullPath);
                }

                StreamWriter Save = new StreamWriter(TextFullPath, false);
                Save.WriteLine(BookText);
                Save.Close();

                if (!string.IsNullOrEmpty(ImagePath))
                {
                    Image oImage = Image.FromFile(ImagePath);
                    if (oImage == null)
                    {
                        MessageBox.Show("This Selected Image is not Exists !", "Error In Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    if (oImage.Height < 100 || oImage.Width < 100)
                    {
                        MessageBox.Show("Selected Image Size is very small !", "Error Image Size", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    if (oImage.Height > 2000 || oImage.Width > 2000)
                    {
                        MessageBox.Show("Selected Image Size is very big !", "Error Image Size", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    if (File.Exists(TextPath + "\\" + "Image.jpg"))
                    {
                        File.Delete(TextPath + "\\" + "Image.jpg");
                    }

                    oImage.Save(TextPath + "\\" + "Image.jpg");
                    oBookInNewContext.HasImage = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An Unknown error has occurred : \n \n" + ex.Message, "File Error!", MessageBoxButtons.OK);
                return;
            }


            db.Entry(oBookInNewContext).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            db.SaveChanges();

            var resault = MessageBox.Show("The Book Successfuly Edited!", "Book Edited", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (resault == DialogResult.OK)
            {
                this.Close();
            }
        }