66import ir .bigz .springbootreal .exception .AppException ;
77import ir .bigz .springbootreal .exception .HttpErrorCode ;
88import ir .bigz .springbootreal .viewmodel .UserModel ;
9- import org .slf4j .Logger ;
10- import org .slf4j .LoggerFactory ;
11- import org .springframework .beans .factory .annotation .Autowired ;
129import org .springframework .cache .annotation .CacheEvict ;
1310import org .springframework .cache .annotation .CachePut ;
1411import org .springframework .cache .annotation .Cacheable ;
1714import org .springframework .transaction .annotation .Propagation ;
1815import org .springframework .transaction .annotation .Transactional ;
1916
20- import java .util .Collections ;
2117import java .util .List ;
2218import java .util .Objects ;
2319import java .util .Optional ;
2723@ Component
2824public class UserServiceImpl implements UserService {
2925
30- final Logger LOG = LoggerFactory .getLogger (UserServiceImpl .class );
31-
3226 private final UserRepository userRepository ;
3327 private final UserMapper userMapper ;
3428
@@ -48,26 +42,27 @@ public UserModel getUser(Long userId) {
4842 Optional <User > user = userRepository .find (userId );
4943 return userMapper .userToUserModel (user .get ());
5044 }catch (RuntimeException exception ){
51- LOG .info ("user not found" );
5245 throw AppException .newInstance (
5346 HttpErrorCode .ERR_10702 ,
54- String .format ("not found user with id : %s " , userId )
47+ String .format ("user with id, %s not found " , userId )
5548 );
5649 }
5750 }
5851
5952 @ Override
6053 @ Transactional (propagation = Propagation .REQUIRED , rollbackFor = Exception .class )
6154 public UserModel addUser (UserModel userModel ) {
62- if (userRepository .getUserWithNationalId (userModel .getNationalId ()) == null ){
63- User user = userMapper .userModelToUser (userModel );
64- return userMapper .userToUserModel (userRepository .insert (user ));
65- }
66- else {
67- LOG .info ("user has already existed not created" );
55+ try {
56+ if (userRepository .getUserWithNationalId (userModel .getNationalId ()) == null ){
57+ User user = userMapper .userModelToUser (userModel );
58+ return userMapper .userToUserModel (userRepository .insert (user ));
59+ }
60+ throw new RuntimeException ("user has already exist" );
61+
62+ }catch (RuntimeException exception ){
6863 throw AppException .newInstance (
6964 HttpErrorCode .ERR_10700 ,
70- String .format ("user existed with %s nationalId" , userModel .getNationalId ())
65+ String .format ("user has already existed with %s nationalId" , userModel .getNationalId ())
7166 );
7267 }
7368 }
@@ -76,18 +71,16 @@ public UserModel addUser(UserModel userModel) {
7671 @ Transactional (propagation = Propagation .REQUIRED , rollbackFor = Exception .class )
7772 @ CachePut (value = "userCache" , key ="#userId" , condition = "#userId != null" , unless = "#result==null" )
7873 public UserModel updateUser (long userId , UserModel userModel ) {
79- Optional < User > user = userRepository . find ( userId );
80- if ( user . isPresent ()){
74+ try {
75+ Optional < User > user = userRepository . find ( userId );
8176 User sourceUser = user .get ();
8277 User updateUser = userMapper .userModelToUser (userModel );
8378 mapUserForUpdate (sourceUser , updateUser );
8479 return userMapper .userToUserModel (sourceUser );
85- }
86- else {
87- LOG .info (String .format ("user with user id: %s not found" , userId ));
80+ }catch (RuntimeException exception ){
8881 throw AppException .newInstance (
8982 HttpErrorCode .ERR_10703 ,
90- String .format ("user with user id: %s not found" , userId )
83+ String .format ("user with userId %s, not found" , userId )
9184 );
9285 }
9386 }
@@ -96,19 +89,16 @@ public UserModel updateUser(long userId, UserModel userModel) {
9689 @ Override
9790 @ CacheEvict (value = "userCache" , beforeInvocation = true , key = "#userId" )
9891 public String deleteUser (long userId ){
99-
10092 try {
101- if (getUser (userId ) != null ) {
102- userRepository .delete (userId );
103- return "Success" ;
104- }
105- else {
106- LOG .info ("user not found" );
107- return "user not found" ;
108- }
109- }catch (Exception e ){
110- LOG .error ("delete user not complete \n " + e .getMessage ());
111- return "failed" ;
93+ userRepository .find (userId );
94+ userRepository .delete (userId );
95+ return "Success" ;
96+
97+ }catch (RuntimeException exception ){
98+ throw AppException .newInstance (
99+ HttpErrorCode .ERR_10701 ,
100+ String .format ("user with id %s, not found" , userId )
101+ );
112102 }
113103 }
114104
@@ -119,7 +109,6 @@ public List<UserModel> getAll() {
119109 Stream <User > allUser = userRepository .getAll ();
120110 return allUser .map (userMapper ::userToUserModel ).collect (Collectors .toList ());
121111 }catch (RuntimeException exception ){
122- LOG .info ("getAll method has error \n " + exception .getMessage ());
123112 throw AppException .newInstance (
124113 HttpErrorCode .ERR_10701 ,
125114 String .format ("getAll method has error: %s" , exception .getCause ())
0 commit comments