From 64cc3ba659dd60a0b371c2a8f4bb5320c10328c7 Mon Sep 17 00:00:00 2001 From: BEren Date: Tue, 9 Mar 2021 22:01:15 +0300 Subject: [PATCH 1/3] =?UTF-8?q?T=C3=BCm=20operasyonlara=20Async=20eklendi.?= =?UTF-8?q?=20Interceptor=20async=20=C3=A7al=C4=B1=C5=9Fabilmesi=20i=C3=A7?= =?UTF-8?q?in=20Castle.Core.AsyncInterceptor=20eklendi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Business/Abstract/IAuthService.cs | 9 +-- Business/Abstract/ICategoryService.cs | 5 +- Business/Abstract/IProductService.cs | 19 ++++--- Business/Abstract/IUserService.cs | 7 ++- Business/Concrete/AuthManager.cs | 17 +++--- Business/Concrete/CategoryManager.cs | 9 +-- Business/Concrete/ProductManager.cs | 57 ++++++++++--------- Business/Concrete/UserManager.cs | 13 +++-- Core/Aspects/Autofac/Caching/CacheAspect.cs | 2 +- Core/Core.csproj | 1 + .../EntityFramework/EfEntityRepositoryBase.cs | 23 ++++---- Core/DataAccess/IEntityRepository.cs | 11 ++-- .../Interceptors/AspectInterceptorSelector.cs | 2 +- .../Interceptors/MethodInterception.cs | 17 +++++- .../MethodInterceptionBaseAttribute.cs | 16 +++++- DataAccess/Abstract/IProductDal.cs | 3 +- DataAccess/Abstract/IUserDal.cs | 3 +- .../Concrete/EntityFramework/EfProductDal.cs | 5 +- .../Concrete/EntityFramework/EfUserDal.cs | 6 +- .../Concrete/InMemory/InMemoryProductDal.cs | 22 ++++--- WebAPI/Controllers/AuthController.cs | 14 ++--- WebAPI/Controllers/ProductsController.cs | 13 +++-- 22 files changed, 163 insertions(+), 111 deletions(-) diff --git a/Business/Abstract/IAuthService.cs b/Business/Abstract/IAuthService.cs index ab9d40f..da90088 100644 --- a/Business/Abstract/IAuthService.cs +++ b/Business/Abstract/IAuthService.cs @@ -4,15 +4,16 @@ using Entities.DTOs; using System; using System.Text; +using System.Threading.Tasks; namespace Business.Abstract { public interface IAuthService { - IDataResult Register(UserForRegisterDto userForRegisterDto, string password); - IDataResult Login(UserForLoginDto userForLoginDto); - IResult UserExists(string email); - IDataResult CreateAccessToken(User user); + Task> RegisterAsync(UserForRegisterDto userForRegisterDto, string password); + Task> LoginAsync(UserForLoginDto userForLoginDto); + Task UserExistsAsync(string email); + Task> CreateAccessTokenAsync(User user); } } diff --git a/Business/Abstract/ICategoryService.cs b/Business/Abstract/ICategoryService.cs index c576da8..0fbb132 100644 --- a/Business/Abstract/ICategoryService.cs +++ b/Business/Abstract/ICategoryService.cs @@ -3,12 +3,13 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace Business.Abstract { public interface ICategoryService { - IDataResult> GetAll(); - IDataResult GetById(int categoryId); + Task>> GetAllAsync(); + Task> GetByIdAsync(int categoryId); } } diff --git a/Business/Abstract/IProductService.cs b/Business/Abstract/IProductService.cs index 19924bf..d1b8215 100644 --- a/Business/Abstract/IProductService.cs +++ b/Business/Abstract/IProductService.cs @@ -4,20 +4,21 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace Business.Abstract { public interface IProductService { - IDataResult> GetAll(); - IDataResult> GetAllByCategoryId(int id); - IDataResult> GetByUnitPrice(decimal min, decimal max); - IDataResult> GetProductDetails(); - IDataResult GetById(int productId); - IResult Add(Product product); - IResult Update(Product product); - - IResult AddTransactionalTest(Product product); + Task>> GetAllAsync(); + Task>> GetAllByCategoryIdAsync(int id); + Task>> GetByUnitPriceAsync(decimal min, decimal max); + Task>> GetProductDetailsAsync(); + Task> GetByIdAsync(int productId); + Task AddAsync(Product product); + Task UpdateAsync(Product product); + + Task AddTransactionalTestAsync(Product product); //RESTFUL --> HTTP --> } diff --git a/Business/Abstract/IUserService.cs b/Business/Abstract/IUserService.cs index ad1983d..b8db61a 100644 --- a/Business/Abstract/IUserService.cs +++ b/Business/Abstract/IUserService.cs @@ -1,12 +1,13 @@ using Core.Entities.Concrete; using System.Collections.Generic; +using System.Threading.Tasks; namespace Business.Abstract { public interface IUserService { - List GetClaims(User user); - void Add(User user); - User GetByMail(string email); + Task> GetClaimsAsync(User user); + Task AddAsync(User user); + Task GetByMailAsync(string email); } } diff --git a/Business/Concrete/AuthManager.cs b/Business/Concrete/AuthManager.cs index c45ca3a..a42c4cf 100644 --- a/Business/Concrete/AuthManager.cs +++ b/Business/Concrete/AuthManager.cs @@ -4,6 +4,7 @@ using Core.Utilities.Security.Hashing; using Core.Utilities.Security.JWT; using Entities.DTOs; +using System.Threading.Tasks; namespace Business.Concrete { @@ -18,7 +19,7 @@ public AuthManager(IUserService userService, ITokenHelper tokenHelper) _tokenHelper = tokenHelper; } - public IDataResult Register(UserForRegisterDto userForRegisterDto, string password) + public async Task> RegisterAsync(UserForRegisterDto userForRegisterDto, string password) { byte[] passwordHash, passwordSalt; HashingHelper.CreatePasswordHash(password, out passwordHash, out passwordSalt); @@ -31,13 +32,13 @@ public IDataResult Register(UserForRegisterDto userForRegisterDto, string PasswordSalt = passwordSalt, Status = true }; - _userService.Add(user); + await _userService.AddAsync(user); return new SuccessDataResult(user, "Kayıt oldu"); } - public IDataResult Login(UserForLoginDto userForLoginDto) + public async Task> LoginAsync(UserForLoginDto userForLoginDto) { - var userToCheck = _userService.GetByMail(userForLoginDto.Email); + var userToCheck = await _userService.GetByMailAsync(userForLoginDto.Email); if (userToCheck == null) { return new ErrorDataResult("Kullanıcı bulunamadı"); @@ -51,18 +52,18 @@ public IDataResult Login(UserForLoginDto userForLoginDto) return new SuccessDataResult(userToCheck, "Başarılı giriş"); } - public IResult UserExists(string email) + public async Task UserExistsAsync(string email) { - if (_userService.GetByMail(email) != null) + if (await _userService.GetByMailAsync(email) != null) { return new ErrorResult("Kullanıcı mevcut"); } return new SuccessResult(); } - public IDataResult CreateAccessToken(User user) + public async Task> CreateAccessTokenAsync(User user) { - var claims = _userService.GetClaims(user); + var claims =await _userService.GetClaimsAsync(user); var accessToken = _tokenHelper.CreateToken(user, claims); return new SuccessDataResult(accessToken, "Token oluşturuldu"); } diff --git a/Business/Concrete/CategoryManager.cs b/Business/Concrete/CategoryManager.cs index dd4b326..861bceb 100644 --- a/Business/Concrete/CategoryManager.cs +++ b/Business/Concrete/CategoryManager.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace Business.Concrete { @@ -17,16 +18,16 @@ public CategoryManager(ICategoryDal categoryDal) _categoryDal = categoryDal; } - public IDataResult> GetAll() + public async Task>> GetAllAsync() { //İş kodları - return new SuccessDataResult>(_categoryDal.GetAll()); + return new SuccessDataResult>(await _categoryDal.GetAllAsync()); } //Select * from Categories where CategoryId = 3 - public IDataResult GetById(int categoryId) + public async Task> GetByIdAsync(int categoryId) { - return new SuccessDataResult(_categoryDal.Get(c=>c.CategoryId == categoryId)); + return new SuccessDataResult(await _categoryDal.GetAsync(c=>c.CategoryId == categoryId)); } } } diff --git a/Business/Concrete/ProductManager.cs b/Business/Concrete/ProductManager.cs index 8172669..303c73d 100644 --- a/Business/Concrete/ProductManager.cs +++ b/Business/Concrete/ProductManager.cs @@ -18,6 +18,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace Business.Concrete { @@ -37,20 +38,20 @@ public ProductManager(IProductDal productDal,ICategoryService categoryService) [SecuredOperation("product.add,admin")] [ValidationAspect(typeof(ProductValidator))] [CacheRemoveAspect("IProductService.Get")] - public IResult Add(Product product) + public async Task AddAsync(Product product) { //Aynı isimde ürün eklenemez //Eğer mevcut kategori sayısı 15'i geçtiyse sisteme yeni ürün eklenemez. ve - IResult result = BusinessRules.Run(CheckIfProductNameExists(product.ProductName), - CheckIfProductCountOfCategoryCorrect(product.CategoryId), CheckIfCategoryLimitExceded()); + IResult result = BusinessRules.Run(await CheckIfProductNameExistsAsync(product.ProductName), + await CheckIfProductCountOfCategoryCorrectAsync(product.CategoryId),await CheckIfCategoryLimitExcededAsync()); if (result != null) { return result; } - _productDal.Add(product); + await _productDal.AddAsync(product); return new SuccessResult(Messages.ProductAdded); @@ -58,78 +59,78 @@ public IResult Add(Product product) [CacheAspect] //key,value - public IDataResult> GetAll() + public async Task>> GetAllAsync() { if (DateTime.Now.Hour == 1) { return new ErrorDataResult>(Messages.MaintenanceTime); } - return new SuccessDataResult>(_productDal.GetAll(), Messages.ProductsListed); + return new SuccessDataResult>(await _productDal.GetAllAsync(), Messages.ProductsListed); } - public IDataResult> GetAllByCategoryId(int id) + public async Task>> GetAllByCategoryIdAsync(int id) { - return new SuccessDataResult>(_productDal.GetAll(p => p.CategoryId == id)); + return new SuccessDataResult>(await _productDal.GetAllAsync(p => p.CategoryId == id)); } [CacheAspect] //[PerformanceAspect(5)] - public IDataResult GetById(int productId) + public async Task> GetByIdAsync(int productId) { - return new SuccessDataResult(_productDal.Get(p => p.ProductId == productId)); + return new SuccessDataResult(await _productDal.GetAsync(p => p.ProductId == productId)); } - public IDataResult> GetByUnitPrice(decimal min, decimal max) + public async Task>> GetByUnitPriceAsync(decimal min, decimal max) { - return new SuccessDataResult>(_productDal.GetAll(p => p.UnitPrice >= min && p.UnitPrice <= max)); + return new SuccessDataResult>(await _productDal.GetAllAsync(p => p.UnitPrice >= min && p.UnitPrice <= max)); } - public IDataResult> GetProductDetails() + public async Task>> GetProductDetailsAsync() { if (DateTime.Now.Hour == 23) { return new ErrorDataResult>(Messages.MaintenanceTime); } - return new SuccessDataResult>(_productDal.GetProductDetails()); + return new SuccessDataResult>(await _productDal.GetProductDetailsAsync()); } [ValidationAspect(typeof(ProductValidator))] [CacheRemoveAspect("IProductService.Get")] - public IResult Update(Product product) + public async Task UpdateAsync(Product product) { - var result = _productDal.GetAll(p => p.CategoryId == product.CategoryId).Count; - if (result >= 10) + var result =await _productDal.GetAllAsync(p => p.CategoryId == product.CategoryId); + if (result.Count >= 10) { return new ErrorResult(Messages.ProductCountOfCategoryError); } throw new NotImplementedException(); } - private IResult CheckIfProductCountOfCategoryCorrect(int categoryId) + private async Task CheckIfProductCountOfCategoryCorrectAsync(int categoryId) { //Select count(*) from products where categoryId=1 - var result = _productDal.GetAll(p => p.CategoryId == categoryId).Count; - if (result >= 15) + var result = await _productDal.GetAllAsync(p => p.CategoryId == categoryId); + if (result.Count >= 15) { return new ErrorResult(Messages.ProductCountOfCategoryError); } return new SuccessResult(); } - private IResult CheckIfProductNameExists(string productName) + private async Task CheckIfProductNameExistsAsync(string productName) { - var result = _productDal.GetAll(p => p.ProductName == productName).Any(); - if (result) + var result = await _productDal.GetAllAsync(p => p.ProductName == productName); + if (result.Any()) { return new ErrorResult(Messages.ProductNameAlreadyExists); } return new SuccessResult(); } - private IResult CheckIfCategoryLimitExceded() + private async Task CheckIfCategoryLimitExcededAsync() { - var result = _categoryService.GetAll(); + var result =await _categoryService.GetAllAsync(); if (result.Data.Count>15) { return new ErrorResult(Messages.CategoryLimitExceded); @@ -139,16 +140,16 @@ private IResult CheckIfCategoryLimitExceded() } //[TransactionScopeAspect] - public IResult AddTransactionalTest(Product product) + public async Task AddTransactionalTestAsync(Product product) { - Add(product); + await AddAsync(product); if (product.UnitPrice < 10) { throw new Exception(""); } - Add(product); + await AddAsync(product); return null; } diff --git a/Business/Concrete/UserManager.cs b/Business/Concrete/UserManager.cs index 4bca86c..7cc8d21 100644 --- a/Business/Concrete/UserManager.cs +++ b/Business/Concrete/UserManager.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace Business.Concrete { @@ -16,19 +17,19 @@ public UserManager(IUserDal userDal) _userDal = userDal; } - public List GetClaims(User user) + public async Task> GetClaimsAsync(User user) { - return _userDal.GetClaims(user); + return await _userDal.GetClaimsAsync(user); } - public void Add(User user) + public async Task AddAsync(User user) { - _userDal.Add(user); + await _userDal.AddAsync(user); } - public User GetByMail(string email) + public async Task GetByMailAsync(string email) { - return _userDal.Get(u => u.Email == email); + return await _userDal.GetAsync(u => u.Email == email); } } } diff --git a/Core/Aspects/Autofac/Caching/CacheAspect.cs b/Core/Aspects/Autofac/Caching/CacheAspect.cs index f898c57..432e374 100644 --- a/Core/Aspects/Autofac/Caching/CacheAspect.cs +++ b/Core/Aspects/Autofac/Caching/CacheAspect.cs @@ -21,7 +21,7 @@ public CacheAspect(int duration = 60) _cacheManager = ServiceTool.ServiceProvider.GetService(); } - public override void Intercept(IInvocation invocation) + public override void InterceptAsynchronous(IInvocation invocation) { var methodName = string.Format($"{invocation.Method.ReflectedType.FullName}.{invocation.Method.Name}"); var arguments = invocation.Arguments.ToList(); diff --git a/Core/Core.csproj b/Core/Core.csproj index 61e4208..eaa8ac5 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -7,6 +7,7 @@ + diff --git a/Core/DataAccess/EntityFramework/EfEntityRepositoryBase.cs b/Core/DataAccess/EntityFramework/EfEntityRepositoryBase.cs index f267df7..2c01a00 100644 --- a/Core/DataAccess/EntityFramework/EfEntityRepositoryBase.cs +++ b/Core/DataAccess/EntityFramework/EfEntityRepositoryBase.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Threading.Tasks; namespace Core.DataAccess.EntityFramework { @@ -12,52 +13,52 @@ public class EfEntityRepositoryBase: IEntityRepository> filter) + public async Task GetAsync(Expression> filter) { using (TContext context = new TContext()) { - return context.Set().SingleOrDefault(filter); + return await context.Set().SingleOrDefaultAsync(filter); } } - public List GetAll(Expression> filter = null) + public async Task> GetAllAsync(Expression> filter = null) { using (TContext context = new TContext()) { return filter == null - ? context.Set().ToList() - : context.Set().Where(filter).ToList(); + ? await context.Set().ToListAsync() + : await context.Set().Where(filter).ToListAsync(); } } - public void Update(TEntity entity) + public async Task UpdateAsync(TEntity entity) { using (TContext context = new TContext()) { var updatedEntity = context.Entry(entity); updatedEntity.State = EntityState.Modified; - context.SaveChanges(); + await context.SaveChangesAsync(); } } } diff --git a/Core/DataAccess/IEntityRepository.cs b/Core/DataAccess/IEntityRepository.cs index 17762f1..5f625ab 100644 --- a/Core/DataAccess/IEntityRepository.cs +++ b/Core/DataAccess/IEntityRepository.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq.Expressions; using System.Text; +using System.Threading.Tasks; namespace Core.DataAccess { @@ -12,10 +13,10 @@ namespace Core.DataAccess //new() : new'lenebilir olmalı public interface IEntityRepository where T:class,IEntity,new() { - List GetAll(Expression> filter=null); - T Get(Expression> filter); - void Add(T entity); - void Update(T entity); - void Delete(T entity); + Task> GetAllAsync(Expression> filter=null); + Task GetAsync(Expression> filter); + Task AddAsync(T entity); + Task UpdateAsync(T entity); + Task DeleteAsync(T entity); } } diff --git a/Core/Utilities/Interceptors/AspectInterceptorSelector.cs b/Core/Utilities/Interceptors/AspectInterceptorSelector.cs index a4917cb..235eb6a 100644 --- a/Core/Utilities/Interceptors/AspectInterceptorSelector.cs +++ b/Core/Utilities/Interceptors/AspectInterceptorSelector.cs @@ -18,7 +18,7 @@ public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IIntercep .GetCustomAttributes(true); classAttributes.AddRange(methodAttributes); - return classAttributes.OrderBy(x => x.Priority).ToArray(); + return ProxyGeneratorExtensions.ToInterceptors(classAttributes.OrderBy(x => x.Priority).ToArray()); } } diff --git a/Core/Utilities/Interceptors/MethodInterception.cs b/Core/Utilities/Interceptors/MethodInterception.cs index 4a476ea..4e33ef7 100644 --- a/Core/Utilities/Interceptors/MethodInterception.cs +++ b/Core/Utilities/Interceptors/MethodInterception.cs @@ -1,5 +1,6 @@ using Castle.DynamicProxy; using System; +using System.Threading.Tasks; namespace Core.Utilities.Interceptors { @@ -10,13 +11,25 @@ protected virtual void OnBefore(IInvocation invocation) { } protected virtual void OnAfter(IInvocation invocation) { } protected virtual void OnException(IInvocation invocation, System.Exception e) { } protected virtual void OnSuccess(IInvocation invocation) { } - public override void Intercept(IInvocation invocation) + + public override void InterceptAsynchronous(IInvocation invocation) + { + invocation.ReturnValue = InternalInterceptAsynchronous(invocation); + } + + + private async Task InternalInterceptAsynchronous(IInvocation invocation) { + + + TResult result; var isSuccess = true; OnBefore(invocation); try { invocation.Proceed(); + var task = (Task)invocation.ReturnValue; + result = await task; } catch (Exception e) { @@ -32,6 +45,8 @@ public override void Intercept(IInvocation invocation) } } OnAfter(invocation); + return result; + } } diff --git a/Core/Utilities/Interceptors/MethodInterceptionBaseAttribute.cs b/Core/Utilities/Interceptors/MethodInterceptionBaseAttribute.cs index 157d29e..6bdd639 100644 --- a/Core/Utilities/Interceptors/MethodInterceptionBaseAttribute.cs +++ b/Core/Utilities/Interceptors/MethodInterceptionBaseAttribute.cs @@ -4,13 +4,25 @@ namespace Core.Utilities.Interceptors { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] - public abstract class MethodInterceptionBaseAttribute : Attribute, IInterceptor + public abstract class MethodInterceptionBaseAttribute : Attribute, IAsyncInterceptor { public int Priority { get; set; } - public virtual void Intercept(IInvocation invocation) + + + public virtual void InterceptAsynchronous(IInvocation invocation) + { + + } + + public virtual void InterceptAsynchronous(IInvocation invocation) { + + } + public virtual void InterceptSynchronous(IInvocation invocation) + { + } } diff --git a/DataAccess/Abstract/IProductDal.cs b/DataAccess/Abstract/IProductDal.cs index 406dd1e..b4c9e8a 100644 --- a/DataAccess/Abstract/IProductDal.cs +++ b/DataAccess/Abstract/IProductDal.cs @@ -4,12 +4,13 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace DataAccess.Abstract { public interface IProductDal:IEntityRepository { - List GetProductDetails(); + Task> GetProductDetailsAsync(); } } diff --git a/DataAccess/Abstract/IUserDal.cs b/DataAccess/Abstract/IUserDal.cs index 8ec6076..f08bb16 100644 --- a/DataAccess/Abstract/IUserDal.cs +++ b/DataAccess/Abstract/IUserDal.cs @@ -3,11 +3,12 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace DataAccess.Abstract { public interface IUserDal : IEntityRepository { - List GetClaims(User user); + Task> GetClaimsAsync(User user); } } diff --git a/DataAccess/Concrete/EntityFramework/EfProductDal.cs b/DataAccess/Concrete/EntityFramework/EfProductDal.cs index e99dc03..73cb4b7 100644 --- a/DataAccess/Concrete/EntityFramework/EfProductDal.cs +++ b/DataAccess/Concrete/EntityFramework/EfProductDal.cs @@ -8,13 +8,14 @@ using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Threading.Tasks; namespace DataAccess.Concrete.EntityFramework { //NuGet public class EfProductDal : EfEntityRepositoryBase, IProductDal { - public List GetProductDetails() + public async Task> GetProductDetailsAsync() { using (NorthwindContext context = new NorthwindContext()) { @@ -26,7 +27,7 @@ on p.CategoryId equals c.CategoryId ProductId = p.ProductId, ProductName = p.ProductName, CategoryName =c.CategoryName, UnitsInStock = p.UnitsInStock }; - return result.ToList(); + return await result.ToListAsync(); } } } diff --git a/DataAccess/Concrete/EntityFramework/EfUserDal.cs b/DataAccess/Concrete/EntityFramework/EfUserDal.cs index 6b07a8f..f86af5c 100644 --- a/DataAccess/Concrete/EntityFramework/EfUserDal.cs +++ b/DataAccess/Concrete/EntityFramework/EfUserDal.cs @@ -5,12 +5,14 @@ using System.Collections.Generic; using System.Text; using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace DataAccess.Concrete.EntityFramework { public class EfUserDal : EfEntityRepositoryBase, IUserDal { - public List GetClaims(User user) + public async Task> GetClaimsAsync(User user) { using (var context = new NorthwindContext()) { @@ -19,7 +21,7 @@ join userOperationClaim in context.UserOperationClaims on operationClaim.Id equals userOperationClaim.OperationClaimId where userOperationClaim.UserId == user.Id select new OperationClaim { Id = operationClaim.Id, Name = operationClaim.Name }; - return result.ToList(); + return await result.ToListAsync(); } } diff --git a/DataAccess/Concrete/InMemory/InMemoryProductDal.cs b/DataAccess/Concrete/InMemory/InMemoryProductDal.cs index 0d10013..de91a86 100644 --- a/DataAccess/Concrete/InMemory/InMemoryProductDal.cs +++ b/DataAccess/Concrete/InMemory/InMemoryProductDal.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Threading.Tasks; namespace DataAccess.Concrete.InMemory { @@ -23,12 +24,12 @@ public InMemoryProductDal() new Product{ProductId=5, CategoryId=2, ProductName="Fare", UnitPrice=85, UnitsInStock=1} }; } - public void Add(Product product) + public async Task AddAsync(Product product) { - _products.Add(product); + _products.Add(product); } - public void Delete(Product product) + public async Task DeleteAsync(Product product) { //LINQ - Language Integrated Query //Lambda @@ -42,7 +43,7 @@ public List GetAll() return _products; } - public void Update(Product product) + public async Task UpdateAsync(Product product) { //Gönderdiğim ürün id'sine sahip olan listedeki ürünü bul Product productToUpdate = _products.SingleOrDefault(p => p.ProductId == product.ProductId); @@ -57,19 +58,26 @@ public List GetAllByCategory(int categoryId) return _products.Where(p => p.CategoryId == categoryId).ToList(); } - public List GetAll(Expression> filter = null) + + + Task> IProductDal.GetProductDetailsAsync() { throw new NotImplementedException(); } - public Product Get(Expression> filter) + public Task> GetAllAsync(Expression> filter = null) { throw new NotImplementedException(); } - public List GetProductDetails() + public Task GetAsync(Expression> filter) { throw new NotImplementedException(); } + + + + + } } diff --git a/WebAPI/Controllers/AuthController.cs b/WebAPI/Controllers/AuthController.cs index 4a6f687..f65ec54 100644 --- a/WebAPI/Controllers/AuthController.cs +++ b/WebAPI/Controllers/AuthController.cs @@ -20,15 +20,15 @@ public AuthController(IAuthService authService) } [HttpPost("login")] - public ActionResult Login(UserForLoginDto userForLoginDto) + public async Task Login(UserForLoginDto userForLoginDto) { - var userToLogin = _authService.Login(userForLoginDto); + var userToLogin =await _authService.LoginAsync(userForLoginDto); if (!userToLogin.Success) { return BadRequest(userToLogin.Message); } - var result = _authService.CreateAccessToken(userToLogin.Data); + var result =await _authService.CreateAccessTokenAsync(userToLogin.Data); if (result.Success) { return Ok(result.Data); @@ -38,16 +38,16 @@ public ActionResult Login(UserForLoginDto userForLoginDto) } [HttpPost("register")] - public ActionResult Register(UserForRegisterDto userForRegisterDto) + public async Task Register(UserForRegisterDto userForRegisterDto) { - var userExists = _authService.UserExists(userForRegisterDto.Email); + var userExists =await _authService.UserExistsAsync(userForRegisterDto.Email); if (!userExists.Success) { return BadRequest(userExists.Message); } - var registerResult = _authService.Register(userForRegisterDto, userForRegisterDto.Password); - var result = _authService.CreateAccessToken(registerResult.Data); + var registerResult =await _authService.RegisterAsync(userForRegisterDto, userForRegisterDto.Password); + var result =await _authService.CreateAccessTokenAsync(registerResult.Data); if (result.Success) { return Ok(result.Data); diff --git a/WebAPI/Controllers/ProductsController.cs b/WebAPI/Controllers/ProductsController.cs index 29a621a..79423c2 100644 --- a/WebAPI/Controllers/ProductsController.cs +++ b/WebAPI/Controllers/ProductsController.cs @@ -26,11 +26,12 @@ public ProductsController(IProductService productService) } [HttpGet("getall")] - public IActionResult GetAll() + public async Task GetAll() { //Swagger //Dependency chain -- - var result = _productService.GetAll(); + + var result = await _productService.GetAllAsync(); if (result.Success) { return Ok(result); @@ -40,9 +41,9 @@ public IActionResult GetAll() } [HttpGet("getbyid")] - public IActionResult GetById(int id) + public async Task GetById(int id) { - var result = _productService.GetById(id); + var result =await _productService.GetByIdAsync(id); if (result.Success) { return Ok(result); @@ -52,9 +53,9 @@ public IActionResult GetById(int id) } [HttpPost("add")] - public IActionResult Add(Product product) + public async Task Add(Product product) { - var result = _productService.Add(product); + var result =await _productService.AddAsync(product); if (result.Success) { return Ok(result); From cb50e0ad66c4bb1ef53d586935490d75c10938bd Mon Sep 17 00:00:00 2001 From: BEren Date: Sun, 14 Mar 2021 22:38:04 +0300 Subject: [PATCH 2/3] Category Controller Create Getall method add --- WebAPI/Controllers/CategoryController.cs | 36 ++++++++++++++++++++++++ WebAPI/Startup.cs | 3 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 WebAPI/Controllers/CategoryController.cs diff --git a/WebAPI/Controllers/CategoryController.cs b/WebAPI/Controllers/CategoryController.cs new file mode 100644 index 0000000..6029e75 --- /dev/null +++ b/WebAPI/Controllers/CategoryController.cs @@ -0,0 +1,36 @@ +using Business.Abstract; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebAPI.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class CategoryController : Controller + { + private ICategoryService _categoryService; + + public CategoryController(ICategoryService categoryService) + { + _categoryService = categoryService; + } + + public IActionResult Index() + { + return View(); + } + [HttpGet("getall")] + public IActionResult GetAll() + { + var result = _categoryService.GetAll(); + if (result.Success) + { + return Ok(result); + } + return BadRequest(result); + } + } +} diff --git a/WebAPI/Startup.cs b/WebAPI/Startup.cs index 83e1e4a..4a7e3a7 100644 --- a/WebAPI/Startup.cs +++ b/WebAPI/Startup.cs @@ -45,7 +45,7 @@ public void ConfigureServices(IServiceCollection services) //services.AddSingleton(); //services.AddSingleton(); - + services.AddCors(); var tokenOptions = Configuration.GetSection("TokenOptions").Get(); @@ -77,6 +77,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseDeveloperExceptionPage(); } + app.UseCors(builder => builder.WithOrigins("http://localhost:4200").AllowAnyHeader()); app.UseHttpsRedirection(); From cd1d1332974cb89136c6833842abb887a88e07b5 Mon Sep 17 00:00:00 2001 From: BEren Date: Sun, 14 Mar 2021 22:44:26 +0300 Subject: [PATCH 3/3] CategoryControl instead CategoriesControl added --- .../{CategoryController.cs => CategoriesController.cs} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename WebAPI/Controllers/{CategoryController.cs => CategoriesController.cs} (73%) diff --git a/WebAPI/Controllers/CategoryController.cs b/WebAPI/Controllers/CategoriesController.cs similarity index 73% rename from WebAPI/Controllers/CategoryController.cs rename to WebAPI/Controllers/CategoriesController.cs index 6029e75..25ac92b 100644 --- a/WebAPI/Controllers/CategoryController.cs +++ b/WebAPI/Controllers/CategoriesController.cs @@ -9,11 +9,11 @@ namespace WebAPI.Controllers { [Route("api/[controller]")] [ApiController] - public class CategoryController : Controller + public class CategoriesController : Controller { private ICategoryService _categoryService; - public CategoryController(ICategoryService categoryService) + public CategoriesController(ICategoryService categoryService) { _categoryService = categoryService; } @@ -23,9 +23,9 @@ public IActionResult Index() return View(); } [HttpGet("getall")] - public IActionResult GetAll() + public async Task GetAll() { - var result = _categoryService.GetAll(); + var result =await _categoryService.GetAllAsync(); if (result.Success) { return Ok(result);