private void BtnRecover_Click(object sender, EventArgs e)
        {
            if (authenicatedUser.Role < Domain.Enums.Role.Administrator)
            {
                this.Close();
            }

            var result = MessageBox.Show("Are Sure You Want to Recover This Book !?", "Recovering Book", buttons: MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                Domain.Entities.Book oBookInNewContext =
                    db.Books.Find(Book.Id);
                if (oBookInNewContext != null)
                {
                    oBookInNewContext.IsDeleted       = false;
                    oBookInNewContext.DeleteDate      = null;
                    oBookInNewContext.DeleterUserId   = null;
                    db.Entry(oBookInNewContext).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    db.SaveChanges();

                    this.BookDetail_Load(sender, e);
                }
            }
        }
Exemplo n.º 2
0
 private void BtnRead_Click(object sender, EventArgs e)
 {
     if (BookDetailReply.HostLibrary)
     {
         Domain.Entities.Book oBook =
             db.Books.Where(current => current.Name == BookDetailReply.Name)
             .FirstOrDefault();
         if (oBook != null)
         {
             ReadBook ReadBookForm = new ReadBook();
             ReadBookForm.setBook(oBook);
             ReadBookForm.SetAuthenicatedUser(authenicatedUser);
             ReadBookForm.Show();
         }
     }
     else
     {
         if (BookDetailReply.RentedThisBook)
         {
             WSReadBook ReadBookForm = new WSReadBook();
             ReadBookForm.setBook(BookDetailReply.Text);
             ReadBookForm.SetAuthenicatedUser(authenicatedUser);
             ReadBookForm.Show();
         }
     }
 }
        private void BtnEdit_Click(object sender, EventArgs e)
        {
            if (authenicatedUser.Role > Domain.Enums.Role.User || Book.UploaderUserId == authenicatedUser.Id)
            {
                if (Book.IsDeleted)
                {
                    if (authenicatedUser.Role < Domain.Enums.Role.Administrator)
                    {
                        this.Close();
                    }
                }

                Domain.Entities.Book oBookInNewContext =
                    db.Books.Find(Book.Id);
                if (oBookInNewContext != null)
                {
                    db.Entry(oBookInNewContext).Reload();

                    EditBook EditBookForm = new EditBook();
                    EditBookForm.setBook(oBookInNewContext);
                    EditBookForm.SetAuthenicatedUser(authenicatedUser);
                    EditBookForm.Show();
                }
            }
            else
            {
                btnEdit.Enabled = false;
            }
        }
        private void BtnRead_Click(object sender, EventArgs e)
        {
            Domain.Entities.Book oBookInNewContext =
                db.Books.Find(Book.Id);
            if (oBookInNewContext != null)
            {
                db.Entry(oBookInNewContext).Reload();

                ReadBook ReadBookForm = new ReadBook();
                ReadBookForm.setBook(oBookInNewContext);
                ReadBookForm.SetAuthenicatedUser(authenicatedUser);
                ReadBookForm.Show();
            }
        }
Exemplo n.º 5
0
 private void BtnInLibrary_Click(object sender, EventArgs e)
 {
     if (BookDetailReply.HostLibrary)
     {
         Domain.Entities.Book oBook =
             db.Books.Where(current => current.Name == BookDetailReply.Name)
             .FirstOrDefault();
         if (oBook != null)
         {
             BookDetail BookDetailForm = new BookDetail();
             BookDetailForm.setBook(oBook);
             BookDetailForm.SetAuthenicatedUser(authenicatedUser);
             BookDetailForm.Show();
             this.Close();
         }
     }
 }
