Skip to content

Commit

Permalink
Fixed user deletion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Tanner authored and Tyler Tanner committed Oct 27, 2022
1 parent 90872e5 commit b0641d2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ public String searchForBook(String bookSearchAll, int bsearchType, HttpSession s
case 2:
return getBookByAuthor(bookSearchAll, session);
case 3:
return getBookByGenre(bookSearchAll, session);
try {
return getBookByGenre(bookSearchAll, session);
} catch(Exception e) {
return searchForBook(session);
}
case 4:
return getBookByKeyWord(bookSearchAll, session);
case 5:
return getBookByYear(Integer.parseInt(bookSearchAll), session);
try {
return getBookByYear(Integer.parseInt(bookSearchAll), session);
} catch(Exception e) {
return searchForBook(session);
}
case 6:
return getBookByTitle(bookSearchAll, session);
case 7:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ public String deleteUserConfirmation(HttpSession session) {
return "user/deleteUser";
}

@RequestMapping(path = "deleteUserConfirmation.do")
public String delete(HttpSession session) {
User user = (User)(session.getAttribute("user"));
boolean successful = userDao.removeUser(user.getId());
@RequestMapping(path = "deleteUserConfirmation.do", method=RequestMethod.POST)
public String delete(HttpSession session, int id) {
boolean successful = userDao.removeUser(id);
if (successful) {
return "redirect:deleteUserHome.do";
return "redirect:deleteUserHome.do";
}
else {
return "user/userProfile";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.skilldistillery.jpaclubindex.data;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.apache.jasper.tagplugins.jstl.core.If;
import org.apache.tomcat.websocket.AsyncChannelWrapperNonSecure;
import org.springframework.stereotype.Service;

import com.skilldistillery.jpaclubindex.entities.BookClub;
import com.skilldistillery.jpaclubindex.entities.Location;
import com.skilldistillery.jpaclubindex.entities.Review;
import com.skilldistillery.jpaclubindex.entities.User;
import com.skilldistillery.jpaclubindex.entities.UserReadingList;

@Service
@Transactional
Expand Down Expand Up @@ -63,6 +73,35 @@ public User update(User currentUser) {
@Override
public boolean removeUser(int userId) {
User currentUser = em.find(User.class, userId);
List<BookClub> allBookClubs = new ArrayList<>(currentUser.getBookClubs());

for(BookClub bc : allBookClubs) {
if(bc.getOwner().equals(currentUser) && bc.getUsers().size() > 1) {

List<User> users = new ArrayList<>(bc.getUsers());
for(User u : users) {
if(u.equals(currentUser)) {
continue;
} else {
currentUser.removeBookClub(bc);
bc.setOwner(u);
break;
}
}
} else {
em.remove(bc);
}
}
List<Review> allReviews = new ArrayList<>(currentUser.getReviews());
for(Review r : allReviews) {
em.remove(r);
}

List<UserReadingList> allURL = new ArrayList<>(currentUser.getReadingLists());
for(UserReadingList url : allURL) {
em.remove(url);
}

em.remove(currentUser);
return !em.contains(currentUser);
}
Expand Down
13 changes: 4 additions & 9 deletions ClubIndex/src/main/webapp/WEB-INF/user/deleteUser.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
<jsp:include page="../includes/nav.jsp"/>
<div class="container">
<h2>Are you sure you want to delete this account? This is a final decision</h2>
<form action="deleteUserConfirmation.do">
<c:choose>
<c:when test="${not empty user}">
<input type="hidden" name="id" value="${user.id}"/>
</c:when>
</c:choose>
<input type="submit" value="Delete Account">

</form>
<form action="deleteUserConfirmation.do" method="POST">
<input type="hidden" name="id" value="${user.id}"/>
<input type="submit" value="Delete Account">
</form>
</div>
<jsp:include page="../includes/footer.jsp"/>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public class User {
@Column(name="about_me")
private String aboutMe;

@OneToOne(cascade = CascadeType.PERSIST)
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="location_id")
private Location location;

@ManyToMany(mappedBy="users")
private List<BookClub> bookClubs;

@OneToMany(mappedBy="user")
@OneToMany(mappedBy="user", cascade = CascadeType.ALL)
private List<Review> reviews;

@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "user")
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<UserReadingList> readingLists;

public User() {
Expand Down

0 comments on commit b0641d2

Please sign in to comment.