Petapoco: Insert with conditions (time, in this case)
Anyone who knows how to add a post in a database table with petapoco conditions?
where db = ApplicationContext.DatabaseContext.Database;
db.Insert (model);
works fine, but now i want to prevent inserting if the registration has been done within 8 hours before, so how can this be done?
db.Insert(model.StartDate.AddHours(-8)); dosn't give me an error message in Visual Studio when I build the solution, but the method, of course, doesn't work.
I have to read the data from the database to check if the minimum 8-hour criteria are present before I add the new record?
I have tried something similar with a SQL lookup from the repository before inserting. I suppose there is a similar LINQ / Lambda method too. I'm almost in goal, but still a little left regarding the actual addate/getdate format in the SQL table.
Sorry, I left too little information. This comes from my model:
public int nodeId { get; set; }
public int? SelectedHikingDestination { get; set; }
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd MM yyyy}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
A little bit of trouble with time in the date format, since I not have use it until now, so I've now focused on the user being able to post only one item per day, and this was done in the controller:
if (HikingDestinations.GetHikingDestinationStartDate(model.nodeId, model.SelectedHikingDestination.GetValueOrDefault()) == null)
{
var db = ApplicationContext.DatabaseContext.Database;
db.Insert(model);
} else {
TempData["createNotSuccess"] = "Hiking destination is already registered. Same hiking destinations can be registered only with 1 day intervals.";
return RedirectToCurrentUmbracoPage();
}
and this is from my repository:
public static HikingDestinationViewModel GetHikingDestinationStartDate(int NodeId, int SelectedHikingDestination)
{
UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
List<HikingDestinationViewModel> Records = db.Fetch<HikingDestinationViewModel>
("SELECT Id FROM HikingDestinations WHERE nodeId = @0 " +
"AND SelectedHikingDestination = @1 " +
"AND StartDate > DATEADD(day,-1,GETDATE())", NodeId, SelectedHikingDestination);
if (Records.Count > 0)
return Records[0];
else
return null;
}
This works now. The question was whether this could have been done directly with LINQ / Lambda in an operation, if that possibility exists. If not, I'll just let it be as it is now since it works
It would have been possible. But this is not too bad.
You are using Parameters for SQL. It's quick. The issue with Linq is that you might create slow code if you don't do it right. This is plain old, good stuff. Keep it.
If you like, you could have improve de code by
using a stored procedure (if you like SP's)
putting things in a transaction to be sure the user is not posting on two requests together (would be possible with your current code)
Thank you. Then you've answered my question, and I just let it be like it is now. :-)
My demo is with SLQ CE, so it's limited I think, while the customer has SQL, so maybe I get back to my old knowledge of transaction (a little less experience with stored procedures).
You type: putting things in a transaction to be sure the user is not posting on two requests together (would be possible with your current code).
This is important. How can the user do this? I've just tested a little, but the code now looks like preventing double entry so far ...?
Petapoco: Insert with conditions (time, in this case)
Anyone who knows how to add a post in a database table with petapoco conditions?
works fine, but now i want to prevent inserting if the registration has been done within 8 hours before, so how can this be done?
db.Insert(model.StartDate.AddHours(-8));
dosn't give me an error message in Visual Studio when I build the solution, but the method, of course, doesn't work.Anyone who knows how this can be done?
Hi Tom,
Do you mean something like this?
Kind regards
Damiaan
I have to read the data from the database to check if the minimum 8-hour criteria are present before I add the new record?
I have tried something similar with a SQL lookup from the repository before inserting. I suppose there is a similar LINQ / Lambda method too. I'm almost in goal, but still a little left regarding the actual addate/getdate format in the SQL table.
I'll continue tomorrow, giving feedback then.
I don't understand exactly what you want to do.
Where does the date on the "model" object comes from?
Sorry, I left too little information. This comes from my model:
A little bit of trouble with time in the date format, since I not have use it until now, so I've now focused on the user being able to post only one item per day, and this was done in the controller:
and this is from my repository:
This works now. The question was whether this could have been done directly with LINQ / Lambda in an operation, if that possibility exists. If not, I'll just let it be as it is now since it works
Right. Now I can follow. :-)
It would have been possible. But this is not too bad.
You are using Parameters for SQL. It's quick. The issue with Linq is that you might create slow code if you don't do it right. This is plain old, good stuff. Keep it.
If you like, you could have improve de code by
Thank you. Then you've answered my question, and I just let it be like it is now. :-)
My demo is with SLQ CE, so it's limited I think, while the customer has SQL, so maybe I get back to my old knowledge of transaction (a little less experience with stored procedures).
You type: putting things in a transaction to be sure the user is not posting on two requests together (would be possible with your current code).
This is important. How can the user do this? I've just tested a little, but the code now looks like preventing double entry so far ...?
is working on a reply...