Skip to content

Commit

Permalink
Close #1; Add app logic to query db for topic name based on regex and…
Browse files Browse the repository at this point in the history
… display faqs with matching topic names; Add re-routing of GET on /Faqs to landing page
  • Loading branch information
kbmakevin committed Apr 19, 2018
1 parent 2d629f0 commit 7cc31c4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,30 @@ public class DatabaseOperations {

public static List<Faq> getAllFaqRecords() {
TypedQuery<Faq> query = em.createNamedQuery("Faq.findAll", Faq.class);
return getFaqQueryResults(query);
// Query query = em.createQuery("SELECT f FROM Faq f");
// List<Faq> faqList = query.getResultList();
// if (faqList != null && faqList.size() > 0) {
// return faqList;
// } else {
// return null;
// }
}

public static List<Faq> getAllFaqRecordsWithTopicNameContaining(String topicName) {
TypedQuery<Faq> query = em.createNamedQuery("Faq.findByContainingTopicName", Faq.class);
query.setParameter("topicName", topicName);
return getFaqQueryResults(query);
// List<Faq> faqList = query.getResultList();
// if (faqList != null && faqList.size() > 0) {
// return faqList;
// } else {
// return null;
// }
}

// Helper functions
private static List<Faq> getFaqQueryResults(TypedQuery<Faq> query) {
List<Faq> faqList = query.getResultList();
if (faqList != null && faqList.size() > 0) {
return faqList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
* The persistent class for the faq database table.
*
*/
@Entity
@NamedQuery(name = "Faq.findAll", query = "SELECT f FROM Faq f")
@NamedQueries({ @NamedQuery(name = "Faq.findAll", query = "SELECT f FROM Faq f"),
@NamedQuery(name = "Faq.findByContainingTopicName", query = "SELECT f FROM Faq f WHERE LOWER(f.topic.topicName) LIKE LOWER(CONCAT('%', :topicName, '%'))") })
public class Faq implements Serializable {
private static final long serialVersionUID = 1L;

Expand Down
6 changes: 4 additions & 2 deletions GroupKOPS_COMP303_Assignment3_Web/WebContent/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
</c:when>
<c:otherwise>
<!-- show instructions -->
<span>Please enter in the name of a topic title above and
press search to find questions related to the topic.</span>
<p>Please enter in the name of a topic title above and press
search to find questions related to the topic.</p>
<p>If the topic name field is left blank, all questions will be
displayed.</p>
</c:otherwise>
</c:choose>
</div>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class Faqs extends HttpServlet {
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
// should not be able to do get request to /Faqs, redirects to landing page
response.sendRedirect("");
}

/**
Expand All @@ -37,18 +37,20 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

List<Faq> faqQueryResults = DatabaseOperations.getAllFaqRecords();
System.out.println("Found faqs: " + faqQueryResults);
// List<Faq> faqQueryResults = DatabaseOperations.getAllFaqRecords();
// System.out.println("Found faqs: " + faqQueryResults);

// when a user has made a post request to /Faqs, then they have queried the
// db...otherwise they simply visited page, no queries executed yet (show diff
// view on page)
HttpSession session = request.getSession();
String queriedTopicName = request.getParameter("topicName");
List<Faq> faqQueryResults = DatabaseOperations.getAllFaqRecordsWithTopicNameContaining(queriedTopicName);

// if (request.getAttribute("userQueriedDb") == null) {
request.setAttribute("userQueriedDb", true);
// }
request.setAttribute("queriedTopicName", request.getParameter("topicName"));
request.setAttribute("queriedTopicName", queriedTopicName);
request.setAttribute("matchingFaqs", faqQueryResults);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
Expand Down

0 comments on commit 7cc31c4

Please sign in to comment.