Exemplo n.º 1
0
        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(),
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Update(string id, [FromBody] CreateUpdatePayment update)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult(new ErrorsResponse(ModelState)));
            }

            var payments = new List <Payment>();

            for (var i = 0; i < update.Splits.Count; i++)
            {
                var split = update.Splits[i];

                payments.Add(new Payment
                {
                    PaymentId      = id,
                    Date           = split.Date,
                    ExternalId     = update.ExternalId,
                    Type           = update.Type,
                    SchoolDistrict = _districts.GetByAun(update.SchoolDistrictAun),
                    Split          = i + 1,
                    Amount         = split.Amount,
                    SchoolYear     = split.SchoolYear,
                });
            }

            try
            {
                await Task.Run(() => _context.SaveChanges(() => _payments.UpdateMany(payments)));
            }
            catch (NotFoundException)
            {
                return(NotFound());
            }

            return(Ok());
        }