File tree Expand file tree Collapse file tree 3 files changed +50
-4
lines changed
src/main/java/com/manir/springbootecommercerestapi/service Expand file tree Collapse file tree 3 files changed +50
-4
lines changed Original file line number Diff line number Diff line change 6
6
import com .manir .springbootecommercerestapi .repository .FaqRepository ;
7
7
import com .manir .springbootecommercerestapi .service .CommonService ;
8
8
import com .manir .springbootecommercerestapi .service .FaqService ;
9
+ import com .manir .springbootecommercerestapi .service .MapperService ;
9
10
import lombok .AllArgsConstructor ;
10
11
import org .modelmapper .ModelMapper ;
11
12
import org .springframework .stereotype .Service ;
@@ -21,15 +22,15 @@ public class FaqServiceImpl implements FaqService {
21
22
private final FaqRepository faqRepository ;
22
23
@ Resource (name = "modelMapper" )
23
24
private final ModelMapper modelMapper ;
24
- @ Resource (name = "commonService " )
25
- private final CommonService commonService ;
25
+ @ Resource (name = "mapperService " )
26
+ private final MapperService < Faq , FaqDto > mapperService ;
26
27
27
28
@ Override
28
29
public FaqDto addFaq (FaqDto faqDto ) {
29
- Faq faq = mapToEntity (faqDto );
30
+ Faq faq = mapperService . mapToEntity (faqDto );
30
31
Faq addedFaq = faqRepository .save (faq );
31
32
32
- return mapToDto (addedFaq );
33
+ return mapperService . mapToDto (addedFaq );
33
34
}
34
35
35
36
@ Override
Original file line number Diff line number Diff line change
1
+ package com .manir .springbootecommercerestapi .service ;
2
+
3
+ public interface MapperService <E , D >{
4
+ //entity mapper
5
+ E mapToEntity (D type );
6
+
7
+ //dto mapper
8
+ D mapToDto (E type );
9
+ }
Original file line number Diff line number Diff line change
1
+ package com .manir .springbootecommercerestapi .service ;
2
+
3
+ import lombok .AllArgsConstructor ;
4
+ import org .modelmapper .ModelMapper ;
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .stereotype .Component ;
7
+ import org .springframework .stereotype .Service ;
8
+
9
+ import javax .annotation .Resource ;
10
+
11
+ @ AllArgsConstructor
12
+ @ Service
13
+ @ Component ("mapperService" )
14
+ public class MapperServiceImpl <E , D > implements MapperService <E , D > {
15
+
16
+
17
+ @ Resource (name = "modelMapper" )
18
+ private final ModelMapper modelMapper ;
19
+ @ Resource
20
+ private final Class <E > entityClass ;
21
+ @ Resource
22
+ private final Class <D > dtoClass ;
23
+
24
+
25
+ @ Override
26
+ public E mapToEntity (D type ) {
27
+ E model = modelMapper .map (type , entityClass );
28
+ return model ;
29
+ }
30
+
31
+ @ Override
32
+ public D mapToDto (E type ) {
33
+ D dto = modelMapper .map (type , dtoClass );
34
+ return dto ;
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments