@@ -50,11 +50,11 @@ public async Task<IEnumerable<ProductModel>> GetProductByCategory(int categoryId
50
50
return mapped ;
51
51
}
52
52
53
- public async Task < ProductModel > Create ( ProductModel entityDto )
53
+ public async Task < ProductModel > Create ( ProductModel productModel )
54
54
{
55
- await ValidateProductIfExist ( entityDto ) ;
55
+ await ValidateProductIfExist ( productModel ) ;
56
56
57
- var mappedEntity = ObjectMapper . Mapper . Map < Product > ( entityDto ) ;
57
+ var mappedEntity = ObjectMapper . Mapper . Map < Product > ( productModel ) ;
58
58
if ( mappedEntity == null )
59
59
throw new ApplicationException ( $ "Entity could not be mapped.") ;
60
60
@@ -65,42 +65,43 @@ public async Task<ProductModel> Create(ProductModel entityDto)
65
65
return newMappedEntity ;
66
66
}
67
67
68
- public async Task Update ( ProductModel entityDto )
68
+ public async Task Update ( ProductModel productModel )
69
69
{
70
- ValidateProductIfNotExist ( entityDto ) ;
70
+ ValidateProductIfNotExist ( productModel ) ;
71
+
72
+ var editProduct = await _productRepository . GetByIdAsync ( productModel . Id ) ;
73
+ if ( editProduct == null )
74
+ throw new ApplicationException ( $ "Entity could not be loaded.") ;
71
75
72
- var mappedEntity = ObjectMapper . Mapper . Map < Product > ( entityDto ) ;
73
- if ( mappedEntity == null )
74
- throw new ApplicationException ( $ "Entity could not be mapped.") ;
76
+ ObjectMapper . Mapper . Map < ProductModel , Product > ( productModel , editProduct ) ;
75
77
76
- await _productRepository . UpdateAsync ( mappedEntity ) ;
78
+ await _productRepository . UpdateAsync ( editProduct ) ;
77
79
_logger . LogInformation ( $ "Entity successfully updated - AspnetRunAppService") ;
78
80
}
79
81
80
- public async Task Delete ( ProductModel entityDto )
82
+ public async Task Delete ( ProductModel productModel )
81
83
{
82
- ValidateProductIfNotExist ( entityDto ) ;
83
-
84
- var mappedEntity = ObjectMapper . Mapper . Map < Product > ( entityDto ) ;
85
- if ( mappedEntity == null )
86
- throw new ApplicationException ( $ "Entity could not be mapped.") ;
84
+ ValidateProductIfNotExist ( productModel ) ;
85
+ var deletedProduct = await _productRepository . GetByIdAsync ( productModel . Id ) ;
86
+ if ( deletedProduct == null )
87
+ throw new ApplicationException ( $ "Entity could not be loaded.") ;
87
88
88
- await _productRepository . DeleteAsync ( mappedEntity ) ;
89
+ await _productRepository . DeleteAsync ( deletedProduct ) ;
89
90
_logger . LogInformation ( $ "Entity successfully deleted - AspnetRunAppService") ;
90
91
}
91
92
92
- private async Task ValidateProductIfExist ( ProductModel entityDto )
93
+ private async Task ValidateProductIfExist ( ProductModel productModel )
93
94
{
94
- var existingEntity = await _productRepository . GetByIdAsync ( entityDto . Id ) ;
95
+ var existingEntity = await _productRepository . GetByIdAsync ( productModel . Id ) ;
95
96
if ( existingEntity != null )
96
- throw new ApplicationException ( $ "{ entityDto . ToString ( ) } with this id already exists") ;
97
+ throw new ApplicationException ( $ "{ productModel . ToString ( ) } with this id already exists") ;
97
98
}
98
99
99
- private void ValidateProductIfNotExist ( ProductModel entityDto )
100
+ private void ValidateProductIfNotExist ( ProductModel productModel )
100
101
{
101
- var existingEntity = _productRepository . GetByIdAsync ( entityDto . Id ) ;
102
+ var existingEntity = _productRepository . GetByIdAsync ( productModel . Id ) ;
102
103
if ( existingEntity == null )
103
- throw new ApplicationException ( $ "{ entityDto . ToString ( ) } with this id is not exists") ;
104
+ throw new ApplicationException ( $ "{ productModel . ToString ( ) } with this id is not exists") ;
104
105
}
105
106
}
106
107
}
0 commit comments