Esempio n. 1
0
        /// <summary>
        /// Inserts a new <see cref="WatchedApplication"/> object in the database.
        /// </summary>
        /// <param name="application">The new <see cref="WatchedApplication"/> to insert.</param>
        /// <returns>If the parameter was null, returns null. If the insertion operation was successful, returns the newly-inserted <see cref="WatchedApplication"/> object.</returns>
        /// <exception cref="NotSupportedException">If the <see cref="WatchedApplication"/>'s Id is greater than 0 (i.e., if it's already been persisted, throws a <see cref="NotSupportedException"/>.</exception>
        public WatchedApplication InsertWatchedApplication(WatchedApplication application)
        {
            if (application == null)
            {
                return(null);
            }

            if (application.Id > 0)
            {
                throw new NotSupportedException("Attempting to insert duplicate item in the database");
            }

            var sql = @"
                INSERT INTO WatchedApplications(Name, IsWatchEnabled)
                VALUES (?, ?)
";

            using (var connection = GetConnection())
            {
                connection.Execute(sql, new { application.Name, application.IsWatchEnabled });
            }

            application = GetWatchedApplicationByName(application.Name);

            return(application);
        }
Esempio n. 2
0
        /// <summary>
        /// Updates an existing <see cref="WatchedApplication"/> object in the database.
        /// </summary>
        /// <param name="application">The application object to update.</param>
        /// <returns>If the parameter is null, returns null. Otherwise, returns the updated entity.</returns>
        /// <exception cref="NotSupportedException">The <see cref="WatchedApplication"/> has not yet been persisted.</exception>
        public WatchedApplication UpdateApplication(WatchedApplication application)
        {
            if (application == null)
            {
                return(null);
            }

            if (application.Id == 0)
            {
                throw new NotSupportedException("Attempting to update an entity that hasn't been persisted.");
            }

            var sql = @"
                UPDATE WatchedApplications
                SET Name = ?,
                    IsWatchEnabled = ?
                WHERE Id = ?
";

            using (var connection = GetConnection())
            {
                connection.Execute(sql, new { application.Name, application.IsWatchEnabled, application.Id });
            }

            return(application);
        }