public IActionResult AddReservation([FromBody] ReservationForCreationDto reservation)
        {
            var customerId = Convert.ToInt32(RouteData.Values["customerId"]);

            if (ModelState.IsValid && customerId >= 0)
            {
                var result = _unitOfWork.ReservationRepository.Add(reservation, customerId);

                if (result.Item1 < 0 || result.Item2 < 0)
                {
                    throw new Exception("Creating a reservation failed on save.");
                }

                var reservationToReturn = new ReservationDto
                {
                    Id           = result.Item1,
                    SeatNumber   = result.Item2,
                    CustomerId   = customerId,
                    CourseId     = reservation.CourseId,
                    FirstStation = reservation.FirstStation,
                    LastStation  = reservation.LastStation
                };

                return(CreatedAtRoute("GetReservationForCustomer", new { id = reservationToReturn.CustomerId }, reservationToReturn));
            }
            else
            {
                return(BadRequest());
            }
        }
Example #2
0
        public IActionResult CreateReservation([FromBody] ReservationForCreationDto reservation)
        {
            try
            {
                if (reservation == null)
                {
                    _logger.LogError("reservation object sent from client is null.");
                    return(BadRequest("reservation object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid reservation object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var reservationEntity = _mapper.Map <Reservation>(reservation);

                _repository.Reservation.CreateReservation(reservationEntity);
                _repository.Save();

                var createdReservation = _mapper.Map <ReservationDto>(reservationEntity);

                return(CreatedAtRoute("ReservationById", new { id = createdReservation.ReservationId }, createdReservation));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateReservation action: {ex.InnerException.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
        public async Task <ReservationDto> Create(ReservationForCreationDto input)
        {
            var reservation = _mapper.Map <Reservation>(input);
            await _repository.Add(reservation);

            var output = _mapper.Map <ReservationDto>(reservation);

            return(output);
        }
        public async Task <IActionResult> Post([FromBody] ReservationForCreationDto reservationForCreationDto)
        {
            if (reservationForCreationDto == null)
            {
                return(BadRequest());
            }

            var reservationDto = await _reservationAppService.Create(reservationForCreationDto);

            return(CreatedAtRoute(GetReservationActionName, new { id = reservationDto.Id }, reservationDto));
        }
Example #5
0
        public (int, int) Add(ReservationForCreationDto reservation, int customerId)
        {
            try
            {
                int newId      = -1;
                int seatNumber = -1;

                using (var sqlConnection = new SqlConnection(sqlConnectionString))
                    using (var sqlCommand = new SqlCommand($"EXEC ADD_RESERVATION @customerId, @price, @courseId," +
                                                           $" @firstStation, @lastStation, @seatNumber OUTPUT, @reservationId OUTPUT", sqlConnection))
                    {
                        var seatNumberParameter    = sqlCommand.Parameters.Add("@seatNumber", SqlDbType.Int);
                        var reservationIdParameter = sqlCommand.Parameters.Add("@reservationId", SqlDbType.Int);
                        seatNumberParameter.Direction    = ParameterDirection.Output;
                        reservationIdParameter.Direction = ParameterDirection.Output;

                        sqlCommand.Parameters.AddWithValue("@customerId", customerId);
                        sqlCommand.Parameters.AddWithValue("@price", reservation.Price);
                        sqlCommand.Parameters.AddWithValue("@courseId", reservation.CourseId);
                        sqlCommand.Parameters.AddWithValue("@firstStation", reservation.FirstStation);
                        sqlCommand.Parameters.AddWithValue("@lastStation", reservation.LastStation);

                        sqlConnection.Open();

                        sqlCommand.ExecuteNonQuery();

                        newId      = Convert.ToInt32(sqlCommand.Parameters["@reservationId"].Value);
                        seatNumber = Convert.ToInt32(sqlCommand.Parameters["@seatNumber"].Value);
                    }

                return(newId, seatNumber);
            }
            catch (Exception ex)
            {
                logger.LogInformation($"AddReservation DB Connection Error: {ex}");
                return(-1, -1);
            }
        }
Example #6
0
        public ActionResult <ReservationsDto> CreateReservationForClient(int clientId, ReservationForCreationDto reservation)
        {
            if (!_clientRepository.ClientExists(clientId))
            {
                return(NotFound());
            }

            var reservationEntity = _mapper.Map <Entities.Reservation>(reservation);

            _reservationRepository.AddReservation(reservationEntity, clientId);
            _reservationRepository.Save();

            var reservationToReturn = _mapper.Map <ReservationsDto>(reservationEntity);

            return(CreatedAtRoute("GetReservationForClient",
                                  new { clientId = clientId, reservationId = reservationToReturn.ReservationId }, reservationToReturn));
        }