0

I have a controller to get a count of items that are greater then a certain date. The repository appears as:

    public Dictionary<int, int> GetAllComplaintsCount(DateTime start)
    {
        try
        {
            return _context.Checklists
                .Where(a => a.COMPLAINT.Received_DT > start)
                .GroupBy(a => a.MonitorEnteredEmpID)
                .ToDictionary(g => g.Key, g => g.Count());
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get am with checklist", ex);
            return null;
        }
    }

Edit I've included my routers to see if it is correct:

         app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "crams/{controller=Home}/{action=Index}/{id?}");
            routes.MapRoute(
                name: "route",
                template: "crams/{controller}/{action}/{start?}");
        });

Question Without the start parameter, I am able to get http://localhost:8000/crams/api/counts through postman. I am unsure though, how to incorporate the date through postman so it can only pull dates that are greater than start.

I've tried

http://localhost:8000/crams/api/counts/2016-1-1 but it comes back null.

enavuio
  • 1,428
  • 2
  • 19
  • 31

2 Answers2

1

/api/counts/2016-1-1 comes back as null

Your API is:

... GetAllComplaintsCount(DateTime start)

so your url should be:

/api/counts?start=2016-1-1

This would be with the default routing. As you've not included routing in your question, I assume (given the symptoms) that it's the default:

/api/{controller}/{id}

ie using /api/counts/2016-1-1 specifies "2016-1-1" as the id parameter, but you don't have an id parameter and you've not specified a value for start, so you get null.

You can add a route as

/api/{controller}/{start}

I've just created an api as:

    [HttpGet]
    public string Test(DateTime d)
    {
        return d.ToString();
    }

and called, via postman (though any client would give the same result)

http://localhost/api/../Test?d=2016-1-1

and it returned the expected "01/01/2016 00:00:00"

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • I appreciate your thoughtful answer! I have attempted to add a route under my default route and named it route, and now my routes look like this: app.UseMvc(routes => { routes.MapRoute( name: "default", template: "crams/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "route", template: "crams/{controller}/{action}/{start?}"); }); – enavuio Jun 29 '16 at 14:40
  • Then I used your test to see if it would work: so I added the test httpget, but when I run "http://localhost:8000/crams/api/counts/Test?d=2016-1-1" I get 404 not found! Can you tell me if I am making a mistake anywhere? – enavuio Jun 29 '16 at 14:41
  • First - did your original work with `../counts?start=2016-1-1` ? – freedomn-m Jun 29 '16 at 14:55
  • No, nothing has worked with adding a parameter. I am getting a null still. – enavuio Jun 29 '16 at 14:56
  • Next, it looks like you've changed the MVC routes, not the API ones and the 'default' one should always be last as it will hit them in order. – freedomn-m Jun 29 '16 at 14:57
  • Try simplifying the existing method to just return `start.ToString()` before adding new ones / routes. Get the parameter working with `?start=...` before making it "nicer". Put a breakpoint inside the method and see what the value is. Could be that you don't have any records - so eliminate the DB part and stick with simple .ToString – freedomn-m Jun 29 '16 at 14:59
  • I got it :) You rock. – enavuio Jun 29 '16 at 15:05
  • One more questions, if I wanted an end date would I just add another ? as http://localhost:8000/crams/api/counts?startdate=2014-1-1?enddate=2016-1-1 – enavuio Jun 29 '16 at 15:07
  • Well, you'd use `&` for additional query string parameters – freedomn-m Jun 29 '16 at 15:34
0

You can try to pass ticks to your API

http://localhost:8000/crams/api/counts/636027305821590000

And then

public Dictionary<int, int> GetAllComplaintsCount(Int64 dateTicks)
    {
        try
        {
            var startDate = new DateTime(dateTicks);
            return _context.Checklists
                .Where(a => a.COMPLAINT.Received_DT > startDate)
                .GroupBy(a => a.MonitorEnteredEmpID)
                .ToDictionary(g => g.Key, g => g.Count());
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get am with checklist", ex);
            return null;
        }
    }

Here is an info on how to get Ticks from a Date (using JS)

How to convert JavaScript date object to ticks

Hope this helps:

Community
  • 1
  • 1
David Espino
  • 2,177
  • 14
  • 21
  • Sorry, I don't have a .net env to test with for now... do you get errors? I've send DateTime params before to applications using either ticks or a specific format string and used .ParseExact... If there is an error on my code example I can update it – David Espino Jun 29 '16 at 15:44
  • The issue was that the routing didn't match the parameter name - which would also be the case here. – freedomn-m Jun 29 '16 at 18:55