public async Task <ProviderDTO> CreateAsync(ProviderPostRequest model, ApiDbContext apiDbContext) { try { var provider = apiDbContext.Providers.FirstOrDefault(p => p.Code.ToUpper().Trim().Equals(model.Code.ToUpper().Trim())); if (provider != null) { throw new Exception($"Ya existe un proveedor con el código {model.Code}"); } Provider newProvider = new Provider { Code = model.Code, Name = model.Name, Web = model.Web, CreationDate = DateTime.Now }; await apiDbContext.Providers.AddAsync(newProvider); await apiDbContext.SaveChangesAsync(); return(ModelToDTO(newProvider)); } catch (Exception e) { throw new Exception(e.Message); } }
public async Task <IActionResult> Create(ProviderPostRequest model) { try { if (!ModelState.IsValid) { throw new Exception("Petición de alta inválida"); } return(Ok(await _providerService.CreateAsync(model, _apiDbContext))); } catch (Exception e) { return(StatusCode(500, e.Message)); } }