Esempio n. 1
0
        private string AddOrUpdateBookDetails(BookDetails bookDetails)
        {
            string res;
            Table <DAL.BookDetail> bookDetailsTable = db.GetTable <DAL.BookDetail>();

            var matchedBook = (from bkDetails in bookDetailsTable
                               where bkDetails.name == bookDetails.Name
                               select bkDetails).SingleOrDefault();

            res = authorManager.AddOrUpdateAuthor(bookDetails.GetAuthor());
            if (res != "")
            {
                return(res);
            }
            res = publisherManager.AddOrUpdatePublisher(bookDetails.GetPublisher());
            if (res != "")
            {
                return(res);
            }

            if (matchedBook == null)    // Add
            {
                try
                {
                    Random         data    = new Random();
                    DAL.BookDetail newData = new DAL.BookDetail();
                    newData.bookDetailsID = data.Next();    // Dummy init
                    newData.name          = bookDetails.Name;
                    newData.price         = bookDetails.Price;
                    newData.publishDate   = bookDetails.PublishDate;

                    newData.authorID    = authorManager.GetAuthorIDFromAuthorName(bookDetails.GetAuthor().Name);
                    newData.publisherID = publisherManager.GetPublisherIDFromPublisherName(bookDetails.GetPublisher().Name);

                    bookDetailsTable.InsertOnSubmit(newData);
                    bookDetailsTable.Context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }
            else if (matchedBook != null)   // Update
            {
                try
                {
                    matchedBook.name        = bookDetails.Name;
                    matchedBook.price       = bookDetails.Price;
                    matchedBook.publishDate = bookDetails.PublishDate;
                    matchedBook.authorID    = authorManager.GetAuthorIDFromAuthorName(bookDetails.GetAuthor().Name);
                    matchedBook.publisherID = publisherManager.GetPublisherIDFromPublisherName(bookDetails.GetPublisher().Name);

                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }
            return("");
        }