Esempio n. 1
0
        /// <summary>
        /// Validates the book rent event data.
        /// </summary>
        /// <param name="bookRentEventDTO">The book rent event dto.</param>
        /// <returns>
        /// Returns an error message if book rent event data is invalid.
        /// </returns>
        private string ValidateBookRentEventData(BookRentEventDTO bookRentEventDTO)
        {
            StringBuilder errorMessageBuilder = new StringBuilder();

            if (bookRentEventDTO == null)
            {
                errorMessageBuilder.Append("Data missing from request.");
            }

            if (bookRentEventDTO.BookId <= 0)
            {
                errorMessageBuilder.Append("Book data inside the request is invalid.");
            }

            if (bookRentEventDTO.UserId <= 0)
            {
                errorMessageBuilder.Append("User data inside the request is invalid.");
            }

            if (bookRentEventDTO.NumberOfCopiesRented <= 0)
            {
                errorMessageBuilder.Append("Number of copies inside the request is invalid.");
            }

            return(errorMessageBuilder.ToString());
        }
Esempio n. 2
0
        public IActionResult UpdateBookRentEvent(BookRentEventDTO bookRentEventDto)
        {
            var errorMessage = this.ValidateBookRentEventData(bookRentEventDto);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                return(BadRequest(errorMessage));
            }

            try
            {
                var model        = this.mapper.Map <BookRentEvent>(bookRentEventDto);
                var updatedModel = this.bookRentEventRepository.Update(model);
                return(Ok(this.mapper.Map <BookRentEventViewModel>(updatedModel)));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }