Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Engan 430 posts 1173 karma points
    Jan 15, 2018 @ 14:40
    Tom Engan
    0

    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.

    Anyone who knows how this can be done?

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Jan 15, 2018 @ 21:17
    Damiaan
    1

    Hi Tom,

    Do you mean something like this?

    var db = ApplicationContext.DatabaseContext.Database;
    if(model.StartDate > DateTime.Now.AddHours(-8)) {
        db.Insert (model);
    }
    

    Kind regards
    Damiaan

  • Tom Engan 430 posts 1173 karma points
    Jan 15, 2018 @ 22:25
    Tom Engan
    0

    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.

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Jan 16, 2018 @ 00:44
    Damiaan
    0

    I don't understand exactly what you want to do.
    Where does the date on the "model" object comes from?

  • Tom Engan 430 posts 1173 karma points
    Jan 16, 2018 @ 13:32
    Tom Engan
    0

    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

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Jan 16, 2018 @ 13:39
    Damiaan
    100

    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

    • 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)
  • Tom Engan 430 posts 1173 karma points
    Jan 16, 2018 @ 13:49
    Tom Engan
    0

    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 ...?

Please Sign in or register to post replies

Write your reply to:

Draft