Skip to content

Commit

Permalink
Merge pull request #13 from ibtissamelhani/fix/1.0.0
Browse files Browse the repository at this point in the history
fixed
  • Loading branch information
ibtissamelhani authored Oct 13, 2024
2 parents a4e1148 + 5c90f46 commit 58480dc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<packaging>war</packaging>
<groupId>org.example</groupId>
<artifactId>DevSync</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.exception;

public class UserAlreadyExistException extends RuntimeException{
public UserAlreadyExistException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.exception;

public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/example/model/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "first_name", nullable = false)
private String firstName;

@Column(name = "last_name", nullable = false)
private String lastName;

@Column(nullable = false, unique = true)
private String email;

@Column(nullable = false)
private String password;

@Enumerated(EnumType.STRING)
private UserRole role;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.example.repository.interfaces.UserRepository;

import java.util.List;
import java.util.Optional;

public class UserRepositoryImpl implements UserRepository {

Expand All @@ -18,26 +19,31 @@ public UserRepositoryImpl(EntityManagerFactory entityManagerFactory) {
}

@Override
public void save(User user) {
public User save(User user) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist(user);
entityManager.getTransaction().commit();
return user;
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
System.out.println(e.getMessage());
throw e;
}finally {
entityManager.close();
}
}

@Override
public User findById(Long id) {
public Optional<User> findById(Long id) {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
return entityManager.find(User.class, id);
User user = entityManager.find(User.class, id);
return Optional.ofNullable(user);
} catch (Exception e) {
System.err.println("Error finding user with ID " + id + ": " + e.getMessage());
return Optional.empty();
}
}

Expand All @@ -50,12 +56,13 @@ public List<User> findAll() {
}

@Override
public void update(User user) {
public User update(User user) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.merge(user);
entityManager.getTransaction().commit();
return user;
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
Expand All @@ -68,7 +75,7 @@ public void update(User user) {


@Override
public void delete(User user) {
public Boolean delete(User user) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
Expand All @@ -78,11 +85,13 @@ public void delete(User user) {
entityManager.remove(entityManager.merge(user));
}
entityManager.getTransaction().commit();
return true;
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
System.out.println(e.getMessage());
return false;
} finally {
entityManager.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

public interface UserRepository {

void save(User user);
User save(User user);

User findById(Long id);
Optional<User> findById(Long id);

List<User> findAll();

void update(User user);
User update(User user);

void delete(User user);
Boolean delete(User user);
}
33 changes: 23 additions & 10 deletions src/main/java/org/example/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.example.service;

import org.example.exception.UserAlreadyExistException;
import org.example.exception.UserNotFoundException;
import org.example.model.entities.User;
import org.example.repository.interfaces.UserRepository;

import java.util.List;
import java.util.Optional;

public class UserService {

Expand All @@ -13,28 +16,38 @@ public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public void createUser(User user) {
userRepository.save(user);
public User createUser(User user) {
Optional<User> optionalUser = getUserById(user.getId());
if (optionalUser.isPresent()) {
throw new UserAlreadyExistException("User with id " + user.getId() + " already exists");
}
return userRepository.save(user);
}

public User getUserById(Long id) {
public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}

public List<User> getAllUsers() {
return userRepository.findAll();
}

public void updateUser(User user) {
userRepository.update(user);
public User updateUser(User user) {
Optional<User> optionalUser = getUserById(user.getId());
if (optionalUser.isPresent()) {
return userRepository.update(user);
}else {
throw new UserNotFoundException("User with id " + user.getId() + " not found");
}
}

public void deleteUser(Long id) {
User user = getUserById(id);
if (user != null) {
userRepository.delete(user);
public Boolean deleteUser(Long id) {
Optional<User> optionalUser = getUserById(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
return userRepository.delete(user);
}else {
System.out.println("User not found");
throw new UserNotFoundException("User with id " + id + " not found");
}
}
}
12 changes: 7 additions & 5 deletions src/main/java/org/example/servlets/UserServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Optional;

public class UserServlet extends HttpServlet {

Expand Down Expand Up @@ -51,13 +52,14 @@ private void showCreateForm(HttpServletRequest request, HttpServletResponse resp

private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Long userId = Long.parseLong(request.getParameter("id"));
User user = userService.getUserById(userId);
if (user== null) {
Optional<User> optionalUser = userService.getUserById(userId);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
request.setAttribute("user", user);
request.getRequestDispatcher("/WEB-INF/views/editUserForm.jsp").forward(request, response);
}else {
response.sendRedirect(request.getContextPath() + "/users?action=list");
return;
}
request.setAttribute("user", user);
request.getRequestDispatcher("/WEB-INF/views/editUserForm.jsp").forward(request, response);
}

private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
Expand Down
5 changes: 0 additions & 5 deletions src/main/webapp/WEB-INF/views/users.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
email
</th>

<th scope="col" class="px-4 py-3.5 text-sm font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400">
passwor
</th>

<th scope="col" class="px-4 py-3.5 text-sm font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400">
Role
</th>
Expand Down Expand Up @@ -73,7 +69,6 @@
<td class="px-12 py-4 text-sm font-normal text-gray-700 whitespace-nowrap">
${user.email}
</td>
<td class="px-4 py-4 text-sm text-gray-500 dark:text-gray-300 whitespace-nowrap">${user.password}</td>
<td class="px-4 py-4 text-sm text-gray-500 dark:text-gray-300 whitespace-nowrap">${user.role}</td>
<td class="px-4 py-4 text-sm whitespace-nowrap">
<div class="flex gap-2 dark:bg-gray-900 dark:border-gray-700 dark:divide-gray-700">
Expand Down

0 comments on commit 58480dc

Please sign in to comment.