public async Task <IActionResult> AddVendor([FromBody] NewVendorDto newVendor) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (await _vendorRepository.IsDuplicateVendorAsync(newVendor)) { ModelState.AddModelError("vendor", "Vendor already exists"); return(BadRequest(ModelState)); } var vendorId = await _vendorRepository.AddVendorAsync(newVendor); if (vendorId > 0) { return(Ok(vendorId)); } 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 <ActionResult <VendorView> > AddVendor([FromBody] NewVendorDto newVendor) { try { if (newVendor == null) { return(StatusCode(400)); } if (!ModelState.IsValid) { return(new InvalidInputResponse(ModelState)); } var result = await _Mediator.Send(newVendor); var newVendorView = await _Mediator.Send(new GetVendorByIdQuery() { Id = result }); return(StatusCode(201, newVendorView)); } catch (DuplicateTinNumberException e) { ModelState.AddModelError("TinNumber", e.Message); return(new InvalidInputResponse(ModelState)); } }
public async Task <int> AddVendorAsync(NewVendorDto newVendor) { var vendor = new Vendor { Name = newVendor.Name, City = newVendor.City, CreationDate = DateTime.Now, Email = newVendor.Email, Phone = newVendor.Phone, Rfc = newVendor.Rfc.ToUpper(), Address = newVendor.Address, StateId = newVendor.State.Id, IsActive = true }; await _dbContext.Vendors.AddAsync(vendor); if (await _dbContext.SaveChangesAsync() > 0) { return(vendor.Id); } return(0); }
public async Task <bool> IsDuplicateVendorAsync(NewVendorDto vendor) { return(await _dbContext.Vendors.AnyAsync(c => (c.Email.Equals(vendor.Email, StringComparison.InvariantCultureIgnoreCase) || c.Rfc.Equals(vendor.Rfc, StringComparison.InvariantCultureIgnoreCase)) && c.IsActive)); }