Exemplo n.º 6
0
        private void BookGridView_SelectionChanged(object sender, EventArgs e)
        {
            string strSelectedName = BooksGridView.Rows[BooksGridView.CurrentRow.Index].Cells[1].Value.ToString();

            if (!string.IsNullOrEmpty(strSelectedName))
            {
                Domain.Entities.Book oBook =
                    db.Books.Where(current => current.Name == strSelectedName)
                    .FirstOrDefault();
                if (oBook != null)
                {
                    db.Entry(oBook).Reload();

                    BookDetail BookDetailForm = new BookDetail();
                    BookDetailForm.setBook(oBook);
                    BookDetailForm.SetAuthenicatedUser(authenicatedUser);
                    BookDetailForm.Show();
                }
            }
        }
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Are Sure You Want to Delete This Book !?", "Deleting Book", buttons: MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                Domain.Entities.Book oBookInNewContext =
                    db.Books.Find(Book.Id);
                if (oBookInNewContext != null)
                {
                    if (authenicatedUser.Role < Domain.Enums.Role.Administrator)
                    {
                        oBookInNewContext.IsDeleted       = true;
                        oBookInNewContext.DeleteDate      = System.DateTime.Now;
                        oBookInNewContext.DeleterUserId   = authenicatedUser.Id;
                        db.Entry(oBookInNewContext).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        db.Books.Remove(oBookInNewContext);
                        db.SaveChanges();

                        string TextPath = "c:\\BookLibraryData\\Book-" + Book.Id.ToString();
                        try
                        {
                            bool exists = Directory.Exists(TextPath);
                            if (exists)
                            {
                                Directory.Delete(TextPath, true);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }

                    this.Close();
                }
            }
        }
 public void setBook(Domain.Entities.Book book)
 {
     Book = book;
 }
        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();
            }
        }
        private void BtnUplaodWS_Click(object sender, EventArgs e)
        {
            if (authenicatedUser.Role >= Domain.Enums.Role.Administrator || Book.UploaderUserId == authenicatedUser.Id)
            {
                Domain.Entities.Book oBookInNewContext =
                    db.Books.Find(Book.Id);
                if (oBookInNewContext != null)
                {
                    if (!oBookInNewContext.UploadedToWS)
                    {
                        LibraryManagmentConnectSDK.AddBookRequest oRequest =
                            new LibraryManagmentConnectSDK.AddBookRequest()
                        {
                            Name          = oBookInNewContext.Name,
                            Author        = oBookInNewContext.Author,
                            Translator    = oBookInNewContext.Translator,
                            Publisher     = oBookInNewContext.Publisher,
                            PublishedDate = oBookInNewContext.PublishedDate,
                            Circulation   = oBookInNewContext.Circulation,
                            HasImage      = oBookInNewContext.HasImage,
                            ISBN          = oBookInNewContext.ISBN,
                            Price         = (long)oBookInNewContext.Price,
                            ImageData     = Google.Protobuf.ByteString.Empty,
                            Text          = string.Empty,
                        };

                        if (oRequest.HasImage)
                        {
                            var imageStream = Infrastructure.ImageUtils.GetPhoto("c:\\BookLibraryData\\Book-" + oBookInNewContext.Id.ToString() + "\\" + "Image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            imageStream.Seek(0, SeekOrigin.Begin);
                            oRequest.ImageData =
                                Google.Protobuf.ByteString.FromStream(imageStream);
                        }

                        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);
                            }
                            else
                            {
                                StreamReader read = new StreamReader(TextFullPath);
                                oRequest.Text = read.ReadToEnd();
                                read.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("An Unknown error has occurred : \n \n" + ex.Message, "File Error!", MessageBoxButtons.OK);
                            this.Close();
                        }

                        LibraryManagmentConnectSDK.AddBookReply oReply =
                            libraryManagerConnection.AddBook(oRequest);
                        if (oReply == null || !oReply.IsSuccessfull)
                        {
                            MessageBox.Show("An Error Occured in Uploading Book To The Server, Try Agin! \n With Error Message : "
                                            + (oReply == null ? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oReply.ErrorType).ToString()), "Upload Book Error!", MessageBoxButtons.OK);
                            return;
                        }

                        oBookInNewContext.UploadedToWS    = true;
                        oBookInNewContext.BookIdInWS      = oReply.BookId;
                        db.Entry(oBookInNewContext).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        db.SaveChanges();

                        btnUplaodWS.Text    = "Remove Online";
                        checkOnline.Checked = true;

                        Book = oBookInNewContext;
                    }
                    else
                    {
                        LibraryManagmentConnectSDK.RemoveBookReply oReply =
                            libraryManagerConnection.AddBook(new LibraryManagmentConnectSDK.RemoveBookRequest {
                            BookId = oBookInNewContext.BookIdInWS
                        });
                        if (oReply == null || !oReply.IsSuccessfull)
                        {
                            MessageBox.Show("An Error Occured in Removing Online Book From The Server, Try Agin! \n With Error Message : "
                                            + (oReply == null ? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oReply.ErrorType).ToString()), "Remove Online Book Error!", MessageBoxButtons.OK);
                            return;
                        }

                        oBookInNewContext.UploadedToWS    = false;
                        oBookInNewContext.BookIdInWS      = string.Empty;
                        db.Entry(oBookInNewContext).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        db.SaveChanges();

                        btnUplaodWS.Text    = "Upload";
                        checkOnline.Checked = false;

                        Book = oBookInNewContext;
                    }
                }
            }
        }