Skip to content

Commit 2067d04

Browse files
committed
Made the User entity bi directional to support cascading
1 parent 1b28118 commit 2067d04

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
@Import(JpaConfig.class)
1717
public class WebRootConfig {
1818

19-
public ModelMapper
19+
@Bean
20+
public ModelMapper modelMapper(){
21+
return new ModelMapper();
22+
}
2023
@Bean
2124
public static YamlPropertiesFactoryBean yamlPropertiesFactoryBean(ConfigurableEnvironment env ){
2225
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
2326
yamlPropertiesFactoryBean.setResources( new ClassPathResource("application.yml"));
2427
Properties yamlProps = yamlPropertiesFactoryBean.getObject();
2528
env.getPropertySources().addLast(new PropertiesPropertySource("yaml",yamlProps));
2629
return yamlPropertiesFactoryBean;
27-
28-
2930
}
3031
}
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package lk.ijse.dep.note.entity;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
5-
import lombok.NoArgsConstructor;
3+
import jdk.internal.dynalink.linker.LinkerServices;
4+
import lombok.*;
65

7-
import javax.persistence.Column;
8-
import javax.persistence.Entity;
9-
import javax.persistence.Id;
6+
import javax.persistence.*;
7+
import java.util.ArrayList;
8+
import java.util.List;
109

1110
@NoArgsConstructor
1211
@AllArgsConstructor
1312
@Entity
1413
@Data
14+
1515
public class User implements SuperEntity {
1616
@Id
1717
private String id;
@@ -21,4 +21,13 @@ public class User implements SuperEntity {
2121
private String password;
2222
@Column(name="full_name",unique = true)
2323
private String fullName;
24+
25+
@Setter(AccessLevel.NONE)
26+
@OneToMany(mappedBy = "user",cascade = {CascadeType.REMOVE})
27+
private List<Note> notes=new ArrayList<>();
28+
29+
@PrePersist
30+
private void beforePersist(){
31+
notes.forEach(note->note.setUser(this));
32+
}
2433
}
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
package lk.ijse.dep.note.service.impl;
22

33
import lk.ijse.dep.note.dto.UserDTO;
4+
import lk.ijse.dep.note.entity.User;
45
import lk.ijse.dep.note.repository.custom.UserRepository;
56
import lk.ijse.dep.note.service.UserService;
67
import lk.ijse.dep.note.service.exception.DuplicateEmailException;
78
import lk.ijse.dep.note.service.exception.NotFoundException;
9+
import lk.ijse.dep.note.service.util.EntityDTOConverter;
10+
import org.modelmapper.ModelMapper;
811
import org.springframework.beans.factory.annotation.Autowired;
912
import org.springframework.stereotype.Service;
1013
import org.springframework.transaction.annotation.Transactional;
1114

15+
import java.util.Optional;
16+
1217
@Service
1318
@Transactional
1419
public class UserServiceImpl implements UserService {
1520

1621
@Autowired
1722
private UserRepository userRepository;
23+
@Autowired
24+
private EntityDTOConverter transformer;
25+
1826

1927
@Override
2028
public UserDTO registerUser(UserDTO user) throws DuplicateEmailException {
2129
if(userRepository.existUserByEmail(user.getEmail()) ) throw new DuplicateEmailException("Email already exists");
22-
return userRepository
30+
return transformer.getUserDTO(userRepository.save(transformer.getUserEntity(user)));
2331
}
2432

2533
@Override
2634
public void updateUser(UserDTO user) throws NotFoundException {
35+
Optional<User> optUser = userRepository.findById(user.getId());
36+
if(!optUser.isPresent()) throw new NotFoundException("invalid user id");
37+
optUser.get().setFullName(user.getFullName());
38+
optUser.get().setPassword(user.getPassword());
2739

2840
}
2941

3042
@Override
3143
public UserDTO getUserInfo(String userId) throws NotFoundException {
32-
return null;
44+
return userRepository.findById(userId).map(transformer::getUserDTO).orElseThrow(()->new NotFoundException("Invalid user Id"));
45+
3346
}
3447

3548
@Override
3649
public void deleteUser(String userId) throws NotFoundException {
50+
if(!userRepository.existById(userId)) throw new NotFoundException("Invalid User id");
51+
3752

3853
}
3954
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package lk.ijse.dep.note.service.util;
2+
3+
import lk.ijse.dep.note.dto.UserDTO;
4+
import lk.ijse.dep.note.entity.User;
5+
import org.modelmapper.ModelMapper;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class EntityDTOConverter {
11+
@Autowired
12+
private ModelMapper mapper;
13+
14+
public EntityDTOConverter(ModelMapper mapper){
15+
this.mapper=mapper;
16+
}
17+
18+
public User getUserEntity(UserDTO user){
19+
return mapper.map(user,User.class);
20+
}
21+
public UserDTO getUserDTO(User user){
22+
return mapper.map(user,UserDTO.class);
23+
}
24+
}

0 commit comments

Comments
 (0)