Skip to content

Commit 5b6d56a

Browse files
committed
Add Model mapper dependency
1 parent d17f199 commit 5b6d56a

File tree

6 files changed

+78
-30
lines changed

6 files changed

+78
-30
lines changed

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@
9191
<version>5.3.21</version>
9292
<scope>test</scope>
9393
</dependency>
94-
94+
<dependency>
95+
<groupId>org.modelmapper</groupId>
96+
<artifactId>modelmapper</artifactId>
97+
<version>3.0.0</version>
98+
</dependency>
9599

96100
</dependencies>
97101

src/main/java/lk/ijse/dep/note/api/UserController.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,43 @@ public class UserController {
1515

1616
@Autowired
1717
private UserService userService;
18+
1819
@ResponseStatus(HttpStatus.CREATED)
19-
@PostMapping(consumes = "application/json",produces = "application/json")
20-
public UserDTO registerUser(@RequestBody UserDTO user){
20+
@PostMapping(consumes = "application/json", produces = "application/json")
21+
public UserDTO registerUser(@RequestBody UserDTO user) {
2122
//validate
2223

2324

24-
try {
25-
userService.registerUser(user);
26-
} catch (DuplicateEmailException e) {
27-
throw new ResponseStatusException(HttpStatus.CONFLICT,"Email already exists",e);
28-
}
25+
userService.registerUser(user);
26+
2927
return user;
3028
}
31-
@GetMapping(path = "{userId:[A-Fa-f0-\\~]{36}}}",produces = "application/json")
32-
public UserDTO getUserInfo(@PathVariable String userId){
33-
System.out.println("get"+userId);
34-
try {
35-
return userService.getUserInfo(userId);
36-
} catch (NotFoundException e) {
37-
throw new ResponseStatusException(404,"invalid user id",e);
38-
}
29+
30+
@GetMapping(path = "{userId:[A-Fa-f0-\\~]{36}}}", produces = "application/json")
31+
public UserDTO getUserInfo(@PathVariable String userId) {
32+
System.out.println("get" + userId);
33+
34+
return userService.getUserInfo(userId);
35+
3936
}
37+
4038
@ResponseStatus(HttpStatus.NO_CONTENT)
4139
@DeleteMapping("{userId:[A-Fa-f0-\\~]{36}}")
42-
public void deleteuser(@PathVariable String userId){
43-
try {
44-
userService.deleteUser(userId);
45-
} catch (NotFoundException e) {
46-
throw new ResponseStatusException(404,"invalid user id",e);
47-
}
40+
public void deleteuser(@PathVariable String userId) {
41+
42+
userService.deleteUser(userId);
43+
4844
}
45+
4946
@ResponseStatus(HttpStatus.NO_CONTENT)
50-
@PatchMapping(path = "{userId:[A-Fa-f0-\\~]{36}}",consumes = "application/json")
51-
public void updateUser(@PathVariable String userId,@RequestBody UserDTO user){
47+
@PatchMapping(path = "{userId:[A-Fa-f0-\\~]{36}}", consumes = "application/json")
48+
public void updateUser(@PathVariable String userId, @RequestBody UserDTO user) {
5249

5350
//todo:validate the user
54-
try {
55-
user.setId(userId);
56-
userService.updateUser(user);
57-
} catch (NotFoundException e) {
58-
throw new ResponseStatusException(404,"invalid user id",e);
59-
}
51+
52+
user.setId(userId);
53+
userService.updateUser(user);
54+
6055
}
6156

6257
}

src/main/java/lk/ijse/dep/note/config/JpaConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.context.annotation.Configuration;
77
import org.springframework.context.annotation.PropertySource;
88
import org.springframework.core.env.Environment;
9+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
910
import org.springframework.orm.jpa.JpaTransactionManager;
1011
import org.springframework.orm.jpa.JpaVendorAdapter;
1112
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -57,4 +58,8 @@ public DataSource dataSource(){
5758
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
5859
return new JpaTransactionManager(emf);
5960
}
61+
62+
public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
63+
return new PersistenceExceptionTranslationPostProcessor();
64+
}
6065
}

src/main/java/lk/ijse/dep/note/repository/custom/UserRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77

88
public interface UserRepository extends CrudRepository<User,String> {
9+
boolean existUserByEmail(String email);
910
}

src/main/java/lk/ijse/dep/note/repository/custom/impl/UserRepositoryImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77

88
@Repository
99
public class UserRepositoryImpl extends CrudRepositoryImpl<User,String> implements UserRepository {
10+
@Override
11+
public boolean existUserByEmail(String email) {
12+
return !entityManager.createQuery("SELECT u FROM lk.ijse.dep.note.entity.User u WHERE u.email= :email").setParameter("email",email).getResultList().isEmpty();
13+
}
1014
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lk.ijse.dep.note.service.impl;
2+
3+
import lk.ijse.dep.note.dto.UserDTO;
4+
import lk.ijse.dep.note.repository.custom.UserRepository;
5+
import lk.ijse.dep.note.service.UserService;
6+
import lk.ijse.dep.note.service.exception.DuplicateEmailException;
7+
import lk.ijse.dep.note.service.exception.NotFoundException;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Service
13+
@Transactional
14+
public class UserServiceImpl implements UserService {
15+
16+
@Autowired
17+
private UserRepository userRepository;
18+
19+
@Override
20+
public UserDTO registerUser(UserDTO user) throws DuplicateEmailException {
21+
if(userRepository.existUserByEmail(user.getEmail()) ) throw new DuplicateEmailException("Email already exists");
22+
return userRepository
23+
}
24+
25+
@Override
26+
public void updateUser(UserDTO user) throws NotFoundException {
27+
28+
}
29+
30+
@Override
31+
public UserDTO getUserInfo(String userId) throws NotFoundException {
32+
return null;
33+
}
34+
35+
@Override
36+
public void deleteUser(String userId) throws NotFoundException {
37+
38+
}
39+
}

0 commit comments

Comments
 (0)