Example #1
0
        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));
            }
        }