Пример #1
0
        public async Task <Result <BookItem, Error> > LoadAsync(string bookId)
        {
            try
            {
                using var connection = _dbConnectionFactory.Create();
                var sql      = $"SELECT * from books.book_items WHERE book_id = '{bookId}'";
                var bookItem = await connection.QuerySingleOrDefaultAsync <BookItem>(sql);

                return(bookItem == null?Result.Failure <BookItem, Error>(ErrorTypes.FailedLocatingItem(bookId)) : Result.Success <BookItem, Error>(bookItem));
            }
            catch (MySqlException mySqlException)
            {
                _logger.Fatal(mySqlException, "failed loading book: {BookId}", bookId);
                return(Result.Failure <BookItem, Error>(ErrorTypes.FailedLoadingItem(bookId, mySqlException.Number)));
            }
        }
Пример #2
0
        public async Task <Result <Unit, Error> > DeleteAsync(string bookId)
        {
            try
            {
                using var conn = _dbConnectionFactory.Create();
                const string sql =
                    @"DELETE FROM 
                            book_items
                        WHERE
                            book_id = @BookId";

                var updateResult = await conn.ExecuteAsync(sql, new { BookId = bookId });

                return(updateResult == 0 ? Result.Failure <Unit, Error>(ErrorTypes.FailedLocatingItem(bookId)) : Result.Success <Unit, Error>(Unit.Instance));
            }
            catch (MySqlException mySqlException)
            {
                _logger.Fatal(mySqlException, "failed deleting book: {BookId}", bookId);
                return(Result.Failure <Unit, Error>(ErrorTypes.FailedDeletingItem(bookId, mySqlException.Message, mySqlException.Number)));
            }
        }
Пример #3
0
        public async Task <Result <Unit, Error> > UpdateAsync(BookItem model)
        {
            try
            {
                using var conn = _dbConnectionFactory.Create();
                const string updateSql =
                    @"UPDATE 
                            book_items 
                        SET 
                            author_id = @AuthorId,
                            name = @Name
                        WHERE
                            book_id = @BookId";

                var updateResult = await conn.ExecuteAsync(updateSql, model);

                return(updateResult == 0 ? Result.Failure <Unit, Error>(ErrorTypes.FailedLocatingItem(model.BookId)) : Result.Success <Unit, Error>(Unit.Instance));
            }
            catch (MySqlException mySqlException)
            {
                _logger.Fatal(mySqlException, "failed updating book: {BookId}", model?.BookId);
                return(Result.Failure <Unit, Error>(ErrorTypes.FailedUpdatingItem(model?.BookId, mySqlException.Message, mySqlException.Number)));
            }
        }