From b5546810b109b084ddab8d9d5785e36f2c0760c7 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Tue, 7 May 2024 17:49:02 +0330 Subject: [PATCH] ... --- Back/Controllers/CustomerController.cs | 35 +++++++- Back/Program.cs | 1 + Back/Services/servCustomer.cs | 86 ++++++++++---------- Back/Validations/AddCustomerValidation.cs | 61 ++++++++++++++ Shared/DTOs/RCustomer.cs | 10 +-- TaxPayerFull/CUSComponent/CustomerItem.razor | 76 ++++++++++++++++- 6 files changed, 217 insertions(+), 52 deletions(-) create mode 100644 Back/Validations/AddCustomerValidation.cs diff --git a/Back/Controllers/CustomerController.cs b/Back/Controllers/CustomerController.cs index bf85d9c..fc84c4d 100644 --- a/Back/Controllers/CustomerController.cs +++ b/Back/Controllers/CustomerController.cs @@ -1,4 +1,5 @@ using Back.Services; +using Back.Validations; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Shared.DTOs; @@ -13,9 +14,11 @@ namespace Back.Controllers private readonly CheckPermission _checkPermission; private readonly servUser _servUser; private readonly servCustomer _servCus; - public CustomerController(CheckPermission checkPermission, servUser servUser, servCustomer servCus) + private readonly AddCustomerValidation _addCustomerValidation; + public CustomerController(CheckPermission checkPermission, servUser servUser + , servCustomer servCus, AddCustomerValidation addCustomerValidation) { - + _addCustomerValidation = addCustomerValidation; _checkPermission = checkPermission; _servUser = servUser; _servCus = servCus; @@ -33,5 +36,33 @@ namespace Back.Controllers } + [HttpPost("Add")] + public async Task> Add(RCustomer item) + { + var claim = HttpContext.User.Claims.First(c => c.Type == "UserID"); + var UserID = claim.Value; + var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID)); + + var resultValidationmodel = await _addCustomerValidation.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID,item)); + if (!resultValidationmodel.IsValid) + return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList()); + + return Ok(_servCus.AddCustomer(new Data.Models.Customer + { + Address = item.Address, + BranchID = item.BranchID, + CompanyID = user.RolUsers.First().CompanyID, + CustomerType = item.CustomerType, + EconomicCode = item.EconomicCode, + Email = item.Email, + FullName = item.FullName, + Info = item.Info, + MeliCode = item.MeliCode, + PassportNumber = item.PassportNumber, + Phone = item.Phone, + ZipCode = item.ZipCode, + IsDeleted = false + })) ; + } } } diff --git a/Back/Program.cs b/Back/Program.cs index aafc6d3..60d9175 100644 --- a/Back/Program.cs +++ b/Back/Program.cs @@ -39,6 +39,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(c => new mpNuget.RestClient("09119660045", "C54S2")); string origins = "OriginTaxPayer"; diff --git a/Back/Services/servCustomer.cs b/Back/Services/servCustomer.cs index 83cc57e..41138b1 100644 --- a/Back/Services/servCustomer.cs +++ b/Back/Services/servCustomer.cs @@ -98,48 +98,48 @@ namespace Back.Services // }) // .FirstOrDefaultAsync(); //} - //public async Task AddORUpdateCustomer(Customer item) - //{ - // try - // { - // SysLog log = new SysLog() - // { - // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, - // Datetime = DateTime.Now.ConvertMiladiToShamsi(), - // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer", - // Value ="*"+ JsonConvert.SerializeObject(item), - // Route = _httpContextAccessor.HttpContext.Request.Path, - // Type = "User" - // }; - // _contextMongodb.InsertItem(log); + public async Task AddCustomer(Customer item) + { + try + { + //SysLog log = new SysLog() + //{ + // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, + // Datetime = DateTime.Now.ConvertMiladiToShamsi(), + // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer", + // Value = "*" + JsonConvert.SerializeObject(item), + // Route = _httpContextAccessor.HttpContext.Request.Path, + // Type = "User" + //}; + //_contextMongodb.InsertItem(log); - // if (item.ID == null || item.ID == 0) - // { - // var ret = await _repositores.ADD(item); - // _checkPermission.ExtensionofAccess(item.CompanyID, 5, "-1"); - // return item; - // } - // else - // { - // return await _repositores.UPDATE(item); - // } - // } - // catch (Exception ex) - // { - // SysLog log = new SysLog() - // { - // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, - // Datetime = DateTime.Now.ConvertMiladiToShamsi(), - // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer", - // Value = ex.Message, - // Route = _httpContextAccessor.HttpContext.Request.Path, - // Type = "catch" - // }; - // _contextMongodb.InsertItem(log); - // return null; - // } - - //} + if (item.ID == null || item.ID == 0) + { + var ret = await _repoCus.AddBoolResultAsync(item); + await _checkPermission.ExtensionofAccess(item.CompanyID, 5, "-1"); + return ret; + } + else + { + return await _repoCus.UpdateAsync(item); + } + } + catch (Exception ex) + { + //SysLog log = new SysLog() + //{ + // TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier, + // Datetime = DateTime.Now.ConvertMiladiToShamsi(), + // Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCustomer", + // Value = ex.Message, + // Route = _httpContextAccessor.HttpContext.Request.Path, + // Type = "catch" + //}; + //_contextMongodb.InsertItem(log); + return false; + } + + } //public async Task DeleteCustomer(Customer item) //{ @@ -172,10 +172,10 @@ namespace Back.Services // }; // _contextMongodb.InsertItem(log); // return false; - + // } //} - + } } diff --git a/Back/Validations/AddCustomerValidation.cs b/Back/Validations/AddCustomerValidation.cs new file mode 100644 index 0000000..400e16c --- /dev/null +++ b/Back/Validations/AddCustomerValidation.cs @@ -0,0 +1,61 @@ +using Back.Services; +using FluentValidation; +using Shared.DTOs; + +namespace Back.Validations +{ + public class AddCustomerValidation : AbstractValidator> + { + public AddCustomerValidation(CheckPermission _checkPermission) + { + CascadeMode = CascadeMode.Stop; + RuleFor(r => r.Item1) + .Custom((CompanyID, context) => { + if (! _checkPermission.AllowAddCustomerInCompany(CompanyID).Result) + context.AddFailure("اضافه کردن مشتری محدود شده است"); + }); + + + RuleFor(r => r.Item2.FullName) + .NotNull().WithMessage("نام نمی تواند خالی باشذ") + .NotEmpty().WithMessage("نام نمی تواند خالی باشذ") + .MinimumLength(5).WithMessage("حداقل کارکتر مجار نام 5 می باشد"); + + RuleFor(r => r.Item2.Phone) + .NotNull().WithMessage("تلفن نمی تواند خالی باشذ") + .NotEmpty().WithMessage("تلفن نمی تواند خالی باشذ") + .MinimumLength(8).WithMessage("حداقل کارکتر مجار تلفن 8 می باشد"); + + RuleFor(r => r.Item2.Email) + .Custom((model, context) => { + if (!string.IsNullOrEmpty(model) && (!model.Contains("@") || !model.Contains("."))) + context.AddFailure("فرمت پست الکترونیک صحبح نمی باشد"); + }); + RuleFor(r => r.Item2.EconomicCode) + .Custom((model, context) => { + if (!string.IsNullOrEmpty(model) && (model.Length<11 || model.Length > 14)) + context.AddFailure("تعداد شماره اقتصادی صحبح نمی باشد"); + }); + RuleFor(r => r.Item2.BranchID) + .Custom((model, context) => { + if (!string.IsNullOrEmpty(model) && (model.Length > 10 )) + context.AddFailure("تعداد کد شعبه صحبح نمی باشد"); + }); + RuleFor(r => r.Item2.ZipCode) + .Custom((model, context) => { + if (!string.IsNullOrEmpty(model) && (model.Length !=10)) + context.AddFailure("تعداد کد پستی صحبح نمی باشد"); + }); + RuleFor(r => r.Item2.MeliCode) + .Custom((model, context) => { + if (!string.IsNullOrEmpty(model) && (model.Length < 10 || model.Length > 12)) + context.AddFailure("تعداد شناسه ملی صحبح نمی باشد"); + }); + RuleFor(r => r.Item2.PassportNumber) + .Custom((model, context) => { + if (!string.IsNullOrEmpty(model) && (model.Length!=9)) + context.AddFailure("تعداد شماره گذرنامه صحبح نمی باشد"); + }); + } + } +} diff --git a/Shared/DTOs/RCustomer.cs b/Shared/DTOs/RCustomer.cs index 3c72d79..9dcd399 100644 --- a/Shared/DTOs/RCustomer.cs +++ b/Shared/DTOs/RCustomer.cs @@ -10,11 +10,11 @@ namespace Shared.DTOs public class RCustomer { [Display(Name ="شناسه")] - public int ID { get; set; } + public int? ID { get; set; } [Display(Name = "نام کامل")] public string FullName { get; set; } [Display(Name = "نوع مشتری")] - public string CustomerTypeTitle { get; set; } + public string? CustomerTypeTitle { get; set; } [Display(Name = "تلفن")] public string Phone { get; set; } [Display(Name = "پست الکترونیک")] @@ -24,15 +24,15 @@ namespace Shared.DTOs [Display(Name = "کد اقتصادی")] public string? EconomicCode { get; set; } [Display(Name = "کد شعبه")] - public string? BranchID { get; set; } + public string? BranchID { get; set; }// [Display(Name = "توضیحات")] - public string? Info { get; set; } + public string? Info { get; set; }// //------------ public CustomerType CustomerType { get; set; } public string? ZipCode { get; set; } public string? MeliCode { get; set; } - public string? PassportNumber { get; set; } + public string? PassportNumber { get; set; }// } } diff --git a/TaxPayerFull/CUSComponent/CustomerItem.razor b/TaxPayerFull/CUSComponent/CustomerItem.razor index dc99b87..cd0f57f 100644 --- a/TaxPayerFull/CUSComponent/CustomerItem.razor +++ b/TaxPayerFull/CUSComponent/CustomerItem.razor @@ -1,5 +1,76 @@ @using Shared.DTOs -

سلام

+
+
+
+ + +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ +
+
+ + +
+ + +
+ +
+ + @if (Cus.ID==-1) {