Exemplo n.º 1
0
        public async Task <IActionResult> PostBookUserMood([FromBody] BookUserMood bookUserMood)
        {
            try
            {
                BookUserMood result = await Context.SaveBookUserMood(bookUserMood);

                if (result == null)
                {
                    return(BadRequest(new BadRequestResponse("Error saving book for mood in database")));
                }

                return(Ok(result));
            }
            catch (BookNotFoundException e)
            {
                return(BadRequest(new BadRequestResponse(e.Message)));
            }
            catch (UserNotFoundException e)
            {
                return(BadRequest(new BadRequestResponse(e.Message)));
            }
            catch (MoodNotFoundException e)
            {
                return(BadRequest(new BadRequestResponse(e.Message)));
            }
            catch (Exception e)
            {
                return(BadRequest(new BadRequestResponse("Error saving book for mood in database")));
            }
        }
Exemplo n.º 2
0
        public async Task <BookUserMood> SaveBookUserMood(BookUserMood bookUserMood)
        {
            MySqlConnection connection = GetConnection();

            await connection.OpenAsync();

            MySqlCommand bookCommand = new MySqlCommand("SELECT * FROM Book WHERE bookID = @bookID", connection);

            bookCommand.Parameters.AddWithValue("@bookID", bookUserMood.BookID);

            var bookReader = await bookCommand.ExecuteReaderAsync();

            if (bookReader.HasRows)
            {
                bookReader.Close();

                await connection.CloseAsync();

                connection = GetConnection();

                await connection.OpenAsync();

                var userCommand = new MySqlCommand("SELECT * FROM DBUser WHERE userID = @userID", connection);
                userCommand.Parameters.AddWithValue("@userID", bookUserMood.UserID);

                var userReader = await userCommand.ExecuteReaderAsync();

                if (userReader.HasRows)
                {
                    userReader.Close();

                    await connection.CloseAsync();

                    connection = GetConnection();

                    await connection.OpenAsync();

                    var moodCommand = new MySqlCommand("SELECT * FROM Mood WHERE moodID = @moodID", connection);
                    moodCommand.Parameters.AddWithValue("@moodID", bookUserMood.MoodID);

                    var moodReader = await moodCommand.ExecuteReaderAsync();

                    if (moodReader.HasRows)
                    {
                        moodReader.Close();

                        await connection.CloseAsync();

                        connection = GetConnection();

                        await connection.OpenAsync();

                        var checkCommand = new MySqlCommand("SELECT* FROM BookUserMood WHERE bookID = @bookID AND userID = @userID", connection);
                        checkCommand.Parameters.AddWithValue("@bookID", bookUserMood.BookID);
                        checkCommand.Parameters.AddWithValue("@userID", bookUserMood.UserID);

                        var checkReader = await checkCommand.ExecuteReaderAsync();

                        if (checkReader.HasRows)
                        {
                            checkReader.Close();

                            await connection.CloseAsync();

                            connection = GetConnection();

                            await connection.OpenAsync();

                            var updateCommand = new MySqlCommand("UPDATE BookUserMood SET bookID = @bookID, userID = @userID, moodID = @moodID WHERE bookID = @bookID AND userID = @userID", connection);
                            updateCommand.Parameters.AddWithValue("@bookID", bookUserMood.BookID);
                            updateCommand.Parameters.AddWithValue("@userID", bookUserMood.UserID);
                            updateCommand.Parameters.AddWithValue("@moodID", bookUserMood.MoodID);

                            await updateCommand.ExecuteNonQueryAsync();
                        }
                        else
                        {
                            checkReader.Close();

                            await connection.CloseAsync();

                            connection = GetConnection();

                            await connection.OpenAsync();

                            var insertCommand = new MySqlCommand("INSERT INTO BookUserMood(bookID, userID, moodID) VALUES (@bookID, @userID, @moodID)", connection);
                            insertCommand.Parameters.AddWithValue("@bookID", bookUserMood.BookID);
                            insertCommand.Parameters.AddWithValue("@userID", bookUserMood.UserID);
                            insertCommand.Parameters.AddWithValue("@moodID", bookUserMood.MoodID);

                            await insertCommand.ExecuteNonQueryAsync();
                        }
                    }
                    else
                    {
                        moodReader.Close();
                        throw new MoodNotFoundException("Mood not found in database");
                    }
                }
                else
                {
                    userReader.Close();
                    throw new UserNotFoundException("User not found in database");
                }
            }
            else
            {
                bookReader.Close();
                throw new BookNotFoundException("Book not found in database");
            }

            await connection.CloseAsync();

            return(bookUserMood);
        }