You don't necessarily need to have a column in your database table for status, you could use an aggregated column when you do queries from the table.
In this way there is no need update the table.
advert_id | name | date_created | description | duration | user_id | cat_id |
---|
1 | Whatever | 2014-10-15 | The status will be inactive | P7D | 5 | 8 |
1 | Whatever | 2014-10-25 | The status will be active | P7D | 7 | 9 |
Note For duration I am using
xsd:duration[
^] format and the column type should be VARCHAR(25).
In your query you can aggregate the status using a calculation.
As I mostly work in MySQL I use this syntax, but it should be possible to translate the code to other SQL flavors.
SELECT `name`, IF(DATE_SUB(CURDATE(), INTERVAL TRIM(TRAILING 'D' FROM SUBSTRING(duration, 2)) DAY) > date_created, 'Inactive', 'Active') AS `status` FROM adverts;
(The way to get the number of days out of duration can be improved.)
You can also do this in c#.
DataTable dt = new DataTable();
dt.RowChanged += dt_RowChanged;
dt.Columns.Add("advert_id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("date_created", typeof(DateTime));
dt.Columns.Add("duration", typeof(TimeSpan));
dt.Columns.Add("status", typeof(string));
dt.Rows.Add(1, "Row1", DateTime.Now.Subtract(new TimeSpan(10, 0, 0, 0, 0)), new TimeSpan(7, 0, 0, 0, 0));
dt.Rows.Add(2, "Row2", DateTime.Now, new TimeSpan(7, 0, 0, 0, 0));
The event handler
static void dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
if (e.Action == DataRowAction.Add)
{
DateTime created = (DateTime)e.Row["date_created"];
TimeSpan duration = (TimeSpan)e.Row["duration"];
e.Row["status"] = (created > DateTime.Now.Subtract(duration)) ? "Active" : "Inactive";
}
}