예제 #1
0
        PostPenalty([FromForm] PenaltyRequest penaltyRequest)
        {
            if (!await CheckIsUserAuthorizedForAction
                    (penaltyRequest.Id, penaltyRequest.Token))
            {
                return(BadRequest("You have not enough permissions for this action"));
            }
            if (penaltyRequest.CarId == null)
            {
                return(BadRequest("Cannot create penalty for empty CarId"));
            }
            var car = await _carService.GetById(penaltyRequest.CarId);

            if (car == null)
            {
                return(BadRequest($"The car with id {penaltyRequest.CarId} not exist"));
            }

            var penalty = _customMapper
                          .PenaltyRequestToPenalty(penaltyRequest);

            await _penaltyRepository.Create(penalty);

            await _penaltyRepository.SaveChangesAsync();

            return(Created(
                       ApiRoutes.PenaltiesRoutes.Create,
                       new Response <PenaltyResponse>(
                           _customMapper.PenaltyToPenaltyResponse(penalty))));
        }
예제 #2
0
        public async Task <PenaltyDTO> Create(PenaltyDTO dto)
        {
            var result = await this.Handler.Execute(_log, async() =>
            {
                Penalty penalty = _penaltyFactory.CreateDomainObject(dto);
                penalty.Validate();

                penalty = await _penaltyRepository.Create(penalty);
                return(_penaltyMapper.ToDto(penalty));
            });

            return(result);
        }
예제 #3
0
        public IActionResult Save([FromBody] Penalty penalty)
        {
            if (penalty is null)
            {
                return(BadRequest("Penalty is null."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            if (penalty.PenaltyId == 0)
            {
                _repository.Create(penalty);
                return(new JsonResult(new { create = true, penalty }));
            }
            else
            {
                _repository.Update(penalty);
                return(new JsonResult(new { update = true, penalty }));
            }
        }