/// <summary> /// Creates new event record in database. /// </summary> /// <param name="details">Event details.</param> /// <returns>ID of new record.</returns> public override int CreateEvent(Event details) { int id = -1; using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = transaction; command.CommandText = string.Format( @"INSERT INTO {0} (Title, Date, Description, DateFormat, OwnerID, IsPublic) VALUES (@Title, @Date, @Description, @DateFormat, @OwnerID, @IsPublic) SELECT @@IDENTITY", DBEventsTableName); command.Parameters.Add("@Date", SqlDbType.DateTime).Value = details.DateTime; command.Parameters.Add("@Description", SqlDbType.NVarChar).Value = details.Description; command.Parameters.Add("@Title", SqlDbType.NVarChar).Value = details.Title; command.Parameters.Add("@DateFormat", SqlDbType.NVarChar).Value = details.DateFormat; command.Parameters.Add("@OwnerID", SqlDbType.Int).Value = details.OwnerID; command.Parameters.Add("@IsPublic", SqlDbType.Bit).Value = details.IsPublic; try { id = Convert.ToInt32(ExecuteScalar(command)); transaction.Commit(); details.ID = id; m_Cache[id] = details; } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } return id; }
/// <summary> /// Creates new event in database. /// </summary> /// <param name="details">Event.</param> /// <returns>ID of new database record.</returns> public abstract int CreateEvent(Event details);
/// <summary> /// Add personal user event. /// </summary> /// <param name="userID">User id.</param> /// <param name="details">Event details.</param> /// <returns>ID of added event.</returns> public abstract int AddPersonalUserEvent(int userID, Event details);
/// <summary> /// Add group event. /// </summary> /// <param name="groupID">Group id.</param> /// <param name="details">Event details.</param> /// <returns>ID of added event.</returns> public abstract int AddGroupEvent(int groupID, Event details);
/// <summary> /// Updates event information in database. /// </summary> /// <param name="details">Event details.</param> /// <returns>True if information was updated; false, otherwise.</returns> public abstract bool UpdateEvent(Event details);
/// <summary> /// Add event to object (user or group). /// </summary> /// <param name="objectID">ID of object.</param> /// <param name="details">Event details.</param> /// <param name="TableName">Name of the table in DB.</param> /// <param name="ColumnIDName">Name of column in DB.</param> /// <returns></returns> private int addEventToObject(int objectID, Event details, string TableName, string ColumnIDName) { if (details.IsSaved && isReferenceExist(TableName, ColumnIDName, objectID, details.ID.Value)) return 0; int eventID = -1; using (SqlConnection connection = new SqlConnection(ConnectionString)) { eventID = (details.IsSaved) ? details.ID.Value : CreateEvent(details); connection.Open(); SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = transaction; command.CommandText = string.Format( @"INSERT INTO {0} ({1}, EventID) VALUES (@objectID, @eventID) SELECT @@IDENTITY", TableName, ColumnIDName); command.Parameters.Add("@objectID", SqlDbType.Int).Value = objectID; command.Parameters.Add("@eventID", SqlDbType.Int).Value = eventID; try { Convert.ToInt32(ExecuteScalar(command)); transaction.Commit(); //m_Cache[id] = details; } catch { transaction.Rollback(); } } return eventID; }
/// <summary> /// Add event to group. /// </summary> /// <param name="groupID">Group id.</param> /// <param name="details">Event details.</param> /// <returns>ID of event.</returns> public override int AddGroupEvent(int groupID, Event details) { return addEventToObject(groupID, details, DBGroupEventsTableName, "GroupID"); }
/// <summary> /// Add personal event to user. /// </summary> /// <param name="userID">User id.</param> /// <param name="details">Event details.</param> /// <returns>ID of event.</returns> public override int AddPersonalUserEvent(int userID, Event details) { return addEventToObject(userID, details, DBUserEventsTableName, "UserID"); }
/// <summary> /// Returns event from data reader. /// </summary> /// <param name="reader">Data reader.</param> /// <returns>Event from data reader.</returns> protected virtual Event GetEventDataFromReader(IDataReader reader) { Event details = new Event(); details.ID = (int)reader["ID"]; details.OwnerID = (int)reader["OwnerID"]; details.DateTime = (DateTime)reader["Date"]; details.Title = (string)reader["Title"]; details.IsPublic = (bool)reader["IsPublic"]; details.Description = reader["Description"] == DBNull.Value ? null : (string)reader["Description"]; details.DateFormat = reader["DateFormat"] == DBNull.Value ? null : (string)reader["DateFormat"]; return details; }
/// <summary> /// Updates event information in database. /// </summary> /// <param name="details">Event details.</param> /// <returns>True if record was successfully updated; false, otherwise.</returns> public override bool UpdateEvent(Event details) { using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); SqlTransaction transaction = connection.BeginTransaction(); SqlCommand command = connection.CreateCommand(); command.Transaction = transaction; command.CommandText = string.Format( "UPDATE {0} SET Date = @Date, Description = @Description, Title = @Title, DateFormat = @DateFormat, IsPublic = @IsPublic WHERE ID = @ID", DBEventsTableName); command.Parameters.Add("@ID", SqlDbType.Int).Value = details.ID; command.Parameters.Add("@Date", SqlDbType.DateTime).Value = details.DateTime; command.Parameters.Add("@Description", SqlDbType.NVarChar).Value = details.Description; command.Parameters.Add("@DateFormat", SqlDbType.NVarChar).Value = details.DateFormat; command.Parameters.Add("@Title", SqlDbType.NVarChar).Value = details.Title; command.Parameters.Add("@IsPublic", SqlDbType.Bit).Value = details.IsPublic; try { bool result = (ExecuteNonQuery(command) == 1); if (result) { transaction.Commit(); m_Cache[details.ID.Value] = details; } else transaction.Rollback(); return result; } catch { transaction.Rollback(); return false; } } }
/// <summary> /// Add individual event to person. /// </summary> /// <param name="userID">User ID.</param> /// <param name="eventData">event data.</param> public static void AddIndividualEvent(int userID, Event eventData) { SiteProvider.Events.AddPersonalUserEvent(userID, eventData); }