-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReadMe.txt
1111 lines (881 loc) · 36.4 KB
/
ReadMe.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1. "ECommerceApp" ismiyle Blank Solution açýlýr.
2. "ECommerceApp.DomainLayer" ismiyle Class Library (.Core) projesi eklenir.
Yüklenecek Paketler : -Microsoft.AspNetCore.Http.Features(5.0.4)
-Microsoft.EntityFrameworkCore(5.0.4)
-Microsoft.Extensions.Identity.Stores(5.0.4)
-Microsoft.VisualStudio.Web.CodeGeneration.Design(3.1.5)
2.1. "Enums" dosyasý açýlýr.
2.1.1. "Status" Class ý eklenir.
2.2. "Entities" dosyasý açýlýr.
2.2.1. Interface dosyasý açýlýr.
2.2.1.1. "IBaseEntity" Classý oluþturulur.
2.2.2. Concrete dosyasý açýlýr.
2.2.2.1. AppUser.cs
"IdentityUser<int>, IBaseEntity" class larýndan kalýtým alýnýr.
2.2.2.2. Product.cs
"BaseEntity<int>" clasýndan kalýtým alýnýr.
2.2.2.3. AppUserToProduct.cs
"BaseEntity<int>" clasýndan kalýtým alýnýr.
2.2.2.4. AppRole.cs
"IdentityRole<int>, IBaseEntity" class larýndan kalýtým alýnýr
2.2.2.5. Category.cs
"BaseEntity<int>" clasýndan kalýtým alýnýr.
2.2.2.6. BaseEntity.cs oluþturulur.
"IBaseEntity" class ýndan kalýtým alýnýr.
2.3. "Repository" dosyasý oluþturulur.
2.3.1. "BaseRepository" klasörü açýlýr.
2.3.1.1. "IBaseRepository" interface i oluþturulur ve repositoryler yazýlmaya baþlanýr.
public interface IBaseRepository<T> where T: IBaseEntity
{
Task<List<T>> Get(Expression<Func<T, bool>> expression);
Task<T> GetById(int id);
Task<T> FirstOrDefault(Expression<Func<T, bool>> expression);
Task Add(T entity);
void Update(T entity);
void Delete(T entity);
Task<TResult> GetFilteredFirstOrDefault<TResult>(Expression<Func<T, TResult>> selector,
Expression<Func<T, bool>> expression = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderby = null,
Func<IQueryable<T>, IIncludableQueryable<T, object>> inculude = null,
bool disableTracking = true);
Task<List<TResult>> GetFilteredList<TResult>(Expression<Func<T, TResult>> selector,
Expression<Func<T, bool>> expression = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderby = null,
Func<IQueryable<T>, IIncludableQueryable<T, object>> inculude = null,
bool disableTracking = true);
}
2.3.2. "EntityRepository" klasörü açýlýr.
2.3.2.1. "IAppUserRepository" interface i açýlýr.
"IBaseRepository" interface inden "AppUser" tipinde kalýtým alýnýr.
2.3.2.2. "ICategoryRepository" interface i açýlýr.
"IBaseRepository" interface inden "Category" tipinde kalýtým alýnýr.
2.3.2.3. "IProductRepository" interface i açýlýr.
"IBaseRepository" interface inden "Product" tipinde kalýtým alýnýr.
2.4. "UnitOfWork" dosyasý oluþturulur.
2.4.1. "IUnitOfWork" Dosyasý Oluþturulur.
public interface IUnitOfWork:IAsyncDisposable
{
IAppUserRepository AppUserRepository { get; }
ICategoryRepository CategoryRepository { get; }
IProductRepository ProductRepository { get; }
Task Commit();
Task ExecuteSqlRaw(string sql, params object[] parameters);
}
3. "ECommerceApp.InfastructureLayer" ismiyle Class Library (.Core) projesi eklenir.
Yüklenecek Paketler : -Microsoft.AspNetCore.Identity.EntityFrameworkCore(5.0.4)
-Microsoft.EntityFrameworkCore(5.0.4)
-Microsoft.EntityFrameworkCore.SqlServer(5.0.4)
-Microsoft.EntityFrameworkCore.Tools(5.0.4)
Referans Proje : -"ECommerceApp.DomainLayer"
3.1. "Mapping" klasör açýlýr.
3.1.1. "Abstract" klasörü açýlýr.
3.1.1.1. "BaseMap" class ý açýlýr.
public abstract class BaseMap<T> : IEntityTypeConfiguration<T> where T : class, IBaseEntity
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder.Property(x => x.CreateDate).IsRequired(true);
builder.Property(x => x.ModifiedDate).IsRequired(false);
builder.Property(x => x.DeleteDate).IsRequired(false);
builder.Property(x => x.Status).IsRequired(true);
}
}
3.1.2. "Concrete" klasörü açýlýr.
3.1.2.1. "AppRole" class ý açýlýr.
"BaseMap" abstract class ýndan "AppRole" tipinde kalýtým alýnýr.
3.1.2.2. "AppUserMap" class ý açýlýr.
"BaseMap" abstract class ýndan "AppUser" tipinde kalýtým alýnýr.
3.1.2.3. "AppUserToProductMap" class ý açýlýr.
"BaseMap" abstract class ýndan "AppUserToProduct" tipinde kalýtým alýnýr.
3.1.2.4. "ProductMap" class ý açýlýr.
"BaseMap" abstract class ýndan "Product" tipinde kalýtým alýnýr.
3.1.2.5. "CategoryMap" class ý açýlýr.
"BaseMap" abstract class ýndan "Category" tipinde kalýtým alýnýr.
3.2. "Repository" klasörü açýlýr.
3.2.1. "BaseRepository" folderý açýlýr
3.2.1.1. "BaseRepository" abstract class ý oluþturulur.
"IBaseRepository" interface inden T Type olarak kalýtým alýr. Ve interfacein içerisinde tanýmlanmýþ methodlar gövdelendirilir.
3.2.2. "EntityTypeRepositor" fodler ý açýlýr
3.2.2.1. "AppUserRepository" interface i açýlýr.
"BaseRepository"den "AppUser" tipinde ve IAppUserRepository dan kalýtým alýnýr.
Icerisine Db baðlantýsý tanýmlanýr.
public class AppUserRepository : BaseRepository<AppUser>, IAppUserRepository
{
public AppUserRepository(ApplicationDbContext applicationDbContext) : base(applicationDbContext) { }
}
3.2.2.2. "CategoryRepository" interface i açýlýr.
"BaseRepository"den "Category" tipinde ve ICategoryRepository den kalýtým alýnýr.
Icerisine Db baðlantýsý tanýmlanýr.
public class CategoryRepository:BaseRepository<Category>,ICategoryRepository
{
public CategoryRepository(ApplicationDbContext applicationDbContext) : base(applicationDbContext) { }
}
3.2.2.3. "ProductRepository" interface i açýlýr.
"BaseRepository"den "Product" tipinde ve IProductRepository den kalýtým alýnýr.
Icerisine Db baðlantýsý tanýmlanýr.
public class ProductRepository:BaseRepository<Product>,IProductRepository
{
public ProductRepository(ApplicationDbContext applicationDbContext) : base(applicationDbContext) { } // => Database baðlantýsý yapýldý
}
3.3 "UnitOfWork" klasörü açýlýr.
3.1. "UnitOfWork" classý açýlýr. UnitOfWork için gerekli iþlemler yapýlýr.
"IUnitOfWork" interface implement edilir.
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _db;
public UnitOfWork(ApplicationDbContext db)
{
this._db = db ?? throw new ArgumentNullException("Database Boþ Olamaz..!"); // => ?? Karar mekanizmasýný baþlattýk.Bu karar mekanizmasý ya bize db baðlantýsýný dönecek ya da ArgumentNullException ile hata mesajý verecek.
}
private IAppUserRepository _appUserRepository;
public IAppUserRepository AppUserRepository
{
get
{
if (_appUserRepository == null) _appUserRepository = new AppUserRepository(_db); // =Z _appUserRepository boþ gelirse databse baðlantýsýný üret.
return _appUserRepository; // => eðer dolu gelirse _appUserRepository döndür .
}
}
private ICategoryRepository _categoryRepository;
public ICategoryRepository CategoryRepository
{
get
{
if (_categoryRepository == null) _categoryRepository = new CategoryRepository(_db);
return _categoryRepository;
}
}
private IProductRepository _productRepository;
public IProductRepository ProductRepository
{
get
{
if (_productRepository == null) _productRepository = new ProductRepository(_db);
return _productRepository;
}
}
public async Task Commit() => await _db.SaveChangesAsync();
private bool isDisposing = false;
public async ValueTask DisposeAsync()
{
if (!isDisposing)
{
isDisposing = true;
await DisposeAsync(true);
GC.SuppressFinalize(this); // => Nesnemizi tamamýyla temizlenmesini saðlayacak.
}
}
private async Task DisposeAsync(bool disposing)
{
if (disposing) await _db.DisposeAsync(); // => Üretilen db nesnemizi dispose ettik.
}
public Task ExecuteSqlRaw(string sql, params object[] parameters)// => Bu method içerisine sql sorgusu alacak execute edececek ve paramters gönderecek
{
throw new NotImplementedException();
}
}
3.3. "Context" folderý açýlýr.
3.3.1. "ApplicationDbContext.cs" classý oluþturulur. Db baðlantýsý için gerekli iþlemler yapýlýr.
public class ApplicationDbContext : IdentityDbContext<AppUser,AppRole,int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<AppUserToProduct> AppUserToProducts { get; set; }
public DbSet<AppRole> AppRoles { get; set; }
public DbSet<AppUser> AppUsers { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new AppUserMap());
builder.ApplyConfiguration(new AppRoleMap());
builder.ApplyConfiguration(new AppUserToProductMap());
builder.ApplyConfiguration(new CategoryMap());
builder.ApplyConfiguration(new ProductMap());
base.OnModelCreating(builder);
}
}
4. "ECommerceApp.ApplicationLayer" ismiyle Class Library (.Core) projesi eklenir.
Yüklenecek Paketler : -Autofac(6.1.0)
-AutoMapper(10.1.1)
-AutoMapper.Extensions.Microsoft.DependencyInjection(8.1.1)
-FluentValidation.AspNetCore(9.5.3)
-Microsoft.AspNetCore.Identity(2.2.0)
-SixLabors.ImageSharp(1.0.3)
Referans Proje : -"ECommerceApp.DomainLayer"
-"ECommerceApp.InfrastructureLayer"
4.1. "Model" Folderý açýlýr.
4.1.1. DTOs Fodlerý açýlýr. Iþ odaklý olarak kullaným ihtiyacýna göre DTO lar oluþtuýrulur.
4.2. "Mapper" Folderý açýlýr.
4.2.1. "Mapping" class ý açýlýr. ve DTO larýn oluþturulduðu varlýklarý ile maplama iþlemi yapýlýr. AutoMApper Paketinden yararlanmak için "Profile" dan kalýtým alýnýr.
public class Mapping : Profile
{
public Mapping()
{
CreateMap<AppUser, EditProfileDTO>().ReverseMap();
CreateMap<AppUser, LoginDTO>().ReverseMap();
CreateMap<AppUser, ProfileDTO>().ReverseMap();
CreateMap<AppUser, RegisterDTO>().ReverseMap();
CreateMap<Category, CategoryDTO>().ReverseMap();
CreateMap<CategoryDTO, Category>().ReverseMap();
CreateMap<Product, ProductDTO>().ReverseMap();
CreateMap<ProductDTO, Product>().ReverseMap();
}
}
4.3. "Services" Folderý açýlýr.
4.3.1. "Interface" Folderý açýlýr.
4.3.1.1. "IAppUserService" interface i açýlýr ve içine "AppUserService" de gövdelendirilecek olan ve "AppUser" ile ilgili kullanýlmak istenilen Methodlar tanýmlanýr.
public interface IAppUserService
{
Task DeleteUser(int id);
Task EditUser(EditProfileDTO editProfileDTO);
Task<IdentityResult> Register(RegisterDTO registerDto);
Task<SignInResult> LogIn(LoginDTO loginDTO);
Task LogOut();
Task<int> GetUserIdFromName(string userName);
Task<EditProfileDTO> GetById(int id);
Task<LoginDTO> GetLoginById(int id);
Task<ProfileDTO> GetByUserName(string userName);
}
4.3.1.2. "ICategoryService" interface i açýlýr ve içine "CategoryService" de gövdelendirilecek olan ve "Category" ile ilgili kullanýlmak istenilen Methodlar tanýmlanýr.
public interface ICategoryService
{
Task Create(CategoryDTO categoryDTO);
Task Delete(CategoryDTO categoryDTO);
Task Update(CategoryDTO categoryDTO);
Task<CategoryDTO> GetById(int id);
Task<CategoryDTO> GetCategoryName(CategoryDTO categoryDTO);
Task<List<Category>> CategoryList();
}
4.3.1.3. "IProductService" interface i açýlýr ve içine "ProductService" de gövdelendirilecek olan ve "Product" ile ilgili kullanýlmak istenilen Methodlar tanýmlanýr.
public interface IProductService
{
Task Create(ProductDTO productDTO);
Task Update(ProductDTO productDTO);
Task<ProductDTO> GetById(int id);
Task<List<Product>> GetAll();
Task<List<Product>> GetOrderByList();
Task<List<Category>> GetCategory();
Task<List<Product>> GetList(int id);
}
4.3.2. "Concrete" Folderý açýlýr.
4.3.2.1. "AppUserService" class ý açýlýr. "IAppUserService" interface i implement edilir. ve Methodlar gövdelendirilir.
public class AppUserService : IAppUserService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly SignInManager<AppUser> _signInManager;
private readonly UserManager<AppUser> _userManager;
public AppUserService(IUnitOfWork unitOfWork,
IMapper mapper,
SignInManager<AppUser> signInManager,
UserManager<AppUser> userManager)
{
this._unitOfWork = unitOfWork;
this._mapper = mapper;
this._signInManager = signInManager;
this._userManager = userManager;
}
public async Task DeleteUser(int id)
{
AppUser user = await _unitOfWork.AppUserRepository.GetById(id);
_unitOfWork.AppUserRepository.Delete(user);
}
public async Task EditUser(EditProfileDTO editProfileDTO)
{
var user = await _unitOfWork.AppUserRepository.GetById(editProfileDTO.Id);
if (user != null)
{
if (editProfileDTO.Image != null)
{
using var image = Image.Load(editProfileDTO.Image.OpenReadStream());
//image.Mutate(x => x.Resize(256, 256));
image.Save("wwwroot/images/users/" + user.UserName + ".jpg");
user.ImagePath = ("/images/users/" + user.UserName + ".jpg");
_unitOfWork.AppUserRepository.Update(user);
await _unitOfWork.Commit();
}
if (editProfileDTO.UserName != user.UserName)
{
var newUserName = await _userManager.FindByNameAsync(editProfileDTO.UserName);
if (newUserName == null)
{
await _userManager.SetUserNameAsync(user, editProfileDTO.UserName);
//user.UserName = editProfileDTO.UserName;
await _signInManager.SignInAsync(user, isPersistent: true);
}
}
if (editProfileDTO.FullName != user.FullName)
{
user.FullName = editProfileDTO.FullName;
}
if (editProfileDTO.Email != user.Email)
{
var isnewEmail = await _userManager.FindByEmailAsync(editProfileDTO.Email);
if (isnewEmail == null)
{
await _userManager.SetEmailAsync(user, editProfileDTO.Email);
}
}
if (editProfileDTO.Password != null)
{
user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, editProfileDTO.Password);
}
_unitOfWork.AppUserRepository.Update(user);
await _unitOfWork.Commit();
}
}
public async Task<EditProfileDTO> GetById(int id)
{
var user = await _unitOfWork.AppUserRepository.GetById(id);
return _mapper.Map<EditProfileDTO>(user);
}
public async Task<LoginDTO> GetLoginById(int id)
{
var user = await _unitOfWork.AppUserRepository.GetById(id);
return _mapper.Map<LoginDTO>(user);
}
public async Task<ProfileDTO> GetByUserName(string userName)
{
var user = await _unitOfWork.AppUserRepository.GetFilteredFirstOrDefault(
selector: x => new ProfileDTO {
ImagePath = x.ImagePath,
UserName = x.UserName
},
expression: y=>y.UserName==userName
);
return user;
}
public async Task<int> GetUserIdFromName(string userName)
{
var user = await _unitOfWork.AppUserRepository.GetFilteredFirstOrDefault(
selector: x => x.Id,
expression: x => x.UserName == userName);
return Convert.ToInt32(user); // NOT => Burada int tipinde id dönmesi gerekiyor kontrol
}
public async Task<SignInResult> LogIn(LoginDTO loginDTO)
{
var user = await _signInManager.PasswordSignInAsync(loginDTO.UserName, loginDTO.Password, loginDTO.RememberMe, false);
return user;
}
public async Task LogOut()
{
await _signInManager.SignOutAsync();
}
public async Task<IdentityResult> Register(RegisterDTO registerDTO)
{
var user = _mapper.Map<AppUser>(registerDTO);
var result = await _userManager.CreateAsync(user, registerDTO.ConfirmPassword);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false); //isPersistent => Tarayýcý kullanýcý giriþ bilgilerini hafýza da tutmsun mu diye sorar.
}
return result;
}
}
4.3.2.2. "CategoryService" class ý açýlýr. "ICategoryService" interface i implement edilir. ve Methodlar gövdelendirilir.
public class CategoryService : ICategoryService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
public CategoryService(IUnitOfWork unitOfWork,
IMapper mapper
)
{
this._unitOfWork = unitOfWork;
this._mapper = mapper;
}
public async Task<List<Category>> CategoryList()
{
List<Category> categories = await _unitOfWork.CategoryRepository.GetFilteredList(
selector: x => new Category
{
Id = x.Id,
CategoryName = x.CategoryName,
},
expression: y => y.Status != DomainLayer.Enums.Status.Passive);
//List<Category> categories = await _unitOfWork.CategoryRepository.Get(x => x.Status != Status.Passive);
return categories;
}
public async Task Create(CategoryDTO categoryDTO)
{
var category = await _unitOfWork.CategoryRepository.FirstOrDefault(x => x.CategoryName == categoryDTO.CategoryName); // eðer böyle bir category yoksa ekle
if (category==null)
{
var newCategory = _mapper.Map<CategoryDTO, Category>(categoryDTO);
await _unitOfWork.CategoryRepository.Add(newCategory);
await _unitOfWork.Commit();
}
}
public async Task Delete(CategoryDTO categoryDTO)
{
var category = await _unitOfWork.CategoryRepository.GetById(categoryDTO.Id);
if (category != null)
{
_unitOfWork.CategoryRepository.Delete(category);
await _unitOfWork.Commit();
}
}
public async Task<CategoryDTO> GetById(int id)
{
var category = await _unitOfWork.CategoryRepository.GetById(id);
return _mapper.Map<CategoryDTO>(category);
}
public async Task<CategoryDTO> GetCategoryName(CategoryDTO categoryDTO)
{
var category = await _unitOfWork.CategoryRepository.GetFilteredFirstOrDefault(
selector: y => new CategoryDTO
{
CategoryName = categoryDTO.CategoryName
},
expression: x => Convert.ToInt32(x.Id) == categoryDTO.Id
);
return category;
}
public async Task Update(CategoryDTO categoryDTO)
{
var category = await _unitOfWork.CategoryRepository.FirstOrDefault(x => x.CategoryName == categoryDTO.CategoryName);
if (category!=null)
{
category.CategoryName = categoryDTO.CategoryName;
}
_unitOfWork.CategoryRepository.Update(category);
await _unitOfWork.Commit();
}
}
4.3.2.2. "ProductService" class ý açýlýr. "IProductService" interface i implement edilir. ve Methodlar gövdelendirilir.
public class ProductService : IProductService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly IWebHostEnvironment _webHostEnvironment;
public ProductService(IUnitOfWork unitOfWork,
IMapper mapper,
IWebHostEnvironment webHostEnvironment)
{
this._unitOfWork = unitOfWork;
this._mapper = mapper;
this._webHostEnvironment = webHostEnvironment;
}
public async Task Create(ProductDTO productDTO)
{
if (productDTO != null)
{
string imageName = "noimage.png";
if (productDTO.Image != null)
{
string uploadDir = Path.Combine(_webHostEnvironment.WebRootPath, "images/product");
string newName = productDTO.Description.Trim().Replace(" ", string.Empty).Substring(0, 7);
imageName = newName + "_" + productDTO.Image.FileName;
string filePath = Path.Combine(uploadDir, imageName);
FileStream fileStream = new FileStream(filePath, FileMode.Create);
await productDTO.Image.CopyToAsync(fileStream);
fileStream.Close();
}
productDTO.ImagePath = imageName;
Product product = _mapper.Map<ProductDTO, Product>(productDTO);
await _unitOfWork.ProductRepository.Add(product);
await _unitOfWork.Commit();
}
}
public async Task<List<Product>> GetAll()
{
List<Product> products = await _unitOfWork.ProductRepository.Get(x => x.Status != Status.Passive);
return products;
}
public async Task<ProductDTO> GetById(int id)
{
var product = await _unitOfWork.ProductRepository.GetById(id);
return _mapper.Map<ProductDTO>(product);
}
public async Task<List<Product>> GetList(int id)
{
var category = await _unitOfWork.CategoryRepository.GetById(id);
List<Product> products = await _unitOfWork.ProductRepository.Get(x => x.CategoryId == category.Id);
return products;
}
public async Task<List<Product>> GetOrderByList()
{
var productList = await _unitOfWork.ProductRepository.GetFilteredList(
selector: x => new Product
{
ProductName = x.ProductName,
Description = x.Description,
DescText = x.DescText,
Image = x.Image,
ImagePath = x.ImagePath,
UnitPrice = x.UnitPrice
},
orderby: x => x.OrderByDescending(x => x.CreateDate)
);
return productList;
}
public async Task<List<Category>> GetCategory()
{
var categoryList = await _unitOfWork.CategoryRepository.Get(x => x.Status != Status.Passive);
return categoryList;
}
public async Task Update(ProductDTO productDTO)
{
var products = await _unitOfWork.ProductRepository.FirstOrDefault(x => x.Id == productDTO.Id);
if (productDTO != null)
{
if (productDTO.Image != null)
{
string uploadDir = Path.Combine(_webHostEnvironment.WebRootPath, "images/product");
if (!string.Equals(products.Image, "noimage.png"))
{
string oldPath = Path.Combine(uploadDir, products.ImagePath);
if (File.Exists(oldPath))
{
File.Delete(oldPath);
}
}
string imageName = productDTO.ProductName + "_" + productDTO.Image.FileName;
string filePath = Path.Combine(uploadDir, imageName);
FileStream fileStream = new FileStream(filePath, FileMode.Create);
await productDTO.Image.CopyToAsync(fileStream);
fileStream.Close();
products.ImagePath = imageName;
}
if (productDTO.Description != null)
{
products.Description = productDTO.Description;
}
_unitOfWork.ProductRepository.Update(products);
await _unitOfWork.Commit();
}
}
}
4.4. "IOC" folderý açýlýr. Third PArt IOC container kullanmak istediðim için normalde Startup classýnda yapýlmasý gereken IOC ocntainer iþlemini burada "AutoFact Container ile gerçekleþtiricem.
4.4.1. "AutoFactContainer" classý açýlýr.
public class AutoFactContainer:Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<AppUserService>().As<IAppUserService>().InstancePerLifetimeScope();
builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerLifetimeScope();
builder.RegisterType<ProductService>().As<IProductService>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
}
}
5. "ECommerceApp.PresentationLayer" ismiyle ASP.NET Core Web App(Model-View-Controller) projesi eklenir.
Yüklenecek Paketler : -Autofac.Extensions.DependencyInjection(7.1.0)
-Microsoft.AspNetCore.Identity.EntityFrameworkCore(5.0.4)
-Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation(3.1.0)
-Microsoft.EntityFrameworkCore(5.0.4)
-Microsoft.EntityFrameworkCore.Design(5.0.4)
-Microsoft.EntityFrameworkCore.SqlServer(5.0.4)
-Microsoft.VisualStudio.Web.CodeGeneration.Design(3.1.5)
Referans Proje : -"ECommerceApp.ApplicationLayer"
-"ECommerceApp.InfrastructureLayer"
5.1. "Startup.cs" class ýnýn içerisi düzenlenir.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddHttpClient(); //buna bak
services.AddMemoryCache(); //buda
services.AddSession(); //xx
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddAutoMapper(typeof(Mapping));
services.AddIdentity<AppUser, AppRole>(
x =>
{
x.SignIn.RequireConfirmedAccount = false;
x.SignIn.RequireConfirmedEmail = false;
x.SignIn.RequireConfirmedPhoneNumber = false;
x.User.RequireUniqueEmail = false;
x.Password.RequiredLength = 3; // => password e girilen karakterin minimum 3 olmasýný saðladýk. Varsayýlan deðer 6 dýr.
x.Password.RequiredUniqueChars = 0;
x.Password.RequireLowercase = false; // =>özelliði; þifre içerisinde en az 1 adet küçük harf zorunluluðu olmasý özelliðini false yaptýk.
x.Password.RequireUppercase = false; // => özelliði; þifre içerisinde en az 1 adet büyük harf zorunluluðu olmasýný false yaptýk.
x.Password.RequireNonAlphanumeric = false; // => özelliði; þifre içerisinde en az 1 adet alfanümerik karakter zorunluluðu olmasý özelliði false.
})
.AddEntityFrameworkStores<ApplicationDbContext>() // => AddEntityFrameworkStores<ApplicationDbContext>() metodu da; dahil ettiðimiz Identity ara katmanýndaki kullanýcý bilgilerini yönetirken hangi DbContext sýnýfýnýn kullanýlmasý gerektiðini belirtmektedir
.AddDefaultTokenProviders();
services.AddScoped<AppRole>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthentication(); // metodu; web uygulamamýzýn, eklediðimiz Identity ara katmanýný yetkilendirme için kullanmasýný saðlamaktadýr.
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
5.2 "appsetting.json" içerisine Db için gerekli iþlem yazýlýr.
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=RIDVANORUN;Database=ECommerceApp;Trusted_Connection=True;MultipleActiveResultSets=True;"
}
5.3. "Program.cs" içerisine third part ýoc container için gerkli iþlem yazýlýr.
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterModule(new AutoFactContainer());
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
5.4. "Migration" iþlemi yapýlýr. Database Güncellenir.
5.5. Controllers Folderý içerise Controllerlar eklenir ve içleri düzenlenir.
5.5.1. AccountController.cs
public class AccountController : Controller
{
private readonly IAppUserService _appUser;
public AccountController(IAppUserService appUser)
{
_appUser = appUser;
}
#region Register
public IActionResult Register()
{
if (User.Identity.IsAuthenticated) return RedirectToAction("Index", "Home");
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterDTO model)
{
if (ModelState.IsValid)
{
var result = await _appUser.Register(model);
if (result.Succeeded)
return RedirectToAction("Index", "Home");
foreach (var item in result.Errors) ModelState.AddModelError(string.Empty, item.Description);
}
return View(model);
}
#endregion
#region LogIn
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginDTO model)
{
if (model!=null)
{
if (ModelState.IsValid)
{
var userId = await _appUser.GetUserIdFromName(model.UserName);
var user = await _appUser.GetLoginById(userId);
var result = await _appUser.LogIn(model);
if (result.Succeeded)
{
return RedirectToAction(nameof(HomeController.Index), "Home"); // Eðer giriþ baþarýlý olursa HomeController'daki Home Index'a yönlendir.
}
ModelState.AddModelError(String.Empty, "Geçersiz giriþ denemesi..!");
}
return View();
}
return View();
}
#endregion
#region LogOut
[HttpPost]
public async Task<IActionResult> LogOut()
{
await _appUser.LogOut();
return RedirectToAction(nameof(HomeController.Index), "Home");
}
#endregion
public async Task<IActionResult> EditProfile(string userName)
{
if (userName == User.Identity.Name)
{
var user = await _appUser.GetById(User.GetUserId());
if (user == null) return NotFound();
return View(user);
}
else
return RedirectToAction(nameof(HomeController.Index), "Home");
}
[HttpPost]
public async Task<IActionResult> EditProfile(EditProfileDTO model)
{
await _appUser.EditUser(model);
return RedirectToAction(nameof(HomeController.Index), "Home");
}
public async Task<IActionResult> Details (ProfileDTO model)
{
var user = await _appUser.GetByUserName(model.UserName);
return View(user);
}
}
5.5.2. CartController.cs
public class CartController : Controller
{
private readonly ApplicationDbContext _applicationDbContext;
public CartController(ApplicationDbContext applicationDbContext) => _applicationDbContext = applicationDbContext;
public IActionResult Index()
{
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart") ?? new List<CartItem>();
CartViewModel cartViewModel = new CartViewModel
{
CartItems = cart,
GrandTotal = cart.Sum(x => x.Price * x.Quantity)
};
return View(cartViewModel);
}
public async Task<IActionResult> Add(int id)
{
Product product = await _applicationDbContext.Products.FindAsync(id);
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart") ?? new List<CartItem>();//çart bilgilerini session'da tutma karararý almýþtýk. Mantýksýz bir hareket deðildir. Çoðu e-ticaret sitesi bu yaklaþýmý kullanmaktadýr (günümüz için check edin). Bunun için uygulam içerisinde bulunan varlýklarý "json" dönüþtürmmemiz gerekmekteydi bunun için extension method yazdýk. burada bu eztension methodun Deserialize Object kýsmýný kullandýk. Neden? Çünkü Session üzerinden alýyorum cart'ýn bilgilerini.
CartItem cartItem = cart.Where(x => x.ProductId == id).FirstOrDefault();
if (cartItem == null)
{
cart.Add(new CartItem(product));
// sepete eklenmek istenilen ürün yok ise yani ürün sepete ilk kez eklenecekse
}
else
{
cartItem.Quantity += 1;//sepette var ise increase ediyoruz
}
HttpContext.Session.SetJson("Cart", cart);
if (HttpContext.Request.Headers["X-Request-With"] != "XMLHttpRequest")
{
return RedirectToAction("Index", "Product");//"actionName, controllerName"
}
return RedirectToAction("Index"/*, "Product"*/);
}
public IActionResult Decrease(int id)
{
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart") ?? new List<CartItem>();
CartItem cartItem = cart.Where(x => x.ProductId == id).FirstOrDefault();
if (cartItem.Quantity > 1)
--cartItem.Quantity;
else
cart.RemoveAll(x => x.ProductId == id);
if (cart.Count == 0)
HttpContext.Session.Remove("Cart");
else
HttpContext.Session.SetJson("Cart", cart);
return RedirectToAction("Index");
}
public IActionResult Remove(int id)
{
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart");
cart.RemoveAll(x => x.ProductId == id);
if (cart.Count == 0)