File tree 8 files changed +129
-8
lines changed
src/Ordering/Ordering.Application
8 files changed +129
-8
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ using Ordering . Application . Models ;
2
+ using System . Collections . Generic ;
3
+ using System . Threading . Tasks ;
4
+
5
+ namespace Ordering . Application . Interfaces
6
+ {
7
+ interface IOrderService
8
+ {
9
+ Task < OrderModel > CheckOut ( OrderModel order ) ;
10
+ Task < IEnumerable < OrderModel > > GetOrdersByUserName ( string userName ) ;
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ using AutoMapper ;
2
+ using System ;
3
+
4
+ namespace Ordering . Application . Mapper
5
+ {
6
+ // The best implementation of AutoMapper for class libraries -> https://www.abhith.net/blog/using-automapper-in-a-net-core-class-library/
7
+ public static class ObjectMapper
8
+ {
9
+ private static readonly Lazy < IMapper > Lazy = new Lazy < IMapper > ( ( ) =>
10
+ {
11
+ var config = new MapperConfiguration ( cfg =>
12
+ {
13
+ // This line ensures that internal properties are also mapped over.
14
+ cfg . ShouldMapProperty = p => p . GetMethod . IsPublic || p . GetMethod . IsAssembly ;
15
+ cfg . AddProfile < OrderMappingProfile > ( ) ;
16
+ } ) ;
17
+ var mapper = config . CreateMapper ( ) ;
18
+ return mapper ;
19
+ } ) ;
20
+ public static IMapper Mapper => Lazy . Value ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ using AutoMapper ;
2
+ using Ordering . Application . Models ;
3
+ using Ordering . Core . Entities ;
4
+
5
+ namespace Ordering . Application . Mapper
6
+ {
7
+ public class OrderMappingProfile : Profile
8
+ {
9
+ public OrderMappingProfile ( )
10
+ {
11
+ CreateMap < Order , OrderModel > ( ) . ReverseMap ( ) ;
12
+ }
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ namespace Ordering . Application . Models . Base
2
+ {
3
+ public class BaseModel
4
+ {
5
+ public int Id { get ; set ; }
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ using Ordering . Application . Models . Base ;
2
+
3
+ namespace Ordering . Application . Models
4
+ {
5
+ public class OrderModel : BaseModel
6
+ {
7
+ public string UserName { get ; set ; }
8
+ public decimal TotalPrice { get ; set ; }
9
+
10
+ // BillingAddress
11
+ public string FirstName { get ; set ; }
12
+ public string LastName { get ; set ; }
13
+ public string EmailAddress { get ; set ; }
14
+ public string AddressLine { get ; set ; }
15
+ public string Country { get ; set ; }
16
+ public string State { get ; set ; }
17
+ public string ZipCode { get ; set ; }
18
+
19
+ // Payment
20
+ public string CardName { get ; set ; }
21
+ public string CardNumber { get ; set ; }
22
+ public string Expiration { get ; set ; }
23
+ public string CVV { get ; set ; }
24
+ public int PaymentMethod { get ; set ; }
25
+ }
26
+ }
Original file line number Diff line number Diff line change 4
4
<TargetFramework >netcoreapp3.1</TargetFramework >
5
5
</PropertyGroup >
6
6
7
+ <ItemGroup >
8
+ <PackageReference Include =" AutoMapper" Version =" 9.0.0" />
9
+ </ItemGroup >
10
+
11
+ <ItemGroup >
12
+ <ProjectReference Include =" ..\Ordering.Core\Ordering.Core.csproj" />
13
+ </ItemGroup >
14
+
7
15
</Project >
Original file line number Diff line number Diff line change
1
+ using Ordering . Application . Interfaces ;
2
+ using Ordering . Application . Mapper ;
3
+ using Ordering . Application . Models ;
4
+ using Ordering . Core . Entities ;
5
+ using Ordering . Core . Repositories ;
6
+ using System ;
7
+ using System . Collections . Generic ;
8
+ using System . Threading . Tasks ;
9
+
10
+ namespace Ordering . Application . Services
11
+ {
12
+ public class OrderService : IOrderService
13
+ {
14
+ private readonly IOrderRepository _orderRepository ;
15
+
16
+ public OrderService ( IOrderRepository orderRepository )
17
+ {
18
+ _orderRepository = orderRepository ?? throw new ArgumentNullException ( nameof ( orderRepository ) ) ;
19
+ }
20
+
21
+ public async Task < OrderModel > CheckOut ( OrderModel orderModel )
22
+ {
23
+ var mappedEntity = ObjectMapper . Mapper . Map < Order > ( orderModel ) ;
24
+ if ( mappedEntity == null )
25
+ throw new ApplicationException ( $ "Entity could not be mapped.") ;
26
+
27
+ var newEntity = await _orderRepository . AddAsync ( mappedEntity ) ;
28
+
29
+ var newMappedEntity = ObjectMapper . Mapper . Map < OrderModel > ( newEntity ) ;
30
+ return newMappedEntity ;
31
+ }
32
+
33
+ public async Task < IEnumerable < OrderModel > > GetOrdersByUserName ( string userName )
34
+ {
35
+ var orderList = await _orderRepository . GetOrdersByUserName ( userName ) ;
36
+ var mapped = ObjectMapper . Mapper . Map < IEnumerable < OrderModel > > ( orderList ) ;
37
+ return mapped ;
38
+ }
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments