Skip to content

Commit

Permalink
Merge pull request #10 from giakinh0823/giakinh0823
Browse files Browse the repository at this point in the history
fix delete user and signup user. test send email multi user is done
  • Loading branch information
giakinh0823 authored Mar 11, 2022
2 parents e0fad1e + 4c70c7e commit f49dac9
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void processGet(HttpServletRequest request, HttpServletResponse respon
HttpSession session = request.getSession();
User user = (User) session.getAttribute("admin");

ArrayList<Calendar> calendars = calendarDB.listByUser(user.getId());
ArrayList<Calendar> calendars = calendarDB.list();
ArrayList<CategoryCalendar> listCategory = categoryDB.list();
ArrayList<EventCalendar> events = eventDB.list();
ArrayList<User> users = userDB.list();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author giaki
*/
public class ChangePassworControllẻ extends HttpServlet {
public class ChangePassworController extends HttpServlet {

private final Validate validate = new Validate();

Expand Down Expand Up @@ -56,7 +56,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)

}
} else {
request.setAttribute("error", "Username or olđ password wrong!");
request.setAttribute("error", "Username or old password wrong!");
request.getRequestDispatcher("/views/auth/change.jsp").forward(request, response);
}
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion src/java/dal/auth/PermissionDBContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Permission findOne(String name) {
String sql = "SELECT [id]\n"
+ " ,[name]\n"
+ " FROM [permission]\n"
+ " WHERE name = ?";
+ " WHERE LOWER(name) = LOWER(?)";
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
Expand Down
30 changes: 25 additions & 5 deletions src/java/dal/auth/UserDBContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package dal.auth;

import dal.DBContext;
import dal.calendar.AdditionalCalendarDBContext;
import dal.calendar.CalendarDBContext;
import dal.calendar.EventCalendarDBContext;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -15,6 +18,9 @@
import model.auth.Permission;
import model.auth.User;
import model.auth.UserPermission;
import model.calendar.AdditionalCalendar;
import model.calendar.Calendar;
import model.calendar.EventCalendar;

/**
*
Expand Down Expand Up @@ -495,7 +501,6 @@ public User getUser(int id) {
@Override
public User insert(User user) {
PreparedStatement statement = null;
UserPermissionDBContext userPermissionDB = new UserPermissionDBContext();
try {
String sql = "INSERT INTO [user]\n"
+ " ([username]\n"
Expand Down Expand Up @@ -530,12 +535,15 @@ public User insert(User user) {
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
User new_user = getUser(id);
user.setId(id);
UserPermissionDBContext userPermissionDB = new UserPermissionDBContext();
UserPermission userPermission = new UserPermission();
userPermission.setUserId(new_user.getId());
userPermission.setUserId(id);
userPermission.setPermissionId(user.getUser_permission().getId());
userPermissionDB.insert(userPermission);
return new_user;
userPermission = userPermissionDB.insert(userPermission);
System.out.println(user.getUsername());
System.out.println(userPermission.getId());
return user;
}
} catch (SQLException ex) {
Logger.getLogger(UserDBContext.class.getName()).log(Level.SEVERE, null, ex);
Expand Down Expand Up @@ -723,7 +731,19 @@ public void updateAvatar(User user) {
@Override
public void delete(int id) {
UserPermissionDBContext userPermissionDB = new UserPermissionDBContext();
CalendarDBContext calendarDB = new CalendarDBContext();
EventCalendarDBContext eventDB = new EventCalendarDBContext();
AdditionalCalendarDBContext additionalDB = new AdditionalCalendarDBContext();
try {
ArrayList<Calendar> calendars = calendarDB.listByUser(id);
for (Calendar calendar : calendars) {
ArrayList<AdditionalCalendar> additionals = additionalDB.findMany("calendarId", calendar.getId() + "");
for (AdditionalCalendar additional : additionals) {
eventDB.deleteByAdditional(additional.getId());
additionalDB.delete(additional.getId());
}
}
calendarDB.deleteByUser(id);
userPermissionDB.deleteByUser(id);
String sql = "DELETE FROM [user]\n"
+ "WHERE id = ? ";
Expand Down
24 changes: 23 additions & 1 deletion src/java/dal/auth/UserPermissionDBContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,29 @@ public ArrayList<UserPermission> list() {

@Override
public UserPermission get(int id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
String sql = "SELECT [id]\n"
+ ",[licensed]\n"
+ ",[userId]\n"
+ ",[permissionId]\n"
+ " FROM [user_permission]\n"
+ " WHERE id = ?";
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
while (result.next()) {
UserPermission userPermission = new UserPermission();
userPermission.setId(result.getInt("id"));
userPermission.setLicensed(result.getBoolean("licensed"));
userPermission.setUserId(result.getInt("userId"));
userPermission.setPermissionId(result.getInt("permissionId"));
return userPermission;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/java/dal/calendar/CalendarDBContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,19 @@ public void delete(int id) {
}
}


public void deleteByUser(int id) {
try {
String sql = "DELETE FROM [calendar]\n"
+ "WHERE [userId] = ? ";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(AdditionalCalendarDBContext.class.getName()).log(Level.SEVERE, null, ex);
}
}

public int getSize() {
String sql = "SELECT COUNT([calendar].[id]) as 'size' FROM [calendar]";
PreparedStatement statement = null;
Expand Down
12 changes: 12 additions & 0 deletions src/java/dal/calendar/EventCalendarDBContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,18 @@ public void delete(int id) {
Logger.getLogger(EventCalendarDBContext.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void deleteByUser(int id) {
try {
String sql = "DELETE FROM [event]\n"
+ "WHERE [userId] = ? ";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(EventCalendarDBContext.class.getName()).log(Level.SEVERE, null, ex);
}
}

public int getSize() {
String sql = "SELECT COUNT([event].[id]) as 'size' FROM [event]";
Expand Down
2 changes: 1 addition & 1 deletion web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
</servlet>
<servlet>
<servlet-name>ChangePassworControllẻ</servlet-name>
<servlet-class>controller.auth.ChangePassworControllẻ</servlet-class>
<servlet-class>controller.auth.ChangePassworController</servlet-class>
</servlet>
<servlet>
<servlet-name>ErrorHandler</servlet-name>
Expand Down
2 changes: 1 addition & 1 deletion web/views/auth/signup.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
} else{
location.pathname = "/login";
$("#showErrorForm").addClass("hidden")
$('#contentSuccessForm').text("Update success")
$('#contentSuccessForm').text("Signup success")
$("#showSuccessForm").removeClass("hidden")
}
})
Expand Down

0 comments on commit f49dac9

Please sign in to comment.