File tree Expand file tree Collapse file tree 15 files changed +244
-98
lines changed
Expand file tree Collapse file tree 15 files changed +244
-98
lines changed Original file line number Diff line number Diff line change 1+ namespace NorthwindCRUD . Helpers
2+ {
3+ public static class IdGenerator
4+ {
5+ public static string CreateLetterId ( int length )
6+ {
7+ var random = new Random ( ) ;
8+ const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
9+ return new string ( Enumerable . Repeat ( chars , length )
10+ . Select ( s => s [ random . Next ( s . Length ) ] ) . ToArray ( ) ) ;
11+ }
12+ public static int CreateDigitsId ( )
13+ {
14+ Random r = new Random ( ) ;
15+ return r . Next ( 10000000 , 99999999 ) ;
16+ }
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ namespace NorthwindCRUD . Helpers
2+ {
3+ using System . Reflection ;
4+
5+ public static class PropertyHelper < T >
6+ {
7+ public static void MakePropertiesEmptyIfNull ( T model )
8+ {
9+ if ( model != null )
10+ {
11+ foreach ( PropertyInfo prop in model ? . GetType ( ) ? . GetProperties ( ) )
12+ {
13+ var value = prop . GetValue ( model ) ;
14+ var type = prop . PropertyType ;
15+ if ( value == null && type == typeof ( string ) )
16+ {
17+ prop . SetValue ( model , "" ) ;
18+ }
19+ }
20+ }
21+ }
22+ }
23+ }
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ public class CustomerDb : ICustomer
1010 [ DatabaseGenerated ( DatabaseGeneratedOption . None ) ]
1111 public string CustomerId { get ; set ; }
1212
13- public string CompanyName { get ; set ; }
13+ public string ? CompanyName { get ; set ; }
1414
1515 public string ContactName { get ; set ; }
1616
Original file line number Diff line number Diff line change 55 public class AddressInputModel : IAddress
66 {
77 public string Street { get ; set ; }
8+
89 public string City { get ; set ; }
10+
911 public string Region { get ; set ; }
12+
1013 public string PostalCode { get ; set ; }
14+
1115 public string Country { get ; set ; }
16+
1217 public string Phone { get ; set ; }
1318 }
1419}
Original file line number Diff line number Diff line change @@ -5,13 +5,10 @@ namespace NorthwindCRUD.Models.InputModels
55{
66 public class CategoryInputModel : ICategory
77 {
8- [ Required ]
98 public int CategoryId { get ; set ; }
109
11- [ Required ]
1210 public string Description { get ; set ; }
1311
14- [ Required ]
1512 public string Name { get ; set ; }
1613 }
1714}
Original file line number Diff line number Diff line change 11namespace NorthwindCRUD . Models . InputModels
22{
33 using NorthwindCRUD . Models . Contracts ;
4- using NorthwindCRUD . Models . DbModels ;
54 using System . ComponentModel . DataAnnotations ;
65
76 public class CustomerInputModel : ICustomer
87 {
9- [ Required ]
108 public string CustomerId { get ; set ; }
119
1210 [ Required ]
1311 public string CompanyName { get ; set ; }
1412
15- [ Required ]
1613 public string ContactName { get ; set ; }
1714
18- [ Required ]
1915 public string ContactTitle { get ; set ; }
2016
21- [ Required ]
2217 public AddressInputModel Address { get ; set ; }
2318 }
2419}
Original file line number Diff line number Diff line change @@ -7,34 +7,24 @@ public class EmployeeInputModel : IEmployee
77 {
88 public int EmployeeId { get ; set ; }
99
10- [ Required ]
1110 public string LastName { get ; set ; }
1211
13- [ Required ]
1412 public string FirstName { get ; set ; }
1513
16- [ Required ]
1714 public string Title { get ; set ; }
1815
19- [ Required ]
2016 public string TitleOfCourtesy { get ; set ; }
2117
22- [ Required ]
2318 public string BirthDate { get ; set ; }
2419
25- [ Required ]
2620 public string HireDate { get ; set ; }
2721
28- [ Required ]
2922 public string AddressId { get ; set ; }
3023
31- [ Required ]
3224 public AddressInputModel Address { get ; set ; }
3325
34- [ Required ]
3526 public string Notes { get ; set ; }
3627
37- [ Required ]
3828 public string AvatarUrl { get ; set ; }
3929 }
4030}
Original file line number Diff line number Diff line change @@ -7,30 +7,22 @@ public class OrderInputModel : IOrder
77 {
88 public int OrderId { get ; set ; }
99
10- [ Required ]
1110 public string CustomerId { get ; set ; }
1211
13- [ Required ]
1412 public int EmployeeId { get ; set ; }
1513
16- [ Required ]
1714 public string OrderDate { get ; set ; }
1815
19- [ Required ]
2016 public string RequiredDate { get ; set ; }
2117
22- [ Required ]
2318 public int ShipVia { get ; set ; }
2419
25- [ Required ]
2620 public double Freight { get ; set ; }
2721
28- [ Required ]
2922 public string ShipName { get ; set ; }
3023
3124 public string ShipAddressId { get ; set ; }
3225
33- [ Required ]
3426 public AddressInputModel ShipAddress { get ; set ; }
3527 }
3628}
Original file line number Diff line number Diff line change 66using NorthwindCRUD . Services ;
77
88var builder = WebApplication . CreateBuilder ( args ) ;
9+ var MyAllowSpecificOrigins = "_myAllowSpecificOrigins" ;
910
1011builder . Logging . ClearProviders ( ) ;
1112builder . Logging . AddConsole ( ) ;
1213
13- builder . Services . AddControllers ( ) . AddNewtonsoftJson ( options =>
14- options . SerializerSettings . ReferenceLoopHandling = Newtonsoft . Json . ReferenceLoopHandling . Ignore
15- ) ; ;
14+ builder . Services . AddControllers ( options =>
15+ options . SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true )
16+ . AddNewtonsoftJson ( options =>
17+ options . SerializerSettings . ReferenceLoopHandling = Newtonsoft . Json . ReferenceLoopHandling . Ignore
18+ ) ;
19+
1620builder . Services . AddEndpointsApiExplorer ( ) ;
1721builder . Services . AddSwaggerGen ( ) ;
22+
23+ builder . Services . AddCors ( options =>
24+ {
25+ options . AddPolicy ( name : MyAllowSpecificOrigins ,
26+ policy =>
27+ {
28+ policy . WithOrigins ( "http://localhost:7244" , "http://localhost:4200" )
29+ . AllowAnyMethod ( )
30+ . AllowAnyHeader ( ) ;
31+
32+ policy . AllowAnyOrigin ( )
33+ . AllowAnyHeader ( )
34+ . AllowAnyMethod ( ) ;
35+ } ) ;
36+ } ) ;
37+
1838builder . Services . AddDbContext < DataContext > ( options =>
1939{
2040 options . UseSqlServer ( builder . Configuration . GetConnectionString ( "DBConnectionString" ) ) ;
4060
4161var app = builder . Build ( ) ;
4262
63+ app . UseCors ( MyAllowSpecificOrigins ) ;
64+
4365if ( app . Environment . IsDevelopment ( ) )
4466{
4567 app . UseSwagger ( ) ;
Original file line number Diff line number Diff line change 12721272 "country" : " Poland" ,
12731273 "phone" : " (26) 642-7012"
12741274 }
1275+ },
1276+ {
1277+ "customerID" : " KLLZA" ,
1278+ "companyName" : " Wolski Zajazd" ,
1279+ "contactName" : " Zbyszek Piestrzeniewicz" ,
1280+ "contactTitle" : " Owner" ,
1281+ "address" : {
1282+ "street" : " " ,
1283+ "city" : " " ,
1284+ "region" : " " ,
1285+ "postalCode" : " " ,
1286+ "country" : " " ,
1287+ "phone" : " "
1288+ }
12751289 }
12761290]
You can’t perform that action at this time.
0 commit comments