public async Task <IActionResult> AddClient([FromBody] NewClientDto newClient) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (await _clientRepository.IsDuplicateClientAsync(newClient)) { ModelState.AddModelError("client", "Client already exists"); return(BadRequest(ModelState)); } var clientId = await _clientRepository.AddClientAsync(newClient); if (clientId > 0) { return(Ok(clientId)); } return(StatusCode(500, "An error ocurred in server")); } catch (Exception e) { _logger.LogCritical($"POST {Route} - {e.GetType().Name} - {e.Message} - {e.StackTrace}"); return(StatusCode(500, "An error ocurred in server")); } }
public async Task <IActionResult> Create(NewClientDto contract) { var resource = await _resourceRepo.GetAsync(contract.ResourceId, include : true); if (resource == null) { return(NotFound("Не найден")); } var cookieName = resource.Clients.SingleOrDefault(t => t.Name == contract.Name); if (cookieName != null) { return(BadRequest("Такое наименование уже существует")); } var client = new Client { Name = contract.Name, ResourceId = contract.ResourceId }; await _clientRepo.CreateClientAsync(client); await _clientRepo.SaveChangesAsync(); // Из шаблонов берем список куков и заполняем. var cookies = resource.CookieTemplates.Select(t => new Cookie { ClientId = client.Id, CookieTemplateId = t.Id }).ToArray(); await _cookieRepo.CreateRangeCookieAsync(cookies); await _cookieRepo.SaveChangesAsync(); return(Ok(new ClientDto { Id = client.Id, Name = client.Name, Cookies = cookies.Select(async t => { var cookieTemplate = await _cookieTemplateRepo.GetByTemplateAsync(t.CookieTemplateId); return new CookieDto { Value = t.Value, Id = t.Id, }; }).Select(t => t.Result).ToArray() })); }
public async Task <NewClientDto> Create(NewClientDto newClientDto) { if (newClientDto == null) { throw new ArgumentNullException(nameof(newClientDto)); } var newClient = _mapper.Map <Client>(newClientDto); await _repository.Create(newClient); var clientDto = _mapper.Map <NewClientDto>(newClient); return(clientDto); }
public async Task Update(int id, NewClientDto updateData) { if (updateData == null) { throw new ArgumentNullException(nameof(updateData)); } var itemToUpdate = await _repository.GetById(id); if (itemToUpdate == null) { throw new InvalidOperationException(); } _mapper.Map(updateData, itemToUpdate); await _repository.Update(itemToUpdate); }
/// <summary> /// Валидация данных клиента /// </summary> /// <param name="newClientDto">Данные клиента</param> public bool IsValid(NewClientDto newClientDto, ICollection <string> errors) { // ошибки валидации var results = new List <ValidationResult>(); // контекст var context = new ValidationContext(newClientDto); // валидируем модель if (!Validator.TryValidateObject(newClientDto, context, results)) { foreach (var r in results) { errors.Add(r.ErrorMessage); } return(false); } else { return(true); } }
public async Task <int> AddClientAsync(NewClientDto newClient) { var client = new Client { Name = newClient.Name, City = newClient.City, CreationDate = DateTime.Now, Email = newClient.Email, Phone = newClient.Phone, Rfc = newClient.Rfc.ToUpper(), Address = newClient.Address, StateId = newClient.State.Id, IsActive = true }; await _dbContext.Clients.AddAsync(client); if (await _dbContext.SaveChangesAsync() > 0) { return(client.Id); } return(0); }
public async Task <IActionResult> Post(NewClientDto newClient) { var createdClient = await _clientsService.Create(newClient); return(Ok(createdClient)); }
public async Task <IActionResult> Put(int id, [FromBody] NewClientDto newClient) { await _clientsService.Update(id, newClient); return(NoContent()); }
public async Task <bool> IsDuplicateClientAsync(NewClientDto client) { return(await _dbContext.Clients.AnyAsync(c => (c.Email.Equals(client.Email, StringComparison.InvariantCultureIgnoreCase) || c.Rfc.Equals(client.Rfc, StringComparison.InvariantCultureIgnoreCase)) && c.IsActive)); }