public async Task <PurchaseTypeDTO> CreateAsync(PurchaseTypePostRequest model, ApiDbContext apiDbContext) { try { var purchaseType = apiDbContext.PurchaseTypes.FirstOrDefault(p => p.Code.ToUpper().Trim().Equals(model.Code.ToUpper().Trim())); if (purchaseType != null) { throw new Exception($"Ya existe un tipo de pedido con el código {model.Code}"); } PurchaseType newPurchaseType = new PurchaseType { Code = model.Code, Name = model.Name, CreationDate = DateTime.Now }; await apiDbContext.PurchaseTypes.AddAsync(newPurchaseType); await apiDbContext.SaveChangesAsync(); return(ModelToDTO(newPurchaseType)); } catch (Exception e) { throw new Exception(e.Message); } }
public async Task <IActionResult> Create(PurchaseTypePostRequest model) { try { if (!ModelState.IsValid) { throw new Exception("Petición de alta inválida"); } return(Ok(await _purchaseTypeService.CreateAsync(model, _apiDbContext))); } catch (Exception e) { return(StatusCode(500, e.Message)); } }