0

I am working on a web api and I need to be able to search a path with a DateTime and receive all books that contain that Date.

So for example /api/books/published/2012 returns all with 2012 in their date. As well as api/books/published/2012/8/15 returns all with 2012-08-15 in their's.

I have tried

 public ActionResult GetBooksSortedByDate()
    {
        if (!_bookList.Any())
            return NotFound();
        return Ok(_bookList);
    }
    [HttpGet("published/{published}")]
    public IActionResult GetBookByDate(DateTime published)
    {
        var book = FindBookByDate(published);
        if (book is null) return NotFound();
        return Ok(book);
    }
    //returnerar böcker innehållandes sökt datum
    private IEnumerable<Book> FindBookByDate(DateTime published)
    {
        return _bookList.Where(_bookList => _bookList.Publish_date.Equals(published));
    }

Using /api/books/published/2012-8-15 in Postman gets me the book created then.

meanwhile /api/books/published/2012 does not give me any objects. And I know this is because I have set _booklist.Publish_date.Equals(published)); .

So what do I need to replace .Equals with to get something along the lines of a .Contains(Value)

And regarding double

I want to be able to do the following.

/api/books/price/30.0&35.0 returns all with price between '30.0' och '35.0'

Same situation here, I get results on /api/books/price/33.0 but I do not know how to do a price between 30.0 and 35.0.

My code looks like this. I believe I need to send in 2 doubles here for it to check both of them?

  //Visar /price/value&value
    [HttpGet("price/{price}&{price}")]
    public IActionResult GetBooksByTwoPrices(double firstPrice, double secondPrice)
    {
        var book = FindBooksByTwoPrices(firstPrice,secondPrice);
        if (book is null) return NotFound();
        return Ok(book);
    }
    
    private IEnumerable<Book> FindBooksByTwoPrices(double firstPrice, double secondPrice)
    {
        var allBooksBetweenPrices = _bookList.Where(_bookList => _bookList.Price.Equals(firstPrice) && _bookList.Price.Equals(secondPrice);
        return allBooksBetweenPrices;
        //return _bookList.Where(_bookList => _bookList.Price.Equals(price));
    }

I know my var allBooksBetweenPrices = ... does not work as it can't be 2 value's at once but I guess this is somewhat the way to approach it?

Worth to note... My Book.cs only contains public double price { get; set; } Do I need another one to check for the other price?

4
  • Reason why 2012-8-15 works as a DateTime and 2012 not is because model binder tries to parse the route parameter string to DateTime and 2012 is not a valid date time string. You can see for yourself using DateTime.Parse() method. Also, are route-based parameters a requirement? If not, then I suggest accepting the parameters from query, that would make your life much easier, you could have multiple parameters for example. Read more about model binding in the docs. Commented Apr 23, 2021 at 17:20
  • @Prolog I've been trying to understand the route-based parameters but it seems that I get them wrong every time. Can you show how it's supposed to look like on my code for example? Might be easier to understand with a smaller example.. As for the DateTime... Where do I do DateTime.Parse()? Commented Apr 23, 2021 at 17:48
  • Are you using ASP.NET Core or ASP.NET? Please specify framework version you are using. If you don't know, see this question. Commented Apr 23, 2021 at 18:20
  • @Prolog I'm using ASP.NET 5. It was supposed to be ASP.NET 5.0 Core but I did not have that when creating a new project and I noticed it too late.. Commented Apr 23, 2021 at 18:23

1 Answer 1

0

For the first part of your question, i'd suggest something like:

_bookList.Where(_bookList => _bookList.Publish_date.ToString("yyyy-MM-dd").Contains(published));

For the second part, you could do something like:

var allBooksBetweenPrices = _bookList.Where(_bookList => (_bookList.Price >= firstPrice) && (_bookList.Price <= secondPrice));
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your contribution. On the first part I just get an argument cannot convert from 'system.DateTime' to 'char' and I dont really understand what's expecting a char..? For the second part. How do I actually setup the method? firstPrice & secondPrice both have 0 in value, they dont recieve any values so to say. Is it because my Book.cs only contain 1 ` public double price { get; set; }` ?
its because i didnt realize you have already parsed the string to a DateTime at that point. .Contains() needs a string / char array not a DateTime. I read your thoughts and concluded that you are taking in strings to define the date. i think we should have different methods here, one to compare an exact date and another to do timespans like a whole year - if i didn't missunderstand you...
To clarify. api/books/published/2012 returns all books from 2012 meanwhile /api/books/published/2012/8/15 returns all books from 2012-08-15. It looks as if my DateTime variable sets to "2012-08-01" when I write api/books/published/2012-08. So could it be done so that published/value is a string and I return it as a DateTime? That way I could use .Contains() ?
you are still confusing a little: is it /2021/8/16 or 2015-8-15 now? you can't use contains like that when 2021 is converted to 2021-01-01
My bad. It want to be able to use produced/2015/8/15 as a path to reach A book that is created at that date. While at the same time be able to use produced/2015 as a path to reach ALL books that are created that year.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.