Esempio n. 1
0
        public static ActionStatus AddArticle(Article articleDto)
        {
            // Create the Database object, using the default database service. The
            // default database service is determined through configuration.
            SqlDatabase db = new SqlDatabase(Config.ConnString);

            ActionStatus status = new ActionStatus();

            Int32 articleId;

            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();

                DbTransaction txn = null;

                try
                {
                    //Begin the transaction
                    txn = connection.BeginTransaction();

                    ArticleInsertHelper artHlpr = new ArticleInsertHelper();

                    artHlpr.InitCommand(db, articleDto);

                    articleId = artHlpr.Execute(db, txn);

                    foreach (ArticleDocument doc in articleDto.Documents)
                    {
                        //Load the data in a stream
                        doc.LoadData();

                        ArticleDocInsertHelper artDocHlpr = new ArticleDocInsertHelper();

                        artDocHlpr.InitCommand(db, articleId, doc);

                        artDocHlpr.Execute(db, txn);
                    }

                    status.IsSuccessful = true;

                    // Commit the transaction.
                    txn.Commit();
                }
                catch (SqlException sqlEx)
                {
                    // Roll back the transaction.
                    txn.Rollback();

                    Console.WriteLine(sqlEx.ToString());

                    throw new DataException("An exception occured adding an article into the database.", sqlEx);
                }
                catch (IOException ioEx)
                {
                    // Roll back the transaction.
                    txn.Rollback();

                    Console.WriteLine(ioEx.ToString());

                    throw new DataException("An exception occured trying to read document data.", ioEx);
                }
                finally
                {
                    connection.Close();
                }
            }

            return(status);
        }
Esempio n. 2
0
        public static ActionStatus UpdateArticle(Article artDto)
        {
            Database db = DatabaseFactory.CreateDatabase();

            ActionStatus status = new ActionStatus();

            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();

                DbTransaction txn = null;

                try
                {
                    //Begin the transaction
                    txn = connection.BeginTransaction();

                    ArticleUpdateHelper artHlpr = new ArticleUpdateHelper();

                    artHlpr.InitCommand(db, artDto);

                    artHlpr.Execute(db, txn);

                    foreach (ArticleDocument doc in artDto.Documents)
                    {
                        if (doc.IsNew)
                        {
                            //Load the data in a stream
                            doc.LoadData();

                            ArticleDocInsertHelper artDocHlpr = new ArticleDocInsertHelper();

                            artDocHlpr.InitCommand(db, artDto.ArticleId, doc);

                            artDocHlpr.Execute(db, txn);
                        }

                        if (doc.IsDeleted)
                        {
                            ArticleDocDeleteHelper artDocDelHlpr = new ArticleDocDeleteHelper();

                            artDocDelHlpr.InitCommand(db, artDto.ArticleId, doc);

                            artDocDelHlpr.Execute(db, txn);
                        }
                    }

                    status.IsSuccessful = true;

                    // Commit the transaction.
                    txn.Commit();
                }
                catch (SqlException sqlEx)
                {
                    // Roll back the transaction.
                    txn.Rollback();

                    Console.WriteLine(sqlEx.ToString());

                    throw new DataException("An exception occured updating an article in the database.", sqlEx);
                }
                catch (IOException ioEx)
                {
                    // Roll back the transaction.
                    txn.Rollback();

                    Console.WriteLine(ioEx.ToString());

                    throw new DataException("An exception occured trying to read document data.", ioEx);
                }
                finally
                {
                    connection.Close();
                }
            }

            return(status);
        }