Esempio n. 1
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text) || txtName.Text.Length < 5 || txtName.Text.Length > 20)
            {
                MessageBox.Show("Your Name Lenghth Should Be Between 5 to 20 Character, Try a New Name!", "Data Input Error!", MessageBoxButtons.OK);
                return;
            }

            Domain.Entities.ApplicationSetting oApplicationSetting
                = db.ApplicationSettings.OrderByDescending(current => current.RegisterDate).FirstOrDefault();

            LibraryManagmentConnectSDK.LibraryEditReply oRegistreationReply =
                libraryManagerConnection.LibraryEdit(new LibraryManagmentConnectSDK.LibraryEditRequest {
                AuthCode = Utility.GetApplicationSetting().LibraryAuthCode,
                Name     = txtName.Text,
            });
            if (oRegistreationReply == null || oRegistreationReply.IsSuccessfull == false)
            {
                DialogResult result = MessageBox.Show("Couldnt Edit This Library From The Library Managment System, Try Agin! \n With Error : "
                                                      + oRegistreationReply == null ? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oRegistreationReply.ErrorType).ToString(), "Edit From Server Error!", MessageBoxButtons.OK);
                return;
            }

            oApplicationSetting.LibraryName = txtName.Text;

            db.Entry(oApplicationSetting).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            db.SaveChanges();
            Utility.UpdateApplicationSetting();

            this.Close();
        }
        public static Domain.Entities.ApplicationSetting UpdateApplicationSetting()
        {
            _applicationSetting = null;

            if (_applicationSetting == null)
            {
                lock (new object())
                {
                    if (_applicationSetting == null)
                    {
                        lock (new object())
                        {
                            using (var databaseContext = new Persistence.DatabaseContext())
                            {
                                _applicationSetting = databaseContext.ApplicationSettings.OrderByDescending(current => current.RegisterDate).FirstOrDefault();
                                if (_applicationSetting == null)
                                {
                                    _applicationSetting = new Domain.Entities.ApplicationSetting()
                                    {
                                        LibraryName          = "FirstStepLibrary",
                                        LibraryRegisterdInWS = false,
                                        LibraryAuthCode      = string.Empty,
                                    };

                                    databaseContext.ApplicationSettings.Add(_applicationSetting);
                                    databaseContext.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            return(_applicationSetting);
        }
Esempio n. 3
0
        private void Loading_Load(object sender, EventArgs e)
        {
            DatabaseContext databaseContextCreated = new DatabaseContext();

            try
            {
                bool _databaseInitilized = databaseContextCreated.Database.EnsureCreated();
                if (_databaseInitilized && (!databaseContextCreated.Users.Any() || !databaseContextCreated.ApplicationSettings.Any()))
                {
                    ApplicationSetting oApplicationSetting = new ApplicationSetting()
                    {
                        LibraryName          = "Library Number " + Utility.RandomDoubleCodeGeneratorByDigitsCount(2).ToString(),
                        LibraryRegisterdInWS = false,
                        LibraryAuthCode      = string.Empty,
                    };

                    User oUser = new User()
                    {
                        FirstName          = "Zahra",
                        LastName           = "Nabizzz",
                        Username           = oApplicationSetting.LibraryName.Trim().Replace("  ", " ").Replace(" ", "").ToLower(),
                        PhoneNumber        = "09372302283",
                        PhoneNumberIsoCode = "IR",
                        EmailAddress       = "*****@*****.**",
                        Role     = Domain.Enums.Role.Administrator,
                        Password = Hashing.GetSha256("123456789"),
                        IsPhoneNumberVerified  = true,
                        IsEmailAddressVerified = true,
                    };
                    databaseContextCreated.Users.Add(oUser);
                    databaseContextCreated.SaveChanges();

                    databaseContextCreated.ApplicationSettings.Add(oApplicationSetting);
                    databaseContextCreated.SaveChanges();
                }
                _databaseCreatedSuccessfuly = true;
            }
            catch (Exception ex)
            {
                DialogResult result = MessageBox.Show("DatabaseCreationProcess Error With Detail : \n \n" + ex.Message, "Database Error!", MessageBoxButtons.OK);
                if (result == DialogResult.OK)
                {
                    MessageBoxOkClick();
                }
            }

            LibraryManagmentConnectSDK.ConnectionSetting.SetupConnectionChannel("localhost:5000");
            if (!LibraryManagmentConnectSDK.ConnectionSetting.ConnectedToServer)
            {
                MessageBox.Show("Couldnt Connect To The Library Managment System !", "Connection Error!", MessageBoxButtons.OK);
            }
            if (!Utility.GetApplicationSetting().LibraryRegisterdInWS)
            {
                if (!LibraryManagmentConnectSDK.ConnectionSetting.ConnectedToServer)
                {
                    DialogResult result = MessageBox.Show("Couldnt Connect To The Library Managment System \n For The First Time You Should Connect To WS Server \n Try Agin!", "Connection Error!", MessageBoxButtons.OK);
                    if (result == DialogResult.OK)
                    {
                        MessageBoxOkClick();
                    }
                }
                else
                {
                    LibraryManagmentConnectSDK.ServerConnection     oServerConnection   = new LibraryManagmentConnectSDK.ServerConnection();
                    LibraryManagmentConnectSDK.LibraryRegisterReply oRegistreationReply =
                        oServerConnection.LibraryRegister(new LibraryManagmentConnectSDK.LibraryRegisterRequest {
                        Name = Utility.GetApplicationSetting().LibraryName
                    });
                    if (oRegistreationReply == null || oRegistreationReply.IsSuccessfull == false)
                    {
                        DialogResult result = MessageBox.Show("Couldnt Register This Library To The Library Managment System \n For The First Time You Should Register To WS Server \n Try Agin! \n With Error : "
                                                              + ((oRegistreationReply == null)? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oRegistreationReply?.ErrorType).ToString()), "Registration Error!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            MessageBoxOkClick();
                            return;
                        }
                    }

                    Domain.Entities.User oAdminUser =
                        databaseContextCreated.Users.Where(current => current.Username == Utility.GetApplicationSetting().LibraryName.Trim().Replace("  ", " ").Replace(" ", "").ToLower())
                        .FirstOrDefault();
                    if (oAdminUser == null)
                    {
                        DialogResult result = MessageBox.Show("Couldnt Connect To The Library Managment System \n For The First Time You Should Connect To WS Server \n Try Agin!", "Connection Error!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            MessageBoxOkClick();
                            return;
                        }
                    }

                    oServerConnection.SetLibrary(oRegistreationReply.AuthCode);

                    LibraryManagmentConnectSDK.AddUserRequest oAddUserRequest =
                        new LibraryManagmentConnectSDK.AddUserRequest
                    {
                        FirstName          = oAdminUser.FirstName,
                        LastName           = oAdminUser.LastName,
                        Username           = oAdminUser.Username,
                        EmailAddress       = oAdminUser.EmailAddress,
                        PhoneNumber        = oAdminUser.PhoneNumber,
                        Password           = oAdminUser.Password,
                        PhoneNumberIsoCode = oAdminUser.PhoneNumberIsoCode,
                        Role = (int)oAdminUser.Role,
                    };

                    LibraryManagmentConnectSDK.AddUserReply oAddUserReply =
                        oServerConnection.AddUser(oAddUserRequest);
                    if (oAddUserReply == null || oAddUserReply.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 : "
                                                              + oAddUserReply == null ? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oAddUserReply.ErrorType).ToString(), "Registration Error!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            MessageBoxOkClick();
                            return;
                        }
                    }

                    oAdminUser.ConnectedToWS = true;
                    oAdminUser.WSAuthCode    = oAddUserReply.UserAuthCode;

                    Domain.Entities.ApplicationSetting oApplicationSetting
                        = databaseContextCreated.ApplicationSettings.OrderByDescending(current => current.RegisterDate).FirstOrDefault();
                    oApplicationSetting.LibraryRegisterdInWS = true;
                    oApplicationSetting.LibraryAuthCode      = oRegistreationReply.AuthCode;

                    databaseContextCreated.Entry(oAdminUser).State          = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    databaseContextCreated.Entry(oApplicationSetting).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    databaseContextCreated.SaveChanges();
                    Utility.UpdateApplicationSetting();
                }
            }

            MainTimer          = new Timer();
            MainTimer.Interval = 2000;
            MainTimer.Enabled  = true;
            MainTimer.Tick    += new EventHandler(t_Tick);
        }