public async Task <IActionResult> Create([FromBody] CreateUpdateRefund create) { if (!ModelState.IsValid) { return(new BadRequestObjectResult(new ErrorsResponse(ModelState))); } var username = User.FindFirst(c => c.Type == JwtRegisteredClaimNames.Sub).Value; var refund = new Refund { Amount = create.Amount, CheckNumber = create.CheckNumber, Date = create.Date, Username = username, SchoolYear = create.SchoolYear, SchoolDistrict = _districts.GetByAun(create.SchoolDistrictAun), }; try { refund = await Task.Run(() => _context.SaveChanges(() => _refunds.Create(refund))); return(new CreatedResult($"/api/refunds/{refund.Id}", new RefundResponse { Refund = new RefundDto(refund), })); } catch (DbUpdateException) { return(new StatusCodeResult(409)); } }
private static IList <Payment> XlsxToPayments(ISchoolDistrictRepository districts, DateTime?date, Stream stream) { var wb = new Workbook(stream); var sheet = wb.Worksheets[0]; const int aunIndex = 0; const int amountIndex = 3; var amountHeader = sheet.Cells[0, amountIndex].StringValue.Trim(); const string pattern = @"^(\w+) (\d{4}).*$"; var matches = Regex.Matches(amountHeader, pattern); if (matches.Count != 1) { throw new ArgumentException($"Column {amountIndex + 1} header has invalid format. Expected '{pattern}'."); } var groups = matches[0].Groups; var month = Month.ByName()[groups[1].Value]; var year = int.Parse(groups[2].Value); string schoolYear; if (month.FirstYear) { schoolYear = $"{year}-{year + 1}"; } else { schoolYear = $"{year - 1}-{year}"; } var payments = new List <Payment>(); for (var i = 1; i < sheet.Cells.MaxDataRow; i++) { var row = sheet.Cells.GetRow(i); if (row == null || row.IsBlank) { continue; } payments.Add(new Payment { Split = 1, Date = date.HasValue ? date.Value : new DateTime(year, month.Number, 1), ExternalId = "PDE UNIPAY", Type = PaymentType.UniPay, Amount = (double)sheet.Cells[i, amountIndex].DoubleValue, SchoolYear = schoolYear, SchoolDistrict = districts.GetByAun(sheet.Cells[i, aunIndex].IntValue), }); } return(payments); }
public async Task <IActionResult> Create([FromBody] CreateUpdatePayment create) { if (!ModelState.IsValid) { return(new BadRequestObjectResult(new ErrorsResponse(ModelState))); } var username = User.FindFirst(c => c.Type == JwtRegisteredClaimNames.Sub).Value; var payments = new List <Payment>(); for (var i = 0; i < create.Splits.Count; i++) { var split = create.Splits[i]; payments.Add(new Payment { Date = split.Date, ExternalId = create.ExternalId, Type = create.Type, SchoolDistrict = _districts.GetByAun(create.SchoolDistrictAun), Split = i + 1, Amount = split.Amount, SchoolYear = split.SchoolYear, Username = username, }); } try { payments = await Task.Run(() => _context.SaveChanges(() => _payments.CreateMany(payments)).ToList()); } catch (DbUpdateException) // check the inner exception? { return(StatusCode(409)); } return(Created($"/api/payments/{payments[0].PaymentId}", new PaymentsResponse { Payments = payments.Select(p => new PaymentDto(p)).ToList(), })); }