I 'm building an application to store and retrieve books, but I have an issue with retrieving data from the db.
I'm using REST and testing with postman. Everything works OK. Currently I have a series of methods written in Java at the backend like so

@Override
    @POST
    @Path("/add")
    //@Produces("application/json") 
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)

    public Response addBook(Book book) {        
        dbAccess.connectToDb();
        return dbAccess.addBook(book);
    }

    @Override
    @DELETE
    @Path("/{id}/delete")
    @Produces("application/json")
    public Response deleteBook(@PathParam("id") int id) {
        dbAccess.connectToDb();
    return  dbAccess.deleteBook(id);
    }

    @Override
    @GET
    @Path("/{id}/get")
    @Produces("application/json")
    public Book getBook(@PathParam("id") int id) {
        dbAccess.connectToDb();
        return dbAccess.getBook(id);
    }

So in postman I have a request like http://localhost:8080/book-storage-REST/book/15/get to retrieve a record.
Eventually I will write a front end, maybe in angular, which allows users to do their searches and display the results back to them.
Now, in the real world, a user of course wouldn't use an ID to search for a record, but he/she will be more likely to use the author name or book title. So my question is:
do I need to rewrite the methods above and not use an id parameter or should the front end do the job of taking a search term (title or author), query the db to find the equivalent id, do the necessry filtering in case of more than 1 record and only then, call the relevant methods above wth only one ID?

Recommended Answers

All 4 Replies

I'd probably implement a method like this:

@Override
@GET
@Path("/{text}/search")
@Produces("application/json")
public BookList searchBooks(@PathParam("text") string text) {
    dbAccess.connectToDb();
    return dbAccess.searchBooks(text);
}

and perhaps add another parameter to indicate whether to search isbn, title or author.

I’m not sure what data set sever you’re using, but you may need to add indexes to the appropriate lookup columns for decent performance.

I'd probably implement a method like this:

Thanks, and sorry for the long delay. I could yes, but then what about the other methods using ids? I thought that one of the advantages of using rest was to be able to use ids for easier (and more reliable) retrieval, and while I was looking into it before developing, I indeed found that most of the implementations use ids...which then led to my above question, lol. So, if I use the method you suggested then the ones using ids become redundant, right?
Unless you meant like using the exta searchBooks method not to find the record but to perhaps call another method to retrieve the ids and then I could use the ids to call the get method with the relevant id...

I’m not sure what data set sever you’re using,

I'm using Tomcat at the moment (if I understood your question :-)). I don't envisage the db to grow too big though

Information recovery implies acquiring information from an information base administration framework, for example, ODBMS. For this situation, it is viewed as that information is spoken to in an organized manner, and there is no equivocalness in information. To recover the ideal information the client present a bunch of models by an inquiry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.