/// <summary> /// JB. Asyncronously create a new Client. Add Secret and Scope and return a response witht he info needed for Client Integration. /// </summary> /// <param name="client"></param> /// <returns></returns> public async Task <ClientResponseDto> AddClient(Client client, ClientBindingDto dto) { //JB. Build a newly randomdized Secret, this is what is passed to the client and it is not hashed yet. It will be hashed at persisting time. string NewSecret = await RandomStringGenerator.GeneratedString(); ClientResponseDto response = null; int clientId = 0; try { await Task.Run(async() => { using (/*var*/ ctx /*= new ResourceConfigDbContext()*/) { ctx.Clients.Add(client); ctx.SaveChanges(); clientId = client.Id; }; //JB. Add now Secret _secretsRepo.AddClientSecret(_factory.CreateClientSecret(clientId, NewSecret)); //JB. Add Client Grant Type await _grantRepo.AddGrantType(new ClientGrantType { ClientId = clientId, GrantType = IdentityServer4.Models.GrantType.ClientCredentials }); //JB. Add Scopes this client is allowed in the system. foreach (var scopev in dto.AllowedScopes) { await _scopeRepo.CreateClientScope(new ClientScope { ClientId = clientId, Scope = scopev }); } response = new ClientResponseDto { ClientName = client.ClientName, Client_Id = client.ClientId, Secret = NewSecret, AllowedScopes = dto.AllowedScopes, Claims = dto.Claims }; //JB. Add claims. Info about this Client foreach (var c in dto.Claims) { await _claimRepo.AddClaim(new ClientClaim { ClientId = clientId, Type = c["Type"], Value = c["Value"] }); } }); } catch (Exception ex) { ClientErrorResponseDto errorResponse = new ClientErrorResponseDto { Error = "Not Found. " + ex.Message }; } return(response); }
public async Task <object> AddClient(Client client, ClientBindingDto dto) { //JB. Build a newly randomdized Secret, this is what is passed to the client and it is not hashed yet. It will be hashed at persisting time. string NewSecret = await RandomStringGenerator.GeneratedString(); Object response; int clientId = 0; try { using (_ctx) { //JB. Create the client. _ctx.Clients.Add(client); _ctx.SaveChanges(); clientId = client.Id; //JB. Add now the Secret await _ctx.ClientSecrets.AddAsync(_factory.CreateClientSecret(clientId, NewSecret)); //JB. Add Client Grant Type await _ctx.ClientGrantTypes.AddAsync(new ClientGrantType { ClientId = clientId, GrantType = IdentityServer4.Models.GrantType.ClientCredentials }); //JB. Add Scopes this client is allowed in the system. //foreach (var scopev in dto.AllowedScopes) //{ // await _ctx.ClientScopes.AddAsync(new ClientScope { ClientId = clientId, Scope = scopev }); //} ////JB. Add claims. Info about this Client //foreach (var c in dto.Claims) //{ // await _ctx.ClientClaims.AddAsync(new ClientClaim { ClientId = clientId, Type = c["Type"], Value = c["Value"] }); //} _ctx.SaveChanges(); }; response = new ClientResponseDto { ClientName = client.ClientName, Client_Id = client.ClientId, Secret = NewSecret, AllowedScopes = dto.AllowedScopes, Claims = dto.Claims }; } catch (Exception ex) { response = new ClientErrorResponseDto { Error = HttpStatusCode.InternalServerError.ToString(), Message = ex.Message }; } return(response); }
public ClientResponseDto GetClientCampaignList() { var login = _jwtTokenService.GetClaimByName("Login"); var client = _advertDbContext.Clients.Where(c => login.Equals(c.Login)) .Include(c => c.Campaigns) .ThenInclude(c => c.Banners) .FirstOrDefault(); if (client == null) { throw new ResourceNotFoundException("No such client"); } var clientResponseDto = new ClientResponseDto { FirstName = client.FirstName, LastName = client.LastName, Email = client.Email, Phone = client.Phone, Campaigns = client.Campaigns.Select(c => new CampaignResponseDto { StartDate = c.StartDate, EndDate = c.EndDate, PricePerSquareMeter = c.PricePerSquareMeter, Banners = c.Banners.Select(b => new BannerResponseDto { Name = b.Name, Area = b.Area, Price = b.Price }).ToList() }).ToList() }; clientResponseDto.Campaigns = clientResponseDto.Campaigns .OrderByDescending(c => c.StartDate).ToList(); return(clientResponseDto); }
//Paginar por 10 public async Task <IEnumerable <ClientResponseDto> > GetAll() { List <ClientResponseDto> lst = new List <ClientResponseDto>(); //Logger.LogInformation($"Getting all clients"); var clients = await unitOfWork.ClientRepository.GetAll(); foreach (var client in clients) { //Logger.LogInformation($"Getting a bank account for ClientId"); var bankAccounts = await unitOfWork.BankAccountRepository.GetBankAccountByClientId(client.ClientId); foreach (var account in bankAccounts) { ClientResponseDto dto = new ClientResponseDto(); dto.FullName = client.FullName; dto.SocialNumber = client.SocialNumber; dto.Product = account.Product; dto.InitialBalance = await getTotalAmount(account.InitialBalance, account.BankAccountId); lst.Add(dto); } } return(lst); }