Beispiel #1
0
        /// <summary>
        /// Adds a new user book read.
        /// </summary>
        /// <param name="bookReadAddRequest">The new book read to try to add.</param>
        /// <returns>The action result.</returns>
        public BookReadAddResponse AddNewBookRead(BookReadAddRequest bookReadAddRequest)
        {
            BookReadAddResponse response =
                new BookReadAddResponse
            {
                ErrorCode  = (int)BookReadAddResponseCode.Success,
                FailReason = "",
                UserId     = bookReadAddRequest.UserId
            };

            // First check that the user exists
            User userLogin =
                _userDatabase.LoadedItems.FirstOrDefault(x => x.Id.ToString() == bookReadAddRequest.UserId);

            if (userLogin == null)
            {
                response.ErrorCode  = (int)BookReadAddResponseCode.UnknownUser;
                response.FailReason = "Could not find this user.";
                return(response);
            }

            // Check the book data is valid
            BookRead newBook;

            if (!GetBookRead(bookReadAddRequest, out newBook))
            {
                response.ErrorCode  = (int)BookReadAddResponseCode.InvalidItem;
                response.FailReason = "Invalid book data please try again.";
                return(response);
            }

            // Check if this is duplicate
            GeographyProvider geographyProvider;
            BooksReadProvider booksReadProvider;

            _books = new ObservableCollection <Book>();

            if (GetProviders(out geographyProvider, out booksReadProvider))
            {
                BookRead match = booksReadProvider.BooksRead.FirstOrDefault(
                    x => x.Author == newBook.Author &&
                    x.Title == newBook.Title &&
                    x.DateString == newBook.DateString);

                if (match != null)
                {
                    response.ErrorCode  = (int)BookReadAddResponseCode.Duplicate;
                    response.FailReason = "This book has already been added.";
                    return(response);
                }
            }

            newBook.User = userLogin.Name;
            _booksReadDatabase.AddNewItemToDatabase(newBook);
            response.NewItem = new Book(newBook);

            return(response);
        }
Beispiel #2
0
        /// <summary>
        /// Gets if there is a valid new book read based on the request.
        /// </summary>
        /// <param name="addRequest">The request to add a book read.</param>
        /// <param name="newBook">The new book  to add on exit.</param>
        /// <returns>True if a valid new book, false otherwise.</returns>
        private bool GetBookRead(BookReadAddRequest addRequest, out BookRead newBook)
        {
            newBook = new BookRead
            {
                Author           = addRequest.Author,
                Title            = addRequest.Title,
                Pages            = addRequest.Pages,
                Note             = addRequest.Note,
                Nationality      = addRequest.Nationality,
                OriginalLanguage = addRequest.OriginalLanguage,
                ImageUrl         = addRequest.ImageUrl,
                Tags             = addRequest.Tags.ToList()
            };

            // Check the required strings are ok.
            if (string.IsNullOrWhiteSpace(newBook.Author) ||
                string.IsNullOrWhiteSpace(newBook.Title) ||
                string.IsNullOrWhiteSpace(newBook.Nationality) ||
                string.IsNullOrWhiteSpace(newBook.OriginalLanguage))
            {
                return(false);
            }

            // Check the format is valid
            switch (addRequest.Format)
            {
            case "Book":
                newBook.Format = BookFormat.Book;
                break;

            case "Comic":
                newBook.Format = BookFormat.Comic;
                break;

            case "Audio":
                newBook.Format = BookFormat.Audio;
                break;

            default:
                return(false);
            }

            // Check the date
            if (DateTime.Now < addRequest.Date || addRequest.Date < EarliestDate)
            {
                return(false);
            }

            // Set the date string
            newBook.Date       = addRequest.Date;
            newBook.DateString =
                SuffixedDaysOfMonths[newBook.Date.Day] + newBook.Date.ToString(" MMMM yyyy");

            return(true);
        }
        public IActionResult Post([FromBody] BookReadAddRequest bookReadAddRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            BookReadAddResponse response =
                _booksDataControllerUtilities.AddNewBookRead(bookReadAddRequest);

            return(Ok(response));
        }