Example #1
0
        PayPenalty([FromBody] PenaltyPayRequest penaltyPayRequest)
        {
            if (penaltyPayRequest.Id == null)
            {
                return(BadRequest("empty penaltyPayRequest id"));
            }


            var penalty = await _penaltyRepository.GetById(penaltyPayRequest.Id);

            if (penalty == null)
            {
                return(NotFound());
            }

            if (penalty.Price > penaltyPayRequest.Fee)
            {
                return(BadRequest(
                           new Response <AuthFailedResponse>(
                               new AuthFailedResponse()
                {
                    Errors = new string[] { "not enough money to pay penalty" }
                }
                               )
                           ));
            }

            var updatedResult = await _penaltyRepository.PayPenalty(penalty.Id);


            if (updatedResult > 0)
            {
                var updatedPenalty = await _penaltyRepository
                                     .GetPenaltyWithCar((int)penaltyPayRequest.Id);

                var response = $"penalty with id {penalty.Id} payed ";

                if (updatedPenalty != null && updatedPenalty.Car != null)
                {
                    response = $"penalty with id {updatedPenalty.Id} " +
                               $"for car {updatedPenalty.Car.UniqueNumber} payed";
                }
                return(Ok(new Response <string>
                              (response)));
            }
            return(NoContent());
        }