示例#1
0
        public ActionResult Post([FromBody] BorrowRecordDto borrowRecordDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!_carRepository.CheckCarExists(borrowRecordDto.CarRego) || !_userRepository.CheckUserExists(borrowRecordDto.UserId))
            {
                return(NotFound());
            }

            int borrowExpInHours = Convert.ToInt32(_configuration["Settings:BorrowExpInHours"]);


            if (!_borrowRecordRepostitory.CheckCarAvailability(borrowRecordDto.CarRego, borrowExpInHours))
            {
                return(BadRequest("The car is not available."));
            }

            var newRecord = new BorrowRecord
            {
                CarRego    = borrowRecordDto.CarRego,
                UserId     = borrowRecordDto.UserId,
                CreateDate = DateTime.Now,
                IsActive   = true,
            };

            try
            {
                _borrowRecordRepostitory.Add(newRecord);
                _borrowRecordRepostitory.AppDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                var errorMessage = ex.Message;
                // TODO: Log Error
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(Ok(newRecord));
        }