Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasBarrera committed Nov 16, 2022
2 parents 959b779 + 9a2fd36 commit a54eb35
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class DeleteAdsServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

String title = (String) request.getParameter("title");
System.out.println(title);
long id = Long.parseLong( request.getParameter("id"));
System.out.println(id);

Ad ad = DaoFactory.getAdsDao().ByTitle(title); //FIND THE AD TO DELETE
Ad ad = DaoFactory.getAdsDao().ById(id); //FIND THE AD TO DELETE
DaoFactory.getAdsDao().delete(ad); // DELETE AD
response.sendRedirect("/ads");

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/codeup/adlister/controllers/RegisterServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
request.setAttribute("passwordMismatch", "Please confirm your password you re-entered is correct.");
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);

<<<<<<< HEAD
} else if (password.length() <= 6) {
request.setAttribute("passwordLength", "Please confirm your password you re-entered is the correct length.");
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
Expand All @@ -63,6 +64,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)



=======

} else {
correctInfo = true;
}
}
}
>>>>>>> 9a2fd364ccc8157288adddc4a51e0f568c34fd8d




Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/codeup/adlister/controllers/UpdateAdServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
@WebServlet(name = "controllers.UpdateAdServlet", urlPatterns = "/updateAd")
public class UpdateAdServlet extends HttpServlet {
String titleOld;
long id;
String oldDescription;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

titleOld = request.getParameter("title");
id = Long.parseLong(request.getParameter("id"));
oldDescription = request.getParameter("description");


request.getRequestDispatcher("/WEB-INF/ads/updateAd.jsp")
.forward(request, response);
Expand All @@ -26,10 +31,17 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
User user = (User) request.getSession().getAttribute("user");

String title = request.getParameter("title");
String description = request.getParameter("description");
if( title.isEmpty()){title = titleOld;}

String description = request.getParameter("description");
if( description.isEmpty()){ description = oldDescription;}




System.out.println(titleOld);
Ad ad = DaoFactory.getAdsDao().ByTitle(titleOld); //FIND THE AD TO UPDATE
System.out.println(id);
System.out.println(oldDescription);
Ad ad = DaoFactory.getAdsDao().ById(id); //FIND THE AD TO UPDATE
DaoFactory.getAdsDao().update(ad,title,description); // UPDATE AD
response.sendRedirect("/ads");

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/codeup/adlister/dao/Ads.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface Ads {



Ad ByTitle(String title);
Ad ById(long id);

void delete(Ad ad);

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/codeup/adlister/dao/ListAdsDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public void update(Ad ad, String title, String description) {
}

@Override
public Ad ByTitle(String title) {
public Ad ById(long id) {
return null;
}




@Override
public void delete(Ad ad) {

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/codeup/adlister/dao/MySQLAdsDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ public void update(Ad ad, String title,String description) {



//**********************Find ad by the ad id to delete****************************
//**********************Find ad by id to delete****************************

public Ad ByTitle(String title) {
public Ad ById(long id) {

String sql = "SELECT * FROM ads WHERE title LIKE ?";
String sql = "SELECT * FROM ads WHERE id = ?";
PreparedStatement stmt = null;
try {
stmt = connection.prepareStatement(sql);
stmt.setString(1,"%"+title+"%");
stmt.setLong(1,id);
ResultSet rs = stmt.executeQuery();
List<Ad> ad = createAdsFromResults(rs);

Expand Down
15 changes: 10 additions & 5 deletions src/main/webapp/WEB-INF/profile.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@
<p class="card-text">${ad.description}</p>

<div class="row">
<div class="col-6">
<div class="col-6 mt-4">
<form method="get" action="updateAd" >

<label for="edit"></label>
<input id="edit" class="invisible" type="text" name="title" value="${ad.title}">
<label for="update"></label>
<input id="update" type="text" class="invisible" name="description" value="${ad.description}">
<input type="text" class="invisible" name="title" value="${ad.title}">

<input type="text" class="invisible" name="id" value="${ad.id}">
<button class="btn btn-primary" type="submit" >Update Ad</button>
</form>
</div>


<div class="col-6">
<div class="col-6 mt-4">
<form method="post" action="deleteAd" >
<label for="delete"></label>
<input id="delete" class="invisible" type="text" name="title" value="${ad.title}">
<input type="text" class="invisible" name="description" value="${ad.description}">

<input id="delete" type="text" class="invisible" name="id" value="${ad.id}">

<button class="btn btn-primary" type="submit" >Delete Ad</button>
</form>
Expand Down

0 comments on commit a54eb35

Please sign in to comment.